东师附中 1185 栈 中缀转后缀

本文详细介绍了如何将中缀表达式转换为后缀表达式,包括核心算法步骤、输入输出规范及示例解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1185: 栈 中缀转后缀

时间限制: 1 Sec   内存限制: 128 MB
提交: 9   解决: 5
[ 提交][ 状态][ 讨论版]

题目描述

请编写程序将一个中缀表达式转换为后缀表达式。

输入

仅一行,是一个中缀表达式。输入的符号中只有这些基本符号“0123456789+-*/()”,并且不会出现形如2*-3的格式,所有数字都是个位数,“/”表示整除运算。

输出

仅一行,是转换后的后缀表达式。数字之间、运算符之间、数字和运算符之间都用一个空格隔开

样例输入

8-(3+2*6)/5+4

样例输出

8 3 2 6 * + 5 / - 4 +

提示

表达式长度<=200

来源

[ 提交][ 状态][ 讨论版]

基本的中缀表达式转后缀表达式,代码如下:


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
#include<stack>
using namespace std;

stack<char > op;
queue<char > num;
queue<char > last;
char ch[210];

bool is_op(char a)
{
    if(a=='+'||a=='-'||a=='*'||a=='/'||a=='('||a==')')
        return true;
    else
        return false;
}

int fun(char a)
{
    switch(a)
    {
        case '+':
        case '-':return 1;break;
        case '*':
        case '/':return 2;break;
        case '(':return 3;break;
        default:return 0;break;
    }
}

int main()
{
    int i,len;
    char c;
    scanf("%s",ch);

    len = strlen(ch);
    for(i = 0;i < len;i++)
    {
        if(!is_op(ch[i]))//if a number
            num.push(ch[i]);
        else//if a symbol
        {
            if(op.empty())
                op.push(ch[i]);
            else
            {
                if(ch[i] != ')')//the new one is not a ')'
                {
                    if(op.top() == '('||fun(op.top()) < fun(ch[i]))
                        op.push(ch[i]);
                    else
                    {
                        while(!num.empty())
                        {
                            last.push(num.front());
                            num.pop();
                        }
                        if(!op.empty())
                        {
                            last.push(op.top());
                            op.pop();
                        }
                        --i;
                    }
                }
                else//the new one is a ')'
                {
                    while(!num.empty())
                    {
                        last.push(num.front());
                        num.pop();
                    }
                    while(!op.empty())
                    {
                        if(op.top() != '(')
                        {
                            c = op.top();
                            last.push(op.top());
                            op.pop();
                        }
                        else
                        {
                            op.pop();
                            if(!op.empty())
                            {
                                if(op.top() != '('&&fun(op.top()) > fun(c))
                                {
                                    last.push(op.top());
                                    op.pop();
                                    break;
                                }
                                else
                                    break;
                            }
                        }
                    }
                }
            }
        }
    }
    while(!num.empty())
    {
        last.push(num.front());
        num.pop();
    }
    while(!op.empty())
    {
        last.push(op.top());
        op.pop();
    }
    while(last.size()>=2)
    {
        cout<<last.front()<<" ";
        last.pop();
    }
    cout<<last.front()<<endl;
    last.pop();
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值