• 编程菜鸟的日记-初学尝试编程-正整数表达式的四则运算


    //计算正整数算术表达式可分两个步骤1)将中缀表达式转换成后缀表达式;2)计算后缀表达式的值

    #include <iostream>

    using namespace std;

    #define Maxsize 20

    //1)将中缀表达式转换成后缀表达式;

    void trans(char *exp,char postexp) //将exp转换成postexp

    {

       struct

         {

            char data[Maxsize];//

             int top;//栈指针

          }op;//运算符栈

        int i=0;//postexp的下标

        op.top=-1;

        while(*exp!='')

          {

                switch(*exp)

                 {

                      case'(':

                        op.top++;op.data[op.top]=*exp;

                        exp++;

                        break;

                     case')':

                       while(op.data[op.top]!='(')//查找运算符栈顶是否为除(外的其他运算符

                          { postexp[i++]=op.data[op.top];//存在,则逐个出栈存在postexp中

                             op.top--;

                          }

                          op.top--;//直到在运算符栈中遇到(,则将(出栈

                          exp++;//继续扫描exp

                          break;

                       case'+':

                       case'-'://判断栈是否为空以及(前是否存在其他运算符

                          while(op.top!=-1 && op.data[op.top]!='(')

                             { post[i++]=op.data[op.top];//存在则将运算符保存在postexp中

                                op.top--;

                              }

                           op.top++;                      

                           op.data[op.top]=*exp;

                           exp++;

                           break;

                        case'*':

                        case'/'://判断运算符栈中是否存在*或者/

                         while(op.data[op.top]=='*'||op.data[op.top]=='/')//保存新遇到的*或者/之前,先将已有的*或者/出栈存在postexp中

                            { postexp[i++]=op.data[op.top];

                               op.top--;

                             }

                            op.top++;//将新的*或者/存在运算符栈中

                            op.data[op.top]=*exp;

                            exp++;

                            break;

                          case' ':break;//过滤掉空格

                          default://处理数字字符

                              while(*exp>='0' && *exp<='9')

                                     {postexp[i++]=*exp;

                                        exp++;

                                      }

                                 postexp[i++]='#';

                             }

                       }

           while(op.top!=-1)//扫描完exp,将运算符栈中剩余的字符逐个出栈保存在postexp中

              { postexp[i++]=op.data[op.top];

                 op.top--;

               }

           postexp[i]='';//给postexp结束标识符

    }

    //2)计算后缀表达式的值

    float compvalue(char *postexp)

    {

         struct

           {

                float data[Maxsize];

                int top;

           }st;

        float a,b,c,d;

        st.top=-1;

        while(*postexp!='')

          {

                 switch(*postexp)

                   {

                      case'+':

                          a=st.data[st.top];

                          st.top--;

                          b=st.data[st.top];

                          st.top--;

                          c=b+a;

                          st.top++;

                          st.data[st.top]=c;

                          break;

                      case'-':

                          a=st.data[st.top];

                          st.top--;

                          b=st.data[st.top];

                          st.top--;

                          c=b-a;

                          st.top++;

                          st.data[st.top]=c;

                          break;

                      case'*':

                          a=st.data[st.top];

                          st.top--;

                          b=st.data[st.top];

                          st.top--;

                          c=b*a;

                          st.top++;

                          st.data[st.top]=c;

                          break;

                      case'/':

                          a=st.data[st.top];

                          st.top--;

                          b=st.data[st.top];

                          st.top--;

                          if(a)

                           {

                               c=b/a;

                               st.top++;

                               st.data[st.top]=c;

                            }

                           else

                            {

                               cout<<"除零错误!"<<endl;

                                 exit(0);

                              }

                             break;

                       default:

                         d=0;

                          while(*postexp>='0' && *postexp<='9')

                              { d=10*d+*postexp-'0';

                                 postexp++;

                              }

                           st.top++;

                           st.data[st.top]=d;

                           break;

                    }

                   postexp++;

             }

         return(st.data[st.top]);

    }

    //调用函数,测试表达式可由键盘输入

    void main()
    {
     char exp[Maxsize];//"6+3*(1+3*4)";
     cout<<"请输入中缀表达式:";
     cin>>exp;
     char postexp[Maxsize];
     trans(exp,postexp);
     float val;
     val=compvalue(postexp);
     cout<<"中缀表达式:"<<exp<<endl;
     cout<<"后缀表达式"<<postexp<<endl;
     cout<<"表达式的值:"<<val<<endl;
     system("pause");
    }                                      

                                                            

                       

                     

       

  • 相关阅读:
    基于Twisted的简单聊天室
    小学题的python实现
    初识Go(8)
    初识Go(7)
    初识Go(6)
    初识Go(5)
    初识Go(4)
    初识Go(3)
    初识Go(2)
    初识Go(1)
  • 原文地址:https://www.cnblogs.com/lynnycy/p/3390984.html
Copyright © 2020-2023  润新知