Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 中缀到后缀_C_Stack_Infix Notation_Postfix Notation - Fatal编程技术网

C 中缀到后缀

C 中缀到后缀,c,stack,infix-notation,postfix-notation,C,Stack,Infix Notation,Postfix Notation,我一直在努力解决这个问题。 我有一项任务是做一个基本的计算器 为此,我需要后缀中的说明。 我在网上找到了一些代码,这些代码有效,但使用了gets() 我试着换掉了。。。但该计划不再有效。这是代码,我希望有人能找到错误(使用2+4作为输入,它读取并识别2为数字,然后+为运算符,然后4为数字…然后卡在线路的某个地方) 明确地说,对于我的任务来说,使用这个代码是公平的,只要我引用它作为参考(因为它只是一小部分) #包括 #包括 #包括 #定义最大值10 #定义空-1 结构堆栈 { 字符数据[MAX];

我一直在努力解决这个问题。 我有一项任务是做一个基本的计算器

为此,我需要后缀中的说明。 我在网上找到了一些代码,这些代码有效,但使用了gets()

我试着换掉了。。。但该计划不再有效。这是代码,我希望有人能找到错误(使用2+4作为输入,它读取并识别2为数字,然后+为运算符,然后4为数字…然后卡在线路的某个地方)

明确地说,对于我的任务来说,使用这个代码是公平的,只要我引用它作为参考(因为它只是一小部分)

#包括
#包括
#包括
#定义最大值10
#定义空-1
结构堆栈
{
字符数据[MAX];
int top;
};
int isempty(结构堆栈*s)
{
printf(“isempty\n”);
返回(s->top==空)?1:0;
}
void emptystack(结构堆栈*s)
{
printf(“清空堆栈”\n);
s->top=空;
}
无效推送(结构堆栈*s,int项)
{
printf(“push\n”);
如果(s->top==(最大值1))
{
printf(“\n堆栈已满”);
}
其他的
{
printf(“添加到堆栈\n”);
++s->top;
s->数据[s->顶部]=项目;
}
}
char pop(结构堆栈*s)
{
printf(“pop\n”);
char ret=(char)为空;
如果(!isempty(s))
{
ret=s->data[s->top];
--s->top;
}
返回ret;
}
无效显示(结构堆栈)
{
printf(“显示\n”);
while(s.top!=空)
{
printf(“非空\n”);
printf(“\n%d”,s.data[s.top]);
s、 顶部--;
}
}
整数等运算符(字符e)
{
getchar();
printf(“等运算符”);
如果(e='+'| | e='-'| | e='*'| | e='/'| | e='%'))
返回1;
其他的
返回0;
}
整数优先级(字符e)
{
printf(“优先级”);
int-pri=0;
如果(e='*'| | e='/'| | e=='%'))
pri=2;
其他的
{
如果(e=='+'| | e=='-')
pri=1;
}
返回优先级;
}
void infix2postfix(char*infix,char*postfix,int-insertspace)
{
printf(“infix2postfix\n中”);
char*i,*p;
结构堆栈X;
字符n1;
清空堆栈(&X);
i=&中缀[0];
p=&postfix[0];
而(*i)
{
而(*i=''| |*i='\t')
{
i++;
}
if(isdigit(*i)| isalpha(*i))
{
printf(“是数字。\n”);
while(isdigit(*i)| isalpha(*i))
{
*p=*i;
p++;
i++;
}
/*空间码*/
if(insertspace)
{
*p='';
p++;
}
/*结束空间代码*/
}
如果(*i=='(')
{
推送(&X,*i);
i++;
}
如果(*i='))
{
n1=pop(&X);
而(n1!='('))
{
*p=n1;
p++;
/*空间码*/
if(insertspace)
{
*p='';
p++;
}
/*结束空间代码*/
n1=pop(&X);
}
i++;
}
if(等参算子(*i))
{
if(i空(&X))
推送(&X,*i);
其他的
{
n1=pop(&X);
while(优先级(n1)>=优先级(*i))
{
*p=n1;
p++;
/*空间码*/
if(insertspace)
{
*p='';
p++;
}
/*结束空间代码*/
n1=pop(&X);
}
推送(&X,n1);
推送(&X,*i);
}
i++;
}
}
而(!isempty(&X))
{
n1=pop(&X);
*p=n1;
p++;
/*空间码*/
if(insertspace)
{
*p='';
p++;
}
/*结束空间代码*/
}
*p='\0';
}
int main()
{
[50]中的字符,post[50],temp[50];
strcpy(&post[0],“”);
printf(“输入中缀表达式:”);
fflush(stdin);
fgets(in,50,stdin);
printf(“%s”,in);
infix2postfix(&in[0],&post[0],1);
printf(“后缀表达式为:%s\n”,&post[0]);
返回0;
}
谢谢你的帮助,我真的很感激:)

