zoukankan      html  css  js  c++  java
  • (算法)求表达式(含括号)的值

    题目:

    求字符串表达式的值,如"-2+(3+4)-5*6",返回-25.

    思路:

    分离数字和运算符并考虑运算符的优先级,如(),*,/,+,-等。

    两个栈:一个存数字,一个存运算符

    代码:

    #include<iostream>
    #include<stack>
    #include<sstream>
    #include<string>
    using namespace std;
    
    double calc(double a,char op,double b){
        if(op=='+')
            return a+b;
        if(op=='-')
            return a-b;
        if(op=='*')
            return a*b;
        if(op=='/')
            return a/b;
    }
    
    bool isdigit(char s){
        int a=s-'0';
        if(a>=0 && a<=9)
            return true;
        return false;
    }
    
    template<class out_T,class in_T>
    out_T convert(const in_T &t){
        stringstream ss;
        out_T result;
        ss<<t;
        ss>>result;
        return result;
    }
    
    int main(){
        string s;
        double x,y;
        char op;
        int priv[300];
        priv['+']=priv['-']=2;
        priv['*']=priv['/']=1;
        priv['(']=10;
    
        while(cin>>s){
            stack<double> num;
            stack<char> oper;
            int len=s.size();
            int start=0;    
            string str;
            char last=0;
    
            for(int i=0;i<len;i++){
                if(isdigit(s[i])){
                //    num.push(s[i]-'0');
                    start=i;
                    for(;i+1<len && (isdigit(s[i+1]) || s[i+1]=='.');i++);
                    str=s.substr(start,i-start+1);
                    num.push(convert<double>(str));
                }
                else if(s[i]=='('){
                    oper.push(s[i]);
                }
                else if(s[i]==')'){
                    while(!oper.empty() && oper.top()!='('){
                        x=num.top();
                        num.pop();
                        y=num.top();
                        num.pop();
                        op=oper.top();
                        oper.pop();
                        num.push(calc(x,op,y));
                    }
                    oper.pop();
                }
                else if(s[i]=='-' && (last==0 || last=='(')){
                    num.push(0.0);
                    oper.push('-');
                }
                else if(priv[s[i]]>0){
                    while(oper.size()>0 && priv[s[i]]>=priv[oper.top()]){
                        y=num.top();
                        num.pop();
                        x=num.top();
                        num.pop();
                        op=oper.top();
                        oper.pop();
                        num.push(calc(x,op,y));
                    }
                    oper.push(s[i]);
                }
                else
                    continue;
                last=s[i];
            }
            while(oper.size()>0){
                y=num.top();
                num.pop();
                x=num.top();
                num.pop();
                op=oper.top();
                oper.pop();
                num.push(calc(x,op,y));
            }
            cout<<num.top()<<endl;
        }
        return 0;
    }
  • 相关阅读:
    js设计模式-迭代器模式
    js设计模式-代理模式
    js设计模式-策略模式
    js设计模式-单例模式
    js设计模式中的高阶函数的一些应用
    深入react技术栈记录(二)
    深入react技术栈记录(一)
    响应式开发(1)
    Angular Js 与bootstrap, angular 与 vue.js
    for in 中的index
  • 原文地址:https://www.cnblogs.com/AndyJee/p/4907682.html
Copyright © 2011-2022 走看看