在C ++中将堆栈用于中缀和后缀表达式 [英] Using Stacks In C++ for infix and postfix expressions

查看:103
本文介绍了在C ++中将堆栈用于中缀和后缀表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,该程序需要用户输入,并使用堆栈根据优先级将中缀表达式转换为后缀表达式,并且操作数始终位于运算符之前。例如,如果用户输入:

I'm writing a program that takes user input and uses stacks to convert an infix expression into a postfix expression based on precedence, with operands always going before operators. For example, if a user inputs:

(a + b * c)

(a+b*c)

,则程序应显示:

abc * +

到目前为止,我有:

#include <iostream>
#include <stack>
#include <string>


using namespace std;

int main()
{
    stack<char> s;
    char input;
    while (cin.get(input) && input != '\n')
        {
            if (isalnum(input))
                cout << input << "\n";
            else if (input == '(')
                s.push(input);
            else if (input == ')')
            {
        while (!s.empty() && s.top() != '(')
            {
            cout << s.top();
            s.pop();
        }
            if(!s.empty()) 
                    s.pop();
            else
                cout << "ERROR: No Matching ( \n";
        }
     else if (s.empty() && input == '*'||'/'||'+'||'-' && s.top() < input) // Error Begins Here?
     {
         char a = '*';
         char b = '/';
         char c = '+';
         char d = '-';
         bool prec (char a, char b, char c, char d);
             return ('*' > '/' > '+' > '-');
             s.push(input);
     }
         else if (input == '*'||'/'||'+'||'-' && s.top() >= input)
             while (!s.empty()) 
          {
              cout << s.top();
          s.pop();
                  s.push(input);
          }
        }
    while (!s.empty())
    {
        cout << s.top();
        s.pop();
    }
}

可以编译并运行,但不能正常运行。输入 ab之类的表达式时,程序将按原样显示 ab,但如果我输入 a + b + c,则仅显示 a。这意味着程序不会将运算符放入堆栈中以供以后显示。我需要帮助的是修改程序,以便在输入运算符时,应将其添加到堆栈中,然后根据输入的优先级(*> /> +>-)在完成输入后显示。

Which compiles and runs but is not functioning as it should. When an expression like "ab" is input, the program will display "ab" as it should but if I input "a+b+c", then only "a" will be displayed. This means the program is not placing the operators into the stack to be displayed later on. What I need help with is modifying the program so that when an operator is input, it should be added onto the stack and then displayed based on it's precedence (*>/>+>-) after the operands, when the input is done.

我一般对C ++和编程都是陌生的,所以任何建议都是不错的。

I'm quite new to C++ and programming in general, so any suggestions would be great.

推荐答案

else if (input == '*'||'/'||'+'||'-' && s.top() >= input)

这不会做您认为做的事。

This does not do what you think it does. You need to do

else if (input == '*'|| input == '/'|| input == '+'|| input == '-' && s.top() >= input)

这看起来也像是一个错误

And this looks like an error too

bool prec (char a, char b, char c, char d);

这是函数原型的语法。您确定可以编译吗?

That's the syntax for a function prototype. Are you sure this compiles?

这篇关于在C ++中将堆栈用于中缀和后缀表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