Mathematics arithmetic game for pupils

Keywords: C

Title requirements:
1 And implement a small arithmetic game CAI Software systems that require friendly interface design (good people)
Computer interaction), easy to operate, helps pupils perform four arithmetic operations of one digit (primary) or two digits (advanced).
2 , the main interface gives the rules of the game, prompting pupils to choose any of the operations of addition, subtraction, multiplication and division
Challenge. When the game starts, two numbers (one digit) randomly appear on the screen, prompting the pupil to give an answer, and answering
Wrong request for re-entry, only for each question 5 Opportunity to answer the next question directly after the opportunity is exhausted.
3 , statistical score, to reach a certain score value (for example: 95 Score above) can be advanced, that is, two-digit calculation
Challenge. Likewise, you can set a maximum number of questions (e.g., total) 50 Question), Complete 50 Remind those who fail the test
his " Study hard " .
4 And related knowledge points: process-oriented structured program design ideas, subfunction design, menu design, branch /
Design of Cycle Structure, Random Functions rand() Application, program end function exit() Application, application of screen clearing function, etc.
5 , Provider test scenarios, program capabilities can vary from person to person, but the program must withstand a variety of tests
A test of evidence.
Note: Skip to the next question when the difference is negative or when the division is not complete.
Analysis:
The code is divided into the following modules:
1. Addition module: add()
2. Subtraction module: sub()
3. Division module: divi()
4. Multiplication module: mult()
5. Menu module: menu()
6. Judgement module: trial()
Ideas:
(1) The first four functions need a reference to the judgment module at the end.
(2) The rank here is used to get the rest of the random numbers, so the lower rank is 10 and the higher rank is 100.
(3) Menu module uses human-computer interaction with users.
#include<stdio.h>
#include <stdlib.h>

void add(int level);
void sub(int level);
void mult(int level);
void divi(int level);
void trial(int score, int level);
void menu(int level); 

int main()
{
    printf("Welcome to the Computing Practice System!!!\n");
    menu(10);
    return 0;
}

//Menu interface
void menu(int level)
{
    int flag = 1, choice;
    while (flag)
    {
        printf("******** menu *********\n");
        printf("*     1.Addition operation      *\n");
        printf("*     2.Subtraction operation      *\n");
        printf("*     3.Multiplication      *\n");
        printf("*     4.Division      *\n");
        printf("***********************\n");
        printf("Please enter the corresponding serial number and return to start the game:\n");
        scanf("%d", &choice);
        switch (choice)
        {
            case 1:  
                add(level);
                flag = 0;
                break;          
            case 2:          
                sub(level);
                flag = 0;
                break;           
            case 3:            
                mult(level);
                flag = 0;
                break;            
            case 4:          
                divi(level);
                flag = 0;
                break;
            default:
                printf("Please enter the correct serial number!!!\n");
                break;
        }    
    }
}
//Addition operation
void add(int level)
{
    int doing, bedone, result, score = 0;
    for (int i = 0; i < 50; i++)
    {
        printf("Enter the result of the following formula:\n");
        //If upgraded, set the value to 2 digits
        if (level == 100)
        {
            do
            {
                doing = rand() % level, bedone = rand() % level;
            } while (doing < 10 || bedone < 10);
        }
        else
            doing = rand() % level, bedone = rand() % level;

        printf("%d + %d =", doing, bedone);
        for (int n = 5; n > 0; n--)
        {
            scanf("%d", &result);
            if (result == (doing + bedone))
            {

                printf("That's right. It doesn't stamp!\n");
                score += 2;
                break;
            }
            else
            {
                if ((n - 1) > 0)
                {
                    printf("Wrong answer!!!\n You still have%d Second Opportunity\n Please enter the correct answer:\n", (n - 1));
                }
            }
        }
    }
    //Call the scoring function
    trial(score, level);
}

//subtraction
void sub(int level)
{
    int doing, bedone, result, score = 0;
    for (int i = 0; i < 50; i++)
    {
        printf("Enter the result of the following formula:\n");
        //If upgraded, set the value to 2 digits
        if (level == 100)
        {
            do
            {
                doing = rand() % level, bedone = rand() % level;
            } while (doing < 10 || bedone < 10 || bedone - doing < 0);
        }
        else
            //Before upgrading
            do
            {
                doing = rand() % level, bedone = rand() % level;
            } while (bedone - doing < 0);

            printf("%d - %d =", bedone, doing);
            for (int n = 5; n > 0; n--)
            {
                scanf("%d", &result);
                if (result == (bedone - doing))
                {

                    printf("That's right. It doesn't stamp!\n");
                    score += 2;
                    break;
                }
                else
                {
                    if ((n - 1) > 0)
                    {
                        printf("Wrong answer!!!\n You still have%d Second Opportunity\n Please enter the correct answer:\n", (n - 1));
                    }
                }
            }
    }
    //Call the scoring function
    trial(score, level);
}

