大家帮忙啊!
下面是表达式求值问题的代码,里面有问题,哪位高手救命啊!
#define TRUE 1
#define FALES 0
#define OK 1
#define ERROR 0
#define OVERFLLOW 0
#define INFEASIBLE -1
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define MAXSTRLEN 255
#define OPSETSIZE 7
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
unsigned char prior[7][7]={
{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=','>'},
{'>','>','>','>',' ','>','>'},
{'<','<','<','<','<','>','='}
};
char opset[7]={'+' , '-' , '*' , '/' ,'(' , ')' , '#'};
typedef int status;
typedef struct{
char *base;
char *top;
int stacksize;
}stackchar;
typedef struct{
float *base;
float *top;
int stacksize;
}stackfloat;
status initstackc(stackchar *s)
{s->base=(char*)malloc(STACK_INIT_SIZE*sizeof(char));
if(!s->base)exit(OVERFLLOW);
s->top=s->base;
s->stacksize=STACK_INIT_SIZE;
return OK;
}
status initstackf(stackfloat *s)
{s->base=(float*)malloc(STACK_INIT_SIZE*sizeof(float));
if(!s->base)exit(OVERFLLOW);
s->top=s->base;
s->stacksize=STACK_INIT_SIZE;
return OK;
}
char gettopc(stackchar *s)
{char e;
if(s->top==s->base)return ERROR;
else {
e=*(s->top-1);
return e;
}
}
float gettopf(stackfloat *s)
{float e;
if(s->top==s->base)return ERROR;
else {
e=*(s->top-1);
return e;
}
}
status pushc(stackchar *s,char e)
{while(s->top-s->base>=s->stacksize)
{s->base=(char*)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(char));
if(!s->base)exit(OVERFLLOW);
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREMENT;
}
*s->top++=e;
return OK;
}
status pushf(stackfloat *s,float e)
{while(s->top-s->base>=s->stacksize)
{s->base=(float*)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(float));
if(!s->base)exit(OVERFLLOW);
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREMENT;
}
*s->top++=e;
return OK;
}
status popc(stackchar *s,char *e)
{if(s->top==s->base)return(ERROR);
else {
*e=*--s->top;
return OK;
}
}
status popf(stackfloat *s,float *e)
{if(s->top==s->base)return(ERROR);
else {
*e=*--s->top;
return OK;
}
}
float operate(float a,unsigned char theta, float b)
{switch(theta) {
case '+': return a+b;
case '-': return a-b;
case '*': return a*b;
case '/': return a/b;
default : return 0;
}
}
int in(char test,char *testop)
{ int i,find=0;
for(i=0;i<7;i++)
{if(test==testop[i])find=1;}
return find;
}
int returnopord(char op,char *testop)
{int i;
for(i=0;i<7;i++)
if(op==testop[i]){return i;}
}
char precede(char aop, char bop)
{return prior[returnopord(aop,opset)][returnopord(bop,opset)];}
float evaluate(char *myexpression)
{ stackchar OPTR;
stackfloat OPND;
char tempdata[20];
float data,a,b,tempf;
char theta,*c,x,Dr[2];
initstackc(&OPTR);
pushc(&OPTR,'#');
initstackf(&OPND);
c=myexpression;
strcpy(tempdata,"\0");
while (*c!='#'||gettopc(&OPTR)!='#')
{
if(!in(*c,opset))
{
Dr[0]=*c;
Dr[1]='\0';
strcat(tempdata,Dr);
c++;
if(in(*c,opset))
{
data=atof(tempdata);
pushf(&OPND, data);
strcpy(tempdata,"\0");
}
}
else {
switch (precede(gettopc(&OPTR),*c))
{
case '<':
pushc(&OPTR,*c);
c++;
break;
case '=':
popc(&OPTR,&x);
c++;
break;
case '>':
popc(&OPTR,&theta);
popf(&OPND,&b);
popf(&OPND,&a);
tempf=operate(a,theta,b);
pushf(&OPND,tempf);
break;
}
}
}
tempf=gettopf(&OPND);
printf("%f",tempf);
return tempf;
}
void main()
{char str[500];
float m;
printf("please input expression:\n");
gets(str);
m=evaluate(str);
printf("the result is: %f.\n",m);
}
----------------解决方案--------------------------------------------------------
ou bing
----------------解决方案--------------------------------------------------------
大家快做啊!
----------------解决方案--------------------------------------------------------
这里有你所要的程序:
http://cstudy.cc.topzj.com/viewthread.php?tid=139404&sid=BmeDqD
----------------解决方案--------------------------------------------------------