额,这是小甲鱼2012年数据结构里的逆波兰算法,我不知道错误出在哪。求大佬指点!
#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;
double d,e;
InitStack(&s);
int i=0;
printf("请输入:\n");
scanf("%c",&c);
while(c!='#')
{
while( isdigit(c) || c == '.')
{
str = c;
str = '\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;
}
求指点! hello,大佬!指点下呗!谢谢
页:
[1]