2017-11-11
2017.11.11 靳艺伦【第一天】
今天开始编写一个随机计算器程序。实现随机产生一个可以自己选择难度,并且可以统计做对题目数的随机计算。
#include<iostream>
#include<cmath>
#include<ctime>
using namespace std;
static int n=0, s=0;//用来统计算对的和算错的题目数量
int random(int x)//定义一个可以生成随机数值的函数
{
int b, c;//b表示输出值,c作为一个中间变量,用于b的赋值
c = pow(10, 5-x);
b = rand() / c;//随机生成一个小于c的数
return b;
}
void count(char sign,double shu1,double shu2)//定义一个计算器函数
{
double answer, test;
switch(sign)
{
case'+':cout << shu1 << "+" << shu2 << "=?";
cin >> answer;
test = shu1 + shu2;
if (test == answer)
{ cout << "You're right!" << endl; n = n + 1; }
else cout << "You're wrong!" << endl;
s = s + 1;
break;
case'-':cout << shu1 << "-" << shu2 << "=?";
cin >> answer;
test = shu1 - shu2;
if (test == answer)
{
cout << "You're right!" << endl; n = n + 1;
}
else cout << "You're wrong!"<< endl;
s = s + 1;
break;
case'*':cout << shu1 << "*" << shu2 << "=?";
cin >> answer;
test = shu1 *shu2;
if (test == answer)
{
cout << "You're right!" << endl; n = n + 1;
}
else cout << "You're wrong!" << endl;
s = s + 1;
break;
case'/':cout << shu1 << "/" << shu2 << "=?";
//assert(shu2 != 0 && shu1 <shu2);
cin >> answer;
test = shu1/ shu2;
if (test == answer)
{
cout << "You're right!" << endl; n = n + 1;
}
else cout << "You're wrong!" << endl;
s = s + 1;
break;
}
}
int main()
{
int x;
char sign;
cout << "请选择难度1或2" << endl;
cin >> x;
cout << "请选择计算方法" << endl;
cin >> sign;
int shu1, shu2;
do {srand(time(0));
shu1 = random(x);
srand(rand());
shu2 = random(x);
count(sign, shu1, shu2);
cout << "算对的题数为" << n << '\t' << "算错的题数为" << s - n << endl;;
system("pause");
} while (s < 11);
}
明日计划:(I)对这个程序的部分不足进行改正:(1)修正除法时可能出现分母为0的情况(2)修正选择难度为2时可能出现难度1的题目的情况。
(II)复习函数内容,开始学习数组