QT实现 三个数的加减法自动生成

三位数加减法自动生成函数

输出一个txt文件,可以copy到word文档,打印回家训练娃的加减法

void MainWindow::test2(int answerMax, int count) {
   

  // 创建一个随机数生成器
  QRandomGenerator *generator = QRandomGenerator::global();

  // 创建一个文件
  QString filename =
      QString("%1以内三个数加减法%2道.txt").arg(answerMax).arg(count);
  QFile file(filename);
  if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
    return;

  // 创建一个文本流并附加到文件上
  QTextStream out(&file);

  // 生成N个问题
  int i = 0;
  int j = 0;
  int z = 0;
  while (1) {
   

    // 生成随机数
    int num1 = generator->bounded(1, answerMax + 1);
    int num2 = generator->bounded(1, answerMax + 1);
    int num3 = generator->bounded(1, answerMax + 1);

    // 确保至少有两个数字是不同的,以避免重复或相同的数字
    if (num1 == num2) {
   
      num2 = generator->bounded(1, answerMax + 1);
    }
    if (num1 == num3) {
   
      num3 = generator->bounded(1, answerMax + 1);
    }
    if (num2 == num3) {
   
      num3 = generator->bounded(1, answerMax + 1);
    }

    // 生成运算符
    char op = (generator->bounded(2) == 0) ? '+' : '-';
    char op2 = (generator->bounded(2) == 0) ? '+' : '-';

    // 如果是减法,确保第一个数不大于第二个数
    if (op == '-' && num1 < num2) {
   
      int temp = num1;
      num1 = num2;
      num2 = temp;
    }

    int result1 = 0;
    if (op2 == '-') {
   
      result1 = num1 - num2;

      if (result1 < 0) {
   
        continue;
      }

      if (result1 < num3) {
   
        int temp = result1;
        result1 = num3;
        num3 = temp;
      }
    } else {
   
      result1 = num1 + num2;
    }

    // 计算答案
    int answer;
    if (op2 == '+') {
   
      answer = result1 + num3;
    } else {
   
      answer = result1 - num3;
    }

    if (answer == 0) {
   
      z++;
      if (z >= 10) {
   
        continue;
      }
    }

    if (answer > answerMax) {
   
      continue;
    }

    // 写入问题到文件
    if ((j + 1) % 6 == 0) {
   

      // 添加一个换行符
      out << QString("%1 %2 %3 %4 %5=     \n")
                 .arg(num1)
                 .arg(op)
                 .arg(num2)
                 .arg(op2)
                 .arg(num3);
    } else {
   
      out << QString("%1 %2 %3 %4 %5=     ")
                 .arg(num1)
                 .arg(op)
                 .arg(num2)
                 .arg(op2)
                 .arg(num3);
    }

    i++;
    j++;

    if ((i + 1) % 100 == 0) {
   
      j = 0;
      out << QString("\n\n");
    }

    if (i == count - 1) {
   
      break;
    }
  }

  file.close();
}

界面

在这里插入图片描述

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-01-25 01:06:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-25 01:06:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-25 01:06:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-25 01:06:02       20 阅读

热门阅读

  1. 2024年阿里云优惠云服务器新版价格整理

    2024-01-25 01:06:02       35 阅读
  2. 【嵌入式——C++】引用

    2024-01-25 01:06:02       34 阅读
  3. 论文素材:PSO算法介绍

    2024-01-25 01:06:02       36 阅读
  4. mysql单表查询练习题

    2024-01-25 01:06:02       35 阅读
  5. 在 Spring Boot 中使用事务

    2024-01-25 01:06:02       33 阅读
  6. 【nginx】405 not allowed问题解决方法

    2024-01-25 01:06:02       42 阅读
  7. linux 之 ln 命令

    2024-01-25 01:06:02       35 阅读
  8. c语言之循环语句练习

    2024-01-25 01:06:02       36 阅读