C语言中栈和队列实现表达式求值的实例

时间:2021-11-14 09:05:22

C语言队列实现表达式求值的实例

实现代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include<stdio.h>
#include<stdlib.h>
 
#define OK 1
#define ERROR 0
#define STACK_SIZE 20
#define STACK_INCREMENT 10
#define QUEUE_SIZE 20
 
typedef int Status;
 
typedef char StackElemtype;
typedef struct Stack{
  StackElemtype* base;
  StackElemtype* top;
  int stackSize;
}Stack;
Status StackInit(Stack* s){
  s->base = (StackElemtype*)malloc(sizeof(StackElemtype) * STACK_SIZE);
  if( !s->base )
    return ERROR;
  s->top = s->base;
  s->stackSize = STACK_SIZE;
  return OK;
}
Status Pop(Stack* s,StackElemtype* value){
  if( s->base == s->top ){
    printf("\nstack empty\n");
    return ERROR;
  }
  *value = *(--(s->top));
  return OK;
}
Status Push(Stack* s,StackElemtype value){
  if( s->top - s->base == s->stackSize){
     
    s->base = (StackElemtype*)realloc(s->base,sizeof(StackElemtype) * (STACK_INCREMENT + STACK_SIZE));
    if( !s->base )
      return ERROR;
    s->top = s->base + STACK_SIZE;
    s->stackSize = STACK_SIZE + STACK_INCREMENT;
  }
  *(s->top) = value;
  s->top++;
  return OK;
}
int StackLength(Stack s){
  return s.top - s.base;
}
 
typedef double StackElemtype_ForValueExperssion;
typedef struct Stack_2{
  StackElemtype_ForValueExperssion* base;
  StackElemtype_ForValueExperssion* top;
  int stackSize;
}Stack_2;
Status StackInit_2(Stack_2* s){
  s->base = (StackElemtype_ForValueExperssion*)malloc(sizeof(StackElemtype_ForValueExperssion) * STACK_SIZE);
  if( !s->base )
    return ERROR;
  s->top = s->base;
  s->stackSize = STACK_SIZE;
  return OK;
}
Status Pop_2(Stack_2* s,StackElemtype_ForValueExperssion* value){
  if( s->base == s->top ){
    printf("\nstack empty\n");
    return ERROR;
  }
  *value = *(--(s->top));
  return OK;
}
Status Push_2(Stack_2* s,StackElemtype_ForValueExperssion value){
  if( s->top - s->base == s->stackSize){
    s->base = (StackElemtype_ForValueExperssion*)realloc(s->base,sizeof(StackElemtype_ForValueExperssion) * (STACK_INCREMENT + STACK_SIZE));
    if( !s->base )
      return ERROR;
    s->top = s->base + STACK_SIZE;
    s->stackSize = STACK_SIZE + STACK_INCREMENT;
  }
  *(s->top) = value;
  s->top++;
  return OK;
}
 
typedef double QueueElemtype;
typedef char  QueueOperatorValue;
typedef struct QueueNode{
  QueueElemtype data;
  QueueOperatorValue operator;
  struct QueueNode* next;
  int flag;
}QueueNode,*QueueNodePtr;
typedef struct Queue{
  QueueNodePtr front;
  QueueNodePtr rear;
}Queue;
 
Status QueueInit(Queue* q){
  q->front = (QueueNodePtr)malloc(sizeof(QueueNode));
  if( !q->front )
    return ERROR;
  q->rear = q->front;
  q->rear->next = NULL;
  return OK;
}
Status QueueInsert(Queue* q,QueueElemtype value){
  QueueNodePtr new;
  new = (QueueNodePtr)malloc(sizeof(QueueNode));
  if( !new )
    return ERROR;
  new->data = value;
  new->flag = 1;
  new->next = NULL;
  q->rear->next = new;
  q->rear = new;
  return OK;
}
Status QueueInsert_operatorValue(Queue* q,QueueOperatorValue value){
  QueueNodePtr new;
  new = (QueueNodePtr)malloc(sizeof(QueueNode));
  if( !new )
    return ERROR;
  new->operator = value;
  new->flag = 0;
  new->next = NULL;
  q->rear->next = new;
  q->rear = new;
  return OK;
}
Status QueueDelete(Queue* q,QueueElemtype* value,QueueOperatorValue *operator,int* symbol){
  QueueNodePtr first;
  if( q->front == q->rear )
    return ERROR;
  first = q->front->next;
  if( first->flag == 1 ){
    *value = first->data;
    *symbol = 1;
  }
  else{
    *operator = first->operator;
    *symbol = 0;
  }
  q->front->next = first->next;
  if( first == q->rear ){
    q->rear = q->front;
  }
  return OK;
}
 
/* 利用栈将中缀表达式转化为后缀表达式:
 * ——————————————————————————————————————————————————————————————
 * | 用户的输入  |      进行的处理      |
 * |  0~9:   | 直接输出到控制台        |
 * |  /,*,(  | 直接Push          |
 * |  +,-    | 将栈中的元素Pop直到1.栈空或者是2.遇到(   |
 * |  )     | 在遇到(之前将栈中的元素全部Pop   |
 * ——————————————————————————————————————————————————————————————
 * */
 
