[Informatics osai yiben Tong] Part 3 _ stack ex00004cale (suffix 7), informatics osai
In fact, it takes a lot of effort to replace the Suffix in this fix. It took nearly three hours to implement the algorithm for the first time after the algorithm is understood. ORZ
# Include <stdio. h> # include <string. h> # include <ctype. h> char Mstr [511], Msta [511] = {'@'}, Bstr [511]; int sta [511]; const short list [4] [4] = {0,-, 1}, {,}, {, 1,-2}, {-1, -1, 1 }}; int level (char c) {switch (c) {case '+': case '-': return 0; break; case '*': case '/': return 1; break; case '(': return 2; break; case ')': return 3 ;}} int main () {// freopen (" 6.6.in"," r ", stdin); // freopen (". out "," w ", stdo Ut); _ Bool flag = 1; int I, Br =-1, Ma = 0; gets (Mstr); for (I = 0; Mstr [I-1]! = '@'; I ++) {if (isdigit (Mstr [I]) {Bstr [++ Br] = Mstr [I]; flag = 1 ;} else {if (flag = 1) {Bstr [++ Br] = ''; flag = 0;} int lr = level (Mstr [I]), la = level (Msta [Ma]); int res; if (Msta [Ma] = '@') res = 1; else if (Mstr [I] = '@') res =-1; else res = list [lr] [la]; if (res = 2) {Ma --; continue;} do {switch (res) {case 1: Msta [++ Ma] = Mstr [I]; break; case-1: bstr [++ Br] = Msta [Ma --]; res = list [lr] [lev El (Msta [Ma])]; break; case-2: printf ("input error! "); Return 0;} if (res = 2) Ma --;} while (res =-1); if (res = 0) {Bstr [++ Br] = Msta [Ma]; Msta [Ma] = Mstr [I] ;}} printf ("% s", Bstr); return 0 ;}Symbol 6 of the first version
However, the logic was not strong, and I still couldn't understand it clearly. So it took me nearly two hours to fight again the next day. Using Long variable names does not require comments, and logic self-recognition is much more advanced.
1 # include <stdio. h> 2 # include <string. h> 3 # include <ctype. h> 4 5 char Mstr [32] = {0}, Msta [32] = {0}, Bstr [32] = {0 }; 6 int Bsta [32] = {0}; 7 int MstrLen, BstrLen, MstaTop, BstrTop, BstaTop; 8 9 const short kind (char c) {10 switch (c) {11 case ')': return-1; 12 case '@': return 0; 13 case '+': 14 case '-': return 1; 15 case '*': 16 case '/': return 2; 17 case '(': return 3; 18 case '^': return 4; 19} 20} 21 22 void Mstr_Bsta (void) {23 gets (Mstr); 24 MstrLen = strlen (Mstr); 25 Mstr [MstrLen ++] = '@'; 26 MstaTop = BstrTop =-1; 27 Msta [++ MstaTop] = '@'; 28 29 int SpaceFlag, NoSmallerFlag; 30 int I; 31 32 for (I = 0; I <MstrLen; I ++) {33 if (isdigit (Mstr [I]) {34 Bstr [++ BstrTop] = Mstr [I]; 35 SpaceFlag = 1; 36} 37 else {38 if (SpaceFlag = 1) {39 Bstr [++ BstrTop] = ''; 40 SpaceFlag = 0; 41} 4 2 43 int KindMstr, KindMsta; 44 do {45 NoSmallerFlag = 1; 46 KindMstr = kind (Mstr [I]); 47 KindMsta = kind (Msta [MstaTop]); 48 49 if (KindMstr> KindMsta | (KindMstr = 3 & KindMsta = 4) | (KindMsta = 3 & KindMstr! =-1) 50 Msta [++ MstaTop] = Mstr [I]; 51 else if (KindMstr = KindMsta) {52 Bstr [++ BstrTop] = Msta [MstaTop]; 53 Msta [MstaTop] = Mstr [I]; 54} 55 else {56 if (KindMstr =-1 & KindMsta = 3) {57 -- MstaTop; 58 break; 59} 60 else 61 Bstr [++ BstrTop] = Msta [MstaTop --]; 62 NoSmallerFlag = 0; 63} 64} while (KindMstr <KindMsta & NoSmallerFlag = 0 ); 65} 66} 67} 68 69 int PowerQuickly (int x, int n) {// fast Speed Power 70 int ans = 1; 71 72 while (n> 0) {73 if (n & 1 = 1) 74 ans * = x; 75 n/= 2; 76 x * = x; 77} 78 return ans; 79} 80 81 void Bsta_Count (void) {82 int I; 83 84 for (I = 0, BstaTop = 0; bstr [I]! = '@';) {85 if (isdigit (Bstr [I]) {86 int num = Bstr [I ++]-'0 '; 87 while (Bstr [I]! = '') 88 num = num * = 10 + Bstr [I ++]-'0'; 89 Bsta [++ BstaTop] = num; 90 I ++; 91} 92 else {93 switch (Bstr [I ++]) {94 case '+': Bsta [BstaTop-1] + = Bsta [BstaTop --]; break; 95 case '-': Bsta [BstaTop-1]-= Bsta [BstaTop --]; break; 96 case '*': Bsta [BstaTop-1] * = Bsta [BstaTop --]; break; 97 case '/': Bsta [BstaTop-1]/= Bsta [BstaTop --]; break; 98 case '^': Bsta [BstaTop-1] = PowerQuickly (Bsta [BstaTop-1], bsta [BstaTop]); BstaTop --; 99} 100} 101} 102 103 int main () {104 Mstr_Bsta (); 105 Bsta_Count (); 107 printf ("% d", Bsta [BstaTop]); 108 109 return 0; 110}View Code