第一次作业

需求分析:

1.真分数的分子不能大于分母的数,且分母不能为0,还要考虑计算结果的化简和判断问题。
2.因为本程序自动生成的是小学生运算题目,因此不能出现负数运算。
3.让程序能够接受用户输入的答案,并判定对错,最后给出总共对的数量对其进行打分并给出正确率。
4.能够随机生成整数与真分数四则运算,能用参数控制生成题目个数。

5.程序一次运行生成的题目不能重复,即任何两道题目不能通过有限次交换+和×左右的算术表达式变换为同一道题目。

设计思路:
1.用户在控制台打开程序,并输入题目数,按回车开始答题。
2.第一道题目产生后,用户答完题,判断对错,错则给出正确答案,再继续产生下一道题。
3.答完所有题目后给出成绩及正确率。

代码说明

#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<string.h>
 
using namespace std;
//采用最大公因数来对分数题目以及答案进行化简,得出最简的答案。
int k=0;
int gcd(int x, int y)
{
int z;
if ((z = x % y) != 0)
{
gcd(y, z);
}
else
return y;
}
 
 //真分数产生随机数。
void fraction()
{
int i,j,x,y,z,temp,a,b,c,z1,z2;
srand(unsigned(time(NULL)));//这个函数调用中使用(time(NULL));这个函数调用中使用time(NULL)函数返回值(当前时间)作实参。初始化随机数发生器。
char r1[10], r2[10];
memset(r1, 0, sizeof(r1));
memset(r2, 0, sizeof(r2));
i=rand()%10;//rand()返回一个随机数
j=rand()%10;
while(j==0||i>=j)
{
i=rand()%10;
j=rand()%10;
}
x=rand()%10;
y=rand()%10;
while(y==0||x>=y)
{
x=rand()%10;
y=rand()%10;
}
z=rand()%100/25;
z1=0;z2=0;
switch(z)
{
case 0:
cout<<"("<<x<<"/"<<y<<")"<<"+"<<"("<<i<<"/"<<j<<")"<<"=";
a = (x*j) + (i*y);
b = y*j;
c = gcd(a, b);
a = a / c;
b = b / c;
if (b != 1) {
sprintf(r1, "%d/%d", a, b);
cin >> r2;
if (strcmp(r1, r2) == 0)
{
cout << " " << "True" << endl;
k = k + 1;
}
else cout << " " << "False" << " " << "正确答案是:"<<r1<<endl;
}
else {
cin >> z1;
cin.get();
if (z1 == a)
{
cout << " " << "True" << endl;
k = k + 1;
}
else cout << " " << "False" << " " <<"正确答案是:"<<a<<endl;
}
break;
case 1:
if ((x*j)<(i*y))
{
temp = x;
x = i;
i = temp;
temp = y;
y = j;
j = temp;
}
else;
cout<<"("<<x<<"/"<<y<<")"<<"-"<<"("<<i<<"/"<<j<<")"<<"=";
a = (x*j) - (i*y);
b = y*j;
c = gcd(a, b);
a = a / c;
b = b / c;
if (b != 1) {
sprintf(r1, "%d/%d", a, b);
cin >> r2;
if (strcmp(r1, r2) == 0)
{
cout << " " << "True" << endl;
k = k + 1;
}
else cout << " " << "False" << " " << "正确答案是:"<<r1<<endl;
}
else {
cin >> z1;
cin.get();
if (z1 == a)
{
cout << " " << "True" << endl;
k = k + 1;
}
else cout << " " << "False" << " " << "正确答案是:"<<a<<endl;
}
break;
case 2:
cout<<"("<<x<<"/"<<y<<")"<<"*"<<"("<<i<<"/"<<j<<")"<<"=";
a = x*i;
b = y*j;
c = gcd(a, b);
a = a / c;
b = b / c;
if (b != 1) {
sprintf(r1, "%d/%d", a, b);
cin >> r2;
if (strcmp(r1, r2) == 0)
{
cout << " " << "True" << endl;
k = k + 1;
}
else cout << " " << "False" << " " <<"正确答案是:"<<r1<<endl;
}
else {
cin >> z1;
cin.get();
if (z1 == a)
{
cout << " " << "True" << endl;
k = k + 1;
}
else cout << " " << "False" << " " <<"正确答案是:"<<a<<endl;
}
break;
 
//真分数除法运算。
 
case 3:
cout<<"("<<x<<"/"<<y<<")"<<"/"<<"("<<i<<"/"<<j<<")"<<"=";
a = x*j;
b = y*i;
c = gcd(a, b);
a = a / c;
b = b / c;
if (b != 1) {
sprintf(r1, "%d/%d", a, b);
cin >> r2;
if (strcmp(r1, r2) == 0)
{
cout << " " << "True" << endl;
k = k + 1;
}
else cout << " " << "False" << " " <<"正确答案是:"<<r1<<endl;
}
else {
cin >> z1;
if (z1 == a)
{
cout << " " << "True" << endl;
k = k + 1;
}
else cout << " " << "False" << " " << "正确答案是:"<<a<<endl;
}
break;
default:
cout << "wrong" << endl;
break;
}
}
 
