小学生加减法训练
QT实现–自动生成两位数加减法算式,并输出txt文件
可以copy到word文件,设置适当字体大小和行间距,带回家给娃做做题
void MainWindow::test(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;
  while (1) {
    // 生成两个随机数和一个运算符
    int num1 = generator->bounded(1, answerMax + 1);
    int num2 = generator->bounded(1, answerMax + 1);
    char op = (generator->bounded(2) == 0) ? '+' : '-';
    // 如果是减法,确保第一个数不大于第二个数
    if (op == '-' && num1 < num2) {
      int temp = num1;
      num1 = num2;
      num2 = temp;
    }
    // 计算答案
    int answer;
    if (op == '+') {
      answer = num1 + num2;
    } else {
      answer = num1 - num2;
    }
    if (answer > answerMax) {
      continue;
    }
    // 写入问题到文件
    if ((j + 1) % 6 == 0) {
      // 添加一个换行符
      out << QString("%1 %2 %3 =     \n").arg(num1).arg(op).arg(num2);
    } else {
      out << QString("%1 %2 %3 =     ").arg(num1).arg(op).arg(num2);
    }
    i++;
    j++;
    if ((i + 1) % 100 == 0) {
      j = 0;
      out << QString("\n\n");
    }
    if (i == count - 1) {
      break;
    }
  }
  file.close();
}
界面
界面比较简单,就不贴代码了!
 



















![[Android] Android文件系统中存储的内容有哪些?](https://img-blog.csdnimg.cn/direct/4bf9a1619d9b48dfbd46563a0eb3b2d8.png)