SDUT OJ算术表达式的转换

Toggle navigationHome

算术表达式的转换

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic Discuss

Problem Description

小明在学习了数据结构之后,突然想起了以前没有解决的算术表达式转化成后缀式的问题,今天他想解决一下。

   因为有了数据结构的基础小明很快就解出了这个问题,但是他突然想到怎么求出算术表达式的前缀式和中缀式呢?小明很困惑。聪明的你帮他解决吧。

Input

 输入一算术表达式,以\'#\'字符作为结束标志。(数据保证无空格,只有一组输入)

Output

 输出该表达式转换所得到的前缀式 中缀式 后缀式。分三行输出,顺序是前缀式 中缀式 后缀式。

Sample Input

a*b+(c-d/e)*f#

Sample Output

+*ab*-c/def
a*b+c-d/e*f
ab*cde/-f*+

Hint

 

Source

#include <stdio.h>
#include<string.h>
int st(char ch)
{
    if(ch=='-'||ch=='+')return 1;
    else if(ch=='/'||ch=='*')return 2;
    else if(ch=='(')return 3;
    else if(ch==')')return 4;
    return 0;
}
void q(char str[])
{
    char a[10100];
    char b[10100];
    char c[10100];
    int n;
    int p=0,i,pp=0,k=0;
    n=strlen(str);
    for(i=n-2; i>=0; i--)
    {
        a[k++]=str[i];

    }
    a[k]='#';
    for(i=0; a[i]!='#'; i++)
    {
        if((a[i]>='a'&&a[i]<='z')||(a[i]>='A'&&a[i]<='Z'))
        {
            b[p++]=a[i];
        }
        else
        {
            if(a[i]==')')
                c[++pp]=a[i];
            else if(a[i]=='(')
            {
                while(c[pp]!=')')
                {
                    b[p++]=c[pp];
                    pp--;
                }
                pp--;
            }
            else
            {
                if(st(a[i])<st(c[pp]))
                {
                    if(c[pp]==')')
                    {
                        c[++pp]=a[i];
                    }
                    else
                    {
                        b[p++]=c[pp];
                        c[pp]=a[i];

                    }
                }
                else
                {
                    c[++pp]=a[i];
                }
            }
        }
    }
    while(pp!=0)
    {
        b[p++]=c[pp];
        pp--;
    }
    for(i=p-1; i>=0; i--)
    {
        printf("%c",b[i]);
    }
    printf("\n");
}
void z(char str[])
{
    int i;
    for(i=0; str[i]!='#'; i++)
    {
        if(str[i]!='('&&str[i]!=')')
        {
            printf("%c",str[i]);
        }
    }
    printf("\n");

}
void h(char str[])
{
    char b[10100];
    int top=0;
    int i;
    for(i=0; str[i]!='#'; i++)
    {
        if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))
            printf("%c",str[i]);
        else
        {
            if(top==0)
            {
                top++;
                b[top]=str[i];
            }
            else if(st(str[i])>st(b[top]))
            {
                if(st(str[i])==4)
                {
                    while(b[top]!='(')
                    {
                        printf("%c",b[top]);
                        top--;
                    }
                    top--;
                }
                else
                {
                    top++;
                    b[top]=str[i];
                }
            }
            else
            {
                if(b[top]=='(')
                {
                    top++;
                    b[top]=str[i];
                }
                else
                {
                    printf("%c",b[top]);
                    b[top]=str[i];
                }
            }
        }

    }
    while(top!=0)
    {
        printf("%c",b[top]);
        top--;
    }
    printf("\n");
}
int main()
{
    char str[1000000];
    while(scanf("%s",str)!=EOF)
    {
        q(str);
        z(str);
        h(str);
    }
    return 0;
}

还行,理解了前缀式 中缀式 后缀式就能吧他的代码写下来啦,

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值