void integer()
{
int i,j,z,temp,a,b,c,z1,z2;
srand(unsigned(time(NULL)));
char r1[10], r2[10];
memset(r1, 0, sizeof(r1));
memset(r2, 0, sizeof(r2));
i=rand()%10;
j=rand()%10;
z=rand()%100/25;
z1=0;z2=0;
switch(z)
{
case 0:
cout<<i<<"+"<<j<<"=";
cin>>z1;
cin.get();
z2=i+j;
if (z1 == z2)
{
cout << " " << "True" << endl;
k = k + 1;
}
else cout << " " << "False" << " " <<"正确答案是:"<<z2<<endl;
break;
case 1:
if (i<j)
{
temp = i;
i = j;
j = temp;
}
else;
cout<<i<<"-"<<j<<"=";
cin>>z1;
cin.get();
z2=i-j;
if (z1 == z2)
{
cout << " " << "True" << endl;
k = k + 1;
}
else cout << " " << "False" << " " << "正确答案是:"<<z2<<endl;
break;
case 2:
cout<<i<<"*"<<j<<"=";
cin>>z1;
cin.get();
z2=i*j;
if (z1 == z2)
{
cout << " " << "True" << endl;
k = k + 1;
}
else cout << " " << "False" << " " << "正确答案是:"<<z2<<endl;
break;
case 3:
if (j != 0)
{
cout << i<< "÷" << j << "=";
c = gcd(i, j);
a = i / c;
b = j / c;
if (b != 1)
{
sprintf(r1, "%d/%d", a, b);
cin >> r2;
if (strcmp(r1, r2) == 0)
{
cout << " " << "True" << endl;
k = k + 1;
}
else cout << " " << "False" << " " << "正确答案是:"<<r1<<endl;
}
else
{
cin >> z1;
cin.get();
if (z1 == a) {
cout << " " << "True" << endl;
k = k + 1;
}
else cout << " " << "False" << " " << "正确答案是:"<<a<<endl;
}
 
}
else {
cout << i << "÷" << "1" << "=";
cin >> z1;
cin.get();
if (z1 == i) {
cout << " " << "True" << endl;
k = k + 1;
}
else cout << " " << "False" << " " << "正确答案是:"<<i<<endl;
}
break;
default:
cout << "wrong" << endl;
break;
}
}
 
 //主函数,能用参数控制生成题目个数。
int main(int argc,char *argv[])
{
int n,i;
float m;
n=atoi(argv[2]);
cout<<"生成运算题数目:"<<n<<endl;
for(i=0;i<n;i++)
{
int c=rand()%2;
switch(c)
{
case 0: integer();break;
case 1: fraction();break;
 
}
}
m=(float)k/(float)n;
cout<<"您答对的题数是:"<<k<<" "<<"正确率是:" <<m<<endl;
return 0;
}

 

运行结果:

实验总结:

由于本人编程能力有限,所以还有一些功能没有实现,查重的功能就没有做,本次作业也参考了多名同学的代码,然后结合自己的思考,过程很艰难,但这也让我巩固了以前所学过的知识,而且学到了很多在书本上所没有学到过的知识

https://coding.net/u/chenjunda/p/first/git

posted @ 2017-09-24 11:11  Junda  阅读(124)  评论(1编辑  收藏  举报