#define_stdc_want_lib_ext1_ 1#include<stdio.h>#include<string.h>#include<ctype.h>#include<stdlib.h>#include<stdbool.h>#include<math.h>#defineBuf_len 256intMainvoid){ CharBuf[buf_len]; //Char number_string[30]; CharOP =0;//operatorsize_t index =0; size_t to=0; size_t Buf_length=0; //unsigned int number_length = 0; Doubleresult =0.0; DoubleNumber =0.0; Char*endptr =NULL; printf ("to use this calculator, enter any expression with" "or without spaces."); printf ("\nan expression may include the operators"); printf ("+,-, *,/, percent, or ^ (raise to a power)."); printf ("\nuse = at the beginning of a line to operate on"); printf ("\nthe result of the previous calculation."); printf ("\nenter quit to stop the calculator.\n\n"); //1. Read in the user input string, quit, Exit Char*ptr =NULL; while(true) {ptr=fgets (Buf,buf_len,stdin); if(ptr = =false)//Test{printf ("Error Reading input.\n"); return 1; } if(strcmp (BUF,"quit\n")==0) Break; Buf_length=strnlen_s (Buf,buf_len); buf[--buf_length] =' /'; //2. Remove spaces from the input string for(To =0, index =0; index<=buf_length;++index) { if(* (buf+index)! =' ') * (buf+ to++) = * (buf+index); } buf_length=strnlen_s (Buf,buf_len); //3. Check the = operator at the beginning of the input string,//if there is a = operator, the result of the previous input string is taken as the left operand of the next arithmetic operation. Find the first operandindex =0; if(buf[index]=='=') Index++; Else { //4. Extract the corresponding input character to the left operand of the next arithmetic operation, and convert the string to a double typeresult = Strtod (buf+index,&endptr);//Convert and store numbersprintf"%s\n", endptr); Index= Endptr-buf;//gets the index of the next character } //5. Extract the arithmetic operator and record it. while(Index <buf_length) {op= * (buf+index++); //6. Extract the appropriate input character for the right operand and convert the substring to a double typeNumber = Strtod (buf+index,&endptr); Index= Endptr-buf; //7. Perform the operation and store the result as the next left operand Switch(OP) { Case '+': Result+=Number ; Break; Case '-': Result-=Number ; Break; Case '*': Result*=Number ; Break; Case '/': if(Number = =0) printf ("\n\n\adivision by Zero error!\n"); Elseresult/=Number ; Break; Case '%': if((Long Long) Number = = 0LL) printf ("\n\n\adivision by Zero error!\n"); Elseresult= (Double)((Long Long) Result% (Long Long) number); Break; Case '^': Result=Pow (result,number); Break; default: printf ("\n\n\aillegal operation!\n"); Break; } } //8. If it is not at the end of the string, return to step 6th, if at the end of the string, return to the first stepprintf"=%f\n", result); } return 0;}
7, pointer usage, calculator code