プログラミングレッスン3

タイピングソフトの完成

それでは最後の段階として、ライブラリ標準関数<time.h>を用いて制限時間を設けることにより、
タイピングソフトを完成させましょう。

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>

#define tup 60 //制限時間デフォルト60秒

char mojiStr[] = "abcdefghijklmnopqrstuvwxyz";

//問題文字列生成関数
void GetStr(char* str)
{
int i;
int len = 0;
int index = 0;
int strsuu;
char keyall[102];
char qword[11];

len = strlen(mojiStr) - 1;
strcpy(keyall,mojiStr);

srand((unsigned)time(NULL));
strsuu = (5 + (rand() % 5));

for(i = 0;i <= strsuu; i ++){
index = (rand() % len);
qword[i] = keyall[index];
}

qword[strsuu + 1] = '\0';
strcpy(str,qword);
return;
}

//メイン関数
int main(void)
{
time_t s_time,e_time,n_time;
int point = 0;
int yn;
int atari=0;
char qstr[11] = "";
char estr[11] = "";

while(1){
printf("開始OK? [0]yes [1]no ");
scanf("%d",&yn);
if (yn == 1){return 0;}
if (yn == 0){
time(&s_time);
e_time = s_time + tup;
time(&n_time);

while(e_time >= n_time){
time(&n_time);
printf("残り時間%d秒\n",e_time - n_time);
qstr[0] = '\0';
GetStr(qstr);

while((strcmp(qstr,estr) != 0) & (e_time >= n_time)){
printf("%s\n>",qstr);
scanf("%s",estr);
if (strcmp(qstr,estr) == 0)
atari = 1;
else
{
printf("違うよ\n");
time(&n_time);
if(e_time >= n_time)
printf("残り時間%d秒\n",e_time - n_time);
}
}
if (atari == 1){
printf("正解です\n");
point += 10;
atari = 0;
}
}
printf("終了\n") ;
printf("得点は%d点です。\n\n",point);

printf("リトライ? [0]yes [1]no ");
scanf("%d",&yn);
if (yn == 1){return 0;}
}
}
return 0;
}