FORWARD!  
  1 #include <stdio.h>
  2 #include <ctype.h>
  3 #include <stdlib.h>
  4 
  5 #define STACK_INIT_SIZE 20
  6 #define STACKINCREMENT  10
  7 #define MAXBUFFER 10
  8 
  9 typedef double ElemType;
 10 typedef struct OPTR_SqStack {
 11     char *base;
 12     char *top;
 13     int stackSize;
 14 } OPTR_SqStack;
 15 
 16 typedef struct OPND_SqStack {
 17     ElemType *base;
 18     ElemType *top;
 19     int stackSize;
 20 } OPND_SqStack;
 21 
 22 void Init_OPTR_Stack(OPTR_SqStack *S) {
 23     S->base = (char *)malloc(STACK_INIT_SIZE * sizeof(char));
 24     if(!S->base) {
 25         printf("Memory allocate failed!\n");
 26         exit(EXIT_FAILURE);
 27     }
 28     S->top = S->base;
 29     S->stackSize = STACK_INIT_SIZE;
 30 
 31     printf("栈初始化成功!\n");
 32 }
 33 
 34 void Init_OPND_Stack(OPND_SqStack *S) {
 35     S->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
 36     if(!S->base) {
 37         printf("Memory allocate failed!\n");
 38         exit(EXIT_FAILURE);
 39     }
 40     S->top = S->base;
 41     S->stackSize = STACK_INIT_SIZE;
 42 
 43     printf("栈初始化成功!\n");
 44 }
 45 
 46 /*将数据元素压栈*/
 47 void Push_OPTR(OPTR_SqStack *S, char data) {
 48     if(S->top - S->base > S->stackSize) {
 49         S->base = (char *)realloc(S->base, (S->stackSize + STACKINCREMENT) * sizeof(char));
 50         if(!S->base) {
 51             printf("Memory allocate failed!\n");
 52             exit(EXIT_FAILURE);
 53         }
 54         S->top = S->base + S->stackSize;
 55         S->stackSize += STACKINCREMENT;
 56     }
 57     /*数据元素入栈*/
 58     *S->top++ = data;
 59     printf("操作符\'%c\'入栈成功!\n", data);
 60 
 61 }
 62 
 63 /*元素出栈*/
 64 void Pop_OPTR(OPTR_SqStack *S, char *data) {
 65     if(S->base == S->top) {
 66         printf("Error: Stack is NULL!\n");
 67     }
 68     *data = *--S->top;
 69     printf("操作符\'%c\'成功出栈!\n", *data);
 70 }
 71 
 72 void Push_OPND(OPND_SqStack *S, ElemType data) {
 73     if(S->top - S->base > S->stackSize) {
 74         S->base = (ElemType *)realloc(S->base, (S->stackSize + STACKINCREMENT) * sizeof(ElemType));
 75         if(!S->base) {
 76             printf("Memory allocate failed!\n");
 77             exit(EXIT_FAILURE);
 78         }
 79         S->top = S->base + S->stackSize;
 80         S->stackSize += STACKINCREMENT;
 81     }
 82     /*数据元素入栈*/
 83     *S->top++ = data;
 84     printf("操作数\'%lf\'入栈成功!\n", data);
 85 
 86 }
 87 
 88 /*元素出栈*/
 89 void Pop_OPND(OPND_SqStack *S, ElemType *data) {
 90     if(S->base == S->top) {
 91         printf("Error: Stack is NULL!\n");
 92     }
 93     *data = *--S->top;
 94 }
 95 
 96 /*取到栈顶的元素*/
 97 void GetTop_OPTR(OPTR_SqStack OPTR, char *data) {
 98     if(OPTR.top == OPTR.base) {
 99         printf("Stack is NULL!\n");
100         exit(EXIT_FAILURE);
101     }
102     *data = *(OPTR.top - 1);
103 }
104 
105 int isEmpty(OPTR_SqStack S) {
106     char theta;
107 
108     GetTop_OPTR(S, &theta);
109     if(S.base == S.top || theta == '#')
110         return 1;
111     return 0;
112 }
113 
114 void GetTop_OPND(OPND_SqStack OPND, ElemType *data) {
115     if(OPND.top == OPND.base) {
116         printf("Stack is NULL!\n");
117         exit(EXIT_FAILURE);
118     }
119     *data = *(OPND.top - 1);
120 }
121 
122 int InOP(char opdata) {
123     switch(opdata) {
124     case '+':
125     case '-':
126     case '*':
127     case '/':
128     case '(':
129     case ')':
130     case '#':
131         return 1;
132     default:
133         return 0;
134     }
135 }
136 
137 int getIndex(char theta) { //获取theta所对应的索引
138     int index = 0;
139     switch(theta) {
140     case '+':
141         index = 0;
142         break;
143     case '-':
144         index = 1;
145         break;
146     case '*':
147         index = 2;
148         break;
149     case '/':
150         index = 3;
151         break;
152     case '(':
153         index = 4;
154         break;
155     case ')':
156         index = 5;
157         break;
158     case '#':
159         index = 6;
160     default:
161         break;
162     }
163     return index;
164 }
165 
166 char getPriority(char theta1, char theta2) { //获取theta1与theta2之间的优先级
167     const char priority[][7] = {   //算符间的优先级关系
168         /* +   -   *   /   (   )  \n*/
169         { '>', '>', '<', '<', '<', '>', '>' }, /*  +  */
170         { '>', '>', '<', '<', '<', '>', '>' }, /*  -  */
171         { '>', '>', '>', '>', '<', '>', '>' }, /*  *  */
172         { '>', '>', '>', '>', '<', '>', '>' }, /*  /  */
173         { '<', '<', '<', '<', '<', '=', '0' }, /*  (  */
174         { '>', '>', '>', '>', '0', '>', '>' }, /*  )  */
175         { '<', '<', '<', '<', '<', '0', '=' }, /*  \n */
176     };
177 
178     int index1 = getIndex(theta1);
179     int index2 = getIndex(theta2);
180     return priority[index1][index2];
181 }
182 
183 int Operate(ElemType operand1, char theta, ElemType operand2) {
184     printf("进入开始计算的函数了!\n");
185     printf("开始计算:%lf %c %lf...\n", operand1, theta, operand2);
186     switch(theta) {
187     case '+':
188         return operand1 + operand2;
189     case '-':
190         return operand1 - operand2;
191     case '*':
192         return operand1 * operand2;
193     case '/':
194         if(operand2 != 0)
195             return operand1 / operand2;
196         else {
197             printf("除数不能为0!\n");
198             exit(EXIT_FAILURE);
199         }
200     }
201     printf("错误的操作符:\'%c\'!\n", theta);
202     exit(EXIT_FAILURE);
203 }
204 
205 int isOperator(char opdata) {
206     switch(opdata) {
207     case '+':
208     case '-':
209     case '*':
210     case '/':
211         return 1;
212     default:
213         return 0;
214     }
215 }
216 
217 int main() {
218     int i = 0;
219     ElemType result = 0;
220     char OPDATA[MAXBUFFER];
221     char opdata;/*只用于输入数据*/
222     char theta;/*用于保存输入的字符*/
223     ElemType operand1, operand2;
224     OPND_SqStack OPND;
225     OPTR_SqStack OPTR;
226 
227     Init_OPTR_Stack(&OPTR);
228     Init_OPND_Stack(&OPND);
229     Push_OPTR(&OPTR, '#');
230 
231     /*如果输入的不是换行符就表示继续接收输入*/
232     printf("\n请输入你想要计算的表达式: ");
233     while((opdata = getchar()) != '#') {
234         GetTop_OPTR(OPTR, &theta);
235         printf("\n此时取到的操作符栈顶元素为:\'%c\'\n", theta);
236 
237         /*对操作数的操作*/
238         printf("此时的输入数据为:\'%c\'\n", opdata);
239         if(isdigit(opdata) || opdata == '.') {
240             OPDATA[i++] = opdata;
241         }
242 
243         /*对运算符的处理if(InOP(opdata))*/
244         else {
245             if(i != 0) {
246                 OPDATA[i] = '\0';
247                 operand1 = atof(OPDATA);
248                 Push_OPND(&OPND, operand1);
249                 i = 0;
250             }
251 
252             switch(getPriority(theta, opdata)) { //此处有问题
253             case '<':
254                 /*如果当前的运算符的优先级比前面的高,就入栈*/
255                 printf("\n当前操作符:\'%c\'优先级比前一个\'%c\'高,入栈!\n", opdata, theta);
256                 Push_OPTR(&OPTR, opdata);
257                 break;
258             case '=':
259             case '0':
260                 /*如果是#,或者括号,就出栈*/
261                 printf("\n当前操作符:\'%c\'优先级与前一个\'%c\'相等,出栈!\n", opdata, theta);
262                 Pop_OPTR(&OPTR, &theta);
263                 break;
264             case '>':
265                 /*如果当前操作符优先级比前面的低,退栈并将运算结果入栈*/
266                 printf("\n当前操作符:\'%c\'优先级比前一个\'%c\'低,开始计算!\n", opdata, theta);
267                 Pop_OPTR(&OPTR, &theta);
268                 printf("当前参与运算的操作符为:\'%c\'\n", theta);
269                 printf("输入的下一个数据为:%c\n", opdata);
270 
271                 if(theta == '(')
272                     continue;
273                 if(opdata == ')') {
274                     while(theta != '(') {
275                         Pop_OPND(&OPND, &operand2);
276                         printf("操作数2:%lf\n", operand2);
277                         Pop_OPND(&OPND, &operand1);
278                         printf("操作数1:%lf\n", operand1);
279                         Push_OPND(&OPND, Operate(operand1, theta, operand2));
280                         Pop_OPTR(&OPTR, &theta);
281                         printf("下一个操作符为:\'%c\'\n", theta);
282                     }
283                     continue;
284                 }
285 
286                 /*如果上述if不成立任然要执行的语句*/
287                 Pop_OPND(&OPND, &operand2);
288                 printf("操作数2:%lf\n", operand2);
289                 Pop_OPND(&OPND, &operand1);
290                 printf("操作数1:%lf\n", operand1);
291                 Push_OPND(&OPND, Operate(operand1, theta, operand2));
292 
293                 /*如果当前输入的数据为计算符就入栈*/
294                 if(isOperator(opdata)) {
295                     Push_OPTR(&OPTR, opdata);
296                 }
297                 printf("\n===========现在开始下一次的计算了!===========\n");
298                 break;
299             }
300         }
301     }
302 
303     if(i != 0) {
304         OPDATA[i] = '\0';
305         operand1 = atoi(OPDATA);
306         Push_OPND(&OPND, operand1);
307         i = 0;
308     }
309     printf("\n==========表达式输入结束了,现在开始计算了!==========\n");
310     while(!isEmpty(OPTR)) {
311         Pop_OPTR(&OPTR, &theta);
312         printf("操作符为:\'%c\'\n", theta);
313         if(theta == '(')
314             continue;
315         Pop_OPND(&OPND, &operand2);
316         printf("操作数2:%lf\n", operand2);
317         Pop_OPND(&OPND, &operand1);
318         printf("操作数1:%lf\n", operand1);
319         Push_OPND(&OPND, Operate(operand1, theta, operand2));
320     }
321     GetTop_OPND(OPND, &result);
322     printf("计算的结果为: %.2lf\n", result);
323 
324     return 0;
325 }
326 /*2+(2+3)*5-(4+4)+1
327 2+5*5-(4+4)+1
328 2+25-(4+4)+1
329 27-(4+4)+1
330 27-8+1
331 19+1
332 20
333 20
334 (6+4/2)*5-6/3*4+7
335 40-8+7=39
336 */

 

posted on 2018-11-25 17:03  psland  阅读(586)  评论(0编辑  收藏  举报