c - 如何将中缀表达式转换为后缀表达式?

标签 c infix-notation

我正在尝试编写一个C程序将中缀表达式转换为后缀并使用输入的值进行计算。当我输入 (2+14)*5 时,我得到 (2 14) 5 * + 但它应该是 2 14 + 5 *。所以我的问题是:

  1. 我哪里出错了?
  2. 如何更改代码以删除最后一个表单(后缀)中的括号?

感谢您的帮助。

#include<stdio.h>
#include<string.h>
#include<math.h>

#define oper(x) (x=='+' || x=='-' || x=='*' || x=='/')

char in[30], post[30], stack[30];
int top=-1;

void push(char x)
{
    stack[++top]=x;
}

char pop()
{
    return stack[top--];
}

int precedence(char c)
{
    if (c=='+' || c=='-')
        return 1;
    if (c=='*' || c=='/')
        return 2;
    if (c=='(')
        return 3;
}

main()
{
    char c;
    int l,i,j=0,st1[20],k,h,f,eval,s,N;
    printf("Enter the infix expression : ");
    scanf("%s",&in);
    l=strlen(in);
    for(i=0;i<=l;i++)
    {
        if(oper(in[i]))
        {
            post[j++]=' ';
            while(precedence(in[i])<precedence(stack[top]))
            {
                post[j++]=stack[top];
                pop();
                post[j++]=' ';

            }
            push(in[i]);
        }
        else if(in[i]=='\0')
        {
            while(top!=-1)
            {
                post[j++]=' ';
                post[j++]=stack[top];
                pop();
            }
        }
        else
            post[j++]=in[i];
    }
    post[j]='\0';
    printf("Postfix Expression : %s\n",post);
    i=0;top=-1;f=0;k=0;
    while(i<j)
    {
        if(oper(post[i]))
        {
            f=1;
            c=post[i];
            eval=0;
            switch(c)
            {
                case '+':
                    eval=st1[top-1]+st1[top];
                    break; 
                case '-':
                    eval=st1[top-1]-st1[top];
                    break;
                case '*':
                    eval=st1[top-1]*st1[top];
                    break;
                case '/':
                    eval=st1[top-1]/st1[top];
                    break;
            }
            top--;
            st1[top]=eval; 
        }
        else if(post[i]==' ')
        {
            if(f==0)
            {
                h=i-k;
                s=0;
                while(post[h]!=' ')
                {
                    N=(int)post[h];
                    N=N-48;
                    s=s+N*(pow(10,(k-1)));
                    k--;
                    h++;
                }
                st1[++top]=s;
            }
            k=0;
        }
        else 
        {
            k++;
            f=0;
        }
        i++; 
    }
    printf("Value : %d\n",st1[top]);
}

最佳答案

我没有详细查看您的代码,但您似乎没有以任何特殊方式处理输入中的括号;它们具有非常具体的含义 WRT 操作顺序。

我的猜测是,您的程序将括号视为数字,并将输入解析为(分别用 L 和 R 代替左括号和右括号)L2 + (14R * 5),在这种情况下输出正确。

关于c - 如何将中缀表达式转换为后缀表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14001370/

相关文章:

c - 需要就地反转文件,但它只适用于一行文件

c - 释放分配的内存时出现不同的错误消息

python - 使用堆栈评估中缀表达式时出现算术错误评估和 KeyDict 错误;

java - 从后缀表达式创建二叉树

c - if语句在C中导致 "Segmentation fault: 11"

与等效的双维数组混淆

c - Tim Newsham 格式字符串利用

python - 涵盖 C 程序的更多运算符(例如 <、<= 等)的 Python 中前缀到前缀的良好实现?

java - Java 中缀到后缀转换的括号错误检查

r - 在 R 中,如何确定用户定义的中缀运算符的运算符优先级?