用栈的方法来实现表达式求值问题

gzlsc 2004-03-28 01:19:27
用栈的方法来实现表达式求值的输入,用TC运行输入表达式后并不能得出结果。
请教大侠为什么会这样!?

#define maxsize 6 /*顺序栈容量*/
#include<stdio.h>
#include<malloc.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define pf printf

typedef struct sqstackd
{
double data[maxsize];
double top;
} SqStackTpd;
SqStackTpd *OPND=0;

typedef struct sqstackr
{
char data[maxsize];
char top;
} SqStackTpr;
SqStackTpr *OPTR=0;
char *x,*theta,*n1,*n3;double *z,*y,*n2;

/*初始化*/

int initstackd(SqStackTpd *sq)
{
sq->top = 0;
return (1);
}

/*进栈*/

int pushd(SqStackTpd *sq, double x)
{
if(sq->top == maxsize-1)
{
printf("栈满!\n");
return (1);
}
else
{
sq->top++;
sq->data[sq->top]=x;
return (1);
}
}

/*退栈*/

int popd(SqStackTpd *sq, double *x)
{
if(sq->top == 0)
{
printf("下溢!\n");
return (0);
}
else
{
*x= sq->data[sq->top];
sq->top --;
return (1);
}
}


int initstackr(SqStackTpr *sq)
{
sq->top = 0;
return (1);
}

/*进栈*/

int pushr(SqStackTpr *sq, char x)
{
if(sq->top == maxsize-1)
{
printf("栈满!\n");
return (1);
}
else
{
sq->top++;
sq->data[sq->top]=x;
return (1);
}
}

/*退栈*/

int popr(SqStackTpr *sq, char *x)
{
if(sq->top == 0)
{
printf("下溢!\n");
return (0);
}
else
{
*x= sq->data[sq->top];
sq->top --;
return (1);
}
}

char GetTop_optr(SqStackTpr *sq, char *x)
{
if(sq->top == 0)
return 0;
else
{
*x=sq->data[sq->top];
return (1);
}
}

double GetTop_opnd(SqStackTpd *sq, double *x)
{
if(sq->top == 0)
return 0;
else
{
*x=sq->data[sq->top];
return (1);
}
}

char Precede(char theta,char beta)
{int i,j;
char optr[7]={'+','-','*','/','(',')','#'};
char precede[7][7]={
{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=',' '},
{'>','>','>','>',' ','>','>'},
{'<','<','<','<','<',' ','='}
};
for(i=0;i<7;i++) {
if(optr[i]==theta)break;
}
for(j=0;j<7;j++) {
if(optr[j]==beta)break;
}
return precede[i][j];
}


double Operate(double *a,char *theta,double *b)
{double res;
switch(*theta)
{case '+' : res=*a+*b;break;
case '-' : res=*a-*b;break;
case '*' : res=(*a)*(*b);break;
case '/' : res=(*a)/(*b);break;
}
return(res);
}

double change1(char a[],int k)
{double p=0; int i=0,n,j; int b[10];
j=k-1;
for(i=0;i<=k-1;i++)
{b[i]=a[i]-48;
for(n=0;n<j;n++)
{b[i]=b[i]*10;}
j--;}
for(i=0;i<k-1;i++)
{p=p+b[i];}
b[k-1]=a[k-1]-48;
p=p+b[k-1];
return(p);
}


double change2(char a[],int k)
{int i=0,j=0,n,m,h;
double p1=0,p2=0,p=0,b[10];int c[10];
while(a[j]!='.'){j++;}
m=j;
for(i=0;i<m-1;i++)
{c[i]=a[i]-48;
for(n=0;n<j-1;n++)
{c[i]=c[i]*10;}
j--;
}
for(i=0;i<m-1;i++)
{p1=p1+c[i];}
c[m-1]=a[m-1]-48;
p1=p1+c[m-1];
h=m;
for(i=k-1;i>m;i--)
{b[i]=a[i]-48;
for(j=k-h-2;j>=0;j--)
{b[i]=b[i]/10.0;}
h++;}
for(i=k-1;i>m;i--)
{p2=p2+b[i];}
p=p1+p2;
return(p);
}

double EvaluateExpression()
{double p,L;
char c; char a[10]={0};
int i=0,j=0,k=0,t=0,n;
initstackr(OPTR);pushr(OPTR,'#');
initstackd(OPND);c=getchar();
GetTop_optr(OPTR,n1);
while(c!='#'||*n1!='#')
{while((c>='0' && c<='9') || (c=='.'))
{a[i]=c;i++;k=i;
c=getchar();
t++;
}i=0;
if(t!=0)
{n=0;
for(j=0;j<=k-1;j++)
{if(a[j]=='.')n=1;
}
switch(n)
{case 0: p=change1(a,k);break;
case 1: p=change2(a,k);;break;
}pushd(OPND,p); t=0;
}
GetTop_optr(OPTR,n3);
switch(Precede(*n3,c)){
case'<':
pushr(OPTR,c);c=getchar();break;
case'=':
popr(OPTR,x);c=getchar();break;
case'>':
popr(OPTR,theta);popd(OPND,y);popd(OPND,z);
pushd(OPND,Operate(z,theta,y));break;
}
}
GetTop_opnd(OPND,n2);
L=*n2;
return (L);

}

void main()
{double shuchu;
pf("enter the num:\n");
shuchu=EvaluateExpression();
pf("\n");
pf("the result is:");
pf(" %.4f\n" ,shuchu);
}

...全文
517 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