#include<math.h> double kaipingfang(double *p) { return sqrt(*p); }
double nchifang(double *p,double *q) { double sum; sum=pow((*p),(*q)); return sum; }
double jiecheng(double *a) { double sum=1.0; for(;(*a)>0;(*a)--) sum=sum*(*a); return sum; }
double pailie(double *p,double *q) { double sum=1.0; int c,a,b; a=c=*p;b=*q; for(;a>=c-b+1;a--) sum=sum*a; return sum; }
double zhuhe(double *p,double *q) { double sum,a,b; a=pailie(p,q); b=jiecheng(q); sum=a/b; return sum; }
double readnumber(char f[],int *i) { double x=0.0; int k=0; while(f[*i]>='0'&&f[*i]<='9') { x=x*10+(f[*i]-'0'); (*i)++; } if(f[*i]=='.') { (*i)++; while(f[*i]>='0'&&f[*i]<='9') { x=x*10+(f[*i]-'0'); (*i)++; k++; } } while(k!=0) { x=x/10.0; k=k-1;} return x; }
double evalpost(char f[]) { double obst[100]; int top=0; int i=0; double x1, x2; while(f[i]!='=') { if(f[i]>='0'&&f[i]<='9') { obst[top]=readnumber(f,&i);top++;} else if(f[i]=='~') { x1=obst[--top]; obst[top]=-x1; top++;i++; } else if(f[i]=='A') { x2=obst[--top]; x1=obst[--top]; obst[top]=pailie(&x1,&x2); top++; i++; } else if(f[i]=='C') { x2=obst[--top]; x1=obst[--top]; obst[top]=zhuhe(&x1,&x2); top++; i++; } else if(f[i]=='!') { x1=obst[--top]; obst[top]=jiecheng(&x1); top++; i++; } else if(f[i]=='S') { x1=obst[--top]; obst[top]=kaipingfang(&x1); top++; i++; } else if(f[i]=='^') { x2=obst[--top]; x1=obst[--top]; obst[top]=nchifang(&x1,&x2); top++; i++; } else if(f[i]==' ') i++; else if(f[i]=='+') { x2=obst[--top]; x1=obst[--top]; obst[top]=x1+x2; top++; i++; } else if(f[i]=='-') { x2=obst[--top]; x1=obst[--top]; obst[top]=x1-x2; top++; i++; } else if(f[i]=='*') { x2=obst[--top]; x1=obst[--top]; obst[top]=x1*x2 ; top++; i++;} else if(f[i]=='/') { x2=obst[--top]; x1=obst[--top]; obst[top]=x1/x2; top++; i++; }
} return obst[0]; }
int is_operation(char op) { switch(op) { case'+': case'-': case'*': case'/':return 1; default:return 0; } }
int priority(char op) { switch(op) { case'=':return -1; case'{': case'[': case'(':return 0; case'+': case'-':return 1; case'*': case'/':return 2; case'A': case'C': case'!': case'S': case'^':return 3; case'~':return 4; default:return -1; } }
void postfix(char e[],char f[]) { int i=0, j=0,k=0; char opst[100],c[100]; int top, t; top=0; opst[top]='=';top++; while(e[i]!='=') { if((e[i]>='0'&&e[i]<='9')||e[i]=='.') f[j++]=e[i]; else if(e[i]=='('||e[i]=='['||e[i]=='{') { opst[top]=e[i];top++;c[k]=e[i];k++;} else if(e[i]==')'||e[i]==']'||e[i]=='}') { c[k]=e[i];k++; t=top-1; while(opst[t]!='('&&opst[t]!='['&&opst[t]!='{') { f[j++]=opst[--top];t=top-1;} top--; } else if(is_operation(e[i]))/*改*/ { f[j++]=' '; while(priority(opst[top-1])>=priority(e[i])) f[j++]=opst[--top]; opst[top]=e[i]; top++; } else if(e[i]==',') f[j++]=' '; else { printf("Input datas error\n"); break; }
i++;} while(top) f[j++]=opst[--top]; /*if(match_kuohao(c)== 待改*/}
main() { char s1[80], s2[80]; double result; printf("Plese input information:\n"); gets(s1); postfix(s1,s2); result=evalpost(s2); printf("The result is:%f " ,result); }
[此贴子已经被作者于2004-11-07 12:05:58编辑过]