Task description
Please write a program: the executable file generated after the program is built (assuming the file name is mycal.exe) put it in the root directory of Disk c, at the command prompt, go to c: \ and type mycal 100 + 200 to run 300.00; type mycal 100*200 to run 20000.00, etc. the operators in the program are consistent with those in c language. The calculation results retain two significant digits after the decimal point.
Relevant knowledge
In order to complete the task of this level, you need to master:
(1) Knowledge about parameters in main function
(2) String and character pointer array
Knowledge about parameters in main function
(1) The main function is the entry point for program execution.
(2) The main function can have a return value or no return value.
(3) The main function can take parameters or no parameters.
Example: void main(); int main(); int main(int argc,char* argv[]);
The following code is a typical example of the main function with parameters. If you understand it, you can easily pass this pass:
//argc: represents the number of elements of the character pointer array argv, with a minimum value of 1 //argv: string array. Each element of the array is a string, //argv[0]: indicates the full path where the program is located. The value of this parameter is passed in by the operating system and cannot be changed by the user //argv[1],argv[2]... The user parameters entered for the user when running the program through the command line are strings. #include<stdio.h> int main(int argc, char* argv[]) { int k; printf("Number of parameters:%d\n", argc); for (k = 0; k < argc; k++) { printf("parameter%d:", k); puts(argv[k]); } getchar(); return 0; }
You can copy the above code to your local development environment. After build ing, double-click the generated executable file (assuming project1.exe), and the following running result interface will be displayed.
Double clicking the executable directly is equivalent to that the user does not pass in any parameters to main, so the number of parameters is 1 by default, and argv[0] storage is the value of the first parameter, Is the full path (including file name) of the current application. If you want to pass in parameters, you must run the program on the command line. The operation is as follows: Step 1: enter "cmd" in the search text box in the system "start" menu, as shown in the figure:
Step 2: suppose your program is on disk D (d:\myapp\project1.exe), enter D:, enter, and then enter cd d:\myapp. The directory where the project1.exe file is located will be set as the current directory
At this point, you can run the program from the command line. As shown in the figure below.
According to the above exercise test, you should be able to understand the usage of main with parameters. In other words, the first parameter that the user passes to main through the command line is argv[1], which is a character array (or string). For this problem, your program code is nothing more than parsing the string to get the operator and the operands on the left and right sides, and then do the corresponding operation to output the result.
Programming requirements
The source file of this program contains two. One is the source file main where the main function is located c. The other is the definition source file myfun. Com for the function of parsing a string c. You just need to understand main Based on the code in C, complete myfun at the given place in the code area on the right C (you can also delete the existing code and start from scratch according to your idea). The code of main.c is as follows:
#include<stdio.h> void processUserInputStr(char* s); int main(int argc, char* argv[]) { processUserInputStr(argv[1]); return 0; }
code implementation
#include<string.h> #include <stdlib.h> //s: A string like a [operator] b, for example: 4.5 + 9,6-9,4 / 5,8 * 7 //After processing, the function outputs the operation result of the number. For example, if s is "20.3 + 1", the function outputs 21.30 (2 significant digits after the decimal point are reserved) on the screen //The basic algorithm of internal operation of this function is: //Step 1: find the position index corresponding to the operator //Step 2: copy the preceding string to strA and the following string to strB according to the operator position //Step 3: convert strA and strB into numerical values (use the function atof, please Baidu for specific usage) a and b //Step 4: perform corresponding operations on a and b according to the operator //Step 5: output operation result void processUserInputStr(char* s) { int len=strlen(s); //Number of valid characters in s //RA and STRB are respectively used to store the string corresponding to the two operands char strA[100],strB[100]; //a. B is used to store the number after string conversion, and result is used to save the operation result double a,b,result; char op;//Used to store the string corresponding to the operator in s int k,posOp;//posOp: position index of the symbol /**********begin**********/ for(k=0;s[k]!='+' && s[k]!='-' && s[k]!='*' && s[k]!='/';k++) { strA[k]=s[k]; } op=s[k];int j,i; for(i=0,j=k+1;s[j]!='\0';i++,j++) { strB[i]=s[j]; } a=atof(strA); b=atof(strB); switch(op){ case '+':result=a+b;break; case '-':result=a-b;break; case '*':result=a*b;break; case '/':result=a/b;break; default:printf("input wrong"); } printf("%.2lf",result); /**********end**********/ }