表达式求值C.doc
文本预览下载声明
要是没人回答的话 周末给你找找看 以前写过不知道还在不
运行
按1输入表达式(不用输入=号)
按2计算
按3输出结果
按4退出
程序如下:
#includestdio.h
#include string.h
#include conio.h
#define PLUS 0
#define MINUS 1
#define POWER 2
#define DIVIDE 3
#define LEFTP 4
#define RIGHP 5
#define STARTEND 6
#define DIGIT 7
#define POINT 8
#define NUM 7
#define NO 32767
#define STACKSIZE 20
char a[]={+,-,*,/,(,),#};
int j,PriorityTable[7][7]={{ 1, 1,-1,-1,-1, 1, 1},
{ 1, 1,-1,-1,-1, 1, 1},
{ 1, 1, 1, 1,-1, 1, 1},
{ 1, 1, 1, 1,-1, 1, 1},
{-1,-1,-1,-1,-1, 0, NO},
{ 1, 1, 1, 1,NO, 1, 1},
{-1,-1,-1,-1,-1,NO, 0}
};
int menu(void);
void
InputExpression(char str[])
{ int len;
printf(Input expression string:\n);
scanf(%s,str);
len=strlen(str);
str[len]=#;
str[len+1]=\0;
}
int GetCharType(char ch)
{ int i;
for(i=0;iNUM;i++) if(ch==a[i]) return(i);
if(ch=0 ch=9) return(DIGIT);
if(ch==.) return(POINT);
return(-1);
}
int EXCUTE(char *str,double *Result)
{
int pp,strlength,topTr,topNd,CharType,OPTR[STACKSIZE],a;
double number,temp,OPND[STACKSIZE];
OPTR[0]=STARTEND;
topTr=1;
topNd=0;
pp=0;
while((str[pp]))
{ CharType=GetCharType(str[pp]);
switch(CharType)
{ case -1:
return(0);
case DIGIT:
number=0;
while
(str[pp]=0 str[pp]=9)
{ number=number*10+(str[pp]-48);
pp++;
}
if(str[pp]==.)
{ temp=10.0;
pp++;
while(str[pp]=0 str[pp]=9)
{ number=number+(str[pp]-48)/temp;
temp=temp*10;
pp++;
}
}
OPND[topNd]=number;
topNd++;
break;
case POINT:
number=0;
temp=10.0;
pp++;
while(str[pp]=0 str[pp]=9)
{ number=number+(str[pp]-48)/temp;
temp=temp*10;
显示全部