回答編集履歴

1

修正コード追記

2016/09/12 06:25

投稿

ttyp03
ttyp03

スコア16996

test CHANGED
@@ -31,3 +31,113 @@
31
31
 
32
32
 
33
33
  それから`start = clock()`はfor文の外に出さないと1問ごとの時間になってしまいますよ。
34
+
35
+
36
+
37
+ 修正版コード
38
+
39
+ ```c
40
+
41
+ #include <stdio.h>
42
+
43
+ #include <stdlib.h>
44
+
45
+ #include <string.h>
46
+
47
+ #include <time.h>
48
+
49
+
50
+
51
+ int main()
52
+
53
+ {
54
+
55
+ //単語設定
56
+
57
+ char *word[] = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"};
58
+
59
+ char typing[256];
60
+
61
+ clock_t start,end;
62
+
63
+ char idx[20];
64
+
65
+ int i;
66
+
67
+ int randmax = 20;
68
+
69
+ int wordchange;
70
+
71
+
72
+
73
+ // idx配列の初期化
74
+
75
+ for(i = 0; i < 20; i++) idx[i] = i;
76
+
77
+
78
+
79
+ srand((unsigned)time(NULL)); //乱数発生
80
+
81
+
82
+
83
+ int count; //ループ用変数
84
+
85
+
86
+
87
+ //時間計測スタート
88
+
89
+ start = clock();
90
+
91
+ for(count = 0; count < 10; count++){ //ループ処理
92
+
93
+ wordchange = rand () % randmax;
94
+
95
+
96
+
97
+ while(*word != typing){ //比較処理
98
+
99
+ puts(word[idx[wordchange]]);
100
+
101
+ gets(typing);
102
+
103
+
104
+
105
+ if(strcmp(word[idx[wordchange]],typing) == 0){
106
+
107
+ puts("\n\"right!\"\n"); //正解
108
+
109
+ break;
110
+
111
+ }
112
+
113
+ else{
114
+
115
+ puts("\n\"Nooooo!!!!\"\n"); //不正解
116
+
117
+ }
118
+
119
+ }
120
+
121
+ // idx配列を詰める
122
+
123
+ for(i = wordchange; i < 20; i++) idx[i] = idx[i + 1];
124
+
125
+ randmax--;
126
+
127
+ }
128
+
129
+ end = clock(); //時間計測終了
130
+
131
+ printf("あなたは %d秒 でした。\n",end/CLOCKS_PER_SEC); //結果表示、CLOCKS_PER_SECで秒に変換
132
+
133
+
134
+
135
+ return 0;
136
+
137
+ }
138
+
139
+
140
+
141
+
142
+
143
+ ```