teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

修正コード追記

2016/09/12 06:25

投稿

ttyp03
ttyp03

スコア17002

answer CHANGED
@@ -14,4 +14,59 @@
14
14
  idx[3]=4
15
15
  ...
16
16
 
17
- それから`start = clock()`はfor文の外に出さないと1問ごとの時間になってしまいますよ。
17
+ それから`start = clock()`はfor文の外に出さないと1問ごとの時間になってしまいますよ。
18
+
19
+ 修正版コード
20
+ ```c
21
+ #include <stdio.h>
22
+ #include <stdlib.h>
23
+ #include <string.h>
24
+ #include <time.h>
25
+
26
+ int main()
27
+ {
28
+ //単語設定
29
+ char *word[] = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"};
30
+ char typing[256];
31
+ clock_t start,end;
32
+ char idx[20];
33
+ int i;
34
+ int randmax = 20;
35
+ int wordchange;
36
+
37
+ // idx配列の初期化
38
+ for(i = 0; i < 20; i++) idx[i] = i;
39
+
40
+ srand((unsigned)time(NULL)); //乱数発生
41
+
42
+ int count; //ループ用変数
43
+
44
+ //時間計測スタート
45
+ start = clock();
46
+ for(count = 0; count < 10; count++){ //ループ処理
47
+ wordchange = rand () % randmax;
48
+
49
+ while(*word != typing){ //比較処理
50
+ puts(word[idx[wordchange]]);
51
+ gets(typing);
52
+
53
+ if(strcmp(word[idx[wordchange]],typing) == 0){
54
+ puts("\n\"right!\"\n"); //正解
55
+ break;
56
+ }
57
+ else{
58
+ puts("\n\"Nooooo!!!!\"\n"); //不正解
59
+ }
60
+ }
61
+ // idx配列を詰める
62
+ for(i = wordchange; i < 20; i++) idx[i] = idx[i + 1];
63
+ randmax--;
64
+ }
65
+ end = clock(); //時間計測終了
66
+ printf("あなたは %d秒 でした。\n",end/CLOCKS_PER_SEC); //結果表示、CLOCKS_PER_SECで秒に変換
67
+
68
+ return 0;
69
+ }
70
+
71
+
72
+ ```