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]);

}



C语言中,我们可以使用顺序栈结构来实现中缀表达式转后缀表达式的操作。这可以通过遍历中缀表达式的每个字符,并按照一定的规则将其转换为后缀表达式来完成。 中缀表达式转后缀表达式的原因是为了更方便地进行计算和处理。逆波兰式(也叫后缀表达式)将运算符写在操作数之后,避免了括号的使用和运算符的优先级问题,使得表达式的计算更加直观和简单。 在转换中缀表达式到后缀表达式的过程中,我们可以使用堆栈来辅助实现。按照以下规则进行操作: 1. 遍历中缀表达式的每个字符。 2. 若遇到操作数,直接输出到后缀表达式中。 3. 若遇到运算符,判断运算符的优先级: - 若堆栈为空,或者堆栈顶部元素为左括号'(',直接将运算符入栈。 - 若堆栈顶部元素的优先级小于当前运算符,将当前运算符入栈。 - 若堆栈顶部元素的优先级大于或等于当前运算符,将堆栈顶部的运算符依次弹出并输出到后缀表达式中,直到堆栈为空或者遇到左括号'('为止,然后将当前运算符入栈。 4. 若遇到左括号'(',直接入栈。 5. 若遇到右括号')',将堆栈顶部的运算符依次弹出并输出到后缀表达式中,直到遇到左括号'(',然后将左括号弹出但不输出。 6. 遍历结束后,将堆栈中剩余的运算符依次弹出并输出到后缀表达式中。 通过以上规则,我们可以将中缀表达式转换为后缀表达式。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [c语言实现中缀表达式转后缀并求值](https://download.csdn.net/download/kongchengyeyu/10336788)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [C语言中缀表达式转后缀表达式](https://blog.csdn.net/m0_55124878/article/details/120423212)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [C语言实现中缀表达式到后缀表达式的转换](https://blog.csdn.net/DarkSide_/article/details/104182084)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值