Refer to: https://edu.cnblogs.com/campus/nenu/2020Fall/homework/11245

Coding address: https://e.coding.net/gongwf662/homework/f4.git

Function 1 Four arithmetic

Support four arithmetic questions with four numbers. All questions require the author to be able to answer correctly (hint: 1 / 3! = 0.33333, but infinite).

a key:

(1) According to the requirements of the topic, the formula is generated randomly. We use the time stamp to ensure the randomness of the formula.

(2) In the formula of four numbers and three operators, the priority of operators needs to be considered, that is, the priority of multiplication and division is higher than that of addition and subtraction. Therefore, we use the suffix expression and complete the operation sequence processing of the formula with the help of the stack.

Difficulties:

This function needs to ensure that the author of all questions has the ability to answer correctly, so we should consider the situation that the divisor cannot be divided completely in the division operation.

Programming gains:

At first, I thought that this topic mainly focused on the conversion of infix expression to suffix expression and the operation and evaluation of suffix expression. However, some new problems were found in the writing process, which took more time.

Important code examples:

(1) Infix to suffix

1 //Infix expression to suffix expression 2 void infix_to_suffix(char* infix,char* &suffix) 3 { 4 stack<char>S; 5 int k=0; 6 while(*infix) 7 { 8 //Operand 9 if(*infix>='0'&&*infix<='9') 10 { 11 suffix[k++]=*infix; 12 } 13 //operator 14 else 15 { 16 if(S.empty()) 17 { 18 S.push(*infix); 19 } 20 else if(*infix=='(') 21 { 22 S.push(*infix); 23 } 24 else if(*infix==')') 25 { 26 while(S.top()!='(') 27 { 28 suffix[k++]=S.top(); 29 S.pop(); 30 } 31 S.pop(); 32 } 33 else 34 { 35 while(judge_priority(*infix)<=judge_priority(S.top())) 36 { 37 suffix[k++]=S.top(); 38 S.pop(); 39 if(S.empty()){ 40 break; 41 } 42 } 43 S.push(*infix); 44 } 45 } 46 infix++; 47 } 48 if(!S.empty()) 49 { 50 char c=S.top(); 51 suffix[k++]=c; 52 S.pop(); 53 } 54 }

(2) Suffix expression evaluation

1 //Suffix expression evaluation 2 double suffix_calculate(char* suffix) 3 { 4 stack <double> s; 5 double temp; 6 while(*suffix) 7 { 8 if(*suffix >='0'&& *suffix<='9') 9 { 10 temp = *suffix -'0'; 11 s.push(temp); 12 } 13 else 14 { 15 double a = s.top(); 16 s.pop(); 17 double b = s.top(); 18 s.pop(); 19 double c = calculate(b,a,*suffix); 20 s.push(c); 21 } 22 suffix++; 23 } 24 return s.top(); 25 }

Operation effect diagram:

Function 2 Brackets are supported.

a key:

(1) This function adds parentheses to function 1, and there can be at most two parentheses in the formula. Therefore, we designed to generate the number of parentheses randomly, and the position of parentheses is random. So far, we can get a more random four expressions.

(2) When infix is converted to suffix, the left bracket is directly put into the stack. When the right bracket is encountered, the top element of the stack will continue to pop up until the left bracket is encountered.

Difficulties:

The difficulty of this function is to consider whether the bracket position is correct and whether the left and right brackets are paired.

Programming gains:

In order to ensure the randomness of the formula, we fill in the blank to make the parentheses randomly fill between the operands and operators under certain rules.

Important code examples:

(1) Random parentheses

1 //Generate four operations with parentheses 2 void create_equation(int num,char* &dataStr) 3 { 4 int pos = 0, temp; 5 bool divflag = true, addflag = true; 6 //Generating formula 7 for (int i = 0; i < num; i++) 8 { 9 if(divflag) 10 { 11 temp = random(9); 12 temp += 1; 13 dataStr[pos++] = temp + '0'; 14 } 15 else 16 { 17 dataStr[pos++] = 2 + '0'; 18 } 19 if (i < 3) 20 { 21 temp = random(4); 22 switch (temp) 23 { 24 case 0: 25 dataStr[pos++] = '+'; 26 break; 27 case 1: 28 dataStr[pos++] = '-'; 29 break; 30 case 2: 31 dataStr[pos++] = '*'; 32 break; 33 case 3: 34 dataStr[pos++] = '/'; 35 divflag = false; 36 break; 37 default: 38 break; 39 } 40 } 41 } 42 //Add spaces before and after characters 43 for(int i=pos-1; i>=0; i--) 44 { 45 dataStr[i*2+1]= dataStr[i]; 46 dataStr[i*2+2]= ' '; 47 } 48 dataStr[0] = ' '; 49 //Number of randomly generated parentheses 50 temp = random(2); 51 //Add the first pair of parentheses 52 if(dataStr[temp*4+3]=='-') 53 { 54 temp = 0; 55 } 56 dataStr[4*temp]='('; 57 dataStr[4*temp+6]=')'; 58 char c=' '; 59 dataStr = remove_char(dataStr,strlen(dataStr),c); 60 61 int temp1 = random(3); 62 //Add a second pair of parentheses 63 if(temp1 == 2 && temp < 2) 64 { 65 dataStr[10] = dataStr[8]; 66 dataStr[9] = dataStr[7]; 67 dataStr[8] = ')'; 68 for(int i = 6; i>=0; i--) 69 { 70 dataStr[i+1] = dataStr[i]; 71 } 72 dataStr[0] = '('; 73 dataStr[pos+4] = '\0'; 74 } 75 else 76 dataStr[pos+2] = '\0'; 77 }

