C语言中缀表达式转后缀表达式并利用后缀表达式求值 (小于10)

#include<stdio.h>

#include<stdlib.h>

int precedence(char c)

{

    if(c=='*'||c=='/')

        return 2;

    if(c=='+'||c=='-')

        return 1;

    return 0;

}

int main()

{

    char stack1[1000]; int top_index1=-1;

    char stack2[1000]; int top_index2=-1;

    char c;

    while(1)

    {

        scanf("%c",&c);

        if(c=='\n')

            break;

        else if(c>='0'&&c<='9')

        {

            top_index2++;

            stack2[top_index2]=c;

        }

        else if(c=='(')

        {

            top_index1++;

            stack1[top_index1]=c;

        }

        else if(c==')')

        {

            while(stack1[top_index1]!='(')

            {

                top_index2++;

                stack2[top_index2]=stack1[top_index1];

                top_index1--;

            }

            top_index1--;

        }

        else if(c=='+'||c=='-'||c=='*'||c=='/')

        {

            if(top_index1==-1||precedence(c)>precedence(stack1[top_index1]))

            {

                top_index1++;

                stack1[top_index1]=c;

            }

            else

            {

                while(top_index1>=0&&precedence(stack1[top_index1])>=precedence(c))

                {

                    top_index2++;

                    stack2[top_index2]=stack1[top_index1];

                    top_index1--;

                }

                top_index1++;

                stack1[top_index1]=c;

            }

        }

    }

    while(top_index1!=-1)

    {

        top_index2++;

        stack2[top_index2]=stack1[top_index1];

        top_index1--;

    }

    

   // printf("%s\n",stack2); printf("%d\n",top_index2);

    int stack3[1000]; int top_index3=-1;

    for(int i=0;i<=top_index2;i++)

    {

        if(stack2[i]>='0'&&stack2[i]<='9')

        {

            top_index3++;

            stack3[top_index3]=stack2[i]-48;

            continue;

        }

        else

        {

            if(stack2[i]=='+')

            {

                int t1=stack3[top_index3];

                int t2=stack3[top_index3-1];

                top_index3--;

                stack3[top_index3]=t1+t2;

            }

            if(stack2[i]=='-')

            {

                int t1=stack3[top_index3];

                int t2=stack3[top_index3-1];

                top_index3--;

                stack3[top_index3]=t2-t1;

            }

            if(stack2[i]=='*')

            {

                int t1=stack3[top_index3];

                int t2=stack3[top_index3-1];

                top_index3--;

                stack3[top_index3]=t1*t2;

            }

            if(stack2[i]=='/')

            {

                int t1=stack3[top_index3];

                int t2=stack3[top_index3-1];

                top_index3--;

                stack3[top_index3]=t2/t1;

            }

        }

    }

    printf("%d",stack3[0]);

}



  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值