Four operations on the console and four operations on the console
Four arithmetic operations based on the consoleI. Briefly describe the question requirements:
1. In addition to integers, it also supports four arithmetic operations for real scores, such as 1/6 + 1/8 = 7/24.
2. The operators are +, −, ×, and operator.
3. It is required to be able to process user input, determine errors, and calculate the correct rate of score statistics.
4. You must be able to process the real scores of user input, such as 1/2 and 5/12.
5. Use the-n parameter to control the number of questions generated. For example, execute the following command to generate 10 questions.
Sizeyuansuan.exe-n 10
II. Implementation steps:A. Requirement Analysis
You can enter parameters in the console to write a program and generate a question based on the parameters. The question must satisfy the true score of the fraction, and determine whether the answers entered by the students are correct. In the end, the score must be given.
B. basic functions of Function Design
You can obtain parameters from the console. It is compatible with the fractional and Integer Parameters function. It generates the real fraction question and the simplest fraction function, and the calculation function. It verifies the answer function.
C. Design implementation
To meet the functional requirements, we have written the following six functions:
Int add (); // addition int sub (); // subtraction int mul (); // multiplication int div (); // division operation int ran (); // generate question int check (); // verify the answer
D. Code Description [source file]
- Get parameters from the console
# Include <cstdlib> int main (int argc, char * argv []) {n = atoi (* (argv + 2 )); // convert the parameters obtained on the console into integer variables}
- Compatible with fractional and Integer Parameters
Cin> a; B = cin. get (); if (B! = '\ N') {// If B obtains the "enter" parameter, the second parameter is not obtained. Otherwise, cin> c ;}
- Generate the question of true fraction and the simplest fraction Function
Int ran () {int m = 1, n = 1; srand (time (NULL); // generates different random numbers while (1) {// prevent generation of false fraction q1 = rand () % 10; q2 = rand () % 9 + 1; if (q1 <= q2) break;} while (1) {// prevent generation of false fraction q3 = rand () % 10; q4 = rand () % 9 + 1; if (q3 <= q4) break ;} for (int I = 2; I <100; I ++) {if (q1% I = 0 & q2 % I = 0) {m = I ;} if (q3% I = 0 & q4 % I = 0) {n = I ;}q1 = q1/m; q2 = q2/m; q3 = q3/n; q4 = q4/n; return 0 ;}
Int check () {cin> a; B = cin. get (); if (B! = '\ N') {cin> c;} int w = 1; for (int I = 2; I <100; I ++) {if (r1% I = 0 & r2 % I = 0) {w = I ;}} r1 = r1/w; r2 = r2/w; if (r2 = 1) {if (r1 = a & B! = '/') {N1 ++; cout <"correct answer! "<Endl;} else {n2 ++; cout <" an error is returned. make persistent efforts! "<" Correct answer: "<r1 <endl;} cout <" correct rate: "<setprecision (4) <100 * n1/n <"%" <endl;} else {if (r1 = a & B = '/' & r2 = c) {// judge whether the input is an integer or fraction n1 ++; cout <"correct answer! "<Endl;} else {n2 ++; cout <" an error is returned. make persistent efforts! "<" Correct answer: "<r1 <"/"<r2 <endl;} cout <" correct rate: "<setprecision (4) <100 * n1/n <"%" <endl;} return 0 ;}
E. Test Run
Iii. PSP
PSP2.1 |
Personal Software Process Stages |
Estimated time (min)
|
Actual time (min) |
Planning |
Plan |
12 |
10 |
· Estimate |
Estimated time required for this task |
120 |
115 |
Development |
Development |
80 |
90 |
· Analysis |
Demand Analysis (including learning new technologies) |
10 |
10 |
· Design Spec |
Generate design document |
7 |
10 |
· Design Review |
Design Review |
6 |
5 |
· Coding Standard |
Code Specification |
4 |
5 |
· Design |
Specific design |
30 |
40 |
· Coding |
Code |
35 |
40 |
· Code Review |
Code Review |
10 |
15 |
· Test |
Test (self-test, code modification, and modification submission) |
15 |
15 |
Reporting |
Report |
20 |
25 |
· |
Test report |
5 |
5 |
· |
Computing workload |
4 |
7 |
· |
And propose a process improvement plan |
4 |
4 |
Iv. Summary
I encountered two major issues in the composition of the Write Program, which took a lot of time.
Question 1: How to obtain parameters from the console
I used to obtain parameters through cin, but I have not tested the method for obtaining parameters based on the control platform. Later I read Baidu's notes and learned this method. The specific method has been stated above.
Question 2: judge whether the input parameter is an integer or fraction.
When verifying the answer, you need to determine whether the input integer or fraction is used. This judgment has strong logic, mainly through B = cin. get (). The Code above is described in detail.