C 使用堆栈的中缀表示法到后缀表示法

C 使用堆栈的中缀表示法到后缀表示法,c,data-structures,stack,C,Data Structures,Stack,我正在尝试使用堆栈将中缀符号转换为后缀符号。我已经编写了以下代码,但它给了我错误: /Users/apple/Desktop/infix.c|49|error: expected expression 我无法找到错误。请帮我更正这个代码 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #define MAX 100 char

我正在尝试使用堆栈将中缀符号转换为后缀符号。我已经编写了以下代码,但它给了我错误:

/Users/apple/Desktop/infix.c|49|error: expected expression
我无法找到错误。请帮我更正这个代码

 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <ctype.h>

 #define MAX 100

 char st[MAX];
 int top = -1;

 void push(char st[],char);
 char pop(char st[]);
 void InfixtoPostfix(char source[],char target[]);
 int getPriority(char);

 int main(){
    char infix[100],postfix[100];
    printf("enter any infix expression");
    fflush(stdin);
    gets(infix);
    strcpy(postfix,"");
    InfixtoPostfix(infix,postfix);
    printf("\nthe corresponding postfix expression is:");
    puts(postfix);
    return 0;
}
void InfixtoPostfix(char source[],char target[]){
    int i=0,j=0;
    char temp;
    strcpy(target,"");
    while(source[i]!='\0')
    {
        if(source[i]=='(')
        {
            push(st,source[i]);
            i++;
        }
        else if(source[i]==')')
        {
            while((top!=-1)&&(st[top]!='('))
            {
                target[j]=pop(st);
                j++;
            }
            if(top==-1)
            {
                printf("\nincorrect syntax");
                exit(1);
            }
            temp=pop(st);
            i++;
            else if((isdigit(source[i]))||(isalpha(source[i]))
            {
                target[j]=source[i];
                j++;
                i++;
            }
            else if(source[i]=='+'||source[i]=='-  '||source[i]=='*'||source[i]='/'||source[i]=='%d')
            {
                while((top!=-1)&&(st[top]!='(')&&(getPriority(st[top])>getPriority(source[i])))
                {
                    target[j]=target[i];
                    i++;
                }
                push(st,source[i]);
                i++;
            }
            else{
                printf("\nincorrect expression");
                exit(1);
            }
        }
        while((top!=-1)&&(st[top]!='('))
        {
            target[j]=pop(st);
            j++;
        }
        target[j]='\0';

    }
}
int getPriority(char op)
{
    if(op=='/'||op=='*'||op=='%'||op=='%')
    return 1;
    else if(op=='+'||op=='-')
    return 0;
}
void push(char st[],char val)
{
    if(top==MAX-1)
        printf("overflow");
    else{
        top++;
        st[top]=val;
    }
}
char pop(char st[])
{
    char val=' ';
    if(top==-1)
        printf("underflow");
    else{
        val=st[top];
        top--;
    }
    return val;
}
#包括
#包括
#包括
#包括
#定义最大值100
char-st[MAX];
int top=-1;
无效推送(char st[],char);
char-pop(char-st[]);
void InfixtoPostfix(字符源[],字符目标[]);
int-getPriority(字符);
int main(){
字符中缀[100],后缀[100];
printf(“输入任何中缀表达式”);
fflush(stdin);
获取(中缀);
strcpy(后缀“”);
中缀后缀(中缀,后缀);
printf(“\n对应的后缀表达式为:”);
放置(后缀);
返回0;
}
无效InfixtoPostfix(字符源[],字符目标[]){
int i=0,j=0;
焦炭温度;
strcpy(目标“”);
while(源[i]!='\0')
{
如果(源[i]=='(')
{
推送(st,源[i]);
i++;
}
else if(source[i]==')')
{
而((顶部!=-1)和(&(顶部)!='('))
{
目标[j]=pop(st);
j++;
}
如果(顶部==-1)
{
printf(“\n语法不正确”);
出口(1);
}
温度=波普(st);
i++;
else if((isdigit(source[i]))| |(isalpha(source[i]))
{
目标[j]=源[i];
j++;
i++;
}
else if(source[i]='+'| | source[i]='-'| | source[i]='*'| | | source[i]='/'| | source[i]='%d')
{
而((top!=-1)&&(st[top]!='(')&&&(getPriority(st[top])>getPriority(source[i]))
{
目标[j]=目标[i];
i++;
}
推送(st,源[i]);
i++;
}
否则{
printf(“\n正确的表达式”);
出口(1);
}
}
而((顶部!=-1)和(&(顶部)!='('))
{
目标[j]=pop(st);
j++;
}
目标[j]='\0';
}
}
int getPriority(字符操作)
{
如果(op='/'| | op='*'| | op='%'| | op='%')
返回1;
else if(op='+'| | op=='-')
返回0;
}
无效推送(char st[],char val)
{
如果(顶部==最大值-1)
printf(“溢出”);
否则{
top++;
st[top]=val;
}
}
char pop(char st[])
{
char val='';
如果(顶部==-1)
printf(“下溢”);
否则{
val=st[顶部];
顶部--;
}
返回val;
}

许多问题,其中最重要的是

else if((isdigit(source[i]))||(isalpha(source[i]))
由于缺少右括号,您的编码风格很难引起注意

else if ((isdigit(source[i]) != 0) || (isalpha(source[i]) != 0))
不要使用
gets()
在您的情况下,它已被弃用

fgets(infix, sizeof(infix), stdin);

将起作用,并有防止缓冲区溢出的好处。

虽然代码中存在许多问题, 这是最恐怖的一个:

if(top==-1)
{
    printf("\n incorrect syntex");
    exit(1);
}
temp=pop(st);
i++;
else if((isdigit(source[i]))||(isalpha(source[i]))
{
   target[j]=source[i];
   j++;
   i++;
}
不,你做得不对。FWIK,C不允许这样:

if()
{
    //some code
}
//some code
else{
    //some code
}
  • 另一个问题是:
    else if((isdigit(source[i]))| |(isalpha(source[i]))
    。我希望它是,
    else if((0!=isdigit(source[i]))| |(0!=isalpha(source[i]))
    或者正如下面一条评论中所建议的:如果使用
    if(isalnum(source[i]))
    更好,因为它检查所有字母数字
  • 难道只有我觉得代码不那么可读吗?
    我宁愿
    if((op='/')| |(op=='*')| |(op='%'))

让我们从“第49行是什么?”开始,至少要正确地缩进代码。就我个人而言,我更愿意看到:
if(op='/'.| | op='*'.| op='%'))
在二进制运算符
==
周围有空格,在
if
后面有空格,没有多余的括号,并且没有对
%
进行双重测试。通常使用的“布尔”测试运算符或多或少与问题相同:
if(isdigit(source[i])| isalpha(source[i]))
因此-虽然最好使用
if(isalnum(source[i]))
一次测试字母数字。@JonathanLeffler:是的,使用
if(isalnum(source[i])
是有意义的。很好。我没有发现丢失的
,但行中还有一个更大的问题,那就是
else
前面没有
if
的主体,因此无效。