栈与队列应用:逆波兰计算器(逆波兰表达式;后缀表达式)把运算符放到运算量后边 && 中缀表达式转化为后缀表达式

  1 //1.实现对逆波兰输入的表达式进行计算如(2-1)*(2+3)= 5  就输入2 1 - 2 3 + *   //先把2 1 压栈 遇到-弹栈 再把2 3压进去 遇到+弹栈 最后遇到*弹栈
  2 //2.支持带小数点的数据
  3 例: 正常操作----->逆波兰表达式
  4      a+b   ------>a b +
  5      a+(b-c)----->a b c - +
  6      a+(b-c)*d--->a b c - d * +
  7      a+d*(b-c)--->a d b c - * +
  8 
  9 //2 5 + 4 2 - *  == (2+5)*(4-2) == 14
 10 //1 34 + 4 2 / * ==(1+34)*(4/2)== 70 
 11 #include<stdio.h>
 12 #include<stdlib.h>
 13 #include<ctype.h>  //函数isdigit  检查是否为十进制数 
 14 
 15 #define MAXSIZE 100
 16 #define MAXBUFFER 10 //缓冲区 
 17 
 18 typedef double ElemType;  
 19 
 20 typedef struct
 21 {
 22     ElemType *base;
 23     ElemType *top;
 24     int sizeStack;
 25 }sqStack;
 26 
 27 void InitStack(sqStack *s)
 28 {
 29     s->base = (ElemType *)malloc(MAXSIZE*sizeof(ElemType));
 30     if(!s->base)
 31     {
 32         exit(0);
 33     }
 34     s->top = s->base;
 35     s->sizeStack = MAXSIZE;
 36 }
 37 
 38 void Push(sqStack *s,ElemType e)
 39 {
 40     if(s->top - s->base == s->sizeStack)
 41     {
 42         exit(0);
 43     }
 44     *(s->top) = e;
 45     s->top++;
 46 }
 47 
 48 void Pop(sqStack *s,ElemType *e)
 49 {
 50     if(s->top == s->base)
 51     {
 52         return;
 53     }
 54     *e = *(--(s->top));
 55 }
 56  
 57 int main(void)
 58 {
 59     sqStack s;
 60     char c;
 61     char str[MAXBUFFER];
 62     double d,e;
 63     int i = 0;
 64     
 65     InitStack(&s);
 66     printf("请输入后缀表达式的字符,运算符与数字用空格隔开\n");
 67     scanf("%c",&c);
 68     while(c != '#')
 69     {
 70         while(isdigit(c) || c=='.')  //判断是否遇到数字
 71         {
 72             str[i++] = c;
 73             str[i] = '\0';
 74             if(i >= 10)
 75             {
 76                 printf("单个数字太大啊"); 
 77                 return -1;
 78             }
 79             scanf("%c",&c);
 80             if(c == ' ')
 81             {
 82                 d = atof(str);  //将字符串转换成浮点型  存在<stdlib.h>
 83                 Push(&s,d);
 84                 i = 0;
 85                 break; 
 86             }
 87         }
 88 
 89         switch(c) //判断是否遇到运算符
 90         {
 91             case '+':
 92                 Pop(&s,&e);
 93                 Pop(&s,&d);
 94                 Push(&s,d+e);
 95                 break;
 96             case '-':
 97                 Pop(&s,&e);
 98                 Pop(&s,&d);
 99                 Push(&s,d-e);
100                 break;
101             case '*':
102                 Pop(&s,&e);
103                 Pop(&s,&d);
104                 Push(&s,d*e);
105                 break;
106             case '/':
107                 Pop(&s,&e);
108                 Pop(&s,&d);
109                 if(e != 0)
110                 {
111                     Push(&s,d/e);
112                 }
113                 else
114                 {
115                     printf("被除数不能为0!\n");
116                     return -1;
117                 }
118                 break;
119         }
120         scanf("%c",&c);
121     }
122     Pop(&s,&d);
123     printf("最终结果为:%f",d);
124     return 0;
125 }
126 
127 
128 
129 //将中缀表达式转化为后缀表达式
130 #include<stdio.h>
131 #include<stdlib.h>
132 
133 #define MAXSIZE 20
134 #define INCREMENTSIZE 10
135 
136 typedef char ElemType; 
137 typedef char ElemType2;
138 typedef struct 
139 {
140     ElemType *base;
141     ElemType *top;
142     int StackSize;
143 }sqStack;
144 
145 void InitStack(sqStack *s)
146 {
147     s->base = (ElemType *)malloc(MAXSIZE*sizeof(ElemType));
148     if(!s->base)
149     {
150         printf("内存分配失败!\n");
151         exit(0);
152     }
153     s->top = s->base;
154     s->StackSize = MAXSIZE;
155 } 
156 
157 void Push(sqStack *s, ElemType e)
158 {
159     if(s->top - s->base == s->StackSize)
160     {
161         s->base = (ElemType *)realloc(s->base,(s->StackSize+INCREMENTSIZE)*sizeof(ElemType));
162         if(!s->base)
163         {
164             printf("重新分配内存失败!\n");
165             exit(0); 
166         }
167         s->top=s->base+s->StackSize;
168         s->StackSize=s->StackSize+INCREMENTSIZE;
169     }
170     *(s->top) = e;
171     s->top++;
172 }
173 
174 void Pop(sqStack *s,ElemType *e)
175 {
176     if(s->top == s->base)
177     {
178         return;
179     }
180     *e = *(--(s->top));
181 }
182 
183 int StackLen(sqStack s)
184 {
185     return (s.top - s.base);
186 }
187 
188 int main(void)
189 {
190     sqStack s;
191     char c,e;
192     
193     InitStack(&s);
194     printf("请输入中缀表达式\n");
195     scanf("%c",&c);
196     while(c !='#')
197     {
198         
199         while(c>='0' && c<='9')
200         {
201             printf("%c",c);
202             scanf("%c",&c);
203             if(c<'0' || c>'9')
204             {
205                 printf(" ");
206             }
207         }
208         
209         if(c==')')
210         {
211             Pop(&s,&e);
212             while(e != '(')
213             {
214                 printf("%c ",e);
215                 Pop(&s,&e);
216             }
217         }
218         else if(c=='+' || c=='-')
219         {
220             if(!StackLen(s))
221             {
222                 Push(&s,c);
223             }
224             else
225             {
226                 do
227                 {
228                     Pop(&s,&e);
229                     if(e == '(')
230                     {
231                         Push(&s,e); //遇到左括号就弹走就ok 
232                     }
233                     else
234                     {
235                         printf("%c ",e);
236                     }
237                     
238                 }while(StackLen(s) && e!='(');
239                 Push(&s,c);
240             }
241         }
242         else if(c=='/' || c=='*' || c=='(')
243         {
244             Push(&s,c);
245         }
246         else if(c == '#')
247         {
248             break;
249         }
250         else 
251         {
252             printf("数据输入错误啊!\n");
253             return -1;
254         }
255         scanf("%c",&c);
256     }
257     while(StackLen(s))
258     {
259         Pop(&s,&e);
260         printf("%c ",e);
261     }
262     return 0;
263 }

 

posted @ 2020-03-14 11:48  Xxaj5  阅读(259)  评论(0编辑  收藏  举报