|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include <stdio.h>
- #include <ctype.h>
- #include <stdlib.h>
- #define STACK_INIT_SIZE 20
- #define STACKINCREMENT 10
- #define MAXBUFFER 10
- typedef double ElemType;
- typedef struct
- {
- ElemType *base;
- ElemType *top;
- int stackSize;
- }sqStack;
- InitStack (sqStack *s)
- {
- s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
- if (!s->base)
- exit(0);
- s->stackSize = STACK_INIT_SIZE;
- }
- Push(sqStack *s, ElemType e)
- {
- if(s->top-s->base>=s->stackSize)
- {
- s->base = (ElemType *)realloc(s->base,(s->stackSize+STACKINCREMENT) *sizeof(ElemType) );
- if(!s->base)
- exit(0);
- s->top = s->base + s->stackSize;
- s->stackSize = s->stackSize + STACKINCREMENT;
- }
- *(s->top) = e;
- s->top++;
- }
- Pop(sqStack *s ,ElemType *e)
- {
- if(s->top == s->base)
- return ;
- *e = *--(s->top);
- }
- int stackLen(sqStack s)
- {
- return(s.top- s.base);
- }
- int main()
- {
- sqStack s;
- char c;
- char str[MAXBUFFER];
- double d,e;
- InitStack(&s);
- int i=0;
- printf("请输入:\n");
- scanf("%c",&c);
- while(c!='#')
- {
- while( isdigit(c) || c == '.')
- {
- str[i++] = c;
- str[i] = '\0';
- if (i>=10)
- {
- printf("出错:");
- return -1;
- }
- scanf("%c",&c);
- if (c == ' ')
- {
- d = atof(str);
- Push(&s,d);
- i = 0;
- break;
- }
- }
- switch(c)
- {
- case '+':
- Pop(&s,&d);
- Pop(&s,&e);
- Push(&s,e+d);
- break;
- case '-':
- Pop(&s,&d);
- Pop(&s,&e);
- Push(&s,e-d);
- break;
- case '*':
- Pop(&s,&d);
- Pop(&s,&e);
- Push(&s,e*d);
- break;
- }
- scanf("%c",&c);
- }
- Pop(&s,&e);
- printf("输出的结果是:%f",e);
- return 0;
- }
复制代码 |
|