#if 1
#include <iostream>
#include <conio.h>
#include <time.h>
const int MAX = 51;
using namespace std;
int main(){
char str[MAX];
char ch;
while (1){
cout << "*** game start ***" << endl;
cout << "press any key to start game" << endl;
ch = _getch();
if (ch == 27)
break;
//generate str
srand((unsigned)time(NULL));
for (int i = 0; i < MAX - 1; i++)
str[i] = rand() % 26 + 'a';
str[MAX - 1] = '\0';
cout << str << endl;
//game start
int count = 0;
time_t t_start, t_end;
t_start = time(NULL);
for (int i = 0; i < MAX - 1; i++){
ch = _getch();
if (ch == str[i]){
count++;
cout << ch;
}
else
cout << '_';
if (ch == 27)
break;
}
t_end = time(NULL);
cout << endl << endl << "*** game stop ***" << endl;
cout << "正确率: " << (count*1.0/50*100) << endl;
cout << "耗时: " << (t_end - t_start) << endl;
if (ch == 27)
break;
}
return 0;
}
#endif
参考链接:关于rand()和srand()