fgets()
在字符串中包含一个换行符,因此您得到了一个读作“2+4\n”的字符串。将
while(*i)
替换为
while(*i&&*i!='\n')
,然后看看这会给您带来什么。

\include
#include<stdio.h>
char stack[100];
int lowerBound=0;
int upperBound=99;
int top=upperBound+1;
int size=0;  
char postFix[101];
int postFixLowerBound=0;
void push(char op)
{
  top--;
  stack[top]=op;
  size++;
}
char pop()
{
 char op;
 op=stack[top];
 top++;
 size--;
 return op;
 }
 int isEmpty()
{
  return top==upperBound+1;
}
 int isOperator(char c)
 {
   return(c=='^'||c=='/'||c=='*'||c=='+'||c=='-');
  }
 int isOperand()
 {
    if(isOperator(c)) return 0;
   else return 1;
 }
 int getPrecedenceLevel(char op)
 {
    if(op=='^') return 3;
    if(op=='/' || op=='*') return 2;
    if(op=='+' || op=='-') return 1;
    return 0;
 }
 int getElementAtTop()
 {
   return stack[top];
 }
 void appendToPostFixString(char c)
 {
  postFix[postFixLowerBound]=c;
  postFixLowerBound++;
  postFix[postFixLowerBound]='\0';
 }
  void main()
  {
   char infix[101];
   char c,op;
   int i;
   printf("Enter an infix expression\n");
   scanf("%s",infix);
   i=0;
   while(infix[i]='\0')
   {
    c=infix[i];
    i++;
    if(c=='(')
    {
      push(c);
      continue;
     }
      if(c==')')
     {
       while(1)
      {
       op=pop();
      if(op=='(')
      {
      op=pop();
      if(op=='(')
      {
        break;
      } 
      appendToPostFixString(op);
    }
    continue;
  }
  if(isOperand(c))
  {
    appendToPostFixString(c);
    continue;
  }
  if(isOperator(c))
  {
   if(isEmpty())
   {
     push(c);
   }
   else
   {
     op=getElementAtTop();
     if(op=='(')
     {
       push(c);
     }
     else
     {
       if(getPrecedenceLevel(op)>=getPrecedenceLevel(c))
       {
         while(1)
         {
           if(isEmpty())
           {
             break;
           } 
           op=getElementAtTop();
           if(op=='(')
           {
             break;
           }
           if(getPrecedenceLevel(op)<getPrecedenceLevel(c))
           {
             break;
           }
           op=pop();
           appendToPostFixString(op);
         }
         push(c);
       }
       else
       {
         push(c);
       }
     }
   }
   continue;
  }//while ends
  while(1)
  {
    if(isEmpty())
    {
      break;
    }
 op=pop();
 appendToPostFixString(op);
 }
 printf("Post Fix Expression is \n");
 printf("%s",postFix);
}
字符堆栈[100]; int lowerBound=0; int上限=99; int top=上限+1; int size=0; 字符后缀[101]; int postfix lowerbound=0; 无效推送(字符操作) { 顶部--; 堆栈[top]=op; 大小++; } char pop() { char op; op=堆栈[顶部]; top++; 大小--; 返回op; } int isEmpty() { 返回顶部==上限+1; } 整数等运算符(字符c) { 返回值(c='^'| | c='/'| | c='*'.'| | c='+'| | c='-'); } int等标量() { 如果(等参器(c))返回0; 否则返回1; } int GetPrevenceLevel(字符操作) { if(op=='^')返回3; 如果(op='/'| | op=='*')返回2; 如果(op='+'| | op=='-')返回1; 返回0; } int getElementAtTop() { 返回堆栈[顶部]; } 无效appendToPostFixString(字符c) { 后缀[postFix lowerbound]=c; postFixLowerBound++; 后缀[postFix lowerbound]='\0'; } void main() { 字符中缀[101]; charc,op; int i; printf(“输入中缀表达式\n”); scanf(“%s”,中缀); i=0; while(中缀[i]='\0') { c=中缀[i]; i++; 如果(c=='(') { 推(c); 继续; } 如果(c==')') { 而(1) { op=pop(); 如果(op=='(') { op=pop(); 如果(op=='(') { 打破 } 追加固定字符串(op); } 继续; } if(等规数(c)) { 附件固定字符串(c); 继续; } if(等温线(c)) { 如果(isE)
#include<stdio.h>
char stack[100];
int lowerBound=0;
int upperBound=99;
int top=upperBound+1;
int size=0;  
char postFix[101];
int postFixLowerBound=0;
void push(char op)
{
  top--;
  stack[top]=op;
  size++;
}
char pop()
{
 char op;
 op=stack[top];
 top++;
 size--;
 return op;
 }
 int isEmpty()
{
  return top==upperBound+1;
}
 int isOperator(char c)
 {
   return(c=='^'||c=='/'||c=='*'||c=='+'||c=='-');
  }
 int isOperand()
 {
    if(isOperator(c)) return 0;
   else return 1;
 }
 int getPrecedenceLevel(char op)
 {
    if(op=='^') return 3;
    if(op=='/' || op=='*') return 2;
    if(op=='+' || op=='-') return 1;
    return 0;
 }
 int getElementAtTop()
 {
   return stack[top];
 }
 void appendToPostFixString(char c)
 {
  postFix[postFixLowerBound]=c;
  postFixLowerBound++;
  postFix[postFixLowerBound]='\0';
 }
  void main()
  {
   char infix[101];
   char c,op;
   int i;
   printf("Enter an infix expression\n");
   scanf("%s",infix);
   i=0;
   while(infix[i]='\0')
   {
    c=infix[i];
    i++;
    if(c=='(')
    {
      push(c);
      continue;
     }
      if(c==')')
     {
       while(1)
      {
       op=pop();
      if(op=='(')
      {
      op=pop();
      if(op=='(')
      {
        break;
      } 
      appendToPostFixString(op);
    }
    continue;
  }
  if(isOperand(c))
  {
    appendToPostFixString(c);
    continue;
  }
  if(isOperator(c))
  {
   if(isEmpty())
   {
     push(c);
   }
   else
   {
     op=getElementAtTop();
     if(op=='(')
     {
       push(c);
     }
     else
     {
       if(getPrecedenceLevel(op)>=getPrecedenceLevel(c))
       {
         while(1)
         {
           if(isEmpty())
           {
             break;
           } 
           op=getElementAtTop();
           if(op=='(')
           {
             break;
           }
           if(getPrecedenceLevel(op)<getPrecedenceLevel(c))
           {
             break;
           }
           op=pop();
           appendToPostFixString(op);
         }
         push(c);
       }
       else
       {
         push(c);
       }
     }
   }
   continue;
  }//while ends
  while(1)
  {
    if(isEmpty())
    {
      break;
    }
 op=pop();
 appendToPostFixString(op);
 }
 printf("Post Fix Expression is \n");
 printf("%s",postFix);
}