質問編集履歴

5

;

2016/02/27 05:51

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -306,8 +306,6 @@
306
306
 
307
307
  fflush(stdin);
308
308
 
309
- getchar();
310
-
311
309
  fclose(fp);
312
310
 
313
311
 

4

コードの最初のフォントの修正

2016/02/27 05:51

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -798,6 +798,10 @@
798
798
 
799
799
  return 0;
800
800
 
801
-
801
+
802
802
 
803
803
  }
804
+
805
+ ``````
806
+
807
+ ``````

3

コードを全て載せましたのでお願いします。

2016/02/27 05:23

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,16 +1,528 @@
1
1
  プログラム実行でmain関数がbest= get_data(count); の実行後途中で止まり、enterキーで実行されるのですが、enterキー無しで最後まで実行したいのですが
2
2
 
3
- どうしてとまるのか分からないので教えてくだい。
3
+ どうしてとまるのか分からないので教えてくだい。
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+
12
+
13
+ ``````ここに言語を入力
14
+
15
+ /* ラックナンバーサーチ・トレーニング
16
+
17
+ 過去の履歴と最近の10回分だけを表示している。
18
+
19
+ */
20
+
21
+ #include <stdio.h>
22
+
23
+ #include <time.h>
24
+
25
+ #include <float.h>
26
+
27
+ #include <ctype.h>
28
+
29
+ #include <stdlib.h>
30
+
31
+ #include <sys/types.h>
32
+
33
+ #include <unistd.h>
34
+
35
+ #include "getputch.h"
36
+
37
+
38
+
39
+ #define MAX_STAGE 3
40
+
41
+ #define swap(type, x, y) do { type t = x; x = y; y = t; } while (0)
42
+
43
+ #define MAX_NUM 10
44
+
45
+
46
+
47
+ char dtfile[] = "LACKNUM.DAT";
48
+
49
+ char dtfile2[] = "LACKNUM2.DAT";
50
+
51
+ double score2;
52
+
53
+
54
+
55
+ typedef struct {
56
+
57
+ int tm_year;
58
+
59
+ int tm_mon;
60
+
61
+ int tm_mday;
62
+
63
+ int tm_hour;
64
+
65
+ int tm_min;
66
+
67
+ int tm_sec;
68
+
69
+ double best;
70
+
71
+ } BEST_TEN;
72
+
73
+
74
+
75
+ // --------------- 比較用の関数 cmp -------------------
76
+
77
+ int cmpptr( const void *p, const void *q ) {
78
+
79
+ return (*(BEST_TEN**)p)->best - (*(BEST_TEN**)q)->best;
80
+
81
+ }
82
+
83
+ // ----------------------------------------------------
84
+
85
+
86
+
87
+ //1回実行したとき進むポイントの大きさを求める
88
+
89
+ int kaime_p(fpos_t* kaime_p1)
90
+
91
+ {
92
+
93
+ FILE *fp;
94
+
95
+ struct tm local;
96
+
97
+ double best;
98
+
99
+
100
+
101
+ fp = fopen(dtfile, "rb");
102
+
103
+ fread(&local, sizeof(struct tm), 1, fp);
104
+
105
+
106
+
107
+ fread(&best, sizeof(double), 1, fp);
108
+
109
+
110
+
111
+ //ファイルポインタの位置を取得
112
+
113
+ fgetpos(fp,kaime_p1); //ファイルfpの現在のファイル位置を取得して
114
+
115
+ // kaime_p1の示す場所に格納します。
116
+
117
+ printf("ファイルポインタの位置kaime_p1は「%d」です。\n",*kaime_p1);
118
+
119
+
120
+
121
+ fclose(fp);
122
+
123
+
124
+
125
+ return *kaime_p1;
126
+
127
+ }
128
+
129
+
130
+
131
+ //local のデータを新しい順にならべる。
132
+
133
+ void new_local_data(fpos_t ft,int k)
134
+
135
+ {
136
+
137
+ FILE *fp;
138
+
139
+ FILE *fp2;
140
+
141
+ double best;
142
+
143
+ double best2;
144
+
145
+ int i;
146
+
147
+
148
+
149
+ struct tm local;
150
+
151
+
152
+
153
+ if ((fp = fopen(dtfile, "rb")) == NULL) {
154
+
155
+ printf("ファイルを作成します。\n\n");
156
+
157
+ best = DBL_MAX;
158
+
159
+ } else {
160
+
161
+ printf("最近の10回の点数とそれらの実行日時は\n\n");
162
+
163
+ for(i=ft-k; i>=ft-10*k; i -= k){
164
+
165
+ fseek(fp,i,SEEK_SET);
166
+
167
+
168
+
169
+ fread(&local, sizeof(struct tm), 1, fp);
170
+
171
+ printf("%d年 %d月 %d日 %d時 %d分 %d秒\n",
172
+
173
+ local.tm_year + 1900, local.tm_mon + 1,
174
+
175
+ local.tm_mday, local.tm_hour, local.tm_min, local.tm_sec);
176
+
177
+ fread(&best, sizeof(double), 1, fp);
178
+
179
+ printf("得点(所要時間)は%.1f秒\n\n", best);
180
+
181
+ }
182
+
183
+ printf("enterキーで開始します。\n");
184
+
185
+ fflush(stdin);
186
+
187
+ getchar();
188
+
189
+ }
190
+
191
+ fclose(fp);
192
+
193
+
194
+
195
+ }
196
+
197
+ /*--- 過去のトレーニング情報を取得・表示して最高得点を返す ---*/
198
+
199
+ int get_data(int count)
200
+
201
+ {
202
+
203
+ FILE *fp;
204
+
205
+ FILE *fp2;
206
+
207
+ double best;
208
+
209
+ double best2;
210
+
211
+ int i,j=0;
212
+
213
+ BEST_TEN best_ten[25]={0};
214
+
215
+
216
+
217
+ if ((fp = fopen(dtfile, "rb")) == NULL) {
218
+
219
+ printf("ファイルを作成します。\n\n");
220
+
221
+ best = DBL_MAX;
222
+
223
+ } else {
224
+
225
+
226
+
227
+ struct tm local;
228
+
229
+ double line[256];
230
+
231
+
232
+
233
+
234
+
235
+ printf("\n過去の履歴\n-------------------------- \n");
236
+
237
+
238
+
239
+ while((i = fread(&local, sizeof(struct tm), 1, fp)) > 0 ){
240
+
241
+ printf("%d年 %d月 %d日 %d時 %d分 %d秒\n",
242
+
243
+ local.tm_year + 1900, local.tm_mon + 1,
244
+
245
+ local.tm_mday, local.tm_hour, local.tm_min, local.tm_sec);
246
+
247
+ fread(&best, sizeof(double), 1, fp);
248
+
249
+ printf("得点(所要時間)は%.1f秒\n\n", best);
250
+
251
+
252
+
253
+ best_ten[j].tm_year=local.tm_year;
254
+
255
+ best_ten[j].tm_mon=local.tm_mon;
256
+
257
+ best_ten[j].tm_mday=local.tm_mday;
258
+
259
+ best_ten[j].tm_hour=local.tm_hour;
260
+
261
+ best_ten[j].tm_min=local.tm_min;
262
+
263
+ best_ten[j].tm_sec=local.tm_sec;
264
+
265
+ best_ten[j].best=best;
266
+
267
+ j++;
268
+
269
+ count++;
270
+
271
+ }
272
+
273
+ //printf("countは%d\n\n", count);
274
+
275
+ }
276
+
277
+ BEST_TEN *plst[count];
278
+
279
+ for( i = 0; i < count; i++ ) plst[i] = &best_ten[i];
280
+
281
+
282
+
283
+ qsort( plst, count, sizeof(BEST_TEN*), cmpptr );
284
+
285
+
286
+
287
+
288
+
289
+ printf("\n過去のbestten\n-------------------------- \n");
290
+
291
+ // 並べ替え後の内容を表示
292
+
293
+ for(i=0;i<10;i++){
294
+
295
+ printf( "%d年 %d月 %d日 %d時 %d分 %d秒 %.lf秒\n"
296
+
297
+ ,plst[i]->tm_year+1900, plst[i]->tm_mon+1, plst[i]->tm_mday,
298
+
299
+ plst[i]->tm_hour,plst[i]->tm_min,plst[i]->tm_sec );
300
+
301
+ printf("得点(所要時間)は %.1f秒です。\n\n", plst[i]->best);
302
+
303
+ }
304
+
305
+
306
+
307
+ fflush(stdin);
308
+
309
+ getchar();
310
+
311
+ fclose(fp);
312
+
313
+
314
+
315
+ return count;
316
+
317
+ }
318
+
319
+ /*--- 今回のトレーニング情報を書き込む ---*/
320
+
321
+ void put_data(double best, double best2)
322
+
323
+ {
324
+
325
+ FILE *fp;
326
+
327
+ FILE *fp2;
328
+
329
+ time_t t = time(NULL);
330
+
331
+ struct tm *local = localtime(&t);
332
+
333
+
334
+
335
+ time(&t);
336
+
337
+ local = localtime(&t);
338
+
339
+ if ((fp = fopen(dtfile, "ab")) == NULL){
340
+
341
+ printf("ファイルがあーりません");
342
+
343
+ fflush(stdout);
344
+
345
+ }else {
346
+
347
+ fwrite(local, sizeof(struct tm), 1, fp);
348
+
349
+ fwrite(&best, sizeof(double), 1, fp);
350
+
351
+ fclose(fp);
352
+
353
+ }
354
+
355
+ if(best <= best2){
356
+
357
+ if((fp2 = fopen(dtfile2, "wb")) ==NULL){
358
+
359
+ printf("ファイルがあーりません");
360
+
361
+ fflush(stdout);
362
+
363
+ }else{
364
+
365
+ fwrite(local, sizeof(struct tm), 1, fp2);
366
+
367
+ fwrite(&best2, sizeof(double), 1, fp2);
368
+
369
+ fclose(fp2);
370
+
371
+ }
372
+
373
+ }
374
+
375
+ }
376
+
377
+
378
+
379
+ /*--- トレーニングを実行して得点(所要時間)を返す ---*/
380
+
381
+ double go(void)
382
+
383
+ {
384
+
385
+ int i, j, stage;
386
+
387
+ int dgt[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
388
+
389
+ int a[8];
390
+
391
+ double jikan;
392
+
393
+ clock_t start, end;
394
+
395
+
396
+
397
+ printf("欠けている数字を入力してください。\n");
398
+
399
+ printf("スペースキーで開始します。\n");
400
+
401
+ while (getch() != ' ')
402
+
403
+ ;
404
+
405
+ start = time(NULL);
406
+
407
+
408
+
409
+ for (stage = 0; stage < MAX_STAGE; stage++) {
410
+
411
+ int x = rand() % 9;
412
+
413
+ int no;
414
+
415
+
416
+
417
+ i = j = 0;
418
+
419
+ while (i < 9) {
420
+
421
+ if (i != x)
422
+
423
+ a[j++] = dgt[i];
424
+
425
+ i++;
426
+
427
+ }
428
+
429
+
430
+
431
+ for (i = 7; i > 0; i--) {
432
+
433
+ int j = rand() % (i + 1);
434
+
435
+ if (i != j)
436
+
437
+ swap(int, a[i], a[j]);
438
+
439
+ }
440
+
441
+
442
+
443
+ printf("%d回目:", stage+1);
444
+
445
+ for (i = 0; i < 8; i++){
446
+
447
+ printf("%d ", a[i]);
448
+
449
+ }
450
+
451
+ printf(":");
452
+
453
+ fflush(stdout);
454
+
455
+
456
+
457
+ do {
458
+
459
+ no = getch();
460
+
461
+ if (isprint(no)) {
462
+
463
+ putch(no);
464
+
465
+ if (no != dgt[x] + '0')
466
+
467
+ putch('\b');
468
+
469
+ else
470
+
471
+ printf("\n");
472
+
473
+ }
474
+
475
+ } while (no != dgt[x] + '0');
476
+
477
+ }
478
+
479
+ end = time(NULL);
480
+
481
+
482
+
483
+ jikan = (double)difftime(end, start);
484
+
485
+
486
+
487
+ printf("%.1f秒かかりました。\n", jikan);
488
+
489
+
490
+
491
+ if (jikan > 25.0)
492
+
493
+ printf("鈍すぎます。\n");
494
+
495
+ else if (jikan > 20.0)
496
+
497
+ printf("少し鈍いですね。\n");
498
+
499
+ else if (jikan > 17.0)
500
+
501
+ printf("まあまあですね。\n");
502
+
503
+ else
504
+
505
+ printf("素早いですね。\n");
506
+
507
+
508
+
509
+ return (jikan);
510
+
511
+ }
512
+
513
+
4
514
 
5
515
  int main(void)
6
516
 
7
517
  {
8
518
 
9
- int retry,ndata,count=0; /* もう一度? */
519
+ int retry,ndata,count=0;
10
-
520
+
11
- double score; /* 今回の所要時間 */
521
+ double score;
12
-
522
+
13
- double best; /* 最短所要時間 */
523
+ double best;
524
+
525
+ double jikan;
14
526
 
15
527
  FILE *fp;
16
528
 
@@ -20,100 +532,272 @@
20
532
 
21
533
  struct tm local ;
22
534
 
23
- int k;
535
+ int k,bango;
536
+
537
+ int i, j, x, stage;
538
+
539
+ int dgt[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
540
+
541
+ int a[10];
542
+
543
+ int no;
544
+
545
+
546
+
547
+ clock_t start, end;
548
+
549
+
550
+
551
+ printf("1. ラックナンバーリサーチ\n");
552
+
553
+
554
+
555
+ printf("2. ダブルナンバーリサーチ\n");
556
+
557
+
558
+
559
+ printf("どのゲームを行いますか。番号を入力してください ==> ");
560
+
561
+
562
+
563
+ scanf("%d", &bango);
564
+
565
+
566
+
567
+ switch (bango)
568
+
569
+ {
570
+
571
+ case 1:
572
+
573
+ ndata= get_data(count);
574
+
575
+
576
+
577
+ printf("ndataは%d\n\n", ndata);
578
+
579
+
580
+
581
+ fp = fopen(dtfile, "rb");
24
582
 
25
583
 
26
584
 
27
- best= get_data(count); // get_data()で前回までの最短所要時間を
28
-
29
- // ファイルから読み込んでbestに代入する。
30
-
31
-
32
-
33
-
34
-
35
- fp = fopen(dtfile, "rb");
585
+ kaime_p(&kaime_p1);
36
586
 
37
587
 
38
588
 
589
+ k=kaime_p1;
590
+
591
+ //ファイルポインタの位置を取得
592
+
593
+ printf("mainのkaime_p(&kaime_p1)後のkの値は「%d」です。\n\n",k);
594
+
595
+
596
+
597
+ //ファイルポインタを末尾まで移動
598
+
599
+ fseek(fp,0,SEEK_END);
600
+
601
+
602
+
603
+ //ファイルポインタの位置を取得
604
+
39
- kaime_p(&kaime_p1);
605
+ fgetpos(fp,&ft);
606
+
607
+
608
+
609
+ new_local_data(ft,k);
40
610
 
41
611
 
42
612
 
43
- k=kaime_p1;
44
-
45
- //ファイルポインタの位置を取得
46
-
47
- printf("mainのkaime_p(&kaime_p1)後のkの値は「%d」です。\n\n",k);
48
-
49
-
50
-
51
- //ファイルポインタを末尾まで移動
52
-
53
- fseek(fp,0,SEEK_END);
613
+ fgetpos(fp,&ft);
614
+
615
+
616
+
54
-
617
+ init_getputch();
618
+
619
+ srand(time(NULL));
620
+
621
+
622
+
623
+ do {
624
+
625
+ score = go();
626
+
627
+
628
+
55
- //ファイルポインタの位置を取得
629
+ if (score < best) {
630
+
56
-
631
+ printf("最短所要時間を更新しました!!\n");
632
+
633
+ best = score;
634
+
635
+ }
636
+
637
+
638
+
57
- fgetpos(fp,&ft); //ファイルfpの現在のファイル位置を取得して
639
+ printf("もう一度しますか … (0)いいえ (1)はい:");
58
-
640
+
59
- // &ftの示す場所に格納します。
641
+ scanf("%d", &retry);
642
+
643
+ } while (retry == 1);
644
+
645
+
646
+
647
+ put_data(score,best);
60
648
 
61
649
 
62
650
 
651
+ fclose(fp);
652
+
63
- new_local_data(ft,k);
653
+ term_getputch();
64
654
 
65
655
 
66
656
 
657
+ break;
658
+
659
+
660
+
661
+ case 2:
662
+
663
+
664
+
665
+ init_getputch();
666
+
667
+ srand(time(NULL));
668
+
669
+
670
+
671
+ printf("ダブっている数字を入力してください。\n");
672
+
673
+ printf("スペースキーで開始します。\n");
674
+
67
- fgetpos(fp,&ft);
675
+ fflush(stdout);
676
+
677
+ while (getch() != ' ')
678
+
679
+ ;
680
+
681
+
682
+
683
+ start = clock();
684
+
685
+ for (stage = 0; stage < MAX_STAGE; stage++) {
686
+
687
+ x = rand() % 9;
688
+
689
+
690
+
691
+ i = j = 0;
692
+
693
+ while (i < 9) {
694
+
695
+ a[j++] = dgt[i];
696
+
697
+ if (i == x)
698
+
699
+ a[j++] = dgt[i];
700
+
701
+ i++;
702
+
703
+ }
704
+
705
+
706
+
707
+ for (i = 9; i > 0; i--) {
708
+
709
+ int j = rand() % (i + 1);
710
+
711
+ if (i != j)
712
+
713
+ swap(int, a[i], a[j]);
714
+
715
+ }
716
+
717
+
718
+
719
+ for (i = 0; i < 10; i++)
720
+
721
+ printf("%d ", a[i]);
722
+
723
+ printf(":");
724
+
725
+ fflush(stdout);
726
+
727
+
728
+
729
+ do {
730
+
731
+ no = getch();
732
+
733
+ if (isprint(no)) {
734
+
735
+ putch(no);
736
+
737
+ if (no != dgt[x] + '0')
738
+
739
+ putch('\b');
740
+
741
+ else
742
+
743
+ printf("\n");
744
+
745
+ }
746
+
747
+ } while (no != dgt[x] + '0');
748
+
749
+ }
750
+
751
+ end = clock();
752
+
753
+
754
+
755
+ jikan = (double)(end - start) / CLOCKS_PER_SEC;
756
+
757
+
758
+
759
+ printf("%.1f秒かかりました。\n", jikan);
760
+
761
+
762
+
763
+ if (jikan > 25.0)
764
+
765
+ printf("鈍すぎます。\n");
766
+
767
+ else if (jikan > 20.0)
768
+
769
+ printf("少し鈍いですね。\n");
770
+
771
+ else if (jikan > 17.0)
772
+
773
+ printf("まあまあですね。\n");
774
+
775
+ else
776
+
777
+ printf("素早いですね。\n");
778
+
779
+
780
+
781
+ term_getputch();
782
+
783
+
784
+
785
+ break;
786
+
787
+
788
+
789
+ default: // 上記以外
790
+
791
+
792
+
793
+ printf("番号が不当です\n");
794
+
795
+
796
+
797
+ }
798
+
799
+ return 0;
68
800
 
69
801
 
70
802
 
71
- //best = get_data(ft);
72
-
73
- // get_data()で前回までの最短所要時間を
74
-
75
- //ファイルから読み込んでbestに代入する。
76
-
77
- init_getputch(); //ライブラリの初期処理235p、initscr(),cbreak(),noecho(),refresh()
78
-
79
- srand(time(NULL)); // 乱数の種を初期化
80
-
81
- do {
82
-
83
- score = go(); // トレーニング(go)で実行、返却された所要時間(jika)をscoreに代入する。
84
-
85
-
86
-
87
- if (score < best) {
88
-
89
- printf("最短所要時間を更新しました!!\n");
90
-
91
- best = score; /* 最高得点更新 */
92
-
93
- }
803
+ }
94
-
95
-
96
-
97
- printf("もう一度しますか … (0)いいえ (1)はい:");
98
-
99
- scanf("%d", &retry);
100
-
101
- } while (retry == 1);
102
-
103
-
104
-
105
- put_data(score,best); /* 今回の日付・時刻・得点を書き込む */
106
-
107
-
108
-
109
- fclose(fp);
110
-
111
- term_getputch(); //ライブラリの初期処理235p,endwin()
112
-
113
-
114
-
115
- return 0;
116
-
117
-
118
-
119
- ```

2

説明の修正

2016/02/27 05:06

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- プログラム実行でmain関数が途中で止まり、enterキーで実行されるのですが、enterキー無しで最後まで実行したいのですが
1
+ プログラム実行でmain関数がbest= get_data(count); の実行後途中で止まり、enterキーで実行されるのですが、enterキー無しで最後まで実行したいのですが
2
2
 
3
3
  どうしてとまるのか分からないので教えてください。
4
4
 

1

best=get_data\(countに修正

2016/02/27 00:05

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -24,11 +24,11 @@
24
24
 
25
25
 
26
26
 
27
- ndata= get_data(count); // get_data()で前回までの最短所要時間を
27
+ best= get_data(count); // get_data()で前回までの最短所要時間を
28
28
 
29
29
  // ファイルから読み込んでbestに代入する。
30
30
 
31
- printf("ndataは%d\n\n", ndata);
31
+
32
32
 
33
33
 
34
34