用Python 3.7.4编写的口算练习题程序

提问 未结
5 126
4669 2025-03-08
C++5.11版本的代码(Deepseek完成转换)如下: #include <iostream> #include <cstdlib> #include <ctime> #include <string> #include <iomanip> using namespace std; 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; } cout << w << " " << op << " " << s_val << " = "; int input; cin >> input; if(input<0){ if(input == -1) { cout << "正确 " << cntT << " 道\n"; cout << "错误 " << cntF << " 道\n"; cout << "一共 " << cntT + cntF << " 道\n"; break; } while(input<0){ cout<<"输入错误!请重新输入\n"; cin>>input; } } if(input == result) { cntT++; cout << "True\n"; } else { cntF++; cout << "False\n"; cout << ": " << result << endl; } } } else { int n = a; if(n >= 1) { for(int i = 0; i < n; ++i) { cout << " 小学口算练习题\n 建议用时 10 分钟 实际用时: 分钟\n"; // 生成题目并存储答案 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; } cout << setw(5) << w << op << s_val << "= "; if((o+1) % 5 == 0) cout << endl; } // 输出答案 cout << "************************************************************************\n" << "******************************* 答案 *********************************\n"; for(int j = 0; j < MAX_ANSWERS; ++j) { // 遍历数组输出?:ml-citation{ref="4" data="citationList"} cout << setw(5) << answers[j] << " "; if((j+1) % 10 == 0) cout << endl; } cout << endl; } } } return 0; } Python版的图片如下。因为网站的原因,空格需要根据图片自己输入 img[community/20250308/1741427248062400.png] img[community/20250308/1741427281005024.png] # import random as rd s=1 L=['+','-','*'] cntT=0 cntF=0 print("小学1~3年级加减口算练习题") print("如果输入0,进入在线答题模式;如果输入别的大于等于1的整数n,则将自动输出n份口算练习题") a=int(input()) if(a==0): while s!=-1: w=rd.randint(1,100) s=rd.randint(1,100) y=rd.randint(0,2) u=rd.randint(0,1) t=rd.randint(0,1) if s>w: w,s=s,w if t==0: if w<10 or s<10: print(w,L[y],s,'=',end=' ') s=str(w)+str(L[y])+str(s) a=int(input()) if a==-1: print("正确",cntT,"道") print("错误",cntF,"道") print("一共",cntT+cntF,"道") break if a==eval(s): cntT+=1 print("True") else: cntF+=1 print("False") print(":",eval(s)) else: print(w,L[u],s,'=',end=' ') s=str(w)+str(L[u])+str(s) a=int(input()) if a==-1: print("正确",cntT,"道") print("错误",cntF,"道") print("一共",cntT+cntF,"道") break if a==eval(s): cntT+=1 print("True") #此处删去一行代码 else: cntF+=1 print("False") print(":",eval(s)) if t==1: print(w,L[u],s,'=',end=' ') s=str(w)+str(L[u])+str(s) a=int(input()) if a==-1: print("正确",cntT,"道") print("错误",cntF,"道") print("一共",cntT+cntF,"道") break if a==eval(s): cntT+=1 print("True") else: cntF+=1 print("False") print(":",eval(s)) Li=[] #if(a=='2'): n=int(a) while n>=1: n=n-1 print(" 小学口算练习题\n 建议用时 10 分钟 实际用时: 分钟") o=100 while o>0: if o%5==0: print() print(' ',end='') w=rd.randint(10,99) s=rd.randint(10,99) y=rd.randint(0,2) u=rd.randint(0,1) t=rd.randint(0,1) if s>w: w,s=s,w if t==0: if w<10 or s<10: print(' ',w,L[y],s,'=',end=' ') s=str(w)+str(L[y])+str(s) Li.append(eval(s)) else: print(' ',w,L[u],s,'=',end=' ') s=str(w)+str(L[u])+str(s) Li.append(eval(s)) if t==1: print(' ',w,L[u],s,'=',end=' ') s=str(w)+str(L[u])+str(s) Li.append(eval(s)) o-=1 print("\n************************************************************************\n******************************* 答案 *********************************") for i in range(0,100): if(i%10==0): print() print(' ',end='') if(Li[i]<10): print(' ',Li[i],end=' ') if(Li[i]>=10 and Li[i]<=99): print(' ',Li[i],end=' ') if(Li[i]>=100): print(' ',Li[i],end=' ') #(原创,可挪用)
回帖
  • 2025-03-08 19:53
    python import random def get_valid_input(prompt): while True: try: return int(input(prompt)) except ValueError: print("输入无效,请重新输入数字") def main(): random.seed() operators = ['+', '-', '*'] cntT = 0 cntF = 0 print("小学1~3年级加减乘口算练习题") mode = get_valid_input("输入0进入在线答题模式(输入-1退出)\n输入正整数n生成n份练习题: ") if mode == 0: while True: num1 = random.randint(1, 100) num2 = random.randint(1, 100) if num2 > num1: num1, num2 = num2, num1 op = random.choice(operators) if op == '+': result = num1 + num2 elif op == '-': result = num1 - num2 else: result = num1 * num2 user_input = get_valid_input(f"{num1} {op} {num2} = ") if user_input == -1: print(f"\n正确: {cntT}\n错误: {cntF}\n总计: {cntT + cntF}") break if user_input == result: cntT += 1 print("✓") else: cntF += 1 print(f"✗ 正确答案: {result}") elif mode > 0: for sheet_num in range(mode): answers = [] print(f"\n 小学口算练习题(第{sheet_num+1}份)") print(" 建议用时 10 分钟 实际用时: 分钟") # 生成题目 for i in range(100): num1 = random.randint(10, 99) num2 = random.randint(10, 99) if num2 > num1: num1, num2 = num2, num1 op = random.choice(operators) if op == '+': res = num1 + num2 elif op == '-': res = num1 - num2 else: res = num1 * num2 answers.append(res) print(f"{num1:4}{op}{num2}=".ljust(15), end='') if (i+1) % 5 == 0: print() # 输出答案 print("\n" + "*"*70) print("答案".center(60)) for i, ans in enumerate(answers): print(f"{ans:5}".ljust(8), end='') if (i+1) % 10 == 0: print() if __name__ == "__main__": main()
    1 回复
  • 2025-03-08 19:52
    C++正确的是 #include <iostream> #include <cstdlib> #include <ctime> #include <string> #include <iomanip> #include <limits> using namespace std; // 全局定义运算符数组 const char OPERATORS[] = {'+', '-', '*'}; // 输入验证函数 int getValidInput() { int value; while (true) { cin >> value; if (cin.fail()) { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); cout << "输入无效,请重新输入数字: "; } else { break; } } return value; } void generateQuestion(bool practiceMode) { const int MAX_ANSWERS = 100; int answers[MAX_ANSWERS]; for (int i = 0; i < MAX_ANSWERS; ++i) { int num1 = rand() % 90 + 10; int num2 = rand() % 90 + 10; if (num2 > num1) swap(num1, num2); // 使用全局运算符数组 char op = OPERATORS[rand() % 3]; switch (op) { case '+': answers[i] = num1 + num2; break; case '-': answers[i] = num1 - num2; break; case '*': answers[i] = num1 * num2; break; } cout << setw(5) << num1 << op << num2 << "= "; if ((i+1) % 5 == 0) cout << endl; } // 答案输出 cout << "\n****************************************************************\n" << "*************************** 答案 ******************************\n"; for (int i = 0; i < MAX_ANSWERS; ++i) { cout << setw(5) << answers[i] << " "; if ((i+1) % 10 == 0) cout << endl; } cout << endl; } int main() { srand(time(0)); int cntT = 0, cntF = 0; cout << "小学1~3年级加减乘口算练习题\n"; cout << "输入0进入在线答题模式(输入-1退出)\n" << "输入正整数n生成n份练习题: "; int mode = getValidInput(); if (mode == 0) { while (true) { int num1 = rand() % 100 + 1; int num2 = rand() % 100 + 1; if (num2 > num1) swap(num1, num2); // 使用全局运算符数组 char op = OPERATORS[rand() % 3]; int result; switch (op) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; } cout << num1 << " " << op << " " << num2 << " = "; int input = getValidInput(); if (input == -1) { cout << "\n正确: " << cntT << "\n错误: " << cntF << "\n总计: " << (cntT + cntF) << endl; break; } if (input == result) { cntT++; cout << "?\n"; } else { cntF++; cout << "? 正确答案: " << result << "\n"; } } } else if (mode > 0) { for (int i = 0; i < mode; ++i) { cout << "\n 小学口算练习题(第" << i+1 << "份)\n" << " 建议用时 10 分钟 实际用时: 分钟\n"; generateQuestion(true); } } return 0; }
    2 回复
  • 2025-03-08 19:41
    @a(O(∩_∩)O~3040936)[啦啦啦] 好,谢谢提的建议
    1 回复
  • 2025-03-08 19:34
    ........这个有什么漏洞 以下是代码中存在的主要漏洞和问题: C++ 版本漏洞: 练习题中缺失乘法题 生成练习题时,操作符仅从 + 和 - 中选择(u = rand() % 2),导致练习题模式完全无乘法题,而在线答题模式包含乘法。这与需求(1~3年级含乘法)冲突。 输入非整数导致崩溃 若用户输入非整数(如字母),cin >> a 会进入错误状态,程序无法处理后续逻辑,直接崩溃或进入死循环。 随机性逻辑不一致 在线答题模式中,当 t == 0 且操作数为两位数时,仍可能生成 *(因 y 的范围是 0~2),但练习题模式完全排除该情况,导致题目难度不一致。 Python 版本漏洞: 正确计数器重复累加 在线答题模式的某个分支中,cntT += 1 被错误地重复执行两次,导致正确题数统计翻倍。 python 复制 if a==eval(s): cntT+=1 print("True") cntT+=1 # 错误:重复累加 多份练习题答案错乱 生成多份练习题时,答案列表 Li 未在每次生成前清空,导致后续答案累积。例如,生成 2 份练习题时,第二份答案会错误地输出前 100 题的结果。 输入非整数导致崩溃 未处理 int(input()) 的异常,输入非整数时直接抛出 ValueError。 操作符生成逻辑混乱 练习题模式中,当 w 或 s 小于 10 时可能生成乘法题,而 C++ 版本无此逻辑,导致两个版本行为不一致。 死循环风险 在线答题模式的循环条件 while s != -1 无意义(s 被赋值为随机数 1~100),应改为 while True,否则可能因逻辑混淆导致意外行为。 共同问题: 排版对齐问题 输出题目时,未统一使用固定宽度格式(如 Python 未用 str.ljust() 或类似方法),可能导致排版错乱。 安全性风险 Python 使用 eval() 虽然此处安全,但依赖字符串拼接生成表达式存在潜在隐患,建议改用算术计算。 修复建议: 统一操作符生成逻辑 C++ 练习题模式应包含乘法,Python 需与 C++ 逻辑一致。 添加输入验证 使用异常处理(Python)或流状态检查(C++)确保输入为合法整数。 修复计数器与答案列表 Python 中移除重复累加 cntT,并在生成每份练习题前清空 Li。 简化循环条件 将在线答题模式的循环条件改为 while True,依赖内部 break 退出。 对齐输出格式 使用固定宽度或格式化字符串确保题目和答案对齐。
    3 回复
  • 2025-03-08 18:30
    牛逼 666 face[good] face[赞] face[威武] face[爱你]
    2 回复
1 到第 总共 1 页
有账号去 登录 ,无账号 去打字 可自动生成!
还未登录,登录