前言
运行说明:代码在vscode中运行,需添添加c++插件。 程序实现功能:
- 自动生成100以内的加减乘除题目。
- 题目数量可以1到10设置(这个数字可以随意设置)。
- 加减乘除训练可以选择。
- 最后输出得分,错误题目和答题所用时间。
1.要点解析
这个问题中最主要的是产生随机数,一般使用rand()指令,但它产生的是伪随机数,需要传入srand()充当随机数种子,才能产生真正的随机数,一般传入系统时间,参考链接。 rand()%MOD 会生成0到MOD-1间的整数。 记录时间的方法:
time_t start,end;
start = time (NULL);
end = time (NULL);
cout<<difftime (end,start)<<endl;
2.程序解析
1.选择运算,加减乘除
cout<<"1.add" << '\n' <<"2.divide" << '\n' <<"3.multiply" << '\n' <<"4.minus" << '\n' <<"please enture number: ";
cin >> x;
while(x>4 || x<1)
{
cout<< "please enter number rang 1 to 4:";
cin >>x;
}
switch (x)
{
case 1:
cout<<"choice add" << '\n';break;
case 2:
cout<<"choice divide" << '\n';break;
case 3:
cout<<"choice multiply" << '\n';break;
case 4:
cout<<"choice minus" << '\n';break;
}
2.选择题目数量
cout<< "please enter question number:";
cin >>y;
while(y>10 || y<1)
{
cout<< "please enter question number rang 1 to 10:";
cin >>y;
}
cout<< "question number:"<<y<<endl;
3.生成题目
for (i= 0 ;i< y; i++)
{
item = i +1;
a = (rand()%101);
b = (rand()%101);
if(x == 1)
{
cout << item <<"."<<a <<"+"<< b <<"=";
cin >> answer;
if ((a+b) != answer)
{
use.a.push_back(a);
use.b.push_back(b);
use.i.push_back(item);
count++;
}
}
这里面比较麻烦的是除法,以下代码保证答案为整数:
if(x == 2)
{
b = (rand()%26);
while(b == 0)
{
b = (rand()%26);
}
while(a<b)
{
a = (rand()%101);
}
a = a - a%b;
cout<< item <<"." << a <<"/"<< b <<"=";
cin >> answer;
if ( a/b != answer)
{
use.a.push_back(a);
use.b.push_back(b);
use.i.push_back(item);
count++;
}
}
4.输出结果 打印错误题目,需要与加减乘除对应
cout << "error item:"<<endl;
for(j=0; j<use.a.size(); j++)
{
cout<<use.i[j]<<"." << use.a[j]<<"+"<<use.b[j]<<"="<<( use.a[j]+ use.b[j])<<endl;
}
score = ((y-count)*100/y);
cout << "score:" <<score<<" seconds."<< endl;
cout << "The pause used " << difftime (end,start) <<" seconds."<< endl;
3.演示
1.运行,选择计算方式: 如果输入数字错误,需要重新输入,直到正确: 2.选择题目数目 如果输入数字错误,需要重新输入,直到正确 3.开始答题 4.答完题后输出错误题目和答题时间
4.完整代码
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <vector>
using namespace std;
struct record
{
vector<int> i ;
vector<int> a ;
vector<int> b ;
};
int main()
{
record use;
int x ;
int y ;
int i;
int j;
int item;
int a;
int b;
int answer;
int count;
double score;
time_t start,end;
start = time (NULL);
cout<<"1.add" << '\n' <<"2.divide" << '\n' <<"3.multiply" << '\n' <<"4.minus" << '\n' <<"please enture number: ";
cin >> x;
while(x>4 || x<1)
{
cout<< "please enter number rang 1 to 4:";
cin >>x;
}
switch (x)
{
case 1:
cout<<"choice add" << '\n';break;
case 2:
cout<<"choice divide" << '\n';break;
case 3:
cout<<"choice multiply" << '\n';break;
case 4:
cout<<"choice minus" << '\n';break;
}
cout<< "please enter question number:";
cin >>y;
while(y>10 || y<1)
{
cout<< "please enter question number rang 1 to 10:";
cin >>y;
}
cout<< "question number:"<<y<<endl;
srand(time(NULL));
for (i= 0 ;i< y; i++)
{
item = i +1;
a = (rand()%101);
b = (rand()%101);
if(x == 1)
{
cout << item <<"."<<a <<"+"<< b <<"=";
cin >> answer;
if ((a+b) != answer)
{
use.a.push_back(a);
use.b.push_back(b);
use.i.push_back(item);
count++;
}
}
if(x == 2)
{
b = (rand()%26);
while(b == 0)
{
b = (rand()%26);
}
while(a<b)
{
a = (rand()%101);
}
a = a - a%b;
cout<< item <<"." << a <<"/"<< b <<"=";
cin >> answer;
if ( a/b != answer)
{
use.a.push_back(a);
use.b.push_back(b);
use.i.push_back(item);
count++;
}
}
if(x == 3)
{
cout<< item <<"." << a <<"*"<< b <<"=";
cin >> answer;
if ((a*b)!= answer)
{
use.a.push_back(a);
use.b.push_back(b);
use.i.push_back(item);
count++;
}
}
if(x == 4)
{
cout << item <<"."<< a <<"-"<< b <<"=";
cin >> answer;
cout<<(a-b)<<endl;
if ((a-b)!= answer)
{
use.a.push_back(a);
use.b.push_back(b);
use.i.push_back(item);
count++;
}
}
}
end = time (NULL);
if (x == 1)
{
cout << "error item:"<<endl;
for(j=0; j<use.a.size(); j++)
{
cout<<use.i[j]<<"." << use.a[j]<<"+"<<use.b[j]<<"="<<( use.a[j]+ use.b[j])<<endl;
}
}
if (x == 2)
{
cout << "error item:"<<endl;
for(j=0; j<use.a.size(); j++)
{
cout<<use.i[j]<<"." << use.a[j]<<"/"<<use.b[j]<<"="<<( use.a[j]/use.b[j])<<endl;
}
}
if (x == 3)
{
cout << "error item:"<<endl;
for(j=0; j<use.a.size(); j++)
{
cout<<use.i[j]<<"." << use.a[j]<<"*"<<use.b[j]<<"="<<( use.a[j]*use.b[j])<<endl;
}
}
if (x == 4)
{
cout << "error item:"<<endl;
for(j=0; j<use.a.size(); j++)
{
cout <<use.i[j]<<"." << use.a[j]<<"-"<<use.b[j]<<"="<<( use.a[j]-use.b[j])<<endl;
}
}
score = ((y-count)*100/y);
cout << "score:" <<score<<" seconds."<< endl;
cout << "The pause used " << difftime (end,start) <<" seconds."<< endl;
}
|