PTA 表达式转换

算术表达式有前缀表示法、中缀表示法和后缀表示法等形式。日常使用的算术表达式是采用中缀表示法,即二元运算符位于两个运算数中间。请设计程序将中缀表达式转换为后缀表达式。

输入格式:
输入在一行中给出不含空格的中缀表达式,可包含+、-、*、\以及左右括号(),表达式不超过20个字符。

输出格式:
在一行中输出转换后的后缀表达式,要求不同对象(运算数、运算符号)之间以空格分隔,但结尾不得有多余空格。

输入样例:

2+3*(7-4)+8/4

输出样例:

2 3 7 4 - * + 8 4 / +

在这里插入图片描述

#include<iostream>
#define STACK_INIT_SIZE 100
using namespace std;
class SqStack
{
public:
    char *base;
    char *top;
    void InitStack()
    {
        base = new char[STACK_INIT_SIZE];
        if(!base)exit(1);
        top=base;
    }
    void Push(char x)
    {
        *top++=x;
    }
    void Pop(char &x)
    {
        if(top==base)exit(1);
        else
        {   x=*--top;
        }
    }
    char GetTop()
    {
        if(top==base)return '\0';
        else return *(top-1);
    }
    bool StackEmpty()
    {
        if(top==base)return true;
        else return false;
    }
};
int main()
{

    int cin=0,i=0;//cin用来判断是否为第一次输入,i为保存的数字位数
    char PM=' ',Operand[20],n,x,pre_n=' ';//PM--正负号,Operand[]--保存数字位数的字符数组,n--每次输入的字符,x--接收出栈变量,pre_n--上次输入的字符
    SqStack Operator;//操作符栈
    Operator.InitStack();//初始化栈
    while (scanf("%c",&n)&&n!='\n') {
        cin++;
        if(i&&(n<'0'||n>'9')&&n!='.')//遇到非数字时输出之前所有数字
        {
            if(PM!=' '){printf("%c",PM);PM=' ';}
            for(int j=0;j<i;j++)
                printf("%c",Operand[j]);
            printf(" ");
            i=0;
        }
        if((n>='0'&&n<='9')||n=='.'){Operand[i++]=n;}//存数字
        //遇到左括号,入栈
        else if(n=='('){Operator.Push(n);}
        //遇到右括号,依次出栈至遇到‘(’,将左括号也出栈,右括号不入栈。
        else if (n==')')
        {

            while(1)
            {
                Operator.Pop(x);
                if(x=='(')break;
                printf("%c ",x);
            }
        }
        //遇到+或-,判断是正负号还是操作符
        else if(n=='-'||n=='+')
        {
            //为正负号的情况:第一次输入,前一个为'(',前一个为非数字,但前一个不为‘)’
           // if((cin==1||pre_n=='('||pre_n<'0'||pre_n>'9')&&pre_n!=')')
            if((cin==1||pre_n=='('||pre_n<'0'||pre_n>'9')&&pre_n!=')')
            //为减号,保存
               if(n=='-')PM=n;
            //正号,跳过,输出时不显示
               else {pre_n=n;continue;}//-----
            else
            {
                //栈空或栈顶为‘(’,入栈
            if(Operator.StackEmpty()||(Operator.GetTop()=='('))Operator.Push(n);
            else
            {
                while (!Operator.StackEmpty()&&Operator.GetTop()!='(')
                {
                    Operator.Pop(x);
                    printf("%c ",x);
                }
                Operator.Push(n);}
            }
        }
        else if(n=='*'||n=='/')
        {

            if(Operator.StackEmpty()||Operator.GetTop()=='(')Operator.Push(n);
            else
            {   while (!Operator.StackEmpty()&&Operator.GetTop()!='(') {
                if(*(Operator.top-1)=='*'||*(Operator.top-1)=='/')
                    {Operator.Pop(x);
                        printf("%c ",x);}
                else break;
                }
              Operator.Push(n);
            }
        }
        pre_n=n;
    }
    
    if(i)//输出数字
    {
        if(PM!=' '){printf("%c",PM);PM=' ';}
        for(int j=0;j<i;j++)
            printf("%c",Operand[j]);
        if(!Operator.StackEmpty())
        printf(" ");
    }
    //输出剩下的操作符栈
    while (!Operator.StackEmpty()) {
        Operator.Pop(x);
        if(!Operator.StackEmpty())
        printf("%c ",x);
        else
        printf("%c",x);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

椰子zii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值