Torture primary school students with programming

My brother was just in grade one when he went home in summer vacation. My mother asked me to work out some math problems to write for my brother, and also asked me to copy them. How could I be so lazy.
I want to torture the current primary school students, so I wrote a small program with C, and randomly generated 1000 questions within 20 addition and subtraction. Then import the txt file into the kindle and say with a smile, "OK, copy the title."

When it's time to brush your clothes, you can hide your skills and fame

The code is as follows:

#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int main(void)
{
    int count = 1000;

    FILE * quiz_file;
    FILE * result_file;

    quiz_file = fopen("D:\\exam.txt", "w");
    result_file = fopen("D:\\result.txt", "w");

    if (quiz_file == NULL || result_file == NULL)
    {
        puts("Error,couldn't open files!");
        getchar();
        getchar();
        return 1;
    }

    while (count > 0)
    {
        char str[20];
        int first, second, third;
        char str2[3], str3[3], str4[3];
        int result = 0;

        //srand(time(NULL));
        first = rand() % 20 +1;
        second = rand() % 20 + 1;
        third = rand() % 20 + 1;



        sprintf(str, "%d", first);
        sprintf(str2, "%d", second);
        sprintf(str3, "%d", third);

        switch (rand() % 2)
        {
        case 0:
            strcat(str, "+");
            strcat(str, str2);
            result = first + second;
            if (result < 0 || result > 20)
            {
                continue;
            }
            break;
        case 1:
            strcat(str, "-");
            strcat(str, str2);
            result = first - second;
            if (result < 0 || result > 20)
            {
                continue;
            }
            break;
        default:
            break;
        }

        if (rand() % 2 == 0)
        {
            strcat(str, "+");
            strcat(str, str3);
            result = result + third;
            if (result < 0 || result > 20)
            {
                continue;
            }
        }
        else
        {
            strcat(str, "-");
            strcat(str, str3);
            result = result - third;
            if (result < 0 || result > 20)
            {
                continue;
            }
        }

        strcat(str, "=");
        sprintf(str4, "%d", result);

        fputs(str, quiz_file);
        fputc('\n', quiz_file);
        strcat(str, str4);
        fputs(str, result_file);
        fputc('\n', result_file);
        count--;
    }
    fclose(quiz_file);
    fclose(result_file);
    return 0;
}

The compiler is VS2013.

I'm homesick.

Posted by zipdisk on Sun, 31 May 2020 00:06:15 -0700