加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

227. Basic Calculator II

发布时间:2020-12-14 21:52:08 所属栏目:大数据 来源:网络整理
导读:Implement a basic calculator to evaluate a simple expression string. The expression string contains only?non-negative?integers,? + ,? - ,? * ,? / ?operators and empty spaces? . The integer division should truncate toward zero. Example 1 :I

Implement a basic calculator to evaluate a simple expression string.

The expression string contains only?non-negative?integers,?+,?-,?*,?/?operators and empty spaces?. The integer division should truncate toward zero.

Example 1:

Input: "3+2*2"
Output: 7

写一个计算器,实现四则运算。

?

用逆波兰表示法求解:两个栈 num 和 opt 分别存 运算数字 和 运算符

1、如果是一个数,入栈 num

2、如果是操作符:

2.1、如果是 + 或 -:把 opt 中的所有符号依次出栈,结合位于 num 栈顶的两个数做运算,把运算结果入栈

2.2、如果是 * 或 /:把 opt 中位于栈顶的 * 或 / 依次出栈,结合位于 num 栈顶的两个数做运算,把运算结果入栈

3、遍历完字符后,把 opt 中的剩余的所有符号依次出栈,结合位于 num 栈顶的两个数做运算,把运算结果入栈

最后num中只剩下一个数,就是运算结果

 1 class Solution {
 2 public:
 3     int cal(char opt,int num1,int num2) {
 4         switch(opt) {
 5             case +:
 6                 return num1 + num2;
 7             case -:
 8                 return num2 - num1;
 9             case *:
10                 return num1 * num2;
11             case /:
12                 return num2 / num1;
13         }
14     }
15     int calculate(string s) {
16         stack<int> num;
17         stack<char> opt;
18         size_t i = 0;
19         size_t next_i;
20         while (i < s.length()) {
21             next_i = s.find_first_of("+-*/",i);
22             if (next_i != string::npos) {
23                 num.push(stoi(s.substr(i,next_i - i)));
24                 char op = s[next_i];
25                 if (op == + || op == -) {
26                     while (!opt.empty()) {
27                         char c = opt.top();
28                         opt.pop();
29                         int num1 = num.top();
30                         num.pop();
31                         int num2 = num.top();
32                         num.pop();
33                         num.push( cal(c,num1,num2) );
34                     }
35                 }
36                 if (op == * || op == /) {
37                     while (!opt.empty() && (opt.top() == * || opt.top() == /)) {
38                         char c = opt.top();
39                         opt.pop();
40                         int num1 = num.top();
41                         num.pop();
42                         int num2 = num.top();
43                         num.pop();
44                         num.push( cal(c,num2) );
45                     }
46                 }
47                 opt.push(op);
48                 i = next_i + 1;
49             }
50             else {
51                 num.push(stoi(s.substr(i,next_i - i)));
52                 break;
53             }
54         }
55         
56         while (!opt.empty()) {
57             char c = opt.top();
58             opt.pop();
59             int num1 = num.top();
60             num.pop();
61             int num2 = num.top();
62             num.pop();
63             num.push( cal(c,num2) );
64         }
65         
66         return num.top();
67 
68     }
69 };

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读