Evaluate the value of inverse polish expression for Stack applications, polish expression
1 # include <stdio. h> 2 # include <stdlib. h> 3 # include <ctype. h> 4 5 # define OK 1 6 # define ERROR 0 7 # define STACK_INIT_SIZE 20 8 # define STACK_INCREMENT 10 9 # define DIGITBUFFER 10 10 11 typedef int Status; 12 typedef double Elemtype; 13 typedef struct StackNode {14 Elemtype * base; 15 Elemtype * top; 16 int stackSize; 17} StackNode; 18 typedef struct StackNode * Stack; 19 20 Status InitStack (Stack s ){ 21 s-> base = (Elemtype *) malloc (sizeof (Elemtype) * STACK_INIT_SIZE); 22 if (! S-> base) 23 return ERROR; 24 s-> top = s-> base; 25 s-> stackSize = STACK_INIT_SIZE; 26 return OK; 27} 28 Status Pop (Stack s, Elemtype * result) {29 if (s-> base = s-> top) 30 return ERROR; 31 * result = * (-- s-> top); 32 return ERROR; 33} 34 Status Push (Stack s, Elemtype value) {35 if (s-> top-s-> base = s-> stackSize) {36 s-> base = (Elemtype *) realloc (s-> base, sizeof (Elemtype) * (STACK_INIT_SIZE + STACK_INCR EMENT); 37 if (! S-> base) 38 return ERROR; 39 s-> top = s-> base + STACK_INIT_SIZE; 40 s-> stackSize = STACK_INIT_SIZE + STACK_INCREMENT; 41} 42 * (s-> top) = value; 43 s-> top ++; 44 return OK; 45} 46 int StackLenth (Stack s) {47 return s-> top-s-> base; 48} 49 Status RPT () {// reverse polish notation 50 char c; 51 double operater1, operater2; 52 double result; 53 int I = 0; 54 char bufferDigit [DIGITBUFFER]; 55 56 Stack S; 57 InitStack (s); 58 59 printf ("Please Enter Reverse Polish Notation! (RPN) \ n "); 60 printf (" ------ note: separated by space between ------- \ n "); 61 printf (" ------ number or operator. end of '#' ------- \ n "); 62 63 scanf (" % c ", & c); 64 while (c! = '#') {65/* handle input numbers: Because % c is used to accept input, 66 * input % c for Multiple Digits such as 123 cannot be processed. Therefore, the char array bufferDigit is set to cache the number of multiple digits that input 67. After the multi-digit input is started, the multi-digit 68 * input will be ended with a space. Therefore, if (c = '') is used to determine the end of the Multi-digit input. To convert Multiple Digits cached in the char 69 * array to double and Push. 70 */71 while (isdigit (c) | c = '. ') {72 if (I = 10) {73 printf ("number is too lager \ n"); 74 return ERROR; 75} 76 bufferDigit [I ++] = c; 77 bufferDigit [I] = '\ 0'; 78 scanf ("% c", & c ); 79 if (c = '') {// if it is not a space, it must be the number 80 result = atof (bufferDigit); 81 Push (s, result); 82 I = 0; 83} 84} 85/* operator 86 */87 switch (c) {88 case '+': 89 Pop (s, & operater1); 90 Pop (s, & operater2); 91 Push (s, operater1 + Operater2); 92 break; 93 case '-': 94 Pop (s, & operater1); 95 Pop (s, & operater2); 96 Push (s, operater2-operater1 ); 97 break; 98 case '*': 99 Pop (s, & operater1); 100 Pop (s, & operater2); 101 Push (s, operater1 * operater2); 102 break; 103 case '/': 104 Pop (s, & operater1); 105 Pop (s, & operater2); 106 Push (s, operater2/operater1); 107 break; 108} 109 scanf ("% c", & c); 110} 111 112 Pop (s, & result); 113 printf ("The result Of RPN is % f \ n ", result); 114} 115 116 // test; 117 Status ShowStack (Stack s) {118 while (s-> base! = S-> top) {119 printf ("% f", * (-- (s-> top); 120} 121 printf ("\ n "); 122} 123 Status Test () {124 Stack s1; 125 InitStack (s1); 126 Push (s1, 1); 127 Push (s1, 2); 128 Push (s1, 3); 129 ShowStack (s1); 130} 131 int main () {132 RPT (); 133 134 Stack s; 135 InitStack (s); 136 Push (s, 1); 137 Push (s, 2); 138 Push (s, 3); 139 ShowStack (s); 140 return 0; 141}142/* the RPT function of the program can be completed, but the Text part is puzzled ~ The code for line 1 is exactly the same as that for line 2 to line 2*129. However, it can run smoothly in the main function, and an error occurs in the Test function (no error prompt) 144 * if the error is 124 ~ The Code in line 1 is changed to the following to run smoothly: 129 StackNode s1; 145 InitStack (& s1); 146 Push (& s1, 1); 147 Push (& s1, 2 ); 149 Push (& s1, 3); 150 ShowStack (& s1); 151 I don't know why I look forward to guidance from Daniel. 152 **/