为什么程序进不了循环,
#include<stdio.h>#include<malloc.h>
#include<stdlib.h>
#include<ctype.h>
#define M 600
#define increase 10
typedef double ElemType;
typedef struct node{
ElemType *base;
ElemType *top;
int stacksize;
}stack;
void Initstack(stack *s)
{
s->base=(ElemType*)malloc(sizeof(ElemType));
if(!s->base)
{
exit(0);
}
s->base=s->top;
s->stacksize=M;
}
void push(stack *s,ElemType e)
{
if(s->top - s->base ==s->stacksize)
{
s->base=(ElemType*)realloc(s->base,(s->stacksize+increase)*sizeof(ElemType));
if(!s->base)
{
exit(0);
}
s->top=s->base+s->stacksize;
s->stacksize=s->stacksize+increase;
}
*(s->top)=e;
s->top++;
}
void pop(stack *s,ElemType *e)
{
if(s->top==s->base)
{
return ;
}
*e=*--(s->top);
}
void clear(stack *s)
{
s->top=s->base;
}
void destroy(stack *s)
{
free(s->base);
s->base=s->top=NULL;
s->stacksize=0;
}
int stacklen(stack s)
{
return (s.top-s.base);
}
int main()
{
stack s;
int i,j=0,k=0,len,n;
char c;
double d,e;
char ch[100],str[100];
Initstack(&s);
/*while(c=getchar())
{
if(c!='\n')
{
ch[j++]=c;//保存在数组ch【i】中
len++;//len为输入的长度
}
else if(c=='\n')
break;
else
;
}*/
scanf("%d",&n);
for(j=0;j<n;j++)
{
scanf("%c",&ch[j]);
}
i=0;
while(ch[i]!='\n')///————————————————————这个循环进不去运行,直接跳过了,这是求逆波兰计算器的代码
{
while(isdigit(ch[i]))
{
str[k++]=ch[i];//str缓冲区
str[k]='\0';
i++;
if(!isdigit(ch[i]))
{
d=atof(str);
push(&s,d);
k=0;
break;
}
}
switch(ch[i])
{
case '+':
pop(&s,&e);
pop(&s,&d);
push(&s,d+e);
break;
case '-':
pop(&s,&e);
pop(&s,&d);
push(&s,d-e);
break;
case '*':
pop(&s,&e);
pop(&s,&d);
push(&s,d*e);
break;
case '/':
pop(&s,&e);
pop(&s,&d);
if(e!=0)
{
push(&s,d/e);
}
else
{
printf("wrong");
}
break;
}
i++;
}
pop(&s,&d);
printf("%d",d);
return 0;
}