标签:
尝试自己写一个能自动生成小学四则运算题目的命令行“软件”,分别满足下面的各种需求:
a)自动生成小学四则运算题目,除整数以外,还要支持真分数的四则运算;运用计算机C语言中的rand函数,rand是真正的随机数生成器,而srand()会设置供rand()使用的随机数种子。
b)能接受用户输入答案,运用if语句来判断对错。最后给出总共对错的数量;
c)运用for循环来实现一次批量给出100道的题目,并且运用计算机C语言中的rand函数,rand是真正的随机数生成器,而srand()会设置供rand()使用的随机数种子,来保证题目不能重复。
程序运行结果截图:
程序代码:
#include<iostream>
#include<stdlib.h>
#include<iomanip>
#include<cmath>
#include<time.h>
using namespace std;
#define random() (rand()%100)
void main()
{
int s1,s2,s3,s4;
double s5,s6;
int a,b,c,d,e,f,g;
int right=0;
int wrong=0;
srand((int)time(NULL));
for(int i=0;i<100;i++)
{
s1=random();
s2=random();
int k=(int)rand()%5;
switch(k)
{
case 0:
{
cout<<s1<<"+"<<s2<<"=";
cin>>s5;
if(s1+s2==s5)
{
cout<<"right!"<<endl;right++;
}
else
{
cout<<"wrong!"<<endl;wrong++;
}
break;
}
case 1:
{
cout<<s1<<"-"<<s2<<"=";
cin>>s5;
if(s1-s2==s5)
{
cout<<"right!"<<endl;right++;
}
else
{
cout<<"wrong!"<<endl;wrong++;
}
break;
}
case 2:
{
cout<<s1<<"*"<<s2<<"=";
cin>>s5;
if(s1*s2==s5)
{
cout<<"right!"<<endl;right++;
}
else
{
cout<<"wrong!"<<endl;wrong++;
}
break;
}
case 3:
{
while(s2==0)
s2=random();
cout<<s1<<"/"<<s2<<"=";
if(s1%s2==0)
{cin>>s5;
if(s5==s1/s2)
{
cout<<"right!"<<endl;right++;
}
else
{
cout<<"wrong!"<<endl;wrong++;
}
}
else
{
cin>>s5;cout<<"/"<<endl;cin>>s6;
a=s1;
b=s2;
for(c=a;c>=1&&c<=a;c--)
{
d=a%c;
e=b%c;
if(d==0&&e==0)
{
f=a/c;
g=b/c;
}
}
if((f==s5)&&(g==s6))
{
cout<<"right!"<<endl;right++;
}
else
{
cout<<"wrong!"<<endl;wrong++;
}
break;
}
}
case 4:
{
while(s2==0||s1>=s2)
{
s1=random();
s2=random();
}
s3=random();
s4=random();
while(s4==0||s3>=s4)
{
s3=random();
s4=random();
}
int l=(int)rand()%4;
switch(l)
{
case 0:
{
cout<<"("<<s1<<"/"<<s2<<")"<<"+"<<"("<<s3<<"/"<<s4<<")"<<"=";
cin>>s5;cout<<"/"<<endl;cin>>s6;
a=s1*s4+s2*s3;
b=s2*s4;
if(a>b)
{
for(c=b;c>=1&&c<=b;c--)
{
d=a%c;
e=b%c;
if(d==0&&e==0)
{
f=a/c;
g=b/c;
}
}
}
else
{
for(c=a;c>=1&&c<=a;c--)
{
d=a%c;
e=b%c;
if(d==0&&e==0)
{
f=a/c;
g=b/c;
}
}
}
if((f==s5)&&(g==s6))
{
cout<<"right!"<<endl;right++;
}
else
{
cout<<"wrong!"<<endl;wrong++;
}
break;
}
case 1:
{
cout<<"("<<s1<<"/"<<s2<<")"<<"-"<<"("<<s3<<"/"<<s4<<")"<<"=";
cin>>s5;cout<<"/"<<endl;cin>>s6;
a=s1*s4-s2*s3;
b=s2*s4;
if(a>b)
{
for(c=b;c>=1&&c<=b;c--)
{
d=a%c;
e=b%c;
if(d==0&&e==0)
{
f=a/c;
g=b/c;
}
}
}
else
{
for(c=a;c>=1&&c<=a;c--)
{
d=a%c;
e=b%c;
if(d==0&&e==0)
{
f=a/c;
g=b/c;
}
}
}
if((f==s5)&&(g==s6))
{
cout<<"right!"<<endl;right++;
}
else
{
cout<<"wrong!"<<endl;wrong++;
}
break;
}
case 2:
{
cout<<"("<<s1<<"/"<<s2<<")"<<"*"<<"("<<s3<<"/"<<s4<<")"<<"=";
cin>>s5;cout<<"/"<<endl;cin>>s6;
a=s1*s3;
b=s2*s4;
if(a>b)
{
for(c=b;c>=1&&c<=b;c--)
{
d=a%c;
e=b%c;
if(d==0&&e==0)
{
f=a/c;
g=b/c;
}
}
}
else
{
for(c=a;c>=1&&c<=a;c--)
{
d=a%c;
e=b%c;
if(d==0&&e==0)
{
f=a/c;
g=b/c;
}
}
}
if((f==s5)&&(g==s6))
{
cout<<"right!"<<endl;right++;
}
else
{
cout<<"wrong!"<<endl;wrong++;
}
break;
}
case 3:
{
cout<<"("<<s1<<"/"<<s2<<")"<<"/"<<"("<<s3<<"/"<<s4<<")"<<"=";
cin>>s5;cout<<"/"<<endl;cin>>s6;
a=s1*s4;
b=s2*s3;
if(a>b)
{
for(c=b;c>=1&&c<=b;c--)
{
d=a%c;
e=b%c;
if(d==0&&e==0)
{
f=a/c;
g=b/c;
}
}
}
else
{
for(c=a;c>=1&&c<=a;c--)
{
d=a%c;
e=b%c;
if(d==0&&e==0)
{
f=a/c;
g=b/c;
}
}
}
if((f==s5)&&(g==s6))
{
cout<<"right!"<<endl;right++;
}
else
{
cout<<"wrong!"<<endl;wrong++;
}
break;
}
}
}
}
}
cout<<"the number of right answer:"<<right<<","<<"the number of wrong answer:"<<wrong<<endl;
system("pause");
}
标签:
原文地址:http://www.cnblogs.com/lianxinlong/p/5351173.html