/*
题目:
算术表达式有前缀表示法、中缀表示法和后缀表示法等形式。
前缀表达式指二元运算符位于两个运算数之前
例如2+3*(7-4)+8/4的前缀表达式是:+ + 2 * 3 - 7 4 / 8 4。
请设计程序计算前缀表达式的结果值。
输入格式:
输入在一行内给出不超过30个字符的前缀表达式,
只包含+、-、*、/以及运算数,不同对象(运算数、运算符号)之间以空格分隔。
输出格式:
输出前缀表达式的运算结果,保留小数点后1位,或错误信息ERROR。
*/
前缀转中缀方法
此题主要把握前缀表达式 转成 中缀表达式 的方法,然后用堆栈进行表达就可以了
对于下面一串前缀表达式,从右向左扫描进行下列处理
如图,可总结出
1.从右向左扫描,遇到数字,将数字入栈
2.遇到运算符号,将最上面的两个数字出栈,进行符号运算
3.将运算结果重新入栈
根据上述原理,编出下列代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<stdbool.h> 5 #define Maxsize 100 6 typedef struct SNode1* stack1; 7 typedef struct SNode2* stack2; 8 typedef char ElementType1; 9 typedef double ElementType2; 10 struct SNode1 11 { 12 ElementType1 Data[Maxsize]; 13 int Top; 14 }; 15 struct SNode2 16 { 17 ElementType2 Data[Maxsize]; 18 int Top; 19 }; 20 21 stack1 CreatStack1(); 22 stack2 CreatStack2(); 23 bool push1(stack1, ElementType1); 24 bool push2(stack2, ElementType2); 25 ElementType1 pop1(stack1); 26 ElementType2 pop2(stack2); 27 bool IsEmpty1(stack1); 28 bool IsEmpty2(stack2); 29 stack1 change1(stack1); 30 stack2 change2(stack2); 31 32 int main(void) 33 { 34 int len, i = 0; 35 double num, a = 0, b = 0, sum = 0; 36 char str[Maxsize][Maxsize], opera; 37 38 stack1 s; 39 stack2 number; 40 s = CreatStack1(); 41 number = CreatStack2(); 42 43 while (scanf("%s", str[i]) != EOF) 44 { 45 i++;//这里采用二维数组的方式,记录下所有的值 46 } 47 48 i--;//从右向左 49 while (i >= 0) 50 { 51 if ((str[i][0] == '+' || str[i][0] == '-' || str[i][0] == '*' || str[i][0] == '/') && !(str[i][1] >= '0' && str[i][1] <= '9')) 52 { 53 a = pop2(number); 54 b = pop2(number); 55 switch (str[i][0]) 56 { 57 case '+': 58 sum = a + b; 59 push2(number, sum); 60 break; 61 case '-': 62 sum = a - b; 63 push2(number, sum); 64 break; 65 case '*': 66 sum = a * b; 67 push2(number, sum); 68 break; 69 case '/': 70 if (b != 0) 71 { 72 sum = a / b; 73 push2(number, sum); 74 break; 75 } 76 else 77 { 78 printf("ERROR"); 79 return 0; 80 } 81 82 } 83 } 84 else 85 { 86 num = atof(str[i]);//如果值是数字,那么使用atof函数进行转化 87 push2(number, num); 88 } 89 i--; 90 } 91 sum = pop2(number); 92 printf("%.1lf", sum); 93 return 0; 94 } 95 96 stack1 CreatStack1() 97 { 98 stack1 s = (stack1)malloc(sizeof(struct SNode1)); 99 s->Top = -1; 100 return s; 101 } 102 stack2 CreatStack2() 103 { 104 stack2 s = (stack2)malloc(sizeof(struct SNode2)); 105 s->Top = -1; 106 return s; 107 } 108 109 bool push1(stack1 s, ElementType1 ch) 110 { 111 if (s->Top == Maxsize - 1) 112 { 113 return false; 114 } 115 else 116 { 117 s->Data[++(s->Top)] = ch; 118 return true; 119 } 120 } 121 122 bool push2(stack2 s, ElementType2 num) 123 { 124 if (s->Top == Maxsize - 1) 125 { 126 return false; 127 } 128 else 129 { 130 s->Data[++(s->Top)] = num; 131 return true; 132 } 133 } 134 135 ElementType1 pop1(stack1 s) 136 { 137 if (s->Top == -1) 138 { 139 printf("The Stack1 is Empty\n"); 140 } 141 else 142 { 143 return (s->Data[(s->Top)--]); 144 } 145 } 146 147 ElementType2 pop2(stack2 s) 148 { 149 if (s->Top == -1) 150 { 151 printf("The Stack2 is Empty\n"); 152 } 153 else 154 { 155 return (s->Data[(s->Top)--]); 156 } 157 } 158 159 bool IsEmpty1(stack1 s) 160 { 161 if (s->Top == -1) 162 return true; 163 else 164 return false; 165 } 166 167 bool IsEmpty2(stack2 s) 168 { 169 if (s->Top == -1) 170 return true; 171 else 172 return false; 173 } 174 175 stack1 change1(stack1 s) 176 { 177 stack1 s1; 178 s1 = CreatStack1(); 179 while (!IsEmpty1(s)) 180 { 181 char ch = pop1(s); 182 push1(s1, ch); 183 } 184 return s1; 185 } 186 187 stack2 change2(stack2 s) 188 { 189 stack2 s1; 190 s1 = CreatStack2(); 191 while (!IsEmpty2(s)) 192 { 193 int num = pop2(s); 194 push2(s1, num); 195 } 196 return s1; 197 }