//multiplication
void mult(int level)
{
    int doing, bedone, result, score = 0;
    for (int i = 0; i < 50; i++)
    {

        printf("Enter the result of the following formula:\n");
        //If upgraded, set the value to 2 digits
        if (level == 100)
        {
            do
            {
                doing = rand() % level, bedone = rand() % level;
            } while (doing < 10 || bedone < 10);
        }
        else
            doing = rand() % level, bedone = rand() % level;

        printf("%d * %d =", doing, bedone);
        for (int n = 5; n > 0; n--)
        {
            scanf("%d", &result);
            if (result == (doing * bedone))
            {
                printf("That's right. It doesn't stamp!\n");
                score += 2;
                break;
            }
            else
            {
                if ((n - 1) > 0)
                {
                    printf("Wrong answer!!!\n You still have%d Second Opportunity\n Please enter the correct answer:\n", (n - 1));
                }
            }
        }
    }
    //Call the scoring function
    trial(score, level);
}

//division
void divi(int level)
{
    double doing, bedone, score = 0;
    int result;
    for (int i = 0; i < 50; i++)
    {
        printf("Enter the result of the following formula:\n");

        //If upgraded, set the value to 2 digits
        if (level == 100)
        {
            do
            {
                doing = rand() % level, bedone = rand() % level;
            } while (doing < 10 || bedone < 10);
        }
        else
            doing = rand() % level, bedone = rand() % level;

        //Make the result positive integer denominator not zero
        do
        {
            doing = rand() % level, bedone = rand() % level;
        } while ((bedone / doing) - (int)(bedone / doing) > 0 || doing == 0);

        printf("%.0f / %.0f =", bedone, doing);
        for (int n = 5; n > 0; n--)
        {
            scanf("%d", &result);
            if (result == (int)(bedone / doing))
            {
                printf("That's right. It doesn't stamp!\n");
                score += 2;
                break;
            }
            else
            {
                if ((n - 1) > 0)
                    printf("Wrong answer!!!\n You still have%d Second Opportunity\n Please enter the correct answer:\n", (n - 1));
            }
        }
    }
    //Call the scoring function
    trial(score, level);
}

//Score Function
void trial(int score, int level)
{
    //Determine whether to upgrade
    if (level == 10)
    {
        if (score >= 95)
        {
            int choicelevel, choiced = 0;
            system("cls");
            printf("You're already great. Do you want to go to the next level?\n Yes, please enter 1, return to the main menu please enter 0, end please press-1:\n");

            while (!choiced)
            {
                scanf("%d", &choicelevel);
                switch (choicelevel)
                {
                    case 1:
                        menu(100);
                        break;
                    case 0:
                        menu(10);
                        break;
                    case -1:
                        exit(0);
                    default:                
                        system("cls");
                        printf("Please enter the correct value:");
                        break;   
                }
            }
        }

        if (score < 60)
        {
            system("cls");
            printf("Your score is:%d,Fail!\n\n Have a good study!\n\n\n\n\n\n", score);
            menu(10);
        }
        else if (score < 95)
        {
            system("cls");
            printf("Your score is:%d,Good results!\n\n But not yet%d Scores make progress oh!\n\n\n\n\n\n", score, 10 - score);
            menu(10);
        }
    }

    if (level == 100)
    {
        if (score >= 95)
        {
            system("cls");
            printf("You're already great. Try some other arithmetic!\n\n\n\n");
            menu(100);
        }
        else if (score > 60)
        {
            system("cls");
            printf("Your score is:%d,Good results!\n\n But there's still a distance to excellence!\n\n\n\n\n\n", score);
            menu(100);
        }
        else if (score > 0)
        {
            system("cls");
            printf("Your score is:%d,Fail!\n\n Have a good study!\n\n\n\n\n\n", score);
            menu(100);
        }
    }
}
 

Posted by robs99 on Tue, 30 Nov 2021 09:44:29 -0800