栈的实例—逆波兰表达式

 

1.

 

2.

 

3.

 

  1 //逆波兰表达式
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include <ctype.h>
  5 
  6 #define STACK_INIT_SIZE  20    //存储栈的长度,栈大小
  7 #define STACKINCREMENT   10    //当栈空间不够时,每次增加的幅度
  8 #define MAXBUFFER        10
  9 
 10 typedef double ElemType;
 11 typedef struct
 12 {
 13     ElemType *base;
 14     ElemType *top;
 15     int stackSize;
 16 } sqStack;
 17 
 18 void InitStack(sqStack *s)
 19 {
 20     s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
 21     if(!s->base)
 22     {
 23         exit(0);
 24     }
 25 
 26     s->top = s->base;
 27     s->stackSize = STACK_INIT_SIZE;
 28 }
 29 
 30 //压栈操作
 31 void Push(sqStack *s,ElemType e)
 32 {
 33     if(s->top-s->base>=s->stackSize)
 34     {
 35         s->base=(ElemType *)realloc(s->base,(s->stackSize+STACKINCREMENT) * sizeof(ElemType));
 36         if(!s->base)
 37         {
 38             exit(0);
 39         }
 40     }
 41     *(s->top) = e;      //把数据e压入栈顶指针指向的地址
 42     s->top++;
 43 }
 44 
 45 //出栈操作
 46 void Pop(sqStack *s,ElemType *e)
 47 {
 48     if(s->top==s->base)
 49     {
 50         return;
 51     }
 52     *e = *--(s->top);   //先减再把值赋给e
 53 }
 54 
 55 int StackLen(sqStack s)
 56 {
 57     return (s.top - s.base);
 58 }
 59 
 60 int main()
 61 {
 62     char c;
 63     sqStack  s;
 64     double d, e;
 65     char str[MAXBUFFER];
 66     int i = 0;
 67 
 68     InitStack(&s);
 69     printf("请按逆波兰表达式输入待计算数据,数据与运算符之间用空格隔开,以#作为结束标志:\n");
 70     scanf("%c",&c);
 71 
 72     while(c!='#')
 73     {
 74         //用于过滤数字
 75         while(isdigit(c) || c=='.')
 76         {
 77             str[i++] = c;
 78             str[i] = '\0';      //字符串要有\0作为结束标志,否则会出错
 79             if(i>=10)
 80             {
 81                 printf("出错:输入的单个数据过大!\n");
 82                 return -1;
 83             }
 84             scanf("%c", &c);
 85             if(c==' ')
 86             {
 87                 //字符串转换为double
 88                 d = atof(str);
 89                 Push(&s, d);
 90                 i = 0;
 91                 break;
 92             }
 93         }
 94         switch( c )
 95         {
 96             case '+':
 97                 Pop(&s, &e);
 98                 Pop(&s, &d);
 99                 Push(&s, e + d);
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                 Push(&s, d * e);
110                 break;
111             case '/':
112                 Pop(&s, &e);
113                 Pop(&s, &d);
114                 if(e!=0)
115                 {
116                     Push(&s, d / e);
117                 }
118                 else
119                 {
120                     printf("\n出错:除数为零!\n");
121                     return -1;
122                 }
123                 break;
124         }
125         scanf("%c", &c);
126     }
127 
128     Pop(&s, &e);
129     printf("表达式结果为:%f", e);
130 
131     return 0;
132 }
View Code

 

posted @ 2020-08-23 23:14  wind_y  阅读(202)  评论(0编辑  收藏  举报