用Python 3.7.4编写的口算练习题程序
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=' ')
#(原创,可挪用)