📄 时间函数举例.cpp
字号:
#include <iostream>
using namespace std;
#include <conio.h>
#include <time.h>
int main()
{
cout << "\1\1这是一个测试您猜出正确数字所需时间的小小软件,您可以根据提示输入在1到100之间的数字"
<< ",当您输入的数字不符合条件时,它会给您一个温馨提示,直到您输入正确为止."<<
"系统会计算出你所用时间,好啦,准备好了吗?让我们开始吧!\1\1" << endl;
clock_t start, finish;
time_t begin, end;
double var = 0;
unsigned int i, guess;
srand((unsigned)time(NULL));
cout << "Do you want to play it.('y' or 'n'): ";
char c;
c = getchar();
do
{
i = rand() % 100;
cout << "You guess the number is: " << endl;
start = clock();
time( &begin );
cin >> guess;
while(guess != i)
{
if(guess > i)
{
cout << "Please input a bit smaller." << endl;
cin >> guess;
}
else
{
cout << "Please input a bit bigger." << endl;
cin >> guess;
}
}
finish = clock();
time( &end );
var = (double)(finish - start) / CLOCKS_PER_SEC;
cout << "It took you " << var << " seconds" << endl;
cout << "It took you " << difftime(end, begin) << " seconds" << endl <<endl;
int elapsed_time = difftime(end, begin);
if(elapsed_time < 15)
cout << "You are very clever!" <<endl << endl;
else if(elapsed_time < 25)
cout << "You are normal!" << endl <<endl;
else
cout << "You are stupid!" << endl << endl;
cout << "Congradulations!You have done it." << endl;
cout << "The number you guess is " << i <<endl;
cout << "Now do you want to try it again?('y' or 'n')" << endl;
c=getchar();
}while((c=getchar())!='n');
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -