主题:[讨论]求站内高手帮忙看下程序中的错误,急急急!!!
/*我编的这个程序是用链栈求表达式的值,但是有错误没能找出来,求站内大侠指点,可加QQ445094213随时指导,希望经常交流学习心得*/
[code=c]
请填写代码
[/code]
/*此为头文件*/
#include<stdio.h>
#include<malloc.h>
typedef struct node
{
char data;
struct node *next;
}LinkStackNode,*LinkStack;
void InitStack(LinkStack *S)
{
*S=(LinkStack)malloc(sizeof(LinkStackNode));
(*S)->next=NULL;
}/*链栈初始化*/
LinkStack Push(LinkStack top,char x)
{
LinkStackNode *temp;
temp=(LinkStackNode *)malloc(sizeof(LinkStackNode));
if(temp==NULL)return 0;
temp->data=x;
temp->next=top->next;
top->next=temp;
return top;
}/*链栈元素的入栈*/
LinkStack Pop(LinkStack top,char *x)
{
LinkStackNode *temp;
temp=top->next;
if(temp==NULL)return 0;
top->next=temp->next;
*x=temp->data;
free(temp);
return top;
}/*链栈元素的出栈*/
/*以下为算法实现代码*/
#include<stdio.h>
#include"stack.h"
char GetTop(LinkStack S)
{
if(S->next!=NULL)return (S->next->data);
else return 0;
}/*取栈顶元素函数*/
int In(char ch)
{
if(ch>='0' && ch<='9')
return 0;
else
return 1;
} /*判断读入的字符是否是运算数*/
int GetNumber(char a)
{
int n=0;
do{
n=n*10+(a-'0');
a=getchar();
}while(!In(a));
return n;
} /*将读入的运算数各位数码转化成十进制数*/
char Compare(char s,char a)
{
int i,j;
char A[5]={'+','-','*','/','#'};
char B[5][5]={'>','>','<','<','>'
,'>','>','<','<','>'
,'>','>','>','>','>'
,'>','>','>','>','>'
,'<','<','<','<','='};
for(i=0;i<5;i++)
if(s==A[i])
break;
for(j=0;j<5;j++)
if(a==A[j])
break;
return B[i][j];
} /*比较运算符优先级*/
int Execute(int a,char op,int b)
{
switch(op)
{
case '+':return a+b;break;
case '-':return a-b;break;
case '*':return a*b;break;
case '/':return a/b;break;
}
} /*对a和b进行op运算*/
int ExpEvaluation()
{
char ch,op,a,b;
char n,v;
LinkStack OVS,OPTR;
InitStack(&OVS);
InitStack(&OPTR);
Push(OPTR,'#');
printf("请输入一个表达式串(以#结尾):");
ch=getchar();
while(ch!='#'||GetTop(OPTR)!='#') /*当读入的字符和OPTR栈顶字符韵味#时结束运算*/
{
if(!In(ch))
{
n=GetNumber(ch);
Push(OVS,n);
}
else
switch(Compare(GetTop(OPTR),ch))
{
case '<': /*栈顶运算符优先级比刚读入运算符优先级低时*/
Push(OPTR,ch); /*将刚读入的运算符进运算符栈*/
ch=getchar(); /*继续读入下一个运算符*/
break;
case '>':
case '=': /*栈顶运算符优先级比刚读入运算符优先级高或相等时*/
Pop(OPTR,&op); /*退栈得到当前最优先运算符*/
Pop(OVS,&b); /*退栈得到op第二个运算数*/
Pop(OVS,&a); /*退栈得到op第一个运算数*/
v=Execute(a,op,b); /*对a和b进行op运算*/
Push(OVS,v);
break;
}
}
v=GetTop(OVS);
return v;
}
main()
{
printf("表达式的值是%d",ExpEvaluation());
}
[code=c]
请填写代码
[/code]
/*此为头文件*/
#include<stdio.h>
#include<malloc.h>
typedef struct node
{
char data;
struct node *next;
}LinkStackNode,*LinkStack;
void InitStack(LinkStack *S)
{
*S=(LinkStack)malloc(sizeof(LinkStackNode));
(*S)->next=NULL;
}/*链栈初始化*/
LinkStack Push(LinkStack top,char x)
{
LinkStackNode *temp;
temp=(LinkStackNode *)malloc(sizeof(LinkStackNode));
if(temp==NULL)return 0;
temp->data=x;
temp->next=top->next;
top->next=temp;
return top;
}/*链栈元素的入栈*/
LinkStack Pop(LinkStack top,char *x)
{
LinkStackNode *temp;
temp=top->next;
if(temp==NULL)return 0;
top->next=temp->next;
*x=temp->data;
free(temp);
return top;
}/*链栈元素的出栈*/
/*以下为算法实现代码*/
#include<stdio.h>
#include"stack.h"
char GetTop(LinkStack S)
{
if(S->next!=NULL)return (S->next->data);
else return 0;
}/*取栈顶元素函数*/
int In(char ch)
{
if(ch>='0' && ch<='9')
return 0;
else
return 1;
} /*判断读入的字符是否是运算数*/
int GetNumber(char a)
{
int n=0;
do{
n=n*10+(a-'0');
a=getchar();
}while(!In(a));
return n;
} /*将读入的运算数各位数码转化成十进制数*/
char Compare(char s,char a)
{
int i,j;
char A[5]={'+','-','*','/','#'};
char B[5][5]={'>','>','<','<','>'
,'>','>','<','<','>'
,'>','>','>','>','>'
,'>','>','>','>','>'
,'<','<','<','<','='};
for(i=0;i<5;i++)
if(s==A[i])
break;
for(j=0;j<5;j++)
if(a==A[j])
break;
return B[i][j];
} /*比较运算符优先级*/
int Execute(int a,char op,int b)
{
switch(op)
{
case '+':return a+b;break;
case '-':return a-b;break;
case '*':return a*b;break;
case '/':return a/b;break;
}
} /*对a和b进行op运算*/
int ExpEvaluation()
{
char ch,op,a,b;
char n,v;
LinkStack OVS,OPTR;
InitStack(&OVS);
InitStack(&OPTR);
Push(OPTR,'#');
printf("请输入一个表达式串(以#结尾):");
ch=getchar();
while(ch!='#'||GetTop(OPTR)!='#') /*当读入的字符和OPTR栈顶字符韵味#时结束运算*/
{
if(!In(ch))
{
n=GetNumber(ch);
Push(OVS,n);
}
else
switch(Compare(GetTop(OPTR),ch))
{
case '<': /*栈顶运算符优先级比刚读入运算符优先级低时*/
Push(OPTR,ch); /*将刚读入的运算符进运算符栈*/
ch=getchar(); /*继续读入下一个运算符*/
break;
case '>':
case '=': /*栈顶运算符优先级比刚读入运算符优先级高或相等时*/
Pop(OPTR,&op); /*退栈得到当前最优先运算符*/
Pop(OVS,&b); /*退栈得到op第二个运算数*/
Pop(OVS,&a); /*退栈得到op第一个运算数*/
v=Execute(a,op,b); /*对a和b进行op运算*/
Push(OVS,v);
break;
}
}
v=GetTop(OVS);
return v;
}
main()
{
printf("表达式的值是%d",ExpEvaluation());
}