jx4hjy 2014-06-03 14:10
浏览 899

参照网上,自己改用链栈写了个表达式求值的代码出现的问题

这个代码可以编译运行,但是输入值进行计算的时候就会停止工作

附上代码:
附上代码求指导:
#include
#include
typedef struct rope
{
char date;
struct rope next;
} node,*pnode;
typedef struct rope2
{
char date;
struct rope2 *next;
} nodes,*pnodes;
typedef struct a
{
pnode top;
pnode bottom;
} OPND;
typedef struct b
{
pnodes top;
pnodes bottom;
} OPTR;
char tr[7]= {'+','-','
','/','(',')','#'}; //字符总类型
int comp[7][7]=
{{2,2,1,1,1,2,2}, //用来进行比较运算符优先级的矩阵,3代表'=',2代表'>',1代表'<',0代表不可比
{2,2,1,1,1,2,2},
{2,2,2,2,1,2,2},
{2,2,2,2,1,2,2},
{1,1,1,1,1,3,0},
{2,2,2,2,0,2,2},
{1,1,1,1,1,0,3}};
void initStackOPND(OPND pS)//初始化运算数栈
{
pS->top=(pnode)malloc(sizeof(node));
if(pS->top==NULL)
{
printf("动态内存分配失败!\n");
}
else
{
pS->bottom=pS->top;
pS->bottom->next=NULL;
}
}
void initStackOPTR(OPTR *pS)//初始化运算符栈
{
pS->top=(pnodes)malloc(sizeof(nodes));
if(pS->top==NULL)
{
printf("动态内存分配失败!\n");
}
else
{
pS->bottom=pS->top;
pS->bottom->next=NULL;
}
}
void push(OPND *pS,int val)//运算数压栈
{
pnode new=(pnode)malloc(sizeof(node));
new->date=val;
new->next=pS->top;
pS->top=new;
return;
}
void pushs(OPTR *pS,char val)//运算符压栈
{
pnodes new=(pnodes)malloc(sizeof(nodes));
new->date=val;
new->next=pS->top;
pS->top=new;
return;
}
void pop(OPND *pS,int *x)//运算数出栈
{
if(pS->top=NULL)
{
printf("运算数栈空!\n");
}
else
{
pnode r=pS->top;
*x=r->date;
pS->top=r->next;
free(r);
r=NULL;
}
}
void pops(OPTR *pS,char *x)//运算符出栈
{
if(pS->top=NULL)
{
printf("运算符栈空!\n");
}
else
{
pnodes r=pS->top;
*x=r->date;
pS->top=r->next;
free(r);
r=NULL;
}
}
int gettop(OPND *pS)//得到运算数栈顶元素
{
int c;
pnode r=pS->top;
c=r->date;
if(pS->top=NULL)
{
printf("运算数栈为空!\n");
}
else
{
return c;
}
}
char gettops(OPTR *pS)//得到运算符栈顶元素
{
char c;
pnodes r=pS->top;
c=r->date;
if(pS->top=NULL)
{
printf("运算数栈为空!\n");
}
else
{
return c;
}
}
int judge(char ch)//判断运算符是否正常
{
int i;
for(i=0; i {
if(ch==tr[i])
return 1;
}
return 0;
}
char compare(char ch1,char ch2)//比较运算符优先级
{
int i,a,b;
char t;
int cp;
for(i=0; i {
if(ch1==tr[i])
{
a=i;
}
if(ch2==tr[i])
{
b=i;
}
}
cp=comp[a][b];
switch(cp)
{
case 1:
t=' break;
case 2:
t='>';
break;
case 3:
t='=';
break;
case 0:
t='$';
printf("表达式错误!\n");
break;
}
return t;
}
int caculate(int a,int b,char o)
{
int r;
switch(o)
{
case '+':
r=a+b;
break;
case '-':
r=a-b;
break;
case '
':
r=a*b;
break;
case '/':
r=a/b;
break;
}
return r;
}
int results()
{
int a,b,t,v,i=0;
char c,x;
char str;
OPND Sa;//运算数栈
OPTR Sb;//运算符栈
initStackOPND(&Sa);
initStackOPTR(&Sb);
pushs(&Sb,'#');
printf("请输入表达式,且在末尾加上‘#’!\n");
str=(char *)malloc(50*sizeof(char));
gets(str);
c=str[i];
i++;
while(c!='#'||gettops(&Sb)!='#')
{
if(!judge(c))
{
t=c-'0'; /
将字符转换为十进制数*/
c=str[i];
i++;
while(!judge(c))
{
t=t*10 + c-'0'; /*将逐个读入运算数的各位转化为十进制数*/
c=str[i];
i++;
}
push(&Sa,t);
}
else
{
switch(compare(gettops(&Sb),c))
{
case '<':
pushs(&Sb,c);
c=str[i];
i++;
break;
case '=':
pops(&Sb,&x);//只有()##才会相等出栈
c=str[i];
i++;
break;
case '>':
pops(&Sb,&x);
pop(&Sa,&b);
pop(&Sa,&a);
v=caculate(a,b,x); /* 对a和b进行x运算 */
push(&Sa,v);
break;
}
}
}
v=gettop(&Sa);
return v;
}
int main(void)
{
int result;
void initStackOPND(OPND *pS);
void initStackOPTR(OPTR *pS);
void push(OPND *pS,int val);
void pushs(OPTR *ps,char val);
void pop(OPND *pS,int *x);
void pops(OPTR *pS,char *x);
int gettop(OPND *pS);
char gettops(OPTR *pS);
int judge(char ch);
char compare(char ch1,char ch2);
int caculate(int a,int b,char o);
int results();
result=results();
printf("计算结果为:%d",result);
return 0;
}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
    • ¥15 C# datagridview 单元格显示进度及值
    • ¥15 thinkphp6配合social login单点登录问题
    • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配