| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 574 人关注过本帖
标题:[求助]表达式求值
只看楼主 加入收藏
z334635798
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-8-14
收藏
 问题点数:0 回复次数:3 
[求助]表达式求值

大家帮忙啊!
下面是表达式求值问题的代码,里面有问题,哪位高手救命啊!

#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);
}

搜索更多相关主题的帖子: 求值 表达 
2006-08-14 23:32
论坛
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:1372
专家分:0
注 册:2006-3-27
收藏
得分:0 
ou bing

日出东方,唯我不败! 做任何东西都是耐得住寂寞,任何一个行业要有十年以上的积累才能成为专家
2006-08-14 23:52
未成名的柯南
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-8-14
收藏
得分:0 
大家快做啊!
2006-08-15 07:28
duo
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-8-15
收藏
得分:0 
2006-08-15 15:16
快速回复:[求助]表达式求值
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028040 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved