用编程来折磨小学生

大二暑假回家,那时候我弟弟刚读一年级。妈妈让我出点数学计算题给弟弟写,还让我抄题目。怎么可能,我那么懒。
恶趣味的我,想折磨现在的小学生,于是用C写了个小程序,随机生成1000题20以内加减法。然后把txt文件导入到kindle上,笑着说:“好了,抄题目吧。”

事了拂衣去,深藏功与名

代码如下:

#pragma warning(disable:4996)
#include 
#include 
#include 
#include 

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;
}

编译器是VS2013。

好想家。

你可能感兴趣的:(C语言)