1 #include<iostream> 2 #include<stack> 3 #include<ctype.h> 4 #include<math.h> 5 #include<string> 6 using namespace std; 7 8 static char s1[] = "2*(3+5)+7/1-4 "; 9 static char s2[] = "(1+2^3!-4)*(5!-(6-(7-(89-0!)))) "; 10 11 // 运算符优先级数组 12 const char pri[9][9] = { 13 {'>', '>', '<', '<', '<', '<', '<', '>', '>'}, 14 {'>', '>', '<', '<', '<', '<', '<', '>', '>'}, 15 {'>', '>', '>', '>', '<', '<', '<', '>', '>'}, 16 {'>', '>', '>', '>', '<', '<', '<', '>', '>'}, 17 {'>', '>', '>', '>', '>', '<', '<', '>', '>'}, 18 {'>', '>', '>', '>', '>', '>', ' ', '>', '>'}, 19 {'<', '<', '<', '<', '<', '<', '<', '=', ' '}, 20 {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, 21 {'<', '<', '<', '<', '<', '<', '<', ' ', '='}, 22 }; 23 24 // 运算符映射 25 int symbolIndex(char str) 26 { 27 if ('+' == str) 28 return 0; 29 else if ('-' == str) 30 return 1; 31 else if ('*' == str) 32 return 2; 33 else if ('/' == str) 34 return 3; 35 else if ('^' == str) 36 return 4; 37 else if ('!' == str) 38 return 5; 39 else if ('(' == str) 40 return 6; 41 else if (')' == str) 42 return 7; 43 else if ('