逆波兰的主函数。
int main()
{
sqStack s;
char c;
double d,e;
char str[MAXBUFFER];
int i=0;
InitStack(&s);
printf("please with nibolan expression input,\nnumber and expression use ' ' separation and as '#' over:");
scanf("%c",&c);
while(c!='#')
{
while(isdigit(c)||c=='.')//用于过滤数字。
{
str[i++]=c;
str='\0';
if(i>=10)
{
printf("\nerror,input single data too big!\n");
return -1;
}
scanf("%c",&c);
if(c==' ')
{
d=atof(str);
Push(&s,d);
i=0;
break;
}
}
switch(c)
{
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("\nerror,divisor is zeor.\n");
return -1;
}
break;
}
scanf("%c",&c);
}
Pop(&s,&d);
printf("the operation result in the end:%f\n",d);
return 0;
}
转载于:https://www.cnblogs.com/hunshidamowang/p/10448678.html