File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed
solutions/224.Basic_Calculator Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * Author: illuz <iilluzen[at]gmail.com>
3
+ * File: AC_stack_n.cpp
4
+ * Create Date: 2015-09-07 19:07:22
5
+ * Descripton:
6
+ */
7
+
8
+ #include < bits/stdc++.h>
9
+
10
+ using namespace std ;
11
+ const int N = 0 ;
12
+
13
+ class Solution {
14
+ public:
15
+ int calculate (string s) {
16
+ stack<int > nums;
17
+ stack<int > signs;
18
+ int num = 0 , res = 0 , sign = 1 ;
19
+ for (auto ch : s) {
20
+ if (ch == ' ' ) continue ;
21
+ if (isdigit (ch)) {
22
+ num = num * 10 + (ch - ' 0' );
23
+ } else {
24
+ res += num * sign;
25
+ num = 0 ;
26
+ if (ch == ' +' ) sign = 1 ;
27
+ if (ch == ' -' ) sign = -1 ;
28
+ if (ch == ' (' ) {
29
+ nums.push (res);
30
+ signs.push (sign);
31
+ res = 0 ;
32
+ sign = 1 ;
33
+ }
34
+ if (ch == ' )' ) {
35
+ res = nums.top () + res * signs.top ();
36
+ nums.pop ();
37
+ signs.pop ();
38
+ }
39
+ }
40
+ }
41
+ res += sign * num;
42
+ return res;
43
+ }
44
+ };
45
+
46
+ int main () {
47
+
48
+ return 0 ;
49
+ }
50
+
You can’t perform that action at this time.
0 commit comments