user-0 提问: 求大神帮忙修改这个计算器程序，周四中午12:30前需要

回复:
user: user-1
content: 鉴于今天已经周日，就不回答了。

user: user-2
content: 水平没到，解答不了

user: user-3
content: 鉴于今天已经周六，就不回答了。

user: user-4
content: 爱莫能助，我还看不太懂。。。。。。

user: user-5
content: 仅供参考：/*--------------------------------------- 函数型计算器(VC++6.0,Win32 Console)程序由 yu_hua 于2007-07-27设计完成 功能： 目前提供了10多个常用数学函数:     ⑴正弦sin     ⑵余弦cos     ⑶正切tan     ⑷开平方sqrt     ⑸反正弦arcsin     ⑹反余弦arccos     ⑺反正切arctan     ⑻常用对数lg     ⑼自然对数ln     ⑽ｅ指数exp     ⑾乘幂函数∧ 用法： 如果要求2的32次幂，可以打入2^32<回车> 如果要求30度角的正切可键入tan(Pi/6)<回车> 注意不能打入：tan(30)<Enter> 如果要求1.23弧度的正弦，有几种方法都有效： sin(1.23)<Enter> sin 1.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> using namespace std; const char Tab=0x9; const int  DIGIT=1; const int MAXLEN=16384; char s[MAXLEN],*endss; int pcs=15; double fun(double x,char op[],int *iop) {     while (op[*iop-1]<32) //本行使得函数嵌套调用时不必加括号,如 arc sin(sin(1.234)) 只需键入arc sin sin 1.234<Enter>         switch (op[*iop-1]) {         case  7: x=sin(x);  (*iop)--;break;         case  8: x=cos(x);  (*iop)--;break;         case  9: x=tan(x);  (*iop)--;break;         case 10: x=sqrt(x); (*iop)--;break;         case 11: x=asin(x); (*iop)--;break;         case 12: x=acos(x); (*iop)--;break;         case 13: x=atan(x); (*iop)--;break;         case 14: x=log10(x);(*iop)--;break;         case 15: x=log(x);  (*iop)--;break;         case 16: x=exp(x);  (*iop)--;break;         }     return x; } double calc(char *expr,char **addr) {     static int deep; //递归深度     static char *fname[]={ "sin","cos","tan","sqrt","arcsin","arccos","arctan","lg","ln","exp",NULL};     double ST[10]={0.0}; //数字栈     char op[10]={'+'}; //运算符栈     char c,*rexp,*pp,*pf;     int ist=1,iop=1,last,i;      if (!deep) {         pp=pf=expr;         do {             c = *pp++;             if (c!=' '&& c!=Tab)                 *pf++ = c;         } while (c!='\0');     }     pp=expr;     if ((c=*pp)=='-'||c=='+') {         op[0] = c;         pp++;     }     last = !DIGIT;     while ((c=*pp)!='\0') {         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];                 goto operate ;             }         }         else if (c==')') {//右圆括弧             pp++;             break;         } else if (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);                 }             }         } else if (c=='+'||c=='-'||c=='*'||c=='/'||c=='%'||c=='^') {             char cc;             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;             } else if (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];                 goto operate ;             }         }     }     *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;         }     }     return ST[0]; } int main(int argc,char **argv) {     int a;      if (argc<2) {         if (GetConsoleOutputCP()!=936) system("chcp 936>NUL");//中文代码页         cout << "计算函数表达式的值。"<<endl<<"支持(),+,-,*,/,%,^,Pi,sin,cos,tan,sqrt,arcsin,arccos,arctan,lg,ln,exp"<<endl;         while (1) {             cout << "请输入表达式：";             gets(s);             if (s[0]==0) break;//             cout << s <<"=";             cout << setprecision(15) << calc(s,&endss) << endl;         }     } else if (argc==2 && 0==strcmp(argv[1],"/?")) {         if (GetConsoleOutputCP()!=936) system("chcp 936>NUL");//中文代码页         cout << "计算由≥1个命令行参数给出的函数表达式的值。最后一个参数是.0～.15表示将计算结果保留小数0～15位"<<endl<<"支持(),+,-,*,/,%,^^,Pi,sin,cos,tan,sqrt,arcsin,arccos,arctan,lg,ln,exp"<<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);//将空格间隔的各参数连接到s             if (1==sscanf(argv[a],".%d",&pcs) && 0<=pcs && pcs<=15) {//最后一个参数是.0～.15表示将计算结果保留小数0～15位                 printf("%.*lf\n",pcs,calc(s,&endss));             } else {                 strncat(s,argv[a],MAXLEN-1);                 printf("%.15lg\n",calc(s,&endss));             }         } else {             printf("%.15lg\n",calc(s,&endss));         }     }     return 0; }

user: user-6
content: 直接参看：http://bbs.csdn.net/topics/391829991中4楼的代码。

user: user-0
content: 引用 3 楼 G_R_God 的回复:百度，逆波兰算法. 这个好像需要用堆栈吧。 虽然我知道用堆栈法写计算器比较好，但我对这个不熟悉。 有没有在我的代码基础上上修改的方法呢？

user: user-7
content: 百度，逆波兰算法.

user: user-0
content: 楼上格式发错了，抱歉请看这楼   #include <stdio.h> #include <math.h> #include <string.h>  #define MAX 128  void help(void);//显示帮助信息 int checkGrammar(void);//检查算式语法 int getNumber(void);//抽取输入的数字 void getOperator(void);//抽取输入的运算符 float calculate(void);//计算  int n,i;  float val[MAX];  /* It will be a collection of the numbers 抽出来的数字存进这里*/ char userInput[MAX];  char op[64];  /* It will be a collection of the operators抽出来的运算符存这里 */ float sum; /* Sum stores the result of addition and subtraction 储存加减计算的结果 */ float product;  /*Product stores the result of multiplication and division储存乘除计算结果*/ float temp; /* It records if the "divided by zero" error occurs 一个临时量，在除法时才用，用来记录除数，判断除数是否为0*/ //除数为零，需要输出错误信息，然后程序回到等待输入的状态，但这部分我还没做好，用过goto但出错，想看看大神怎么做的 int main(void) { while(1)//反复运行，直到输入“q”回车 	{  		/*Welcome message*/ 		printf("Simple Calculator\n>>>"); 		fgets(userInput,MAX,stdin);  		/*Type q to quit*/ 		if(userInput[0]=='q' && userInput[1]=='\n' && userInput[2]=='\0') 		{ 			printf("Goodbye!\n"); 			break; 		}  		/*Type h for help*/ 		if(userInput[0]=='h' && userInput[1]=='\n' && userInput[2]=='\0') 		{ 			help(); 			continue; 		}  		/*Check before calculate先检查语法*/ 		if(checkGrammar()==1)//返回1表示算式语法有错 		{ 			printf("Error: Illegal Input!\n"); 			continue; 		} 		 		/*Display an error message if divided by zero*/ 		 //下面这行，我在calculator()里面打算temp==0的时候就用goto跳到这里的，结果编译时出错，只能把这行变成注释了 		/*while(0)div:printf("Error: Divided by zero!\n");continue;*/ 		/*In the line above, div: is a label, but I failed to get back to it using "goto"*/ 		 		getNumber(); 		if(getNumber()==1)//It finds two decimal points in a number返回1表示一个数里面有两个或以上小数点 		{ 			printf("Error: Illegal Input!\n"); 			continue; 		} 		getOperator(); 		calculate();  		if(temp==0)//这是判断除数为0的，只写了这里，calculator()里面对应部分没写，看有没有大神能补上 		{ 			printf("Error: Divided by zero!\n"); 			continue; 		} 		 		printf("%.6f\n",val[n]); 	} return 0; }  void help(void)//一堆说明文字 /*Display help message*/ { 	printf("\nSimple Calculator understands the following arithemetic operatoins:\n" 		"^ exponentiation\n" 		"+ addition\n" 		"- subtraction\n" 		"* multiplication\n" 		"/ division\n" 		"e scientific e notation\n" 		"() brackets\n\n"); }  int checkGrammar(void) /*This is a funtion to check if the input string is valid*/ { //这个函数检查输入是否有效 //允许输入带有空格 	int check=0; 	int bracket=0; 	for(check=0;userInput[check]!='\0';check++) 	{  		/*Check if there are invalid characters, ignore white spaces检查有没有不是数字，不是运算符，不是小数点，不是括号的，其中字母只接受科学计数法用的e和E*/ 		if((userInput[check]>'9' || userInput[check]<'0')&&(userInput[check]!='+' && userInput[check]!='-' && userInput[check]!='*' && userInput[check]!='/'  && userInput[check]!='^' && userInput[check]!='e' && userInput[check]!='E'  && userInput[check]!='(' && userInput[check]!=')' && userInput[check]!='.' && userInput[check]!='\0' && userInput[check]!='\n'&& userInput[check]!=' ')) 			return 1;  		/*Check if '+' '-' is followed by anything other than number or '('检查加减号后面是否有不是左括号或者数字的东西*/ 		if(userInput[check]=='+' || userInput[check]=='-') 		{ 			while(userInput[check+1] == ' ')check++;//skip white space 			if(userInput[check+1]!='(' && (userInput[check+1]<'0' || userInput[check+1]>'9')) 				return 1; 		}  		/*Check if there are two operators one folloewed by another and the second one is not '-'检查是否有两个运算符连在一起，比如"**","*/"，但除了加减号，允许算符下一个算符为“-”，表示下一个数是负数*/ 		if(userInput[check]=='+' || userInput[check]=='-' || userInput[check]=='*' || userInput[check]=='/'  || userInput[check]=='^' || userInput[check]=='e' || userInput[check]=='E' || userInput[check]=='.') 		{ 			while(userInput[check+1] == ' ')check++;//skip white space 			if(userInput[check+1]=='+' || userInput[check+1]=='*' || userInput[check+1]=='/'  || userInput[check+1]=='^' || userInput[check+1]=='e' || userInput[check+1]=='E' || userInput[check+1]=='.') 				return 1; 		}  		/*Count brackets检查括号数量和出现次序是否正确*/ 		if(userInput[check]=='(') 			bracket++; 		if(userInput[check]==')') 			bracket--;	 		if(bracket<0) 			return 1; 	} 	 	/*If the number of '(' not equal to ')'*/ 	if(bracket!=0) 		return 1; 	/*If the input is finished with something wrong, which means, not a number or a right bracket*/        //下面这三行检查最后是否以数字或者右括号之外的东西结尾，但运行出错，只能变成注释了 	while(userInput[check-1] == ' ')check--;//In the way going back, skip white space 	//if((userInput[check-1]!=')') && (userInput[check-1]>'9'|| userInput[check]<'0') ) 		//return 1; 	 	return 0; }  void getOperator(void) /*It collects the operators in user input*/ { 	int p=0;  	for(i=0;userInput[i]!='\0';i++) 	{  		while(userInput[i] == ' ')i++;//skip white space  		/*If it is +-(),just copy it*/ 		if(userInput[i] == '+' || userInput[i]== '-' || userInput[i]== '(' || userInput[i]== ')') 			op[p++]=userInput[i]; //对乘除，指数，科学计数法，检查后面有没有跟负号 		/*If it is '*' '/' '^' 'e' 'E',check if it is followed by '-'*/ 		/*It means that we allow these operators followed by a negative number*/ 		if(userInput[i]== '*') 		{ 			while(userInput[i+1] == ' ')i++;//skip white space 			if(userInput[i+1]== '-') //下一行的a只是用来替代“*-”的，毕竟一个算符在op[]里面只能占一个位置，下面的bcd同理 				{op[p++]='a'; i++;}/*Since one operator can only take one palce, we use 'a' to refer '*-'*/ 			else 				op[p++]=userInput[i]; 		}  		if(userInput[i]== '/') 		{ 			while(userInput[i+1] == ' ')i++;//skip white space 			if(userInput[i+1]== '-') 				{op[p++]='b'; i++;}/*'b' refers '/-'*/ 			else 				op[p++]=userInput[i]; 		}  		if(userInput[i]== '^') 		{ 			while(userInput[i+1] == ' ')i++;//skip white space 			if(userInput[i+1]== '-') 				{op[p++]='c'; i++;} /*'c' refers '^-'*/ 			else  				op[p++]=userInput[i]; 		}  		if(userInput[i]== 'e' || userInput[i]== 'E') 		{ 			while(userInput[i+1] == ' ')i++;//skip white space 			if(userInput[i+1]== '-') 				{op[p++]='d'; i++;}  /*'d' refers 'e-'*/ 			else 				op[p++]='e'; 		} 		if(userInput[i]== '\n') 			p++; 		op[p]='\0'; 	}  }  int getNumber(void) /*This function extracts numbers from userInput*/ { 	float integer=0,decimal=0,divide=10; 	n=0; //没遇到小数点之前的算法，遇到空格自动跳过 	for(i=0;userInput[i]!='\n';i++) 		{ 			/*Get the integer part*/ 			if(userInput[i]>='0' && userInput[i]<='9') 			{ 				while((userInput[i]>='0'&& userInput[i]<='9')|| userInput[i]==' ') 				{ 					while(userInput[i+1] == ' ')i++;//skip white space 					integer=(userInput[i]-'0')+integer*10; 					i++; 				}  				/*遇到小数点之后If find a decimal point,skip it, and start getting the decimal part*/ 				if(userInput[i]=='.') 				{ 					i++; 					while(userInput[i+1] == ' ')i++;//skip white space 					while((userInput[i]>='0'&& userInput[i]<='9') || userInput[i]=='.') 					{ 						while(userInput[i+1] == ' ')i++;//skip white space 						if (userInput[i]=='.') return 1; 						decimal=decimal+(userInput[i]-'0')/divide; 						divide*=10; 						i++; 					} 				}  			val[n]=integer+decimal;//整数部分加小数部分，得到完整的数，依次存在val[]里面 			n++; /*The next number will be stored in the next place*/ 			} 		/*Reset for the next number*/ 		integer=0; 		decimal=0; 		divide=10; 		} 	return 0; }  float calculate(void) /*This function based on the operators extracted from the user input, decides the right order of operations, proccess the calculation and get the result*/ { 	/*Reset i and n*/ 	int i=0; 	int n=0;     char symbol='+';  	sum=0; 	product=1; 	temp=1;  	//Base on the operators, choose the right order //根据存在op[]里的算符来决定顺序 //程序的核心部分，借用别人成品的成分很大，但自己调试之后就出问题了     for(;op[i]!=')'&& op[i]!='\0';i++)     { 		         switch(op[i]) 		// The following four cases are division and multiplication 		// Once it find this operators, it will run multidiv() 		// which is a function that keep doing division and multiplication until it finds the next operator to be something else          { 			// If finds a bracket,call the funtion itself 			// It will get in and deal with the things in brackets,which means bracket has higher priority             case '(': 				i++; 				calculate(); //调用自身，前一个大神那里照抄的方法 				break;  			case '+'://如果加减后面还是加减，可以做前一个加减 				if(symbol=='+') //If the next step is + or -, then do + or - 					{val[n] = sum + val[n];sum = val[n];}   				else 					{val[n] = sum - val[n];sum = val[n];}  				symbol = '+';                                         				n++; 				break;              case '-':  //和case “+”一样的 				if(symbol== '+') 					{val[n] = sum + val[n];sum=val[n];}   				else 					{val[n] = sum-val[n];sum= val[n];} 				symbol= '-'; 				n++; 				break;              case '*': 				product=val[n];//set product, so that it can be operated with the next number 				while(1) 				{ 					if(op[i] == '*') 					{ 						if(op[i+1] == '(') 							{i+=2;n++;product= product *calculate();}// call calculate() itself since brackets have the highest priority 						else 							{product=product*val[n+1];i++;n++;} 					} 					else if(op[i] == '/') 					{ 						if(op[i+1] == '(') //原计划是在这里先让temp=calculator()，然后判断temp是否为0，不是的话product=product/temp，但好像没效果，下两行temp=val[n+1]时也是这样 							{i+=2;n++;product=product/calculate();} 						else 							{product=product/val[n+1];i++;n++; } 					 } 					else break;//If the next operator is not * or /, stop 				} 				val[n]=product; 				if(op[i]=='+'||op[i]=='-'||op[i]=='\0') 				i--; 				break;  			 //这个和乘那个也是一样的			 case '/': 				product=val[n]; 				while(1) 				{ 					if(op[i] == '*') 					{ 						if(op[i+1] == '(') 							{i+=2;n++;product=product*calculate();} 						else 							{product=product*val[n+1];i++;n++;} 					} 					else if(op[i] == '/') 					{ 						if(op[i+1] == '(') 							{i+=2;n++;product=product/calculate();} 						else 							{product=product/val[n+1];i++;n++; } 					 } 					else break; 				} 				val[n]=product;				if(op[i]=='+'||op[i]=='-'||op[i]=='\0') 				i--; 				break; //乘除case能顺利运行的话，我自己会加ab的case 		} 	} 	return val[n]; }

