【topic】
Given a string str, str represents a formula, there may be an integer in the formula, the left and right parentheses Math symbol, the formula returns the results.
Examples
str = "48 * ((70-65) -43) + 8 * 1", returns --1816.
str = "3 + 1 * 4", 7 return.
str = "3+ (1 * 4)", returns 7.
[Description]
1. It can be considered a given string must be the right formula, that do not need to check the validity of the formula str.
2. If it is negative, you need to be enclosed in parentheses, such as "4 * (- 3)." But if the negative beginning or the beginning of a formula as part of the brackets, they can no parentheses, such as "-3 * 4" and "(-3) * 4" are legitimate.
3. The overflow regardless of the calculation process will occur
[Solving]
Generally use the stack, because stack processing parentheses good question
1, there is no parentheses
Use the stack:
First, the use of string str stack decomposition
When faced with a number, the data began to cloud operator end, when faced with the operator, the data end, the characters begin
And then the data stack operators, if the top of '*' or '/', the top of the stack below the operator and calculates the data out to the current data, the results obtained and then pushed onto the stack .
When the character string data is then pressed into the finished, followed by a pop-up data from the stack and the operator performs operation to
2, there is in brackets
Defined function fun (str, index)
Represents a character string data from index calculated, knowing the right parenthesis encountered or the end of the end of the string, i.e. a simple internal brackets is implemented without brackets fun arithmetic operation
【Code】
1 #pragma once 2 #include <iostream> 3 #include <string> 4 #include <stack> 5 #include <sstream> 6 #include <vector> 7 using namespace std; 8 9 10 class calEquation 11 { 12 public: 13 int cal(const string str)//计算 14 { 15 if (str.length() == 0) 16 return 0; 17 return calFunc(str, 0)[0]; 18 } 19 20 private: 21 vector<int> calFunc(const string str, int index) 22 { 23 stack<string>data; 24 int num = 0; 25 vector<int>v; 26 while (index < str.length() && str[index] != ')') 27 { 28 if (str[index] >= '0'&&str[index] <= '9') 29 num = num * 10 + str[index++] - '0'; 30 else if (str[index] != '(') 31 { 32 addNum(data, num); 33 string sybol = ""; 34 sybol = str[index++]; 35 data.push (sybol); // is the operator, plus 36 NUM = 0 ; 37 [ } 38 is the else // left bracket 39 { 40 V = calFunc (STR, + index . 1 ); 41 is NUM = V [ 0 ] ; 42 is index = V [ . 1 ] + . 1 ; 43 is } 44 is } 45 addNum (Data, NUM); 46 is return {getNum (Data), index}; 47 } 48 49 void addNum(stack<string>&data, int num) 50 { 51 if (!data.empty()) 52 { 53 if (data.top() == "*" || data.top() == "/") 54 { 55 string add = data.top(); 56 data.pop(); 57 int b = stringToNum(data.top()); 58 data.pop(); 59 num = add == "*" ? b * num : b / num; 60 } 61 } 62 data.push(numToStr(num)); 63 } 64 65 66 int getNum(stack<string>&data) 67 { 68 int sum = 0; 69 bool flag = true; 70 while (!data.empty()) 71 { 72 string str = data.top(); 73 data.pop(); 74 if (str == "+") 75 flag = true; 76 else if (str == "-") 77 flag = false; 78 else 79 sum += flag ? stringToNum(str) : -stringToNum(str); 80 } 81 return sum; 82 } 83 84 int stringToNum(string str)//Numeric characters turn 85 { 86 int NUM; 87 the stringstream SS; 88 SS << STR; 89 SS >> NUM; 90 return NUM; 91 is } 92 93 String numToStr ( int NUM) // digital-to-character 94 { 95 String STR; 96 the stringstream SS; 97 SS << NUM; 98 SS >> STR; 99 return STR; 100 } 101 }; 102 103 104 void Test() 105 { 106 string str; 107 calEquation* p = new calEquation; 108 str = "3+4"; 109 cout << str << "= " << p->cal(str) << endl; 110 111 str = "3+4*5+2"; 112 cout << str << "= " << p->cal(str) << endl; 113 114 str = "3+2+3*(1+3)"; 115 cout << str << "= " << p->cal(str) << endl; 116 }