HDU-简单计算器-1237

这道题我做了一天,把中缀表达式转化为后缀表达式,但遇到了储存的问题,考虑了好久,写出后又调试,弄了一天,下面说一下中缀表达式转换后缀表达式:

算法:

中缀表达式转后缀表达式的方法:
1.遇到操作数:直接输出(添加到后缀表达式中)
2.栈为空时,遇到运算符,直接入栈
3.遇到左括号:将其入栈
4.遇到右括号:执行出栈操作,并将出栈的元素输出,直到弹出栈的是左括号,左括号不输出。
5.遇到其他运算符:加减乘除:弹出所有优先级大于或者等于该运算符的栈顶元素,然后将该运算符入栈
6.最终将栈中的元素依次出栈,输出。
例如
a+b*c+(d*e+f)*g ----> abc*+de*f+g*+

遇到a:直接输出:
后缀表达式:a
堆栈:空

遇到+:堆栈:空,所以+入栈
后缀表达式:a
堆栈:+
遇到b: 直接输出
后缀表达式:ab
堆栈:+
遇到*:堆栈非空,但是+的优先级不高于*,所以*入栈
后缀表达式: ab
堆栈:*+
遇到c:直接输出
后缀表达式:abc
堆栈:*+
遇到+:堆栈非空,堆栈中的*优先级大于+,输出并出栈,堆栈中的+优先级等于+,输出并出栈,然后再将该运算符(+)入栈
后缀表达式:abc*+
堆栈:+
遇到(:直接入栈
后缀表达式:abc*+
堆栈:(+
遇到d:输出
后缀表达式:abc*+d
堆栈:(+
遇到*:堆栈非空,堆栈中的(优先级小于*,所以不出栈
后缀表达式:abc*+d
堆栈:*(+
遇到e:输出
后缀表达式:abc*+de
堆栈:*(+
遇到+:由于*的优先级大于+,输出并出栈,但是(的优先级低于+,所以将*出栈,+入栈
后缀表达式:abc*+de*
堆栈:+(+
遇到f:输出
后缀表达式:abc*+de*f
堆栈:+(+
遇到):执行出栈并输出元素,直到弹出左括号,所括号不输出
后缀表达式:abc*+de*f+
堆栈:+
遇到*:堆栈为空,入栈
后缀表达式: abc*+de*f+
堆栈:*+
遇到g:输出
后缀表达式:abc*+de*f+g
堆栈:*+
遇到中缀表达式结束:弹出所有的运算符并输出
后缀表达式:abc*+de*f+g*+
堆栈:空

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

union{
    double n;
    char m;
}a[1000];

int main()
{
    double aq[1000];
    int top1,top2,top3,len,i,d;
    char b[1000],gst[1000];
    while(gets(gst)>0)
    {
        memset(a,0,sizeof(a));
        memset(aq,0,sizeof(aq));
         len=strlen(gst);
        if(gst[0]=='0'&&len==1)
            break;
        top1=top2=top3=-1;
        d=0;
      
        for(i=0; i<len; i++)
        {
            if(gst[i]!=' ')
            {
                if(gst[i]>='0'&&gst[i]<='9')
                {
                    if(d==0)
                        top1++;
                    a[top1].n=a[top1].n*10+gst[i]-48;
                }
                d=1;
                switch(gst[i])
                {
                case'+':
                    while(b[top2]=='*'||b[top2]=='/'||b[top2]=='+'||b[top2]=='-')
                    {
                        if(b[top2]=='*')
                        {
                            top1++;
                            a[top1].m='*';
                            top2--;
                        }
                        if(b[top2]=='/')
                        {
                            top1++;
                            a[top1].m='/';
                            top2--;
                        }
                        if(b[top2]=='+')
                        {
                            top1++;
                            a[top1].m='+';
                            top2--;
                        }
                        if(b[top2]=='-')
                        {
                            top1++;
                            a[top1].m='-';
                            top2--;
                        }
                    }
                    if(top2==-1)
                    {
                        top2++;
                        b[top2]='+';
                    }
                    break;
                case'-':
                    while(b[top2]=='*'||b[top2]=='/'||b[top2]=='+'||b[top2]=='-')
                    {
                        if(b[top2]=='*')
                        {
                            top1++;
                            a[top1].m='*';
                            top2--;
                        }
                        if(b[top2]=='/')
                        {
                            top1++;
                            a[top1].m='/';
                            top2--;
                        }
                        if(b[top2]=='+')
                        {
                            top1++;
                            a[top1].m='+';
                            top2--;
                        }
                        if(b[top2]=='-')
                        {
                            top1++;
                            a[top1].m='-';
                            top2--;
                        }
                    }
                    if(top2==-1)
                    {
                        top2++;
                        b[top2]='-';
                    }
                    break;
                case'*':
                    while(b[top2]=='*'||b[top2]=='/')
                    {
                        if(b[top2]=='*')
                        {
                            top1++;
                            a[top1].m='*';
                            top2--;
                        }
                        if(b[top2]=='/')
                        {
                            top1++;
                            a[top1].m='/';
                            top2--;
                        }
                    }
                    top2++;
                    b[top2]='*';
                    break;
                case'/':
                    while(b[top2]=='*'||b[top2]=='/')
                    {
                        if(b[top2]=='*')
                        {
                            top1++;
                            a[top1].m='*';
                            top2--;
                        }
                        if(b[top2]=='/')
                        {
                            top1++;
                            a[top1].m='/';
                            top2--;
                        }
                    }
                    top2++;
                    b[top2]='/';
                    break;
                }
            }
            else
            {
                d=0;
            }
        }
       
        for(;top2>=0;top2--)
        {
            switch(b[top2])
            {
            case'+':
                top1++;
                a[top1].m=b[top2];
                break;
            case'-':
                top1++;
                a[top1].m=b[top2];
                break;
            case'*':
                top1++;
                a[top1].m=b[top2];
                break;
            case'/':
                top1++;
                a[top1].m=b[top2];
                break;
            }
        }
        for(i=0;i<((len+1)/2);i++)
        {
            if(a[i].m!='+'&&a[i].m!='-'&&a[i].m!='*'&&a[i].m!='/')
            {
                top3++;
                aq[top3]=a[i].n;
            }
            else
            {
                switch(a[i].m)
                {
                case'+':
                    aq[top3-1]+=aq[top3];
                    top3--;
                    break;
                case'-':
                    aq[top3-1]-=aq[top3];
                    top3--;
                    break;
                case'*':
                    aq[top3-1]*=aq[top3];
                    top3--;
                    break;
                case'/':
                    aq[top3-1]/=aq[top3];
                    top3--;
                    break;
                }
            }
        }
        printf("%.2lf\n",aq[0]);
    }
    return 0;
}

这题是练习栈的很好的题,一队友子的代码,没用栈的知识,很好理解:

#include<stdio.h>
#include<string.h>
int main()
{
    char a[1000];
    int i,len,j,k,t,e;
    double b[10000],flag[10000],sum;
    while(gets(a))
    {
        len=strlen(a);k=0;
        for (i=0;i<len;i++)
        if(a[i]=='0')
        k++;
        if(k==len)
        break;
        memset(b,0,sizeof(b));
        memset(flag,0,sizeof(flag));
        k=0;
        for (i=0,j=0;i<=len;i++)
        {
            if(a[i]>='0'&&a[i]<='9')
            k++;
            else if(a[i]==' '||i==len)
            {
                int s=1.0;
                for (t=0,e=i-1;t<k;t++,e--)
                {
                    if(t!=0)
                b[j]=(double)(a[e]-'0')*s+b[j];
                else
                b[j]=(double)(a[e]-'0')*1.0;
                s*=10;
                }

if(k)
                j++;
                k=0;
            }
            else if(a[i]=='+')
            flag[j++]=1;
            else if(a[i]=='-')
            flag[j++]=2;
            else if(a[i]=='*')
            flag[j++]=3;
            else if(a[i]=='/')
            flag[j++]=4;
        }

for (i=0;i<j;i++)
        {
            if(flag[i]==3)
                {
                    b[i+1]=b[i+1]*b[i-1];
                    b[i-1]=0;
                    if (flag[i-2]==2)
                     flag[i]=2;
                     else flag[i]=1;
                }
            else if(flag[i]==4)
                {
                    b[i+1]=b[i-1]/b[i+1];
                     b[i-1]=0;
                     if (flag[i-2]==2)
                     flag[i]=2;
                     else flag[i]=1;
                }
        }

for(i=1;i<j;i++)
        {
            if(flag[i]==2)
            {b[i+1]=-b[i+1];}
        }
        sum=0;
        for (i=0;i<j;i++)
        sum+=b[i];
        printf ("%.2lf\n",sum);
    }
    return 0;
}

上一篇:Spring Cloud Stream消费失败后的处理策略(四):重新入队(RabbitMQ)


下一篇:Java_字符类(Character、String、StringBuffer)_char是基本数据类型,Character是其包装类型。