流云轻响 2021-10-18 18:38 采纳率: 100%
浏览 46
已结题

本地vs2019运行正常,在提交的网站上显示Compile Error

问题:
Description

由小写字母{'a','b','c','d','z'}和{'+','-','*','/','(',')'}可以组成一个中缀表达式,现在需要你输出它的后缀表达式。

Input

输入的第一行是一个int型整数T,表示一个有T组数据。
接下来T行,每行一个一个不含空格的字符串,且每个字符串长度都小于1000。该字符串就是中缀表达式。
数据保证输入的表达式一定合法。

Output

输出T行,每行一个后缀表达式,表示求得的结果。输出的表达式同样不含空格。

Sample Input

2
a+bc+(de+f)*g
(a+b)*c/d+f/g

Sample Output

abc*+def+g+
ab+c*d/fg/+
我的代码如下:

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

using namespace std;

//操作符的栈内、外优先级
int isp(char x)//栈内
{
    if (x == '*' || x == '/') return 5;
    if (x == '+' || x == '-') return 3;
    if (x == '(') return 1;
    else return -1;
}
int icp(char x)//栈外
{
    if (x == '*' || x == '/') return 4;
    if (x == '+' || x == '-') return 2;
    if (x == '(') return 6;
    else return -1;
}

int main()
{
    string infix, postfix;
    stack<char> operators;
    int T;
    cin >> T;
    char ch;
    for (int q = 0; q < T; ++q)
    {
        cin >> infix;
        for (int i = 0; i < infix.size(); i++)
        {
            ch = infix[i];
            if (ch >= 'a' && ch <= 'z')
            {//是操作数,输出
                postfix.push_back(ch);
            }
            else if (operators.empty())
            {//若是空的,让该操作符进栈
                operators.push(ch);
            }
            else if (ch == ')')
            {
                while (operators.top() != '(' && !operators.empty())
                {
                    postfix.push_back(operators.top());
                    operators.pop();
                }
                operators.pop();
            }
            else if (ch == '(')
            {
                operators.push(ch);
            }
            else if (ch == '+' || ch == '-'
                || ch == '*' || ch == '/')
            {//是加减乘除
                while (isp(operators.top()) >= icp(ch))
                {
                    postfix.push_back(operators.top());
                    operators.pop();
                    if (operators.empty())
                    {
                        break;
                    }
                }
                operators.push(ch);
            }
        }
        while (!operators.empty())
        {
            postfix.push_back(operators.top());
            operators.pop();
        }
        cout << postfix << endl;
        infix.clear();
        postfix.clear();
    }
    return 0;
}

  • 写回答

1条回答 默认 最新

  • 关注

    是不是代码复制的时候不小心按到了,这里多了个分号

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月26日
  • 已采纳回答 10月18日
  • 创建了问题 10月18日

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算