用C语言求中缀表达式 找错误。。
使用了栈 抄了书 编译无错误 运行时会R6002 求大神帮忙找一下为什么不行,是不是后面的main函数出错了? #include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100 //栈的最大容量100//
#define TRUE 1
#define FALSE 0
#define NULL 0
typedef int elemtype;
typedef struct
{ elemtype stack[MAXSIZE];
int top;
}sqstack; //定义顺序栈类型sqstack//
sqstack *s; //定义sqstack类型的变量 s//
void InitStack(sqstack *s);
int push(sqstack *s,elemtype x);
elemtype pop(sqstack *s);
elemtype gettop(sqstack *s);
int empty(sqstack *s);
void display_stack(sqstack *s);
void InitStack(sqstack *s) //初始化栈//
{s->top=-1;}
int push(sqstack *s,elemtype x) //入栈操作//
{if(s->top >=MAXSIZE-1)
{printf("stack overflow!\n");return(FALSE);}
else
{s->top++;
s->stack[s->top]=x;
return(TRUE);}
}
elemtype pop(sqstack *s) //出栈操作//
{ if(s->top<0)
{printf("stack underflow!\n");
return(NULL);
}
else
{s->top--;return(s->stack[s->top+1]);}
}
elemtype gettop(sqstack *s) //获取栈顶元素//
{if (s->top<0)
{printf("stack is empty\n");
return(NULL);}
else
return(s->stack[s->top]);}
int empty(sqstack *s)
{if(s->top<0)
return(TRUE);
else
return(FALSE);}
void display_stack(sqstack *s)
{int i;
printf("the elements is the stack are:\n");
for(i=s->top;i>=0;i--)
printf("%d",s->stack[i]);
}
char exp[MAXSIZE]; //存储转换的后缀表达式//
void transform(sqstack *s,char exp[MAXSIZE])
{ //将算术表达式改为后缀表达式//
char str[MAXSIZE]; //存储原来的算术表达式//
char ch;
int sum,i,j,t;
s->top=0;
printf("算术表达式是:\n");
i=0;
do
{i++; //获取输入的表达式//
scanf("%c",&str[i]);}
while(str[i]!='#'&&i!=MAXSIZE);
sum=i; //记录总的数目//
t=1;
i=1;
ch=str[i];
i++;
while(ch!='#')
{switch(ch)
{ case'(': //判定为左括号//
s->top++;
s->stack[s->top]=ch;
break;
case')': //判定为右括号//
while(s->stack[s->top]!='(')
{exp[t]=s->stack[s->top];
s->top--;
t++;
}
s->top--;
break;
case'+': //判定为加减号//
case'-':
while(s->top!=0 && s->stack[s->top]!='(')
{
exp[t]=s->stack[s->top];
s->top--;
t++;
}
s->top++;
s->stack[s->top]=ch;
break;
case'*': //判定为* / //
case'/':
while (s->stack[s->top]=='*'||s->stack[s->top]=='/')
{
exp[t]=s->stack[s->top];
s->top--;
t++;
}
s->top++;
s->stack[s->top]=ch;
break;
case' ':
break;
default:
while (ch>='0'&&ch<='9') //判定为数字//
{exp[t]=ch;
t++;
ch=str[i];
i++;}
i--;
exp[t]='#';
t++;
}
ch=str[i];
i++;}
while (s->top!=0)
{exp[t]=s->stack[s->top];
t++;
s->top--;}
exp[t]='#';
printf("\n\t 原来表达式:");
for(j=1;j<sum;j++)
printf("%c",str[j]);
printf("\n\t后缀表达式:",exp);
for(j=1;j<t;j++)
printf("%c",exp[j]);
}
void compute_value(sqstack *s,char exp[MAXSIZE])
{int d;
char ch;
int t=1;
s->top=0;
ch=exp[t]; //t是exp下标//
t++;
while(ch!='#')
{ switch(ch)
{ case'+':
s->stack[s->top-1]=s->stack[s->top-1]+s->stack[s->top];
s->top--;
break;
case'-':
s->stack[s->top-1]=s->stack[s->top-1]-s->stack[s->top];
s->top--;
break;
case'*':
s->stack[s->top-1]=s->stack[s->top-1]*s->stack[s->top];
s->top--;
break;
case'/':
if(s->stack[s->top]!=0)
s->stack[s->top-1]=s->stack[s->top-1]/s->stack[s->top];
else
{printf("\n\t出错!\n");
exit(0);} //异常退出//
s->top--;
break;
default:
d=0;
while(ch>='0'&&ch<='9')
{
d=10*d-ch-'0'; //数字字符改为数值//
ch=exp[t];
t++;
}
s->top++;
s->stack[s->top]=d;
}
ch=exp[t];
t++;}
printf("\n\t计算结果:%f\n",s->stack[s->top]);
}
void main(void)
{sqstack stack,*s;
s=&stack;
transform(s,exp);
compute_value(s,exp);}