Pair programming second assignment

1. Subject requirements

For this assignment, we have chosen topic 1

This homework requires two people to work together. The roles of the driver and the navigator are self-determined. Everyone is encouraged to switch roles at any time during the work. There will be two questions here, and each group member is asked to choose a question according to their own hobbies.
Topic 1:
We introduced an example of an automatic generation program of four arithmetic operations in elementary school when we first started the class. Please implement it. Requirements:
Ability to automatically generate four arithmetic operation exercises
The number of questions can be customized The
user can choose the operator and the
user can set the maximum number (such as Within 10, within 100, etc.) the
user chooses whether there are brackets and whether there are decimals The
user chooses the output method (such as outputting to a file, printer, etc.)
It is best to provide a graphical user interface (choose according to your own ability, mainly to complete the above functions)

2. Assignment of tasks

  • My role: The driver
    can complete all the code work, the program basically achieves all the required functions, and upload the code to coding.net or GitHub code hosting system to
    objectively give an objective view of the role of the navigator in this programming work Evaluation, and complete a summary of more than 500 words
  • Navigator: Chen Zhuo~ Navigator blog address
    Can assist the driver to complete all the code work, and select appropriate coverage standards for key functions to design test cases, and write code for unit automatic testing
    . Evaluate the current work of the staff and be
    able to complete a summary of more than 500 words on this work

3. Demand analysis

   小学四则运算自动生成程序,解决的主要问题是小学老师或者学生家长给小学生出题的问题,老师和家长给学生出题时,需要很多的时间和精力。使用这个程序可以节省很多时间,并且可以自主选择题目模式,出题也可以实现多样化。

4. Code implementation

The complete code has been submitted to Github~ code address

  • The functions that the code can implement include the six functions required in the homework (automatically generate questions, define the number of questions, define the maximum number, choose whether to have decimals, choose whether to have parentheses, choose whether to output as a file format). And some additional functions: give correct answers, when the subtraction is not enough to subtract, divide by zero, and workarounds that are not divisible, etc.;

The following is an explanation of the main functions of the code:

1. Function prototype and definition

int count;///定义题目数量
int max;///定义最大数字
int ffloat;///选择是否有小数
int file;///选择是否输出为文件
int bracket;///选择是否有括号
int getChoice();
void Menu();
int integer(int);
int decimal(int);
FILE *fp;///定义文件

2. The user selects the function function

  • This function is to let the user input the number corresponding to the option in the function menu to select the function they want to use .
int getChoice()
{
    int choice;
    scanf("%d", &choice);
    return choice;
}

3. Function menu

  • The function of this function is to display several options for selecting questions, which is convenient for users to choose, corresponding to the above user selection function .
void Menu()
{
    printf("===================================\n");
    printf("\n欢迎使用小学生四则运算练习软件\n");
    printf("\n选项如下:\n");
    printf("\t1、加法练习  (有括号时为加法和除法的复合运算)\n");
    printf("\t2、减法练习  (有括号时为减法和乘法的复合运算)\n");
    printf("\t3、乘法练习  (有括号时为加法和乘法的复合运算)\n");
    printf("\t4、除法练习  (有括号时为减法和除法的复合运算)\n");
    printf("\n===================================\n");
}

4. Integer operations

① Generate random integers

  • This is the first time I use the random number generation function, and the explanation is attached below .

    The rand function is a function in the computer C language and is a real random number generator, and srand() will set the random number seed used by rand(). If srand() is not called before the first call to rand(), the system will automatically call srand(). Calling rand() with the same seed and the same number will result in the same sequence of random numbers being generated.
    -- Quoted from Sogou Encyclopedia

    srand(time(NULL)); ///初始化随机数种子
    for (j = 1;j <= 2 * count; j++)
    {
        num1[j] = rand() % max + 1;           ///取0—max之间的随机数
        num2[j] = rand() % max + 1;
        num5[j] = rand() % max + 1;
    }

②Select the operation symbol and whether there are parentheses in the expression (only the example of addition is given here, other symbols are similar to addition)

  • When there are no parentheses in the expression, the addition expression is output directly, and when there are parentheses in the expression, the compound expression of addition and division is output .
