#include <iostream>
#include <time.h>
using namespace std;
class game
{
public:
int h,m,s,sum;
void input(){cout<<"请输入游戏时间(时分秒):"<<endl;cin>>h>>m>>s;}
void guess();
game(){}
~game(){}
};
void game::guess ()
{
sum=h*3600+m*60+s;
srand((int)time(0));
int n;
n=rand()%10000+1;
time_t start,end;
while(1)
{
start=time(NULL);
cout<<"游戏开始!数字范围1~10000,游戏时间:"<<sum<<"s,时间耗尽前未猜出数字则输,猜出就赢"<<endl;
while(1)
{
end=time(NULL);
int a;
cout<<"输入数字:"<<endl;
cin>>a;
if(a<n) {cout<<"低了哦!"<<endl;}
else if(a>n) {cout<<"高了哦!"<<endl;}
else {cout<<"恭喜你,你赢了!"<<endl;return;}
if(end-start>=sum) {cout<<"时间到,游戏结束"<<endl;cout<<"这个数字是:"<<n<<endl;return ;}
}
}
}
int main()
{
game a;
char b;
for(;;)
{
cout<<"是否开始游戏:A 、是 B、否"<<endl;
cin>>b;
if(b=='A')
{
a.input ();
a.guess ();
break;
}
}
return 0;
}
![](https://img2024.cnblogs.com/blog/35695/202408/35695-20240826070647288-243310440.jpg)