加减乘除混合运算/* Note:Your choice isC IDE */ #include "" #include "" #include "" int n=0; char record[20]; float product(); float change(); float muli() { float summ; summ=product(); while(record[n]=='-'||record[n]=='+') { switch(record[n]) { case '+':n++;summ+=product();break; case '-':n++;summ-=product();break; }} return summ; } float product() { float sump; sump=change(); while(record[n]=='*'||record[n]=='/') { switch(record[n]) { case '*':n++;sump*=change();break; case '/':n++;sump/=change();break; }} return sump; } float change() { float sumc; char rec[20],i=0; if(record[n]=='(') { n++; sumc=muli(); } if(record[n]==')') n++; while(isdigit(record[n])||record[n]=='.') { while(isdigit(record[n])||record[n]=='.') rec[i++]=record[n++]; rec[i]='\0'; sumc=atof(rec); } return sumc; } void main() { while(1) { n=0; scanf("%s",record); printf("%s=%g\n",record,muli()); }} #include "" #include "" // 网上找的,在 VC 下测试通过,还改了一个显示的小错。#include "" #include "" //expression evaluate #define iMUL 0 #define iDIV 1 #define iADD 2 #define iSUB 3 #define iCap 4 //#define LtKH 5 //#define RtKH 6 #define MaxSize 100 void iPush(float); float iPop(); float StaOperand[MaxSize]; int iTop=-1; // char Srcexp[MaxSize]; char Capaexp[MaxSize]; char RevPolishexp[MaxSize]; float NumCapaTab[26]; char validexp[]="*/+-()"; char NumSets[]="0123456789"; char StackSymb[MaxSize]; int operands; // void NumsToCapas(char [], int , char [], float []); int CheckExpress(char); int PriorChar(char,char); int GetOperator(char [], char); void counterPolishexp(char INexp[], int slen, char Outexp[]); float CalcRevPolishexp(char [], float [], char [], int); void main() { int ilen; float iResult=; printf("enter a valid number string:\n"); memset(StackSymb,0,MaxSize); memset(NumCapaTab,0,26); //A--, B--, etc. gets(Srcexp); ilen=strlen(Srcexp); //printf("source expression:%s\n",Srcexp); NumsToCapas(Srcexp,ilen,Capaexp,NumCapaTab); printf("Numbers listed as follows:\n"); i
加减乘除混合运算 来自淘豆网m.daumloan.com转载请标明出处.