小学数学练习
【问题描述】
编写一个帮助小学生练习数学的程序,帮助小学生练习 100 以内的四种数学运算:加、减、乘、除。
【基本要求】
a) 程序应先询问用户的 ID 号(ID 号包括两个大写字母和 4 位数字),例如:
请输入用户 ID 号:AB1234
程序应对输入的 ID 号验证,符合 ID 号要求的格式,然后程序提示三种选择:
(1)开始测试
(2)检查分数
(3)退出
b) 测试:该程序将给出 10 道数学题,例如:
12 * 3 =36
48 + 32 =80
…
56 / 28 =2
注意:
i)学生将依次回答每一个问题(在等于号后面给出答案),然后给出下一道题。
ii)试题应包含四种数学运算:加、减、乘、除,它们是随机产生的。相邻的问题应该是不同的操作,
每个操作必须至少出现一次。报告中应给出实现方法或算法。
iii)为每道题随机生成数字,但必须确保参与运算的数字和结果都小于 100 且大于零的整数,除法时 还要注意两个整数要能整除。报告中应给出实现方法或算法。
iv)十道题做完后,记录学生完成这十道题所用的时间。
v)给每个学生一个分数。将该学生的 ID、成绩和使用时间保存到一个名为 record.txt 的文件中。
vi)在屏幕上输出以下信息:(3 列信息,第 1 列是 b)中的测试题,蓝色部分)
问题 | 正确答案 | 你的答案
c) 成绩检查:从文件“record.txt”中列出该学生的所有历史成绩(其他学生的不显示)。例如:
你以前的记录是:
AB1234 80 150 秒
AB1234 50 182 秒
AB1234 90 98 秒
代码
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <fstream>
#include<stdio.h>
using namespace std;
bool loading(string id);
void menu();
bool choose(int n,string id);
void ceshi(string id);
void jisuan();
int main(int argc, char* argv[])
{
int ch;
string id;
cout << "请输入用户id号" << endl;
cin >> id;
if (loading(id)){
do {
menu();
cin >> ch;
} while (choose(ch,id));
}
}
bool loading(string id) {
//判断用户名是否符合要求
if (id.size() == 6) {
char* p = (char*)id.data();
for (int i = 0; i < 2; i++) {
if (p[i] < 'A' || p[i]>'Z') {
cout << "id不符合规则" << endl;
return false;
}
}
for (int i = 2; i < 6; i++) {
if (p[i] < '0' || p[i]>'9') {
cout << "id不符合规则" << endl;
return false;
}
}
}
else {
cout << "id不符合规则" << endl;
return false;
}
}
void menu() {
cout << "1.开始测试" << endl;
cout << "2.检查分数" << endl;
cout << "3.退出" << endl;
cout << "请选择序号" << endl;
}
bool choose(int n,string id) {
bool s = true;
while (s) {
switch (n)
{
case 1:
ceshi(id);
break;
case 2:
jisuan();
break;
case 3:
s = false;
}
return s;
}
}
void ceshi(string id) {
char jilu[10][5];
srand((unsigned)time(NULL));
int score = 0;
clock_t startTime, endTime;
startTime = clock();//开始计时
int lastFuhao = 1;
int daan;
int fuhao=1;
for (int i = 0; i < 10; i++) {
startTime = clock();//开始计时
//随机加减乘除
while (lastFuhao == fuhao) {
fuhao = (rand() % (5 - 1)) + 1;//产生1-4之间的数
}
lastFuhao = fuhao;
if (fuhao==1) {//加法
int m = (rand() % 100 + 1);
int n = (rand() % (100-m) + 1);
cout << m << "+" << n << "=" ;
cin >> daan;
if (daan == (m + n)) {
score += 10;
}
jilu[i][0] = m;//记录每次的题目答案
jilu[i][1] = '+';
jilu[i][2] = n;
jilu[i][3] = daan;
jilu[i][4] = m+n;
}
if (fuhao == 2) {//乘法
int m, n;
do
{
m = (rand() % 100 + 1);
n = (rand() % m + 1);
}
while ((m*n) > 100);
cout << m << "*" << n << "=" ;
cin >> daan;
if (daan == (m * n)) score += 10;
jilu[i][0] = m;//记录每次的题目答案
jilu[i][1] = '*';
jilu[i][2] = n;
jilu[i][3] = daan;
jilu[i][4] = m*n;
}
if (fuhao == 3) {//减法
int m = (rand() % (100 - 1)) + 1;
int n = (rand() % (m - 1)) + 1;
cout << m << "-" << n << "=" ;
cin >> daan;
if (daan == (m - n)) score += 10;
jilu[i][0] = m;//记录每次的题目答案
jilu[i][1] = '-';
jilu[i][2] = n;
jilu[i][3] = daan;
jilu[i][4] = m-n;
}
if (fuhao == 4) {//除法
int m, n;
double res,s;
do
{
m = (rand() % 100 + 1);
n = (rand() % m + 1);
res = (double)m / (double)n;
s = res - (int)res;
} while ( s!=0);
cout << m << "/" << n << "=";
cin >> daan;
if (daan == (m / n)) score += 10;
jilu[i][0] =m;//记录每次的题目答案
jilu[i][1] = '/';
jilu[i][2] = n;
jilu[i][3] = daan;
jilu[i][4] = m/n;
}
}
endTime = clock();//结束计时
double Time = (double)((endTime - startTime) / 100) ;
cout << "计算所用时间为: " << Time << "s" << endl;
cout << "计算所得分数为:" << score << endl;
cout << "问题" << '\t' << "正确答案" << '\t' << "你的答案" << endl;
for (int i = 0; i < 10; i++) {
cout << (int)jilu[i][0] << jilu[i][1] << (int)jilu[i][2] << '\t' << (int)jilu[i][4] << '\t'<<'\t' <<(int)jilu[i][3] << endl;
}
fstream file;
file.open("record.txt",ios::app|ios::in);
if (!file) {
cout << "文件打开失败" << endl;
}
else {
file << id << ' ' << score << ' ' << Time << endl;
}
file.close();
}
void jisuan() {
fstream file;
file.open("record.txt");
if (!file) {
cout << "文件打开失败" << endl;
}
else {
string id;
cout << "请输入查询的用户名:" << endl;
cin >> id;
int n = 0;
string temp;
string sc = id;
cout << "你的记录是:" << endl;
while (getline(file, temp)) {
if (temp.find(sc) != temp.npos) {
cout << temp << endl;
n++;
}
}
if (n == 0) {
cout << "无此id" << endl;
}
}
file.close();
}