已实现功能:
1、程序每次运行时,会一次输出10道简单的加减法练习题。每输出1道题,等待用户输入其计算答案。题目难度限制在百以内的加减法,便于练习。
2、使用循环来实现。每轮循环输出随机生成的问题,如:“2 + 3 =
"。接收用户的输入后,使用if语句检查其正确性。并将检查结果存入相关变量便于循环后统计输出。
3、使用头文件cstdlib
中的rand
函数实现随机生成数字,并将随机生成结果取模,以限制其范围。注意,需使用srand
函数在程序开始前传入随机数种子,以确保每次运行均为不同的随机数。
4、在10道题目全部解答完毕后,程序会给出用户答对与答错的题目个数。
5、制定相应赋分规则,向用户输出答题的最终得分。
未实现:
1、在10道题目全部解答完毕后,给出答错的题目与其正确答案。
2、在答题程序开始前,可以通过简单的输入输出交互,让用户确定题目的数量、难度(运算的大小范围及是否设计乘除法)。
3、通过查阅C++相关功能,实现统计用户回答每一道题的用时,并向用户输出答题平均用时与最短用时。
#include<iostream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;
class Student
{
public:
Student() {}
void expression();//产生随机算术式
};
static int num, num1 = 0, a, b, d, c, sum, result;
void Student::expression()
{
std::cout << "\n\n\n\t\t\t学生算数10道\n\n\n\n";
for (int i = 0; i < 10; i++)
{
std::cout << "第" << i + 1 << "题\n";
c = rand() % 2 + 1;
switch (c)
{
case 1:
{
srand(time(NULL));
a = rand() % 100 + 1;
b = rand() % 100 + 1;
d = a + b;
std::cout << a << " + " << b << " = ";
}
break;
case 2:
{
srand(time(NULL));
a = rand() % 100 + 1;
b = rand() % 100 + 1;
if (a < b)
{
d = a;
a = b;
b = d;
}
d = a - b;
std::cout << a << " - " << b << " = ";
}
break;
}
cin >> result;
if (result != d)
{
cout << "error!\n";
}
else
{
cout << "right!\n";
num++;
}
}
std::cout << "共答对 " << num << " 道题, 共答错, " << num1 << " 用户得分 " << num*10 ;
}
int main()
{
Student stu;
cout << "\t\t^^*^^*^^*^^*^^欢迎进入学生算数系统^^*^^*^^*^^*^^*^^\n\n\n\n";
cout << "^^^^^^^^^^^^^^确定测试y/Y,我还没准备好n/N^^^^^^^^^^^^^^^^^\n";
char ch;
cin >> ch;
if (ch == 'N' || ch == 'n')
return 0;
if (ch == 'Y' || ch == 'y')
{
stu.expression();
}
return 0;
}
改进:1、在10道题目全部解答完毕后,给出答错的题目与其正确答案。
目前情况:未改进成功,最后输出错误题目及其正确答案时,只会输出一个之前第一个计算错误的题目以及相应的答案。同时更BUG的是,代码重新运行时,我们再随机做10道题,最后打印出来的错题仍然时最开始运行代码后所保留的拿到错题,并不是当前训练时所做错的错题。
#include<iostream>
#include <ctime>
#include <cstdlib>
#include <string>
#include<fstream>
using namespace std;
class Student
{
public:
Student() {}
void expression();
void again();
};
static int num, num1 = 0, a, b, d, c, sum, result;
void Student::expression()
{
cout << "\n\n\n\t\t\t学生算数10道\n\n\n\n";
for (int i = 0; i < 10; i++)
{
cout << "第" << i + 1 << "题\n";
c = rand() % 2 + 1;
switch (c)
{
case 1:
{
srand(time(NULL));
a = rand() % 100 + 1;
b = rand() % 100 + 1;
d = a + b;
cout << a << " + " << b << " = ";
}
break;
case 2:
{
srand(time(NULL));
a = rand() % 100 + 1;
b = rand() % 100 + 1;
if (a < b)
{
d = a;
a = b;
b = d;
}
d = a - b;
cout << a << " - " << b << " = ";
}
break;
}
cin >> result;
if (result != d)
{
cout << "error!\n";
ofstream outfile("错题集.txt", ios::app);
if (!outfile)
{
cerr << "open error!" << endl;
exit(1);
}
outfile << a;
switch (c)
{
case 1:outfile << "+"; break;
case 2:outfile << "-"; break;
}
outfile << b;
outfile << endl;
outfile << d;
outfile << endl;
outfile.close();
}
else
{
cout << "right!\n";
num++;
}
}
cout << "\t\t\t共答对 " << num << " 道题\n";
}
void Student::again()
{
char ch, c;
cout << "\t\t\t是否查看错题(按y/Y继续,任意键结束)\n";
cin >> ch;
if (num != 10)
{
if (ch == 'Y' || ch == 'y')
{
ifstream infile("错题集.txt", ios::in);
while (!(infile >> a >> c >> b >> d).eof())
{
cout << a << c << b << " = " << d;
cout << "\t\t\t测试结束\n";
infile.close();
}
}
else
cout << "\t\t\t测试完成\n";
}
}
int main(){
Student stu;
cout << "\t\t^^*^^*^^*^^*^^欢迎进入学生算数系统^^*^^*^^*^^*^^*^^\n\n\n\n";
cout << "^^^^^^^^^^^^^^首次测试选择y/Y,测试过选择n/N^^^^^^^^^^^^^^^^^\n";
char ch;
cin >> ch;
if (ch == 'N' || ch == 'n')
stu.again();
if (ch == 'Y' || ch == 'y')
{
stu.expression();
stu.again();
}
return 0;
}