Operation effect diagram:

Function 3 Limit the number of topics and print out "exquisite" to avoid repetition

a key:

(1) This function requires that the number of formulas can be specified, and the entered value must be a positive integer. Negative numbers, decimals and letters do not meet the requirements.

(2) For "fine printing", you need to set the field width, and then float the formula to the left to output.

(3) Output data to a string.

Difficulties:

Function 3 is relatively simple compared with the first two functions.

Programming gains:

Learned expression operation and stack.

Important code examples:

(1) Command line parameter number control and error checking

1 if(argc == 3 && !strcmp(argv[1],"-c")) 2 { 3 //Command line parameter error checking 4 if (strspn(argv[2], "0123456789") == strlen(argv[2])) 5 { 6 n = atoi(argv[2]); 7 if(n>0) 8 { 9 print_output(n); 10 } 11 else 12 { 13 cout<<"The number of questions must be a positive integer."<<endl; 14 } 15 } 16 else 17 { 18 cout<<"The number of questions must be a positive integer."<<endl; 19 } 20 }

Operation effect diagram:

Function 4 Support score problem setting and operation

a key:

This function needs to pay attention to the operation rules of fractions and cannot directly reuse the four operations of integers.

Difficulties:

(1) Denominator cannot be 0.

(2) After the fraction operation, it needs to be simplified.

(3) There is a case where the score is an integer after the operation.

Programming gains:

When designing the fraction operation, we use the structure type to store the fraction. In the process of writing code, we found that the situation we need to consider is more complex than we thought. In order to simplify the algorithm, we also added some variables in the structure as the flags of operand types.

Important code examples:

(1) Fractional simplification

1 //Fractional simplification 2 struct Fraction simplify(struct Fraction a) 3 { 4 for(int i=a.uper; i>=1; i--) 5 { 6 if((a.uper%i==0)&&(a.lower%i==0)) 7 { 8 a.uper=a.uper/i; 9 a.lower=a.lower/i; 10 } 11 } 12 return a; 13 }

(2) Evaluation of suffix expression of fraction

1 //Calculation results 2 struct Fraction suffix_calculate() 3 { 4 stack <struct Fraction> s; 5 struct Fraction temp; 6 for(int i=0; i<7; i++) 7 { 8 if(suffix[i].fraction!=2) 9 { 10 s.push(suffix[i]); 11 } 12 else 13 { 14 struct Fraction a = s.top(); 15 s.pop(); 16 struct Fraction b = s.top(); 17 s.pop(); 18 struct Fraction c = calculate(b,a,suffix[i].op); 19 s.push(c); 20 } 21 } 22 temp = s.top(); 23 return temp; 24 }

Operation effect diagram:

Experience of pair programming

In this pair programming, I realized the truth that people pick up firewood and the flame is high. In the process of function from fail to pass, we gave full play to our respective advantages and improved the efficiency of the whole team.

(1) Inverse Polish algorithm. Because I haven't touched this kind of algorithm for a long time, it leads to some forgetting, so I spend more time in reviewing.

(2) Handling of special cases when writing four operations. Because everyone has different ideas and solutions to the problem, it is essential to discuss what kind of algorithm is simple and effective.

(3) In the process of writing score operation, there are always some unexpected errors, resulting in a much longer time than planned.

(4) Perfection of details. In the integration stage, teammates found the lack of some small functions one after another.

(5) During the review, it took a long time to analyze and solve the bug because many situations were considered.

Pairing screenshot evidence

Due to the epidemic, the pair programming was completed through teamviewer shared screen and online communication.

version control

Coding address: https://e.coding.net/gongwf662/homework/f4.git

f4.exe and F4 CPP is the executable file and code of functions 1, 2 and 3, which performs four integer operations

f4_fra.exe and f4_fra.cpp is the executable file and code of function 4, which performs four fractional operations