int main() { srand(time(0)); int cntT = 0, cntF = 0; char L[] = {'+', '-', '*'}; // 替换为char数组?:ml-citation{ref="3" data="citationList"} const int MAX_ANSWERS = 100; int answers[MAX_ANSWERS]; // 替换为固定数组?:ml-citation{ref="2,3" data="citationList"}
int a; cout<<"小学1~3年级加减口算练习题"<<endl; cout << "如果输入0,进入在线答题模式(输入-1终止程序);如果输入别的大于等于1的整数n(n<=20),则将自动输出n份口算练习题(无乘法)\n"; cin >> a; if(a<0 or a>20){ while(a<0 or a>20){ cout<<"输入错误!请重新输入\n"; cin>>a; } return 0; } if (a == 0) { while(true) { int w = rand() % 100 + 1; int s_val = rand() % 100 + 1; int y = rand() % 3; int u = rand() % 2; int t = rand() % 2;
if (s_val > w) swap(w, s_val);
char op; int result; if (t == 0 && (w < 10 || s_val < 10)) { op = L[y]; } else { op = L[u]; }
switch(op) { case '+': result = w + s_val; break; case '-': result = w - s_val; break; case '*': result = w * s_val; break; }
// 生成题目并存储答案 for(int o = 0; o < MAX_ANSWERS; ++o) { // 使用固定循环次数?:ml-citation{ref="2" data="citationList"} int w = rand() % 90 + 10; int s_val = rand() % 90 + 10; int u = rand() % 2;
if(s_val > w) swap(w, s_val);
char op = L[u]; switch(op) { case '+': answers[o] = w + s_val; break; // 直接存入数组 case '-': answers[o] = w - s_val; break; case '*': answers[o] = w * s_val; break; }