用C编的算术表达式求值(支持浮点数)
<P>/* Note:Your choice is C IDE */<BR>#include "stdio.h"<BR>#define NULL 0<BR>#define MAXSIZE 15<BR>struct stack1<BR>{<BR> float data[MAXSIZE];<BR> int top1;<BR> int move1;<BR>};<BR>struct stack2<BR>{<BR> char symbol[15];<BR> int top2;<BR> int move2;<BR>};<BR>struct stack1*initstack1()<BR>{<BR> struct stack1*s1;<BR> s1=(struct stack1*)malloc(sizeof(struct stack1));<BR> s1->top1=-1;<BR> s1->move1=-1;<BR> return(s1);<BR>}<BR>struct stack2*initstack2()<BR>{<BR> struct stack2*s2;<BR> s2=(struct stack2*)malloc(sizeof(struct stack2));<BR> s2->top2=-1;<BR> s2->move2=-1;<BR> return(s2);<BR>}<BR>int Emptystack1(struct stack1*s1)<BR>{<BR> if(s1->top1==-1)<BR> return(1);<BR> else<BR> return(0);<BR>}<BR>int Emptystack2(struct stack2*s2)<BR>{<BR> if(s2->top2==-1)<BR> return(1);<BR> else<BR> return(0);<BR>}<BR>int pushstack1(struct stack1*s1,float a)<BR>{<BR> if(s1->top1==MAXSIZE-1)<BR> return(0);<BR> else<BR> {<BR> s1->top1++;<BR> s1->data[s1->top1]=a;<BR> /*printf("\n%g",s1->data[s1->top1]);*/<BR> return(1);<BR> }<BR>}<BR>int pushstack2(struct stack2*s2,char h)<BR>{<BR> s2->top2++;<BR> s2->symbol[s2->top2]=h;<BR> /*printf("\n%c",s2->symbol[s2->top2]);*/<BR> return(1);<BR>}<BR>float popstack1(struct stack1*s1)<BR>{<BR> float a;<BR> if(Emptystack1(s1))<BR> return(0);<BR> else <BR> a=s1->data[s1->top1];<BR> /*printf("\n%g",a);*/<BR> s1->top1--;<BR> return(a);<BR>}<BR>char popstack2(struct stack2*s2)<BR>{<BR> char b;<BR> if(Emptystack2(s2))<BR> return(0);<BR> else <BR> b=s2->symbol[s2->top2];<BR> /*printf("\n%c",b);*/<BR> s2->top2--;<BR> return(b); <BR>}<BR>char GetTop2(struct stack2*s2)<BR>{<BR> char b;<BR> b=s2->symbol[s2->top2];<BR> /*printf("\n%c",a);*/<BR> return(b);<BR>}<BR>char Precede(char a,char b)<BR>{<BR> int i,j;<BR> char Table[9][9]={' ','+','-','*','/','(',')','#','^',<BR> '+','>','>','<','<','<','>','>','<',<BR> '-','>','>','<','<','<','>','>','<',<BR> '*','>','>','>','>','<','>','>','<',<BR> '/','>','>','>','>','<','>','>','<',<BR> '(','<','<','<','<','<','=',' ','<',<BR> ')','>','>','>','>',' ','>','>','>',<BR> '#','<','<','<','<','<',' ','=','<',<BR> '^','>','>','>','>','<','>','>','>',};<BR> for(i=0;i<9;i++)<BR> if(Table[0][i]==b) <BR> break;<BR> for(j=0;j<9;j++) <BR> if(Table[j][0]==a) <BR> break;<BR> return Table[j][i];<BR>}<BR>int count=1;<BR>float MainWork(char*k)<BR>{ <BR> char r,t;<BR> int j,i=0;<BR> int s,setp=1;<BR> float e,m,n,p,num;<BR> struct stack1*p1;<BR> struct stack2*p2;<BR> p1=initstack1();<BR> p2=initstack2(); <BR> pushstack2(p2,'#');<BR> for(;k[i]!='\0';setp++,count+=1)<BR> {<BR> float weight=0.1;<BR> num=0;<BR> gotoxy(1,count+5);<BR> printf("%d",setp); <BR> if(in(k[i]))<BR> {<BR> gotoxy(48,count+5);<BR> printf("%c",k[i]);<BR> switch(Precede(GetTop2(p2),k[i]))<BR> {<BR> case'<':<BR> pushstack2(p2,k[i]);<BR> gotoxy(56,count+5);<BR> printf("pushstack2:%c",k[i]);<BR> i++;<BR> break;<BR> case'>':<BR> m=popstack1(p1);<BR> t=popstack2(p2);<BR> n=popstack1(p1);<BR> gotoxy(56,count+5);<BR> printf("operate:(%g,%c,%g)",n,t,m);<BR> switch(t)<BR> {<BR> case'*':<BR> p=n*m;<BR> pushstack1(p1,p);<BR> break;<BR> case'/':<BR> p=n/m;<BR> pushstack1(p1,p);<BR> break;<BR> case'+':<BR> p=n+m;<BR> pushstack1(p1,p);<BR> break;<BR> case'-':<BR> p=n-m;<BR> pushstack1(p1,p);<BR> break;<BR> case'^':<BR> e=n*n;<BR> for(s=2;s<m;s++)<BR> e*=n;p=e;<BR> pushstack1(p1,p);<BR> break;<BR> }<BR> break;<BR> case'=':<BR> {gotoxy(56,count+5);<BR> printf("popstack2:%c",popstack2(p2));i++;} <BR> }<BR> }<BR> else<BR> {<BR> for(;!in(k[i])&&k[i]!='.';i++) <BR> {num=10*num+k[i]-'0';}<BR> if(k[i]=='.') <BR> {<BR> i++;<BR> while(!in(k[i])) <BR> {<BR> num=num+(k[i]-'0')*weight;<BR> weight*=0.1;<BR> i++;<BR> } <BR> }<BR> gotoxy(56,count+5);<BR> printf("pushstack1:%g",num);<BR> gotoxy(48,count+5);<BR> printf("%g",num);<BR> pushstack1(p1,num);<BR> }<BR> for(p1->move1=0,j=0;p1->move1<=p1->top1;p1->move1++,j=j+8)<BR> {gotoxy(8+j,count+5);<BR> printf("%g",p1->data[p1->move1]);}<BR> for(p2->move2=0,j=0;p2->move2<=p2->top2;p2->move2++,j=j+2)<BR> {gotoxy(35+j,count+5);<BR> printf("%c",p2->symbol[p2->move2]);}<BR> }<BR> gotoxy(1,count+5);<BR> printf("The value of expression is:%g",popstack1(p1));<BR>}<BR>int in(char c)<BR>{<BR> if(c=='+'||c=='-'||c=='('||c==')'||c=='*'||c=='/'||c=='#'||c=='%'||c=='^')<BR> return(1);<BR> else<BR> return(0);<BR>}<BR>char expression[100];<BR>void accept()<BR>{<BR> gotoxy(1,count);<BR> printf("Please input the correct expression:");<BR> scanf("%s",&expression);<BR> gotoxy(1,count+2);<BR> printf("You input the expression is:%s",expression);<BR>}<BR>void output()<BR>{<BR> float num;<BR> gotoxy(1,count+4);<BR> printf("STEP");<BR> gotoxy(8,count+4);<BR> printf("DATASTACK1");<BR> gotoxy(35,count+4);<BR> printf("SYMBOLSTACK2");<BR> gotoxy(48,count+4);<BR> printf("READ");<BR> gotoxy(56,count+4);<BR> printf("OPERATE\n");<BR> MainWork(expression);<BR>} <BR>main()<BR>{<BR> accept();<BR> output();<BR>}<BR>/*输入表达式必须以‘#’结束。目前表达式没有判错功能,本人将进一步完善*/<BR></P>页:
[1]
