1 #include<stack> 2 #include<math.h> 3 #include<iostream> 4 #include<string.h> 5 6 using namespace std; 7 8 const char* s1 = "8 4 + 6 2 * -"; 9 const char* s2 = "2 3 5 + * 7 1 / + 4 -"; 10 11 float calculation(float i, char j, float k) 12 { 13 if ('!' == j) 14 { 15 float factorial = 1; 16 for (int a = 1; a <= i; ++a) 17 { 18 factorial *= a; 19 } 20 return factorial; 21 } 22 else if ('+' == j) 23 { 24 return k + i; 25 } 26 else if ('-' == j) 27 { 28 return k - i; 29 } 30 else if ('*' == j) 31 { 32 return k * i; 33 } 34 else if ('/' == j) 35 { 36 return k / i; 37 } 38 else if ('^' == j) 39 { 40 return pow(k, i); 41 } 42 } 43 44 float calculationStr(const char* S) 45 { 46 stack<float> numStack; 47 for (int i = 0; i < strlen(S); ++i) 48 { 49 if(isalnum(S[i])) 50 { 51 string str = ""; 52 while(isalnum(S[i])) 53 { 54 str += S[i++]; 55 } 56 --i; 57 numStack.push(atof(str.c_str())); 58 } 59 else if(' ' != S[i] && '