发布网友 发布时间:2022-04-23 09:42
共1个回答
热心网友 时间:2023-09-24 02:13
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int randint()
{
int t = random();
return t % 70 + 10;
}
int run(int a, int b, int c, int t)
{
switch(t)
{
case 0:
return a - b - c;
case 1:
return a - b + c;
case 2:
return a + b - c;
default:
return a + b + c;
}
}
void output(int a, int b, int c, int t)
{
switch(t)
{
case 0:
cout<<a<<" - "<<b<<" - "<<c<<" = ";
return;
case 1:
cout<<a<<" - "<<b<<" + "<<c<<" = ";
return;
case 2:
cout<<a<<" + "<<b<<" - "<<c<<" = ";
return;
default:
cout<<a<<" + "<<b<<" + "<<c<<" = ";
}
}
int main()
{
srand(time(0));
while(1)
{
int a = randint(), b = randint(), c = randint();
int t = randint() % 4;
int r = run(a,b,c,t);
if (r >= 1 && r <= 200)
{
output(a,b,c,t);
int in;
cin>>in;
if (in == r)
{
cout<<"^_^答对了"<<endl;
}
else
{
cout<<"T_T打错了,答案是"<<r<<endl;
}
}
}
return 0;
}
追问谢谢你的回答! 程序运行过程中 有一个错误 说是 random 没有定义。。。。 是怎么回事? 谢谢指教~
追答#include<iostream>
#include<stdlib.h>
#include<ctime>
using namespace std;
int randint()
{
int t = rand(); //不好意思,这里是rand(),当时我用GNU C++编译器没问题,现在用VS C++发现问题了。
return t % 70 + 10;
}
int run(int a, int b, int c, int t)
{
switch(t)
{
case 0:
return a - b - c;
case 1:
return a - b + c;
case 2:
return a + b - c;
default:
return a + b + c;
}
}
void output(int a, int b, int c, int t)
{
switch(t)
{
case 0:
cout<<a<<" - "<<b<<" - "<<c<<" = ";
return;
case 1:
cout<<a<<" - "<<b<<" + "<<c<<" = ";
return;
case 2:
cout<<a<<" + "<<b<<" - "<<c<<" = ";
return;
default:
cout<<a<<" + "<<b<<" + "<<c<<" = ";
}
}
int main()
{
srand(time(0));
while(1)
{
int a = randint(), b = randint(), c = randint();
int t = randint() % 4;
int r = run(a,b,c,t);
if (r >= 1 && r <= 200)
{
output(a,b,c,t);
int in;
cin>>in;
if (in == r)
{
cout<<"^_^答对了"<<endl;
}
else
{
cout<<"T_T答错了,答案是"<<r<<endl;
}
}
}
return 0;
}