使用堆栈将符号表示为后缀表示法

时间:2015-07-02 02:58:33

标签: 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 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;
}

2 个答案:

答案 0 :(得分:1)

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

else if((isdigit(source[i]))||(isalpha(source[i]))

没有右括号,你的编码风格很难注意到

else if ((isdigit(source[i]) != 0) || (isalpha(source[i]) != 0))

并且在您的情况下不要使用gets()它已被弃用

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

可以工作,并且可以防止缓冲区溢出。

答案 1 :(得分:1)

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

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])) ) 或者按照以下评论中的Jonathan Leffler建议:如果您使用if (isalnum(source[i])),则会更好,因为它会检查所有字母数字。
  • 只有我发现代码不那么可读吗? 而不是if(op=='/'||op=='*'||op=='%'||op=='%')我更愿意 if((op == '/')||(op == '*')||(op == '%'))就可读性而言。
相关问题