问题描述
#include"stdio.h"#include"string.h"#defineaddress"jisuanqi.txt"charstack[200],out[200];intk=0,t=0;voidup(inta,intb,charflag){intq;if(flag=='*'){q=a*b;out[k]=q+'0';//当q大于9时存入错误,下面除法一样k++;}else{q=(a-'0')/(b-'0');out[k]=q+'0';k++;}}voidresult(){intlen1=strlen(stack)-1,len2=strlen(out)-1;while(len2>0){if(stack[len1]=='+')out[len2-1]=(out[len2]-'0')+(out[len2-1]-'0')+'0';elseout[len2-1]=(out[len2-1]-'0')-(out[len2]-'0')+'0';len2-=1;stack[len1]=NULL;len1--;}printf("%s",out);}voidtranslate(charstr[]){stack[0]='1';charch,flag;inti=0,l=0,j=0;inta,b;ch=str[i];while(ch!=''){switch(ch){case'-':case'+':for(l;l<strlen(stack);l++)if(stack[l]=='*'||stack[l]=='/')break;flag=stack[l];stack[l]=ch;k=k-2;a=out[k]-'0';b=out[k+1]-'0';out[k]=NULL;up(a,b,flag);break;case'*':case'/':stack[t]=ch;t++;break;default:j=0;while(ch>='0'&&ch<='9'){j=j*10+(ch-'0');i++;ch=str[i];}i--;out[k]=j+'0';k++;break;}i++;ch=str[i];}}intmain(){FILE*fp;charstr[200];fp=fopen(address,"w");printf("pleaseinputastring:n");gets(str);fputs(str,fp);translate(str);result();fputc('=',fp);fputs(out,fp);fclose(fp);}
解决方案
解决方案二:
仅供参考:/*---------------------------------------函数型计算器(VC++6.0,Win32Console)功能:目前提供了10多个常用数学函数:⑴正弦sin⑵余弦cos⑶正切tan⑷开平方sqrt⑸反正弦arcsin⑹反余弦arccos⑺反正切arctan⑻常用对数lg⑼自然对数ln⑽e指数exp⑾乘幂函数^⑿向上取整ceil⒀向下取整floor⒁四舍五入取整round用法:如果要求2的32次幂,可以打入2^32<回车>如果要求30度角的正切可键入tan(Pi/6)<回车>注意不能打入:tan(30)<Enter>如果要求1.23弧度的正弦,有几种方法都有效:sin(1.23)<Enter>sin1.23<Enter>sin1.23<Enter>如果验证正余弦的平方和公式,可打入sin(1.23)^2+cos(1.23)^2<Enter>或sin1.23^2+cos1.23^2<Enter>此外两函数表达式连在一起,自动理解为相乘如:sin1.23cos0.77+cos1.23sin0.77就等价于sin(1.23)*cos(0.77)+cos(1.23)*sin(0.77)当然你还可以依据三角变换,再用sin(1.23+0.77)也即sin2验证一下。本计算器充分考虑了运算符的优先级因此诸如:2+3*4^2实际上相当于:2+(3*(4*4))另外函数名前面如果是数字,那么自动认为二者相乘.同理,如果某数的右侧是左括号,则自动认为该数与括弧项之间隐含一乘号。如:3sin1.2^2+5cos2.1^2相当于3*sin2(1.2)+5*cos2(2.1)又如:4(3-2(sqrt5-1)+ln2)+lg5相当于4*(3-2*(√5-1)+loge(2))+log10(5)此外,本计算器提供了圆周率Pi键入字母时不区分大小写,以方便使用。----------------------------------------*/#include<iostream>#include<iomanip>#include<cstdlib>#include<cstring>#include<cctype>#include<cmath>#include<stdio.h>#include<string.h>#include<windows.h>usingnamespacestd;constcharTab=0x9;constintDIGIT=1;constintMAXLEN=16384;chars[MAXLEN],*endss;intpcs=15;doubleround(doubledVal,shortiPlaces){//iPlaces>=0chars[30];doubledRetval;sprintf(s,"%.*lf",iPlaces,dVal);sscanf(s,"%lf",&dRetval);return(dRetval);}doublefun(doublex,charop[],int*iop){while(op[*iop-1]<32)//本行使得函数嵌套调用时不必加括号,如arcsin(sin(1.234))只需键入arcsinsin1.234<Enter>switch(op[*iop-1]){case7:x=sin(x);(*iop)--;break;case8:x=cos(x);(*iop)--;break;case9:x=tan(x);(*iop)--;break;case10:x=sqrt(x);(*iop)--;break;case11:x=asin(x);(*iop)--;break;case12:x=acos(x);(*iop)--;break;case13:x=atan(x);(*iop)--;break;case14:x=log10(x);(*iop)--;break;case15:x=log(x);(*iop)--;break;case16:x=exp(x);(*iop)--;break;case17:x=ceil(x);(*iop)--;break;case18:x=floor(x);(*iop)--;break;case19:x=round(x,0);(*iop)--;break;}returnx;}doublecalc(char*expr,char**addr){staticintdeep;//递归深度staticchar*fname[]={"sin","cos","tan","sqrt","arcsin","arccos","arctan","lg","ln","exp","ceil","floor","round",NULL};doubleST[10]={0.0};//数字栈charop[10]={'+'};//运算符栈charc,*rexp,*pp,*pf;intist=1,iop=1,last,i;if(!deep){pp=pf=expr;do{c=*pp++;if(c!=''&&c!=Tab)*pf++=c;}while(c!='');}pp=expr;if((c=*pp)=='-'||c=='+'){op[0]=c;pp++;}last=!DIGIT;while((c=*pp)!=''){if(c=='('){//左圆括弧deep++;ST[ist++]=calc(++pp,addr);deep--;ST[ist-1]=fun(ST[ist-1],op,&iop);pp=*addr;last=DIGIT;if(*pp=='('||isalpha(*pp)&&strnicmp(pp,"Pi",2)){//目的是:当右圆括弧的右恻为左圆括弧或函数名字时,默认其为乘法op[iop++]='*';last=!DIGIT;c=op[--iop];gotooperate;}}elseif(c==')'){//右圆括弧pp++;break;}elseif(isalpha(c)){if(!strnicmp(pp,"Pi",2)){if(last==DIGIT){cout<<"π左侧遇)"<<endl;exit(1);}ST[ist++]=3.14159265358979323846264338328;ST[ist-1]=fun(ST[ist-1],op,&iop);pp+=2;last=DIGIT;if(!strnicmp(pp,"Pi",2)){cout<<"两个π相连"<<endl;exit(2);}if(*pp=='('){cout<<"π右侧遇("<<endl;exit(3);}}else{for(i=0;(pf=fname[i])!=NULL;i++)if(!strnicmp(pp,pf,strlen(pf)))break;if(pf!=NULL){op[iop++]=07+i;pp+=strlen(pf);}else{cout<<"陌生函数名"<<endl;exit(4);}}}elseif(c=='+'||c=='-'||c=='*'||c=='/'||c=='%'||c=='^'){charcc;if(last!=DIGIT){cout<<"运算符粘连"<<endl;exit(5);}pp++;if(c=='+'||c=='-'){do{cc=op[--iop];--ist;switch(cc){case'+':ST[ist-1]+=ST[ist];break;case'-':ST[ist-1]-=ST[ist];break;case'*':ST[ist-1]*=ST[ist];break;case'/':ST[ist-1]/=ST[ist];break;case'%':ST[ist-1]=fmod(ST[ist-1],ST[ist]);break;case'^':ST[ist-1]=pow(ST[ist-1],ST[ist]);break;}}while(iop);op[iop++]=c;}elseif(c=='*'||c=='/'||c=='%'){operate:cc=op[iop-1];if(cc=='+'||cc=='-'){op[iop++]=c;}else{--ist;op[iop-1]=c;switch(cc){case'*':ST[ist-1]*=ST[ist];break;case'/':ST[ist-1]/=ST[ist];break;case'%':ST[ist-1]=fmod(ST[ist-1],ST[ist]);break;case'^':ST[ist-1]=pow(ST[ist-1],ST[ist]);break;}}}else{cc=op[iop-1];if(cc=='^'){cout<<"乘幂符连用"<<endl;exit(6);}op[iop++]=c;}last=!DIGIT;}else{if(last==DIGIT){cout<<"两数字粘连"<<endl;exit(7);}ST[ist++]=strtod(pp,&rexp);ST[ist-1]=fun(ST[ist-1],op,&iop);if(pp==rexp){cout<<"非法字符"<<endl;exit(8);}pp=rexp;last=DIGIT;if(*pp=='('||isalpha(*pp)){op[iop++]='*';last=!DIGIT;c=op[--iop];gotooperate;}}}*addr=pp;if(iop>=ist){cout<<"表达式有误"<<endl;exit(9);}while(iop){--ist;switch(op[--iop]){case'+':ST[ist-1]+=ST[ist];break;case'-':ST[ist-1]-=ST[ist];break;case'*':ST[ist-1]*=ST[ist];break;case'/':ST[ist-1]/=ST[ist];break;case'%':ST[ist-1]=fmod(ST[ist-1],ST[ist]);break;case'^':ST[ist-1]=pow(ST[ist-1],ST[ist]);break;}}returnST[0];}intmain(intargc,char**argv){inta;if(argc<2){if(GetConsoleOutputCP()!=936)system("chcp936>NUL");//中文代码页cout<<"计算函数表达式的值。"<<endl<<"支持(),+,-,*,/,%,^,Pi,sin,cos,tan,sqrt,arcsin,arccos,arctan,lg,ln,exp,ceil,floor,round"<<endl;while(1){cout<<"请输入表达式:";gets(s);if(s[0]==0)break;//cout<<s<<"=";cout<<setprecision(15)<<calc(s,&endss)<<endl;}}elseif(argc==2&&0==strcmp(argv[1],"/?")){if(GetConsoleOutputCP()!=936)system("chcp936>NUL");//中文代码页cout<<"计算由≥1个命令行参数给出的函数表达式的值。最后一个参数是.0~.15表示将计算结果保留小数0~15位"<<endl<<"支持(),+,-,*,/,%,^^,Pi,sin,cos,tan,sqrt,arcsin,arccos,arctan,lg,ln,exp,ceil,floor,round"<<endl;}else{strncpy(s,argv[1],MAXLEN-1);s[MAXLEN-1]=0;if(argc>2){for(a=2;a<argc-1;a++)strncat(s,argv[a],MAXLEN-1);//将空格间隔的各参数连接到sif(1==sscanf(argv[a],".%d",&pcs)&&0<=pcs&&pcs<=15){//最后一个参数是.0~.15表示将计算结果保留小数0~15位printf("%.*lfn",pcs,calc(s,&endss));}else{strncat(s,argv[a],MAXLEN-1);printf("%.15lgn",calc(s,&endss));}}else{printf("%.15lgn",calc(s,&endss));}}return0;}