c++表达式求值——简易版

问题:本程序表达式求值很简陋,因为:

  • 操作数只有0-9,这个比较好改动,但是简易版嘛,就偷个懒
  • 操作符只有+ - × / 这个要是加个括号感觉就麻烦大了,时间有限,所以~

主要思想:

  • 有两个栈,一个保存操作数,一个保存操作符
  • 当前字符是数字时,直接入栈
  • 当前字符是操作符时,分2种情况
    • 操作符栈空,直接入栈
    • 操作符栈非空,把当前的操作符与栈顶的操作符进行优先级的比较。若栈顶操作符优先级>=当前操作符,就将操作数栈中的前2个操作数拿出来运算,并删除,然后把算出来的结果在入操作数栈
  • 最后再将操作符栈里的字符退栈,并依次进行计算。这样最后操作数中剩下的那个数就是表达式的值

代码如下:

#include <iostream>
#include <stack>
#include <string>
using namespace std;
stack<char> op;
stack<int> num;

int chartoint(char c){//字符转数字
    return c-48;
}
int isoperate(char c){
    /*
    判断字符是数字还是操作符
    操作符:返回1
    数字:返回0
    */
    if(c=='+'||c=='-'||c=='*'||c=='/')
        return 1;
    else return 0;
}
int priori(char c){
    /*
    判断一个字符的优先级,返回的是它的优先级
    */
    if(c=='*'||c=='/') return 2;
    if(c=='+'||c=='-') return 1;
}
int iswhatandcompute(char c,int n1, int n2){
    /*
    c:操作符
    n1,n2:操作数
    返回的是n2 c n1 的值
    */
    if (c=='*') return n1*n2;
    if(c=='/') return n2/n1;
    if(c=='+') return n2+n1;
    if(c=='-') return n2-n1;
}
void compute(string s){//计算表达式的值
    int i;
    int size_s=s.size();
    for(i=0;i<size_s;){
        if (isoperate(s[i])){//判断s[i]是操作符
            if(!op.empty()){//操作符栈非空
                char temp=op.top();
                if(priori(temp)>=priori(s[i])){
                    int n1=num.top();num.pop();
                    int n2=num.top();num.pop();
                    int n3=iswhatandcompute(temp,n1,n2);
                    num.push(n3);
                    op.pop();
                    op.push(s[i]);
                    i++;
                }
                else{
                    op.push(s[i]);i++;
                }
            }
            else
                {op.push(s[i]); i++;}
        }
        else{//是数字
            int n=chartoint(s[i]);
            num.push(n);i++;
        }
    }
    while(!op.empty()){
        int t=op.top();op.pop();
        int t1=num.top();num.pop();
        int t2=num.top();num.pop();
        int t3=iswhatandcompute(t,t1,t2);
        num.push(t3);
    }
    int n=num.top();
    cout<<n<<endl;
}
int main(int argc, char const *argv[])
{
    string str;
    cin>>str;
    compute(str);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/liunan199481/article/details/78597961