Status Infix2Postfix(Queue* q){
  //Queue q;
  //QueueInit(&q);
  Stack s;
  StackInit(&s);
  char c,e;
  char bufferDigit[10];
  int i = 0;
  double longDigit;
  printf("    Please Enter Infix Expression\n");
  printf("------------NOTE: end of '#'--------------\n");
  scanf("%c", &c);
  while( '#' != c){
    while( c <= '9' && c >= '0' || '.' == c ){
      bufferDigit[i++] = c;
      bufferDigit[i] = '\0';
      scanf("%c", &c);
      if(!((c <= '9' && c >= '0' ) || '.' == c )){
        longDigit = atof(bufferDigit);
        QueueInsert(q,longDigit);
        i = 0;
      }
    }
    if( '(' == c || '*' == c || '/' == c ){
      Push(&s, c);
    }
    else if( '+' == c || '-' == c ){
      if( !StackLength(s) )
        Push(&s, c);
      else{
        Pop(&s, &e);
        while( '(' != e ){
          QueueInsert_operatorValue(q, e);
          if( StackLength(s) == 0 ){
            break;
          }else
            Pop(&s, &e);
        }
        if( '(' == e )
          Push(&s, e);
        Push(&s, c);
      }
    }else if( ')' == c ){
      Pop(&s, &e);
      while( '(' != e ){
        QueueInsert_operatorValue(q, e);
        Pop(&s, &e);
      }
    }else if( '#' == c){
      break;
    }else{
      printf("input ERROR!\n");
      return ERROR;
    }
    scanf("%c", &c);
  }
  while(StackLength(s)){
    Pop(&s, &e);
    QueueInsert_operatorValue(q, e);
  }
  QueueInsert_operatorValue(q,'#');
  return OK;
}
Status ShowQueue(Queue q){
  printf("The Reverse Polish Notation is:");
  if(q.front == q.rear){
    printf("Queue Empty");
    return ERROR;
  }
  QueueNodePtr p = q.front->next;
  while(p != q.rear){
    if(p->flag)
      printf("%g ", p->data);
    else
      printf("%c ", p->operator);
    p = p->next; 
  }
  printf("\n");
  return OK;
}
 
/* 利用栈求解后缀表达式(逆波兰表达式)的值。
 * ——————————————————————————————————————————————————————————————————————
 * |  +,-,*,/,  |   将栈顶的两个元素弹出进行计算,将结果压入栈顶 |
 * | 数字      |   将其压入栈顶                 |
 * ———————————————————————————————————————————————————————————————————————
 * */
Status ValueExpression(Queue q){
  Stack_2 s;
  StackInit_2(&s);
  double o1;
  double o2;
  QueueElemtype number;
  QueueOperatorValue operator;
  int symbol;
  QueueDelete(&q,&number,&operator,&symbol);
  while( symbol == 1 || ( symbol == 0 && '#' != operator)){
    if(symbol == 1){
      Push_2(&s, number);
    }
    else if(symbol == 0){
      switch(operator){
        case '+':
          Pop_2(&s,&o1);
          Pop_2(&s,&o2);
          Push_2(&s,o2 + o1);
          break;
        case '-':
          Pop_2(&s,&o1);
          Pop_2(&s,&o2);
          Push_2(&s,o2 - o1);
          break;
        case '*':
          Pop_2(&s,&o1);
          Pop_2(&s,&o2);
          Push_2(&s,o2 * o1);
          break;
        case '/':
          Pop_2(&s,&o1);
          Pop_2(&s,&o2);
          Push_2(&s,o2 / o1);
          break;
      }
    }
    QueueDelete(&q,&number,&operator,&symbol);
  }
  Pop_2(&s,&o1);
  printf("The Value of the Expression is %g\n",o1);
  return OK;
}
 
int main(){
  Queue q;
  QueueInit(&q);
  Infix2Postfix(&q);
  ShowQueue(q);
/*
  QueueElemtype number;
  QueueOperatorValue operator;
  int symbol;
  QueueDelete(&q,&number,&operator,&symbol);
  printf("%f,%c,%d\n",number,operator,symbol);
*/
  ValueExpression(q);
//Stack
/*
  Stack s;
  StackInit(&s);
  StackElemtype c;
  Push(&s,'1');
  Push(&s,'2');
  Push(&s,'3');
  Push(&s,'4');
  Pop(&s,&c);
  printf("%c ", c);
  Pop(&s,&c);
  printf("%c ", c);
  Pop(&s,&c);
  printf("%c ", c);
  Pop(&s,&c);
  printf("%c ", c);
*/
  //Queue
/*
  Queue q;
  QueueElemtype c;
  QueueInit(&q);
  QueueInsert(&q,1);
  QueueInsert(&q,2);
  QueueInsert(&q,3);
  QueueInsert(&q,4);
  QueueDelete(&q,&c);
  printf("%d ", c);
  QueueDelete(&q,&c);
  printf("%d ", c);
  QueueDelete(&q,&c);
  printf("%d ", c);
  QueueDelete(&q,&c);
  printf("%d ", c);
  if(QueueDelete(&q,&c)){
    printf("%d ",c);
  }
*/
/*
  Queue q;
  QueueInit(&q);
  QueueInsert(&q,2.1);
  QueueInsert_operatorValue(&q,'+');
  QueueInsert(&q,43.1);
  QueueInsert_operatorValue(&q,'a');
  QueueInsert_operatorValue(&q,'(');
  int iswho;
  double d;
  char c;
  QueueDelete(&q,&d,&c,&iswho);
  if(iswho == 1)
    printf("%f ",d);
  else
    printf("%c ", c);
  QueueDelete(&q,&d,&c,&iswho);
  if(iswho == 1)
    printf("%f ",d);
  else
    printf("%c ", c);
  QueueDelete(&q,&d,&c,&iswho);
  if(iswho == 1)
    printf("%f ",d);
  else
    printf("%c ", c);
  QueueDelete(&q,&d,&c,&iswho);
  if(iswho == 1)
    printf("%f ",d);
  else
    printf("%c ", c);
*/
  return 0;
}

以上就是C语言数据结构中栈和队列的应用,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://blog.csdn.net/robin_xu_shuai/article/details/50859455