需求:
生成的20個隨機字符串由大小寫組成,規則如下:
程序源碼:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void display();
void list_bulid();
char str[21];
int main(int argc, char const *argv[])
{
/* code */
display();
return 0;
}
void display()
{
printf("*****************************************\n");
printf("*輸入過程無法退出! *\n");
printf("*請按所給字母敲擊鍵盤! *\n");
printf("*按任意鍵開始測試,按下首字母開始計時! *\n");
printf("*輸入出則以 _ 表示 *\n");
printf("*****************************************\n");
system("stty -icanon");//關閉緩衝區,輸入字符無需按回車鍵直接接受
getchar();
printf("\b\n");
list_bulid();
return ;
}
/***********************************
建立一串字母,並對鍵盤輸出進行處理的函數
************************************/
void list_bulid()
{
int i = 0;
int t;
int right = 20;
char c;
time_t start,end;
srand((unsigned)time(NULL));
while(i < 20)
{
t=rand() % 2;
if(t == 0)
c ='a'+rand()%26;
else
c = 'A'+rand()%26;
str[i] = c;
i++;
printf("%c",c);
}
printf("\n");
system("stty -icanon");//關閉緩衝區,輸入字符無需按回車鍵直接接受
i = 0;
while(i < 20)
{
c = getchar();
if(i == 0) //計時開始
{
start = time(0);
}
//printf("%c --- %c -\n", str[i] , c);
if(str[i] != c)
{
printf("\b_");
right--;
}
//else printf("%c!", c);
i++;
//printf("%s\n",str );*/
}
end = time(0); //計時結束
printf("\n完成輸入!\n");
i = end - start;
printf("用時%d s\n", i);
printf("正確率%.2lf %%", right/20.0 * 100);
printf("按下Esc退出,空格鍵繼續!\n");
while(1)
{
c = getchar();
i = c;
if(c == ' ')
{
printf("\n");
display();
}
else if(i == 27)
{
printf("\b");
printf("\b");
printf("\b");
printf("\b");
return ;
}
else
{
printf("\b");
}
}
}