1、随机数相关函数
#include
#include
srand((unsigned int)time(NULL));
rand();
产生26个随机字母--->'A'+rand()%26;'a'+rand()%26;
2、计算耗时
获取当前系统时间:
time_t start_time = time(NULL);
total_time=start_time/end_time;
3.效果
#include
#include
#include
#include
#include
void userWord(char *str,int len)
{
int i=0;
char ch;
double T_len=0.0;
time_t start_time,end_time;
printf ("\t本次游戏所列字符串为:\n");
printf ("%s\n",str);
start_time= time(NULL);
for (;i1;i++)
{
ch=_getch();
if (*(str+i)==ch)
{
printf ("%c",ch);
++T_len;
}
else
printf ("%c",'_');
}
end_time = time (NULL);
putchar('\n');
printf("\t总共花了:%ds\n",end_time-start_time);
printf ("\t正确率:%.2lf%%\n",(T_len/len)*100.0);
}
int main(void)
{
char buf0[50];
char num;
int i,len;
puts ("\t-------------------------");
puts ("\t** **");
puts ("\t**欢迎使用打字小游戏 **");
puts ("\t-------------------------");
len=sizeof(buf0);
srand ((unsigned int)time (NULL));
memset(buf0,0,sizeof(buf0));
for (i=0;i1;i++)
{
num='a'+rand ()%26;
buf0[i] =num;
}
printf ("\t字符串中长度 = %d\n",len);
userWord(buf0,len);
puts ("\t** **");
puts ("\t-------------------------");
system("pause");
return 0;
}