Calculator implemented to calculate a basic value of a simple string expression.
String expressions may comprise a left parenthesis (right parenthesis), plus +, minus -, space and non-negative integers.
Example 1:
Input: "1 + 1"
Output: 2
Example 2:
Input: "2-1 + 2"
Output: 3
Example 3:
Input: "(1 + (4 + 5 + 2) -3) + (6 + 8)"
Output: 23
Description:
You may assume that the given expression is valid.
Do not use the built-in library functions eval.
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/basic-calculator
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
Title probably so, here mainly practice tests STL tools, the difficulty is not particularly large, but he found the time to read with a new model to calculate a method similar to transitions between different states for each situation deal with
Then the first to achieve the most basic function computing functions, because there had only + - two possibilities, multiplication and division have no effect and other factors, and therefore only consider + - operation irrespective of multiplication and division of power and other priorities.
Design data structure is a two stack is a data stack is a stack operators, because in case the brackets to first calculate the result of the current operators and saved, and then calculates, when only appears only bracket end Further operation may last saved, so the demand calculation function is taken from the stack of data elements to the top two stack elements and operators performing operations to stack.
Here implemented calculation function.
void compute (stack<long int> &number_stack ,stack<char> &operation_stack) { if(number_stack.size()< 2)//special type like "(12345)" { return; } long int num2 = number_stack.top(); number_stack.pop(); long int num1 = number_stack.top(); number_stack.pop(); if(operation_stack.top() == '+') { number_stack.push(num1+num2); } else if(operation_stack.top()=='-') { number_stack.push(num1-num2); } operation_stack.pop(); }
Then there is the string to deal with.
We consider all of the following cases
The content may be read into digital: Digital There are two cases, one digit or more digits, if the next read of digital content in a digital or as long as the current number * 10 + the next number to get the right number number.
The content may be read as + - when the case is described direct calculation using only the current operator to read the stored operation result to the next number and saved
The content may be read as a (then state is prioritized operation is not directly next to the content and the content of the previous operation, the operation may need to flag that is set to 0 to not operational until I met) only after results the results of the current and preceding calculates
The content may be read to) then state have priority operation has ended, we only need to direct operation
Then the next digital content is not the place to note is that in some cases such as digital read-in time then you need to read the current pointer string processing is performed backspace does not obstruct the back of the operation because the whole circle of i is has been in the auto-incremented.
Thus is may be generally think this state of FIG.
Is the process of converting to the three states after the end of a specific design for the different results may be obtained for processing.
class Solution{ public: long int calculate(string s) { static const int STATE_BEGIN = 0; static const int NUMBER_STATE = 1; static const int OPERATION_STATE = 2; stack<long int> number_stack; stack<char> operation_stack; long int number = 0; int STATE = STATE_BEGIN; int compuate_flag = 0; for(int i = 0;i < s.length();i++) { if(s[i]==' ') continue; switch(STATE) { case STATE_BEGIN: if(s[i] >= '0' && s[i] <= '9') { STATE = NUMBER_STATE; } else { STATE = OPERATION_STATE; } i--;//back i break; case NUMBER_STATE: if(s[i] >= '0' && s[i] <= '9') { number = number * 10 + s[i] - '0'; } else { number_stack.push(number); if(compuate_flag == 1) { compute(number_stack,operation_stack); } number = 0; i--; STATE = OPERATION_STATE; } break; case OPERATION_STATE: if(s[i] == '+' || s[i] == '-') { operation_stack.push(s[i]); compuate_flag = 1; } else if(s[i] == '(') { STATE = NUMBER_STATE; compuate_flag = 0; } else if(s[i] >= '0' && s[i] <= '9') { STATE = NUMBER_STATE; i--; } else if(s[i] == ')') { compute(number_stack,operation_stack); } break; } } if(number != 0) { number_stack.push(number); compute(number_stack,operation_stack); } if(number == 0 && number_stack.empty()) { return 0; } return number_stack.top(); } void compute (stack<long int> &number_stack ,stack<char> &operation_stack) { if(number_stack.size()< 2)//special type like "(12345)" { return; } long int num2 = number_stack.top(); number_stack.pop(); long int num1 = number_stack.top(); number_stack.pop(); if(operation_stack.top() == '+') { number_stack.push(num1+num2); } else if(operation_stack.top()=='-') { number_stack.push(num1-num2); } operation_stack.pop(); } };
Finally, we just set a string test Jiuhaola
1 #include<stdio.h> 2 #include<stack> 3 #include<iostream> 4 #include<string> 5 6 using namespace std; 7 class Solution{ 8 public: 9 long int calculate(string s) 10 { 11 static const int STATE_BEGIN = 0; 12 static const int NUMBER_STATE = 1; 13 static const int OPERATION_STATE = 2; 14 stack<long int> number_stack; 15 stack<char> operation_stack; 16 long int number = 0; 17 int STATE = STATE_BEGIN; 18 int compuate_flag = 0; 19 20 for(int i = 0;i < s.length();i++) 21 { 22 if(s[i]==' ') 23 continue; 24 25 switch(STATE) 26 { 27 case STATE_BEGIN: 28 if(s[i] >= '0' && s[i] <= '9') 29 { 30 STATE = NUMBER_STATE; 31 } 32 else 33 { 34 STATE = OPERATION_STATE; 35 } 36 i--;//back i 37 break; 38 39 case NUMBER_STATE: 40 if(s[i] >= '0' && s[i] <= '9') 41 { 42 number = number * 10 + s[i] - '0'; 43 } 44 else 45 { 46 number_stack.push(number); 47 if(compuate_flag == 1) 48 { 49 compute(number_stack,operation_stack); 50 } 51 number = 0; 52 i--; 53 STATE = OPERATION_STATE; 54 } 55 break; 56 case OPERATION_STATE: 57 if(s[i] == '+' || s[i] == '-') 58 { 59 operation_stack.push(s[i]); 60 compuate_flag = 1; 61 } 62 else if(s[i] == '(') 63 { 64 STATE = NUMBER_STATE; 65 compuate_flag = 0; 66 67 } 68 else if(s[i] >= '0' && s[i] <= '9') 69 { 70 STATE = NUMBER_STATE; 71 i--; 72 } 73 else if(s[i] == ')') 74 { 75 76 compute(number_stack,operation_stack); 77 } 78 break; 79 } 80 } 81 if(number != 0) 82 { 83 number_stack.push(number); 84 compute(number_stack,operation_stack); 85 } 86 if(number == 0 && number_stack.empty()) 87 { 88 return 0; 89 } 90 return number_stack.top(); 91 } 92 void compute (stack<long int> &number_stack ,stack<char> &operation_stack) 93 { 94 if(number_stack.size()< 2)//special type like "(12345)" 95 { 96 97 return; 98 } 99 long int num2 = number_stack.top(); 100 number_stack.pop(); 101 long int num1 = number_stack.top(); 102 number_stack.pop(); 103 104 if(operation_stack.top() == '+') 105 { 106 107 number_stack.push(num1+num2); 108 } 109 else if(operation_stack.top()=='-') 110 { 111 112 number_stack.push(num1-num2); 113 } 114 operation_stack.pop(); 115 } 116 }; 117 int main() 118 { 119 string s = "1+121 - (14+(5-6) )"; 120 Solution solve; 121 printf("%d\n",solve.calculate(s)); 122 return 0; 123 124 }