PTA数据结构之表达式转换

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


输入格式:


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


输出格式:


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


输入样例:


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

输出样例:


2 3 7 4 - * + 8 4 / +

代码实现:

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int Status;
typedef char SElemType;
typedef struct
{
    SElemType *base;
    SElemType *top;
    int stacksize;
} SqStack;
//初始化
Status Init_Stack(SqStack &S)
{
    S.base=(SElemType *) malloc(sizeof(SElemType));
    if(!S.base) exit(OVERFLOW);
    S.top=S.base;
    S.stacksize=STACK_INIT_SIZE;
    return OK;
}
//入栈
Status Push_Stack(SqStack &S,SElemType e)
{
    //考虑扩容
    if(S.top-S.base==S.stacksize)
    {
        S.base=(SElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
        if(!S.base) exit(OVERFLOW);
        S.top=S.base+S.stacksize;
        S.stacksize+=STACKINCREMENT;
    }
    *S.top++=e;
    return OK;
}
//出栈
Status Pop_Stack(SqStack &S,SElemType &e)
{
    if(S.base==S.top) return ERROR;
    e=*(--S.top);
    return OK;
}
//判断空栈
Status Empty_Stack(SqStack &S)
{
    if(S.base==S.top) return OK;
    else
        return ERROR;
}
//获取栈顶元素
char Get_top(SqStack S)
{
    if(S.top==S.base) return 0;
    else
        return *(S.top-1);
}
int Judge_yxj(char c)
{
    if(c=='+'||c=='-')
        return 1;
    if(c=='*'||c=='/')
        return 2;
    if(c=='(')
        return 3;
    if(c==')')
        return 4;
}
int judge_shu(char c)
{
    if(c=='*'||c=='-'||c=='+'||c=='/')
        return 1;
    else
        return 0;
}
int Priority(char ch1,char ch2)
{
    if(ch1=='*' || ch1=='/' || ch1=='+' || ch1=='-')
        if(ch2=='+' || ch2=='-')
            return 1;
    return 0;
}
int main()
{
    SqStack S;
    Init_Stack(S);
    string e;
    char top,c;
    cin >> e;
    int flag=0;
    for(int i=0; i<e.length(); i++)
    {
        if(i==0 && e[i]=='-')//正负
        {
            cout << e[i];
            continue;
        }
        if(i==0 && e[i]=='+')
            cout<<"";
        if(e[i]=='-' && !(e[i-1]>='0' && e[i-1]<='9'))
        {
            if(flag==1)
                cout << " ";
            cout << e[i];
            flag=0;
            continue;
        }
        if(e[i]=='+' && e[i-1]=='(')
            continue;
        if(e[i]>='0' && e[i]<='9')
        {
            if(flag==1)
                cout << " ";
            cout << e[i];
            while((e[i+1]>='0' && e[i+1]<='9') || e[i+1]=='.')
                cout << e[++i];
            flag=1;
            continue;
        }
        if(e[i]=='(')
        {
            Push_Stack(S,e[i]);
            continue;
        }
        if(e[i]==')')
        {
            while(1)
            {
                top=Get_top(S);
                if(top=='(')
                {
                    break;
                }
                else
                {
                    Pop_Stack(S,c);
                    cout<<" "<<c;
                }
            }
            Pop_Stack(S,c);
            continue;
        }
        while(!Empty_Stack(S) && Priority(*(S.top-1),e[i]))
        {
            char ch;

            Pop_Stack(S,ch);
            cout << " " << ch;
        }
        Push_Stack(S,e[i]);
    }
    while(!Empty_Stack(S))
    {
        char ch;
        Pop_Stack(S,ch);
        cout << " " << ch;
    }
}


  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值