user: user-0
content: #include <stdio.h> #include <math.h> #include <string.h>  #define MAX 128  void help(void);//显示帮助信息 int checkGrammar(void);//检查算式语法 int getNumber(void);//抽取输入的数字 void getOperator(void);//抽取输入的运算符 float calculate(void);//计算  int n,i;  float val[MAX];  /* It will be a collection of the numbers 抽出来的数字存进这里*/ char userInput[MAX];  char op[64];  /* It will be a collection of the operators抽出来的运算符存这里 */ float sum; /* Sum stores the result of addition and subtraction 储存加减计算的结果 */ float product;  /*Product stores the result of multiplication and division储存乘除计算结果*/ float temp; /* It records if the "divided by zero" error occurs 一个临时量，在除法时才用，用来记录除数，判断除数是否为0*/ //除数为零，需要输出错误信息，然后程序回到等待输入的状态，但这部分我还没做好，用过goto但出错，想看看大神怎么做的 int main(void) { while(1)//反复运行，直到输入“q”回车 	{  		/*Welcome message*/ 		printf("Simple Calculator\n>>>"); 		fgets(userInput,MAX,stdin);  		/*Type q to quit*/ 		if(userInput[0]=='q' && userInput[1]=='\n' && userInput[2]=='\0') 		{ 			printf("Goodbye!\n"); 			break; 		}  		/*Type h for help*/ 		if(userInput[0]=='h' && userInput[1]=='\n' && userInput[2]=='\0') 		{ 			help(); 			continue; 		}  		/*Check before calculate先检查语法*/ 		if(checkGrammar()==1)//返回1表示算式语法有错 		{ 			printf("Error: Illegal Input!\n"); 			continue; 		} 		 		/*Display an error message if divided by zero*/ 		 //下面这行，我在calculator()里面打算temp==0的时候就用goto跳到这里的，结果编译时出错，只能把这行变成注释了 		/*while(0)div:printf("Error: Divided by zero!\n");continue;*/ 		/*In the line above, div: is a label, but I failed to get back to it using "goto"*/ 		 		getNumber(); 		if(getNumber()==1)//It finds two decimal points in a number返回1表示一个数里面有两个或以上小数点 		{ 			printf("Error: Illegal Input!\n"); 			continue; 		} 		getOperator(); 		calculate();  		if(temp==0)//这是判断除数为0的，只写了这里，calculator()里面对应部分没写，看有没有大神能补上 		{ 			printf("Error: Divided by zero!\n"); 			continue; 		} 		 		printf("%.6f\n",val[n]); 	} return 0; }  void help(void)//一堆说明文字 /*Display help message*/ { 	printf("\nSimple Calculator understands the following arithemetic operatoins:\n" 		"^ exponentiation\n" 		"+ addition\n" 		"- subtraction\n" 		"* multiplication\n" 		"/ division\n" 		"e scientific e notation\n" 		"() brackets\n\n"); }  int checkGrammar(void) /*This is a funtion to check if the input string is valid*/ { //这个函数检查输入是否有效 //允许输入带有空格 	int check=0; 	int bracket=0; 	for(check=0;userInput[check]!='\0';check++) 	{  		/*Check if there are invalid characters, ignore white spaces检查有没有不是数字，不是运算符，不是小数点，不是括号的，其中字母只接受科学计数法用的e和E*/ 		if((userInput[check]>'9' || userInput[check]<'0')&&(userInput[check]!='+' && userInput[check]!='-' && userInput[check]!='*' && userInput[check]!='/'  && userInput[check]!='^' && userInput[check]!='e' && userInput[check]!='E'  && userInput[check]!='(' && userInput[check]!=')' && userInput[check]!='.' && userInput[check]!='\0' && userInput[check]!='\n'&& userInput[check]!=' ')) 			return 1;  		/*Check if '+' '-' is followed by anything other than number or '('检查加减号后面是否有不是左括号或者数字的东西*/ 		if(userInput[check]=='+' || userInput[check]=='-') 		{ 			while(userInput[check+1] == ' ')check++;//skip white space 			if(userInput[check+1]!='(' && (userInput[check+1]<'0' || userInput[check+1]>'9')) 				return 1; 		}  		/*Check if there are two operators one folloewed by another and the second one is not '-'检查是否有两个运算符连在一起，比如"**","*/"，但除了加减号，允许算符下一个算符为“-”，表示下一个数是负数*/ 		if(userInput[check]=='+' || userInput[check]=='-' || userInput[check]=='*' || userInput[check]=='/'  || userInput[check]=='^' || userInput[check]=='e' || userInput[check]=='E' || userInput[check]=='.') 		{ 			while(userInput[check+1] == ' ')check++;//skip white space 			if(userInput[check+1]=='+' || userInput[check+1]=='*' || userInput[check+1]=='/'  || userInput[check+1]=='^' || userInput[check+1]=='e' || userInput[check+1]=='E' || userInput[check+1]=='.') 				return 1; 		}  		/*Count brackets检查括号数量和出现次序是否正确*/ 		if(userInput[check]=='(') 			bracket++; 		if(userInput[check]==')') 			bracket--;	 		if(bracket<0) 			return 1; 	} 	 	/*If the number of '(' not equal to ')'*/ 	if(bracket!=0) 		return 1; 	/*If the input is finished with something wrong, which means, not a number or a right bracket*/        //下面这三行检查最后是否以数字或者右括号之外的东西结尾，但运行出错，只能变成注释了 	while(userInput[check-1] == ' ')check--;//In the way going back, skip white space 	//if((userInput[check-1]!=')') && (userInput[check-1]>'9'|| userInput[check]<'0') ) 		//return 1; 	 	return 0; }  void getOperator(void) /*It collects the operators in user input*/ { 	int p=0;  	for(i=0;userInput[i]!='\0';i++) 	{  		while(userInput[i] == ' ')i++;//skip white space  		/*If it is +-(),just copy it*/ 		if(userInput[i] == '+' || userInput[i]== '-' || userInput[i]== '(' || userInput[i]== ')') 			op[p++]=userInput[i]; //对乘除，指数，科学计数法，检查后面有没有跟负号 		/*If it is '*' '/' '^' 'e' 'E',check if it is followed by '-'*/ 		/*It means that we allow these operators followed by a negative number*/ 		if(userInput[i]== '*') 		{ 			while(userInput[i+1] == ' ')i++;//skip white space 			if(userInput[i+1]== '-') //下一行的a只是用来替代“*-”的，毕竟一个算符在op[]里面只能占一个位置，下面的bcd同理 				{op[p++]='a'; i++;}/*Since one operator can only take one palce, we use 'a' to refer '*-'*/ 			else 				op[p++]=userInput[i]; 		}  		if(userInput[i]== '/') 		{ 			while(userInput[i+1] == ' ')i++;//skip white space 			if(userInput[i+1]== '-') 				{op[p++]='b'; i++;}/*'b' refers '/-'*/ 			else 				op[p++]=userInput[i]; 		}  		if(userInput[i]== '^') 		{ 			while(userInput[i+1] == ' ')i++;//skip white space 			if(userInput[i+1]== '-') 				{op[p++]='c'; i++;} /*'c' refers '^-'*/ 			else  				op[p++]=userInput[i]; 		}  		if(userInput[i]== 'e' || userInput[i]== 'E') 		{ 			while(userInput[i+1] == ' ')i++;//skip white space 			if(userInput[i+1]== '-') 				{op[p++]='d'; i++;}  /*'d' refers 'e-'*/ 			else 				op[p++]='e'; 		} 		if(userInput[i]== '\n') 			p++; 		op[p]='\0'; 	}  }  int getNumber(void) /*This function extracts numbers from userInput*/ { 	float integer=0,decimal=0,divide=10; 	n=0; //没遇到小数点之前的算法，遇到空格自动跳过 	for(i=0;userInput[i]!='\n';i++) 		{ 			/*Get the integer part*/ 			if(userInput[i]>='0' && userInput[i]<='9') 			{ 				while((userInput[i]>='0'&& userInput[i]<='9')|| userInput[i]==' ') 				{ 					while(userInput[i+1] == ' ')i++;//skip white space 					integer=(userInput[i]-'0')+integer*10; 					i++; 				}  				/*遇到小数点之后If find a decimal point,skip it, and start getting the decimal part*/ 				if(userInput[i]=='.') 				{ 					i++; 					while(userInput[i+1] == ' ')i++;//skip white space 					while((userInput[i]>='0'&& userInput[i]<='9') || userInput[i]=='.') 					{ 						while(userInput[i+1] == ' ')i++;//skip white space 						if (userInput[i]=='.') return 1; 						decimal=decimal+(userInput[i]-'0')/divide; 						divide*=10; 						i++; 					} 				}  			val[n]=integer+decimal;//整数部分加小数部分，得到完整的数，依次存在val[]里面 			n++; /*The next number will be stored in the next place*/ 			} 		/*Reset for the next number*/ 		integer=0; 		decimal=0; 		divide=10; 		} 	return 0; }  float calculate(void) /*This function based on the operators extracted from the user input, decides the right order of operations, proccess the calculation and get the result*/ { 	/*Reset i and n*/ 	int i=0; 	int n=0;     char symbol='+';  	sum=0; 	product=1; 	temp=1;  	//Base on the operators, choose the right order //根据存在op[]里的算符来决定顺序 //程序的核心部分，借用别人成品的成分很大，但自己调试之后就出问题了     for(;op[i]!=')'&& op[i]!='\0';i++)     { 		         switch(op[i]) 		// The following four cases are division and multiplication 		// Once it find this operators, it will run multidiv() 		// which is a function that keep doing division and multiplication until it finds the next operator to be something else          { 			// If finds a bracket,call the funtion itself 			// It will get in and deal with the things in brackets,which means bracket has higher priority             case '(': 				i++; 				calculate(); //调用自身，前一个大神那里照抄的方法 				break;  			case '+'://如果加减后面还是加减，可以做前一个加减 				if(symbol=='+') //If the next step is + or -, then do + or - 					{val[n] = sum + val[n];sum = val[n];}   				else 					{val[n] = sum - val[n];sum = val[n];}  				symbol = '+';                                         				n++; 				break;              case '-':  //和case “+”一样的 				if(symbol== '+') 					{val[n] = sum + val[n];sum=val[n];}   				else 					{val[n] = sum-val[n];sum= val[n];} 				symbol= '-'; 				n++; 				break;              case '*': 				product=val[n];//set product, so that it can be operated with the next number 				while(1) 				{ 					if(op[i] == '*') 					{ 						if(op[i+1] == '(') 							{i+=2;n++;product= product *calculate();}// call calculate() itself since brackets have the highest priority 						else 							{product=product*val[n+1];i++;n++;} 					} 					else if(op[i] == '/') 					{ 						if(op[i+1] == '(') //原计划是在这里先让temp=calculator()，然后判断temp是否为0，不是的话product=product/temp，但好像没效果，下两行temp=val[n+1]时也是这样 							{i+=2;n++;product=product/calculate();} 						else 							{product=product/val[n+1];i++;n++; } 					 } 					else break;//If the next operator is not * or /, stop 				} 				val[n]=product; 				if(op[i]=='+'||op[i]=='-'||op[i]=='\0') 				i--; 				break;  			 //这个和乘那个也是一样的			 case '/': 				product=val[n]; 				while(1) 				{ 					if(op[i] == '*') 					{ 						if(op[i+1] == '(') 							{i+=2;n++;product=product*calculate();} 						else 							{product=product*val[n+1];i++;n++;} 					} 					else if(op[i] == '/') 					{ 						if(op[i+1] == '(') 							{i+=2;n++;product=product/calculate();} 						else 							{product=product/val[n+1];i++;n++; } 					 } 					else break; 				} 				val[n]=product;				if(op[i]=='+'||op[i]=='-'||op[i]=='\0') 				i--; 				break; //乘除case能顺利运行的话，我自己会加ab的case 		} 	} 	return val[n]; }