###前提・実現したいこと
Cで計算問題を解くのに掛かった時間を求めるプログラムを作りたいのですが, 計測時間が必ず0になります。簡単化した関数を作ってみたりしたのですが, 原因が分かりません。ご教授よろしくお願いします。
###発生している問題・エラーメッセージ
Calculate!! 2 + 9 - 5 = -2 Wrong! Retry! 2 + 9 - 5 = 6 8 + 2 + 5 = 15 6 + 8 + 4 = 18 0: 0.0 sec 1: 0.0 sec 2: 0.0 sec Sum: 0.0 sec Average: 0.0 sec
###該当のソースコード
#include<stdio.h> #include<stdlib.h> #include<time.h> #include<string.h> #define MAX 3 double plusplus() { clock_t start, end; int a, b, c; int x; a = 1 + rand() % 9; b = 1 + rand() % 9; c = 1 + rand() % 9; start = clock(); printf("%3d + %3d + %3d = ", a, b, c); do { scanf("%d", &x); if (x == a + b + c) break; printf("Wrong! Retry!\n"); printf("%3d + %3d + %3d = ", a, b, c); } while(1); end = clock(); return (double)(end - start) / CLOCKS_PER_SEC; } double plusminus() { clock_t start = clock(), end; int a, b, c; int x; a = 1 + rand() % 9; b = 1 + rand() % 9; c = 1 + rand() % 9; printf("%3d + %3d - %3d = ", a, b, c); do { scanf("%d", &x); if (x == a + b - c) break; printf("Wrong! Retry!\n"); printf("%3d + %3d - %3d = ", a, b, c); } while(1); end = clock(); return (double)(end - start) / CLOCKS_PER_SEC; } double minusplus() { clock_t start = clock(), end; int a, b, c; int x; a = 1 + rand() % 9; b = 1 + rand() % 9; c = 1 + rand() % 9; printf("%3d - %3d + %3d = ", a, b, c); do { scanf("%d", &x); if (x == a - b + c) break; printf("Wrong! Retry!\n"); printf("%3d - %3d + %3d = ", a, b, c); } while(1); end = clock(); return (double)(end - start) / CLOCKS_PER_SEC; } double minusminus() { clock_t start = clock(), end; int a, b, c; int x; a = 1 + rand() % 9; b = 1 + rand() % 9; c = 1 + rand() % 9; printf("%3d - %3d - %3d = ", a, b, c); do { scanf("%d", &x); if (x == a - b - c) break; printf("Wrong! Retry!\n"); printf("%3d - %3d - %3d = ", a, b, c); } while(1); end = clock(); return (double)(end - start) / CLOCKS_PER_SEC; } int sleep(unsigned long x) { clock_t c1 = clock(), c2; do { if ((c2 = clock()) == (clock_t)-1) return 0; } while (1000.0 * (c2 - c1) / CLOCKS_PER_SEC < x); return 1; } int main() { int i; double t[MAX]; double sum = 0, ave; srand(time(NULL)); printf("Calculate!!\n"); for (i = 0; i < MAX; i++) { switch (rand() % 4) { case 0: t[i] = plusplus(); break; case 1: t[i] = plusminus(); break; case 2: t[i] = minusplus(); break; case 3: t[i] = minusminus(); break; } } for (i = 0; i < MAX; i++) sum += t[i]; ave = sum / MAX; putchar('\n'); for (i = 0; i < MAX; i++) { printf("%2d: %.1f sec\n", i, t[i]); } printf("Sum: %.1f sec\n", sum); printf("Average: %.1f sec\n", ave); return 0; }
###補足情報(言語/FW/ツール等のバージョンなど)
Mac
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2016/08/30 08:13
2016/08/30 08:31 編集
2016/08/30 08:30
退会済みユーザー
2016/08/30 08:34