小学一年级20以内加减法题目自动生成(家长的福利,孩子的魔鬼)

@Adrian

我是魔鬼,来折磨小学生了,这又你做不完的题,不要哭,先来个1000题,静静心。

在这里插入图片描述

给祖国的花朵施施肥

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int main(void)
{
    int count = 100;

    FILE * quiz_file;
    FILE * result_file;

    quiz_file = fopen("D:\\class/exam.txt", "w");
    result_file = fopen("D:\\class/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;
        char str2[3], str4[3];
        int result = 0;

        first = rand() % 20 +1;
        second = rand() % 20 + 1;



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

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


操作步骤:
  1. 复制代码到编译器
  2. 根据代码中指定的路径创建文件夹(例如:在这里插入图片描述
    在D:盘中创建class文件夹)
  3. 执行代码
  4. 在class文件夹下生成:exam.txt(试题)和result.txt(有答案的试题)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值