栈的应用之求逆波兰表达式的值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了栈的应用之求逆波兰表达式的值相关的知识,希望对你有一定的参考价值。
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_INCREMENT));
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 /* 处理输入的数字:由于使用%c接受输入,所以对于123这样的多位数的
66 * 输入%c是不能处理的。所以设置了char型的数组bufferDigit来缓存输
67 * 入的多位数。在开始了多位数的输入之后,必将以空格来结束该多位数
68 * 输入,所以if(c == ‘ ‘)用来判断多位数的结束。以便将缓存在char
69 * 型数组中的多位数转化为double并且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 == ‘ ‘){ //不是空格就一定还是数字
80 result = atof(bufferDigit);
81 Push(s,result);
82 i = 0;
83 }
84 }
85 /* 处理输入的运算符
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 /* 该程序编写的RPT部分功能可以完成,但是Text部分存在不解之处,在程序的124~129行的代码与134到
143 * 139行的代码完全相同。但是在main函数中的可以顺利运行,而在Test函数中的则出现错误(无错误提示)
144 * 如果将124~129行的代码改成如下则可以顺利运行:
145 StackNode s1;
146 InitStack(&s1);
147 Push(&s1,1);
148 Push(&s1,2);
149 Push(&s1,3);
150 ShowStack(&s1);
151 不知为何期待大牛指导。
152 * */
以上是关于栈的应用之求逆波兰表达式的值的主要内容,如果未能解决你的问题,请参考以下文章