栈的应用之表达式求值

对栈的简单应用把中缀表达式变为后缀表达式后进行计算,可以进行简单的四则运算也包含有括号的运算

#include<bits/stdc++.h>
#include<iostream>
using namespace std;
bool cmp(char a,char b)//当前字符与栈顶字符优先级的判断
{
    if(a=='*'||a=='/')
    {

        if(b=='*'||b=='/')
        return false;
        else
            return true;
    }`在这里插入代码片`
    else
        return false;
}
int main()
{
     string s;
     cin>>s;
     stack<char> op;//操作符栈
     stack<double> num;//操作数栈,数字类型为double
     int l=s.length();
     for(int i=0;i<l;i++)
     {
         char c=s[i];
         if(c=='(')//栈外的左括号优先级最高
                op.push(s[i]);
         else if(c>='0'&&c<='9')//数字直接进入操作字栈
         {
            double tt=(c-'0')*1.0;
             while(s[i+1]>='0'&&s[i+1]<='9')//字母到数字的转换
             {
                 tt=(tt*10+s[i+1]-'0')*1.0;
                 i++;

             }
             num.push(tt);//转换的数字进栈

         }
       else if(c==')')
       {

           char opt=op.top();//取得栈顶元素
            while(opt!='(')
             {
                 double x=num.top();num.pop();//取得操作字栈的栈顶数字,出栈
                  double y=num.top();num.pop();
                  double te;
                   if(opt=='+') te=x+y;
                    else if(opt=='-') te=y-x;
                     else if(opt=='*') te=x*y;
                     else if(opt=='/') te=y/x;
                        num.push(te);
                        op.pop();
                        opt=op.top();

             }
             op.pop();//遇到右括号出栈
       }
    else
        {
             if(op.empty())
                  op.push(c);
               else
                {
                 char cp=op.top();
               if(cp=='('||cmp(c,cp))//栈内的右括号等级最低
                      op.push(c);
                 else
                 {
                     while(!cmp(c,cp)&&!op.empty()&&cp!='(')
                           {
                               op.pop();
                  double x=num.top();num.pop();//取得操作字栈的栈顶数字,出栈
                  double y=num.top();num.pop();
                  double te;
                   if(cp=='+') te=x+y;
                    else if(cp=='-') te=y-x;
                     else if(cp=='*') te=x*y;
                     else if(cp=='/') te=y/x;//运算判断
                       num.push(te);
                          if(!op.empty())
                               cp=op.top();

                           }
                          op.push(c);

                 }

               }
        }
     }
     while(!op.empty())//当操作符栈不为空时
     {

         char opt=op.top();
          double x=num.top();num.pop();//取得操作字栈的栈顶数字,出栈
                  double y=num.top();num.pop();
                  double te;
                   if(opt=='+') te=x+y;
                    else if(opt=='-') te=y-x;
                     else if(opt=='*') te=x*y;
                     else if(opt=='/') te=y/x;
                       num.push(te);
                       op.pop();

     }
     cout<<num.top()<<endl;
}

发布了218 篇原创文章 · 获赞 24 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/Wanglinlin_bfcx/article/details/104432904