首页 > 其他 > 详细

227. Basic Calculator II

时间:2015-12-21 23:20:10      阅读:224      评论:0      收藏:0      [点我收藏+]

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.

You may assume that the given expression is always valid.

Some examples:

"3+2*2" = 7
" 3/2 " = 1
" 3+5 / 2 " = 5

 

Note: Do not use the eval built-in library function.

 

 1 class Solution {
 2 public:
 3     int calculate(string s) {
 4         stack<int> stack;
 5         int res = 0;
 6         int len = s.length();
 7         for (int i = 0; i < len; i++) {
 8             char c = s[i];
 9             if (isdigit(c)) {
10                 int cur = c - 0;
11                 while (i + 1 < len && isdigit(s[i + 1])) {
12                     cur = cur * 10 + s[i + 1] - 0;
13                     i++;
14                 }
15                 stack.push(cur);
16             } else if (c == +) {
17                 if (stack.size() == 1)
18                     stack.push(1);
19                 else {
20                     res = stack.top();
21                     stack.pop();
22                     res = res * stack.top();
23                     stack.pop();
24                     res = res + stack.top();
25                     stack.pop();
26                     stack.push(res);
27                     stack.push(1);
28                 }
29             } else if (c == -) {
30                 if (stack.size() == 1)
31                     stack.push(-1);
32                 else {
33                     res = stack.top();
34                     stack.pop();
35                     res = res * stack.top();
36                     stack.pop();
37                     res = res + stack.top();
38                     stack.pop();
39                     stack.push(res);
40                     stack.push(-1);
41                 }
42             } else if (c == *) {
43                 //avoid space
44                 while (i + 1 < len && !isdigit(s[i + 1])) {
45                     i++;
46                 }
47                 int cur = s[++i] - 0;
48                 while (i + 1 < len && isdigit(s[i + 1])) {
49                     cur = cur * 10 + s[i + 1] - 0;
50                     i++;
51                 }
52                 res = stack.top() * cur;
53                 stack.pop();
54                 stack.push(res);
55             } else if (c == /) {
56                 while (i + 1 < len && s[i + 1] ==  ) {
57                     i++;
58                 }
59                 int cur = s[++i] - 0;
60                 while (i + 1 < len && isdigit(s[i + 1])) {
61                     cur = cur * 10 + s[i + 1] - 0;
62                     i++;
63                 }
64                 res = stack.top() / cur;
65                 stack.pop();
66                 stack.push(res);
67             }
68         }
69         if (stack.size() == 1)
70             return stack.top();
71         else {
72             res = stack.top();
73             stack.pop();
74             res = res * stack.top();
75             stack.pop();
76             res = res + stack.top();
77             return res;
78         }
79     }
80 };

 

227. Basic Calculator II

原文:http://www.cnblogs.com/wxquare/p/5064966.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!