算术表达式的转换

算术表达式的转换

Time Limit: 1000MS Memory Limit: 65536KB
Problem Description
小明在学习了数据结构之后,突然想起了以前没有解决的算术表达式转化成后缀式的问题,今天他想解决一下。
   因为有了数据结构的基础小明很快就解出了这个问题,但是他突然想到怎么求出算术表达式的前缀式和中缀式呢?小明很困惑。聪明的你帮他解决吧。
Input
 输入一算术表达式,以\'#\'字符作为结束标志。(数据保证无空格,只有一组输入)
Output
 输出该表达式转换所得到的前缀式 中缀式 后缀式。分三行输出,顺序是前缀式 中缀式 后缀式。
Example Input
a*b+(c-d/e)*f#
Example Output
+*ab*-c/def
a*b+c-d/e*f
ab*cde/-f*+
Hint
 
Author
  看各种百科吧  百科有的真的不如博客
前缀表达式
后缀表达式
其实百科有的逻辑上要求得更严谨而不如分类更明显的那种
#include <bits/stdc++.h>
using namespace std;
int ch(char a)//优先级
{
    if(a=='+'||a=='-') return 1;
    else if(a=='*'||a=='/') return 2;
    else if(a=='(') return 3;
    else if(a==')') return 4;
    return 0;
}
void pre(char s[])
{
    int i,j,t,o=-1,c=-1;
    char sta[200],ans[543];
    for(i=strlen(s)-1; i>=0; i--)
    {
        if(s[i]>='a'&&s[i]<='z')
        {
            ans[++c]=s[i];
        }
        else
        {
            if(o==-1||s[i]==')')
            {
                sta[++o]=s[i];
            }
            else
            {
                if(ch(sta[o])<=ch(s[i]))
                {
                    if(s[i]=='(')
                    {
                        for(;sta[o]!=')';o--)
                        {
                            ans[++c]=sta[o];
                        }
                        o--;
                    }
                    else
                    {
                        sta[++o]=s[i];
                    }
                }
                else//栈顶大于当前操作运算符
                {
                    if(sta[o]==')')
                    {
                        sta[++o]=s[i];
                    }
                    else
                    {
                        ans[++c]=sta[o];
                        sta[o]=s[i];
                    }
                }
            }
        }
    }
    for(i=0;i<=o;i++)
    {
        printf("%c",sta[i]);
    }
    for(i=c;i>=0;i--)
    {
        printf("%c",ans[i]);
    }
    printf("\n");
}
void mid(char s[])
{
    int i,j,o=-1;
    char sta[200];
    for(i=0; i<strlen(s); i++)
    {
        if(s[i]=='('||s[i]==')') continue;
        else printf("%c",s[i]);
    }
    printf("\n");
}
void bac(char s[])
{
    int i,j,t,o=-1;
    char sta[200];
    int f=1;
    for(i=0; i<strlen(s); i++)
    {
        if(s[i]>='a'&&s[i]<='z') printf("%c",s[i]);
        else
        {
            if(o==-1) sta[++o]=s[i];
            else if(ch(s[i])>ch(sta[o]))
            {
                if(ch(s[i])==4)
                {
                    while(sta[o]!='(')
                    {
                        printf("%c",sta[o]);
                        o--;
                    }
                    o--;
                }
                else
                {
                    sta[++o]=s[i];
                }
            }
            else
            {
                if(sta[o]!='(')
                {
                    printf("%c",sta[o]);
                    sta[o]=s[i];
                }
                else
                {sta[++o]=s[i];}
            }

        }
    }
    for(i=o;i>=0;i--)
    {
        printf("%c",sta[i]);
    }
}
int main()
{
    int i,j;
    char c,s[4321];
    int o=-1;
    while(scanf("%c",&c)!=EOF)
    {
        if(c=='#') break;
        else s[++o]=c;
    }
    pre(s);
    mid(s);
    bac(s);
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值