switch (n)
    {
    case 1:
        operation = '+';
        if (bracket == 1)
        {
            for (i = 1;i <= count;i++)
            {
                printf("(%d %c %d)/ %d = \n", num1[i], operation, num2[i], num5[i]);
                fprintf(fp, "(%d %c %d)/ %d = \n", num1[i], operation, num2[i], num5[i]);
            }
        }
        break;

③ The output method when there are no parentheses in the expression (fprintf is output in file format, and the implementation method will be explained later)

if (bracket == 0)
        {
            printf("%d%c%d= \n", num1[i], operation, num2[i]);
            fprintf(fp, "%d%c%d= \n", num1[i], operation, num2[i]);
        }

④ Give the correct answer (also take addition as an example, other symbols are similar)

printf("正确答案为: \n");
    fprintf(fp, "正确答案为: \n");
    for (i = 1;i <= count;i++)
    {
        switch (operation)
        {
        case '+':
            if (bracket == 1)  ///有括号时正确答案的计算
                ranswer1[i] = (num1[i] + num2[i]) / num5[i];
            else if (bracket == 0) ///无括号时正确答案的计算
                ranswer1[i] = num1[i] + num2[i];
            break;

The complete code of the integer operation part is attached below~

int integer(int n)
{
    int i, j, m;
    int ranswer1[count];
    int t = 0;                ///临时变量
    char operation;            ///运算符号
    int num1[count];               ///操作数1
    int num2[count];
    int num5[count];               ///操作数2
    srand(time(NULL)); ///初始化随机数种子
    for (j = 1;j <= 2 * count; j++)
    {
        num1[j] = rand() % max + 1;           ///取0—max之间的随机数
        num2[j] = rand() % max + 1;
        num5[j] = rand() % max + 1;
    }

    switch (n)
    {
    case 1:
        operation = '+';
        if (bracket == 1)
        {
            for (i = 1;i <= count;i++)
            {
                printf("(%d %c %d)/ %d = \n", num1[i], operation, num2[i], num5[i]);
                fprintf(fp, "(%d %c %d)/ %d = \n", num1[i], operation, num2[i], num5[i]);
            }
        }
        break;

    case 2:
        operation = '-';
        if (bracket == 1)
        {
            for (i = 1;i <= count;i++)
            {
                printf("(%d %c %d)* %d = \n", num1[i], operation, num2[i], num5[i]);
                fprintf(fp, "(%d %c %d)* %d = \n", num1[i], operation, num2[i], num5[i]);
            }
        }
        break;

    case 3:
        operation = '*';
        if (bracket == 1)
        {
            for (i = 1;i <= count;i++)
            {
                printf("(%d + %d)%c %d = \n", num1[i], num2[i], operation, num5[i]);
                fprintf(fp, "(%d + %d)%c %d = \n", num1[i], num2[i], operation, num5[i]);
            }
        }
        break;

    case 4:
        operation = '/';
        if (bracket == 1)
        {
            for (i = 1;i <= count;i++)
            {
                printf("(%d - %d)%c %d = \n", num1[i], num2[i], operation, num5[i]);
                fprintf(fp, "(%d - %d)%c %d = \n", num1[i], num2[i], operation, num5[i]);
            }
        }
        break;
    }

    ///减法操作不够减的时候,交换两个操作数
    for (i = 1;i <= count;i++)
    {
        if ((operation == '-') && (num1[i]<num2[i]))
        {
            t = num1[i];
            num1[i] = num2[i];
            num2[i] = t;
        }

        ///除法操作中的除数,即num2为0时就将num2强制置为1
        ///将num1的值赋值给num1*num2,防止不能整除
        if (operation == '/')
        {
            if (num2[i] == 0)
            {
                num2[i] = 1;
            }
            num1[i] = num1[i] * num2[i];        ///防止num1不能被num2整除的语句
        }
        if (bracket == 0)
        {
            printf("%d%c%d= \n", num1[i], operation, num2[i]);
            fprintf(fp, "%d%c%d= \n", num1[i], operation, num2[i]);
        }
    }
    ///计算并给出正确答案
    printf("正确答案为: \n");
    fprintf(fp, "正确答案为: \n");
    for (i = 1;i <= count;i++)
    {
        switch (operation)
        {
        case '+':
            if (bracket == 1)  ///有括号时正确答案的计算
                ranswer1[i] = (num1[i] + num2[i]) / num5[i];
            else if (bracket == 0) ///无括号时正确答案的计算
                ranswer1[i] = num1[i] + num2[i];
            break;

        case '-':
            if (bracket == 1)
                ranswer1[i] = (num1[i] - num2[i])*num5[i];
            else if (bracket == 0)
                ranswer1[i] = num1[i] + num2[i];
            break;

        case '*':
            if (bracket == 1)
                ranswer1[i] = (num1[i] + num2[i])*num5[i];
            else if (bracket == 0)
                ranswer1[i] = num1[i] + num2[i];
            break;

        case '/':
            if (bracket == 1)
                ranswer1[i] = (num1[i] - num2[i]) / num5[i];
            else if (bracket == 0)
                ranswer1[i] = num1[i] + num2[i];
            break;
        }
        printf("%d  \n", ranswer1[i]);
        fprintf(fp, "%d  \n", ranswer1[i]);
    }
    printf("继续请按1,结束请按0\n");///选择是否结束程序
    scanf("%d", &m);
    if (m == 1)
        return 1;
    else
        return 0;
}

5. Decimal Operations

  • Decimal operations are basically the same as integer operations, and only the differences are listed below .

generate random decimals

srand(time(NULL));     ///初始化随机数种子
    for (j = 1;j <2 * count;j++)
    {
        num3[j] = rand() / (double)(RAND_MAX / max) + 1;///产生随机小数
        num4[j] = rand() / (double)(RAND_MAX / max) + 1;
        num6[j] = rand() / (double)(RAND_MAX / max) + 1;
    }

6. Output to file format

①Define a pointer variable fp that points to a file

FILE *fp;

②If you choose to output as a file, open the file and use the file mode as write-only

if (file == 1)
   fp = fopen("四则运算.txt", "w");

③The output is a file format (the read and write object is a file)

fprintf(fp, "%.2f  \n", ranswer2[i]);

④ After the file is used, close the data file

fclose(fp);
  • In addition, when adding the file function, there is a problem. Only an empty file can be generated, and there is no content in the file. Later, it is found that the first operation is over. After the result is output to the file, the program will automatically perform the next operation. The empty file generated at the time will overwrite the content stored in the previous file, and naturally there is nothing in the file. The solution is to transfer part of the content in the function menu to the main function, adjust the function selection order, and add a function for the user to choose whether to continue executing the program, and the problem is solved. .

7. The user chooses whether to exit the program

printf("继续请按1,结束请按0\n");///选择是否结束程序
    scanf("%d", &m);
    if (m == 1)
        return 1;
    else
        return 0;

8. Main function

int main(void)
{
    int a = 1, choice;        ///接收用户的选择
    do      ///循环开始功能
    {
        Menu();                  ///显示菜单
        printf("请输入需要打印题目数量:\n");
        scanf("%d", &count);
        printf("请输入题目中的最大数字:\n");
        scanf("%d", &max);
        printf("请选择是否需要小数(是为1,否为0):\n");
        scanf("%d", &ffloat);
        printf("请选择是否输出到文件(是为1,否为0):\n");
        scanf("%d", &file);
        printf("请选择是否需要括号(是为1,否为0):\n");
        scanf("%d", &bracket);
        printf("请输入你的选择(0--4):\n");
        if (file == 1)
            fp = fopen("四则运算.txt", "w");
        choice = getChoice();
        if (choice != 0)
        {
            ///控制choice的范围
            if (choice<0 || choice>5)
                choice = 1;
            if (ffloat == 0)
            {
                a = integer(choice);
            }
            if (ffloat == 1)
            {
                a = decimal(choice);
            }
        }
        if (choice == 0)
        {
            a = 0;
            if (file == 1)
                fclose(fp);
            break;                ///用户选择0,退出循环(退出系统)
        }
    } while (a == 1);
    printf("感谢使用本软件\n");
    return 0;
}

9. The solution is not enough to subtract when the subtraction operation is performed, the divisor is zero, and it cannot be divided evenly

①When the addition operation is not enough to subtract, exchange the positions of the subtrahend and the minuend

if ((operation == '-') && (num1[i]<num2[i]))
        {
            t = num1[i];
            num1[i] = num2[i];
            num2[i] = t;
        }

②When the divisor is zero, the divisor is forcibly set to 1

if (num2[i] == 0)
            {
                num2[i] = 1;
            }

④ When it is not divisible, assign the product of the multiplier and the multiplicand to the multiplicand. (Integer arithmetic only)

  • But this function has a bug, when the product of the multiplier and the multiplicand is assigned to the multiplicand, the value of the multiplicand may exceed the maximum number given by the user. .
num1[i] = num1[i] * num2[i];

5. Pilot evaluation

In this programming process, the navigator is Chen Zhuo, who has helped me a lot, because my programming work is almost all done in the dormitory, so part of the communication with the navigator is also conducted online of. During this programming process, I wrote more than one version of the program, and every time I completed a part of the function, I sent a copy of this part of the code to Chen Zhuo. The part of correction and upgrading pointed out that the efficiency and accuracy of programming have been greatly improved, and he will propose additional functions that can be added to the code to make the code more complete and rigorous.
Because my programming level is not high, the programming speed is relatively slow, which leads to the slowdown in the progress of pair programming this time. The time for the pilot to conduct unit testing and code review is a little tight, and I am very grateful to the pilot for understanding me. and encouragement.
Pair programming is the first time I came into contact with it. When I typed the code by myself, many problems were ignored, resulting in a lot of loopholes in the program, and it was very troublesome to fix it bit by bit. Pair programming solves this problem very well. Two people program at the same time, which not only improves the efficiency a lot, but also improves the correctness of the code and the integrity of the function.
Finally, a working photo is attached~

6. Personal summary

  • In the process of writing this program, we also encountered many problems. First of all, my programming level is not high. When I chose to play the role of the driver, I was very worried about whether I could complete the job. Fortunately, I basically completed all the functions, although some functions were not perfect. The implementation of many functions in the code I wrote is relatively cumbersome, and the overall language of the code is relatively simple, which may be easier for the navigator to understand because no advanced algorithms and statements are used. During the programming process, the overall direction went wrong once. The overall function of this code is a four-calculus problem-setting program, and the first time I implemented an online four-calculation problem-making program. Only when the user enters the answer to a question, The next question will appear. Later, after reading the topic carefully, I found that it was not consistent with the requirements of the topic. After modification, the overall requirements of the topic were completed. Of course, this also affected the progress of our homework. For the file output problem, because I have never been exposed to the requirements of outputting in file format before, I have not done any exercises in this area, so learning this knowledge also takes a part of the time. In this team programming process, I constantly found problems, solved them, and learned a lot of new knowledge. Therefore, the completion of each assignment is not smooth sailing, and each task is an opportunity to find problems and improve yourself.
  • In addition, the programming environment I am used to is Codeblocks. When doing this assignment, there were some problems with Codeblocks. Perhaps when downloading or uninstalling software or configuring environment variables, the compiler of Codeblocks was completely lost. After compiling, all programs cannot run. After uninstalling it and re-downloading and installing it in the software management of the computer, the problem is still not solved. The final solution is to enter the Codeblocks official website and download a Codeblocks with a compiler (codeblocks-17.12mingw-setup.exe). The Codeblocks downloaded in the software management of my computer does not have its own compiler. After entering the official website, click Downloads, and then click Download the binary release in the figure below

Windows system select codeblocks-17.12mingw-setup.exe to install, this is the file with the compiler

See this article for more on this issue .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324448776&siteId=291194637