class Solution {
public:
    int calculate(string s) {
        stack<int> num;
        stack<char> symbol;
        for(int i=0;i<s.length();i++){
            if(s[i]==' ')   continue;
            else if(s[i]=='('||s[i]=='+'||s[i]=='-')  symbol.push(s[i]);
            else if(s[i]>='0'&&s[i]<='9'){
                for(int j=i+1;j<s.length();j++){
                    if(s[j]<'0'||s[j]>'9'){
                        num.push(stoi(s.substr(i,j-i)));
                        i=j-1;
                        break;
                    }
                       
                }
            }
            else if(s[i]==')'){
                stack<int> tem_i;
                stack<char> tem_c;
                while(symbol.top()!='('){
                    tem_i.push(num.top());
                    num.pop();
                    tem_c.push(symbol.top());
                    symbol.pop();
                }
                symbol.pop();
                while(!tem_c.empty()){
                    char c=tem_c.top();
                    tem_c.pop();
                    if(c=='+'){
                        int a=tem_i.top();
                        tem_i.pop();
                        int b=tem_i.top();
                        tem_i.pop();
                        tem_i.push(a+b);
                    }
                    if(c=='-'){
                        int a=tem_i.top();
                        tem_i.pop();
                        int b=tem_i.top();
                        tem_i.pop();
                        tem_i.push(a-b);
                    }
                }
                num.push(tem_i.top());
                tem_i.pop();
            }
        }
        stack<int> tem_i;
        stack<char> tem_c;
        while(!symbol.empty()){
            tem_i.push(num.top());
            num.pop();
            tem_c.push(symbol.top());
            symbol.pop();
        }
        while(!tem_c.empty()){
                    char c=tem_c.top();
                    tem_c.pop();
                    if(c=='+'){
                        int a=tem_i.top();
                        tem_i.pop();
                        int b=tem_i.top();
                        tem_i.pop();
                        tem_i.push(a+b);
                    }
                    if(c=='-'){
                        int a=tem_i.top();
                        tem_i.pop();
                        int b=tem_i.top();
                        tem_i.pop();
                        tem_i.push(a-b);
                    }
        }
        return tem_i.top();
    }
    int stoi(string s){
        int res=0;
        for(int i=0;i<s.length();i++){
            res=res*10+(s[i]-'0');
        }
        return res;
    }
};

LeetCode() Basic Calculator 不知道哪里错了的更多相关文章

  1. [LeetCode] Basic Calculator II 基本计算器之二

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  2. [LeetCode] Basic Calculator 基本计算器

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  3. LeetCode Basic Calculator II

    原题链接在这里:https://leetcode.com/problems/basic-calculator-ii/ Implement a basic calculator to evaluate ...

  4. LeetCode Basic Calculator

    原题链接在这里:https://leetcode.com/problems/basic-calculator/ Implement a basic calculator to evaluate a s ...

  5. [LeetCode] Basic Calculator III 基本计算器之三

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  6. [LeetCode] Basic Calculator IV 基本计算器之四

    Given an expression such as expression = "e + 8 - a + 5" and an evaluation map such as {&q ...

  7. [LeetCode] Basic Calculator & Basic Calculator II

    Basic Calculator Implement a basic calculator to evaluate a simple expression string. The expression ...

  8. LeetCode——Basic Calculator II

    Description: Implement a basic calculator to evaluate a simple expression string. The expression str ...

  9. LeetCode——Basic Calculator

    Description: Implement a basic calculator to evaluate a simple expression string. The expression str ...

随机推荐

  1. springMVC之<context:annotation-config />标签

    springMVC的配置文件中经常见到<context:annotation-config/>,那么这句话的作用到底是什么呢? 现在的注解非常方便,但是系统如何才能识别注解呢,这就需要相应 ...

  2. 获得APP当前显示的viewController

    UIViewController* topViewController = ((UINavigationController*)self.window.rootViewController).topV ...

  3. Python学习笔记-抽象

    懒惰即美德.代码量少. hasattr判断函数是否可用. 创建函数. def hello(name) return 'hello,'+name+'!' 文档化函数: 加注释(#开头) 文档字符串.函数 ...

  4. Qt之Qwt学习之安装

    QWT+qtcreator 编译.安装使用 目录:一.Qwt简介 二.QWT编译 一.Qwt简介 QWT:Qt Widgets for Technical Applications,是开源的2D绘图库 ...

  5. shared_ptr和多线程

    前一篇文章写得实在太挫,重新来一篇. 多线程环境下生命周期的管理 多线程环境下,跨线程对象的生命周期管理会有什么挑战?我们拿生产者消费者模型来讨论这个问题. 实现一个简单的用于生产者消费者模型的队列 ...

  6. 记AppStore 被打回的经历

    在快驰已然有半年时间之久. 见证了“快货运”产品,在不断摧残的环境中成长着.  两个人,将一个产品亲手从无到有的构建,有过心酸.有过累和有过憔悴,但当“快货运”开始上APP store时,又让人觉得开 ...

  7. SQL server存储过程语法及实例(转)

    存储过程如同一门程序设计语言,同样包含了数据类型.流程控制.输入和输出和它自己的函数库. --------------------基本语法-------------------- 一.创建存储过程cr ...

  8. #define用法解析

    #define Add(a,b) a+b; 在一般使用的时候是没有问题的,但是如果遇到如: c * Add(a,b) * d 的时候就会出现问题,代数式的本意是a+b然后去和c,d相乘,但是因为使用了 ...

  9. windows 下用eclipse搭建java、python开发环境

    本人只针对小白!本文只针对小白!本文只针对小白! 最近闲来无事,加上之前虽没有做过eclipse上java.python的开发工作,但一直想尝试一下.于是边查找资料边试验,花了一天时间在自己的机器上用 ...

  10. js的异常捕获

    try{ ...some code... }catch(e){ ...some code... //处理错误 throw(e.name); //抛出异常 }finally{<BR> // ...