=====第1章 練習問題===== ■1.1 #include #include int main(void) { printf("**************************\n"); printf("* *\n"); printf("* みなさん,こんにちは. *\n"); printf("* *\n"); printf("**************************\n"); return EXIT_SUCCESS; } ■1.2 #include #include int main(void) { double a, b; scanf("%lf%lf", &a, &b); /* 入力書式は %lf */ printf("入力は %f と %f\n", a, b); /* 出力書式は %f */ printf("和は %f\n", a + b); printf("差は %f\n", a - b); printf("積は %f\n", a * b); printf("商は %f\n", a / b); return EXIT_SUCCESS; /* 実数に対して % は定義されない */ } ■1.3 #include #include int main(void) { int mile, yard; double km, ml; scanf("%lf", &km); mile = ml = km / 1.6093; /* mile は ml の整数部分 */ yard = (ml - mile) * 1760; printf("%.2fキロメートルは%dマイル%dヤードです.\n", km, mile, yard); return EXIT_SUCCESS; } ■1.4 #include #include int main(void) { int a, b, c; printf("2桁の整数を考えて下さい.\n"); printf("それを7で割った余りはいくつですか? "); scanf("%d", &a); printf("それを5で割った余りはいくつですか? "); scanf("%d", &b); printf("それを3で割った余りはいくつですか? "); scanf("%d", &c); printf("あなたが考えた数は %d です.\n", (15 * a + 21 * b + 70 * c) % 105); return EXIT_SUCCESS; } =====第2章 練習問題===== ■2.1 #include #include int main(void) { int n, s; printf("入場者数を入力して下さい:"); scanf("%d", &n); if (n < 20) s = 800 * n; else s = 750 * n; printf("入場料の合計は%d円です.\n", s); return EXIT_SUCCESS; } ■2.2 #include #include int main(void) { double m; int f; printf("乗車距離(キロメートル)を入力して下さい:"); scanf("%lf", &m); f = 650; if ((m -= 2.0) > 0.0) f += 80 * (int)((m + 0.5) / 0.6); printf("料金は%d円です.\n", f); return EXIT_SUCCESS; } ■2.3 #include #include int main(void) { int h, w, s; double f, stdw; printf("身長,体重,性別を入力して下さい:"); scanf("%d%d%d", &h, &w, &s); if (s == 1) /* 男 */ stdw = (h - 139) * 0.613 + 42.2; else /* 女 */ stdw = (h - 139) * 0.510 + 43.2; f = (w - stdw) / stdw * 100; printf("あなたの肥満度は%.1fで,", f); if (f >= 20.0) printf("ふとりすぎです.\n"); else if (f >= 10.0) /* else が重要 */ printf("ふとりぎみです.\n"); else if (f > -10.0) printf("ふつうです.\n"); else if (f > -20.0) printf("やせぎみです.\n"); else printf("やせすぎです.\n"); return EXIT_SUCCESS; } ■2.4 #include #include int main(void) { int m; printf("何月ですか? "); scanf("%d", &m); printf("%d月は", m); switch (m) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: printf("31日です.\n"); break; case 2: printf("28日です.\n"); break; case 4: case 6: case 9: case 11: printf("30日です.\n"); break; default: printf("ありません.\n"); } return EXIT_SUCCESS; } =====第3章 練習問題===== ■3.1 #include #include #include int main(void) { int n, i; double s, f; printf("正整数を入力して下さい:"); scanf("%d", &n); assert(n > 0); s = f = 1.0; for (i = 1; i <= n; i++) s += (f /= i); printf("和は%fです.\n", s); return EXIT_SUCCESS; } ■3.2 #include #include int main(void) { int i, j; printf(" "); /* 上の見出し */ for (j = 0; j < 16; j++) printf("%4X", j); printf("\n"); printf(" "); /* 境界線 */ for (j = 0; j < 16; j++) printf("----"); printf("\n"); for (i = 0; i < 16; i++) { printf("%2X |", i); /* 左の見出し */ for (j = 0; j < 16; j++) printf("%4X", i + j); /* 和 */ printf("\n"); } return EXIT_SUCCESS; } ■3.3 #include #include int main(void) { int n, i, c, maxc, maxn; maxc = 0; for (i = 1; i <= 100; i++) { n = i; c = 0; /* 繰り返し回数 */ while (n != 1) { if (n & 1) /* 奇数 */ n = 3 * n + 1; else /* 偶数 */ n >>= 1; c++; } if (c > maxc) { maxc = c; maxn = i; } } printf("%dの寿命が最も長く,%dです.\n", maxn, maxc); return EXIT_SUCCESS; } ■3.4 #include #include int main(void) { int a, b, c, a2, a2b2; for (a = 1; a < 99; a++) { a2 = a * a; for ( b = a + 1; b < 100; b++) { a2b2 = a2 + b * b; for (c = b + 1; c < 101; c++) if (c * c == a2b2) printf("%4d%4d%4d\n", a, b, c); } } return EXIT_SUCCESS; } =====第4章 練習問題===== ■4.1 #include #include #include int main(void) { long pay; int m, d, c; printf("金額を入力して下さい:\n"); scanf("%ld", &pay); assert(pay > 0); m = 10000; d = 2; while (pay) { c = pay / m; pay %= m; if (c) printf("%d円%d枚\n", m, c); m /= d; d = 10 / d; /* d の値は,2, 5, 2, 5, … となる */ } return EXIT_SUCCESS; } ■4.2 #include #include #define MAX 1000 #define NOB 8 #define SIZE (MAX / NOB + 1) #define POS(x) (x / NOB) /* 配列要素の位置 */ #define MASK(x) (1 << (x % NOB)) /* マスク */ int main(void) { char sieve[SIZE]; int i, j, c; /**/ /* 変更した行 */ for (i = 0; i < SIZE; i++) /* 2〜1000を書く */ /**/ sieve[i] = 0; for (i = 2; i * i <= MAX; i++) { /**/ if ((sieve[POS(i)] & MASK(i)) == 0) /* まだ残っている−−素数 */ for (j = 2 * i; j <= MAX; j += i) /**/ sieve[POS(j)] |= MASK(j); /* 倍数を消す */ } for (c = 0,i = 2; i <= MAX; i++) /* 素数の出力 */ /**/ if ((sieve[POS(i)] & MASK(i)) == 0) { printf("%5d", i); if ((++c % 10) == 0) /* 10個で改行 */ printf("\n"); } printf("\n"); return EXIT_SUCCESS; } ■4.3 #include #include #define M 20 #define N 3 int main(void) { int st[M + 1][N]; int i, j, s; for (i = 0; i < M; i++) for (j = 0; j < N; j++) st[i][j] = rand() % 101; printf(" 国語 数学 英語 合計\n"); for (j = 0; j < N; j++) st[M][j] = 0; for (i = 0; i < M; i++) { printf("%2d: ", i + 1); s = 0; for (j = 0; j < N; j++) { printf("%6d", st[i][j]); s += st[i][j]; st[M][j] += st[i][j]; } printf("%6d\n", s); } printf(" "); for (j = 0; j < N; j++) printf("%6.1f", (double)st[M][j] / M); printf("\n"); return EXIT_SUCCESS; } ■4.4 #include #include #include int main(void) { int a[10], n, i, j; printf("1以上10以下の整数を入力して下さい:\n"); scanf("%d", &n); assert(1 <= n && n <= 10); a[0] = 1; for (i = 0; i < n; i++) { a[i] = 1; for (j = i - 1; j > 0; j--) a[j] += a[j - 1]; for (j = 0; j < n - i; j++) printf(" "); for (j = 0; j <= i; j++) printf("%4d", a[j]); printf("\n"); } return EXIT_SUCCESS; } =====第5章 練習問題===== ■5.1 #include #include #define SIZE sizeof x / sizeof(int) int search(int a[], int size, int key); int main(void) { int x[] = {30, 80, 70, 10, 60, 40, 90, 20, 50}; if (search(x, SIZE, 60) >= 0) printf("60はあります.\n"); else printf("60はありません.\n"); if (search(x, SIZE, 55) >= 0) printf("55はあります.\n"); else printf("55はありません.\n"); return EXIT_SUCCESS; } int search(int a[], int size, int key) { int i; for (i = 0; i < size && a[i] != key; i++) ; return i < size ? i : -1; } ■5.2 #include #include #define SIZE sizeof x / sizeof(int) int search(int a[], int size, int key); int main(void) { int x[] = {10, 20, 30, 40, 50, 60, 70, 80, 90}; if (search(x, SIZE, 60) >= 0) printf("60はあります.\n"); else printf("60はありません.\n"); if (search(x, SIZE, 55) >= 0) printf("55はあります.\n"); else printf("55はありません.\n"); return EXIT_SUCCESS; } int search(int a[], int size, int key) { int low, high, middle; low = 0; high = size - 1; while (low <= high) { middle = (low + high) / 2; if (a[middle] == key) return middle; if (a[middle] < key) low = middle + 1; else high = middle - 1; } return -1; } ■5.3 #include #include #include int gcd_a(int x, int y); /* 再帰版 */ int gcd_b(int x, int y); /* 非再帰版 */ int main(void) { int x, y; printf("二つの整数を入力して下さい:"); scanf("%d%d", &x, &y); assert(x >= 0 && y >= 0 && x + y != 0); printf("%dと%dの最大公約数は%dです.\n", x, y, gcd_a(x, y)); printf("%dと%dの最大公約数は%dです.\n", x, y, gcd_b(x, y)); return EXIT_SUCCESS; } /* どちらの方法も,x, y の大小関係は気にする必要がない */ int gcd_a(int x, int y) { return x == 0 || y == 0 ? x + y : gcd_a(y, x % y); } int gcd_b(int x, int y) { int w; while (x && y) { w = x % y; x = y; y = w; } return x + y; } ■5.4 #include #include #include int dgtl_rt(unsigned long n); int main(void) { unsigned long n; printf("負でない整数を入力して下さい:"); scanf("%ld", &n); assert(n >= 0); printf("%ldのdigital rootは%dです.\n", n, dgtl_rt(n)); return EXIT_SUCCESS; } int dgtl_rt(unsigned long n) { return n < 10 ? (int)n : dgtl_rt(n / 10 + n % 10); } =====第6章 練習問題===== ■6.1 #include #include int main(void) { char str[100]; int i, j, c; printf("文字列を入力して下さい:\n"); gets(str); i = j = 0; do { c = str[i++]; if (c != ' ') str[j++] = c; } while (c); printf("空白を削除した文字列は\n%s\nです.\n", str); return EXIT_SUCCESS; } ■6.2 #include #include #include int palin_E(char s[]); int palin_J(char s[]); int main(void) { if (palin_E("madamimadam")) printf("madamimadam は回文です.\n"); else printf("madamimadam は回文ではありません.\n"); if (palin_J("たけやぶやけた")) printf("「たけやぶやけた」は回文です.\n"); else printf("「たけやぶやけた」は回文ではありません.\n"); if (palin_J("ねこのこのこねこ")) printf("「ねこのこのこねこ」は回文です.\n"); else printf("「ねこのこのこねこ」は回文ではありません.\n"); return EXIT_SUCCESS; } int palin_E(char s[]) { int i, j; i = 0; j = strlen(s) - 1; while(i < j && s[i] == s[j]) { i++; j--; } return i >= j; } int palin_J(char s[]) { int i, j; i = 0; j = strlen(s) - 2; while(i < j && s[i] == s[j] && s[i + 1] == s[j + 1]) { i += 2; j -= 2; } return i >= j; } ■6.3 #include #include #include #include void caesar(char s[], char t[], int cnt); int main(void) { char statement[100], cryptogram[100]; int cnt; printf("文字列を入力して下さい:\n"); gets(statement); printf("ずらし量を入力して下さい:\n"); scanf("%d", &cnt); assert(0 < cnt && cnt < 26); caesar(statement, cryptogram, cnt); printf("暗号文:%s\n", cryptogram); return EXIT_SUCCESS; } void caesar(char s[], char t[], int cnt) { int i, j, c; i = j = 0; do { c = s[i++]; if (isalpha(c)) { c = tolower(c) + cnt; if (c > 'z') c -= 26; t[j++] = c; } } while (c); t[j] = '\0'; } ■6.4 #include #include #include void conv(char s[], unsigned long value, int base); int main(void) { char str[100]; unsigned long value; int base; printf("整数を入力して下さい:\n"); scanf("%lu", &value); printf("基底を入力して下さい:\n"); scanf("%d", &base); assert(1 < base && base < 37); conv(str, value, base); printf("10進数%luは,%d進法で%sとなります.\n", value, base, str); return EXIT_SUCCESS; } void conv(char s[], unsigned long value, int base) { static char digit[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; unsigned long v; int len; len = 0; v = value; do { /* 結果の長さを求める */ len++; v /= base; } while (v); s[len] = '\0'; v = value; do { s[--len] = digit[v % base]; v /= base; } while (v); } =====第7章 練習問題===== ■7.1 #include #include #include #define SIZE 20 struct student { char name[11]; int point; }; void gen_str(char s[], int n); void display(struct student x[], int n); int main(void) { struct student group[SIZE], w; int i, j, min; for (i = 0; i < SIZE; i++) { gen_str(group[i].name, rand() % 5 + 6); group[i].point = rand() % 101; } printf("整列前:\n"); display(group, SIZE); for (i = 0; i < SIZE; i++) { min = i; for (j = i + 1; j < SIZE; j++) if (strcmp(group[j].name, group[min].name) < 0) min = j; w = group[i]; group[i] = group[min]; group[min] = w; } printf("整列後:\n"); display(group, SIZE); return EXIT_SUCCESS; } void display(struct student x[], int n) { int i; for (i = 0; i < n; i++) printf("%-15s%4d\n", x[i].name, x[i].point); } void gen_str(char s[], int n) { int i; for (i = 0; i < n; i++) s[i] = rand() % 26 + 'a'; s[i] = '\0'; } ■7.2 #include #include #include #define SIZE 20 struct student { char name[11]; int point_a, point_b; }; void gen_str(char s[], int n); void display(struct student x[], int n); int main(void) { struct student group[SIZE], w; int i, j, max; for (i = 0; i < SIZE; i++) { gen_str(group[i].name, rand() % 5 + 6); group[i].point_a = rand() % 101; group[i].point_b = rand() % 101; } printf("整列前:\n"); display(group, SIZE); for (i = 0; i < SIZE; i++) { max = i; for (j = i + 1; j < SIZE; j++) if (group[j].point_a > group[max].point_a || (group[j].point_a == group[max].point_a && group[j].point_b > group[max].point_b)) max = j; w = group[i]; group[i] = group[max]; group[max] = w; } printf("整列後:\n"); display(group, SIZE); return EXIT_SUCCESS; } void display(struct student x[], int n) { int i; for (i = 0; i < n; i++) printf("%-15s%4d%4d\n", x[i].name, x[i].point_a, x[i].point_b); } void gen_str(char s[], int n) { int i; for (i = 0; i < n; i++) s[i] = rand() % 26 + 'a'; s[i] = '\0'; } ■7.3 #include #include #include struct complex { double re, im; }; struct complex cadd(struct complex a, struct complex b); struct complex csub(struct complex a, struct complex b); struct complex cmult(struct complex a, struct complex b); struct complex cdiv(struct complex a, struct complex b); /* a = 2 + i, b = 3 + 4i のとき,(a + b) / (a - b) を求める. */ int main(void) { struct complex a = {2.0, 1.0}, b = {3.0, 4.0}, c; c = cdiv(cadd(a, b), csub(a, b)); printf("(%f) + (%f)i\n", c.re, c.im); return EXIT_SUCCESS; } struct complex cadd(struct complex a, struct complex b) { struct complex t; t.re = a.re + b.re; t.im = a.im + b.im; return t; } struct complex csub(struct complex a, struct complex b) { struct complex t; t.re = a.re - b.re; t.im = a.im - b.im; return t; } struct complex cmult(struct complex a, struct complex b) { struct complex t; t.re = a.re * b.re - a.im * b.im; t.im = a.re * b.im + a.im * b.re; return t; } struct complex cdiv(struct complex a, struct complex b) { struct complex t; double denom; denom = b.re * b.re + b.im * b.im; assert(denom != 0.0); t.re = b.re; t.im = -b.im; t = cmult(a, t); t.re /= denom; t.im /= denom; return t; } ■7.4 #include #include #include struct point { double x, y; }; struct rect { struct point ul, lr; }; int overlap(struct rect a, struct rect b); int main(void) { struct rect a = {{1.0, 4.0}, {3.0, 1.0}}, b = {{2.0, 2.0}, {5.0, -1.0}}, c = {{-5.0, 2.0}, {-1.0, -1.0}}; if (overlap(a, b)) printf("a と b は重なります.\n"); else printf("a と b は重なりません.\n"); if (overlap(a, c)) printf("a と c は重なります.\n"); else printf("a と c は重なりません.\n"); return EXIT_SUCCESS; } int overlap(struct rect a, struct rect b) { struct rect cover; assert(a.ul.x < a.lr.x && a.ul.y > a.lr.y); assert(b.ul.x < b.lr.x && b.ul.y > b.lr.y); cover.ul.x = a.ul.x < b.ul.x ? a.ul.x : b.ul.x; cover.ul.y = a.ul.y > b.ul.y ? a.ul.y : b.ul.y; cover.lr.x = a.lr.x > b.lr.x ? a.lr.x : b.lr.x; cover.lr.y = a.lr.y < b.lr.y ? a.lr.y : b.lr.y; return (a.lr.x - a.ul.x) + (b.lr.x - b.ul.x) > (cover.lr.x - cover.ul.x) && (a.ul.y - a.lr.y) + (b.ul.y - b.lr.y) > (cover.ul.y - cover.lr.y); } =====第8章 練習問題===== ■8.1 #include #include #include #define SIZE 20 struct student { char *name; int point; }; void gen_str(char s[], int n); void display(struct student x[], int n); int main(void) { struct student group[SIZE], w; int i, j, max; char nm[11]; for (i = 0; i < SIZE; i++) { gen_str(nm, rand() % 5 + 6); group[i].name = malloc(strlen(nm) + 1); strcpy(group[i].name, nm); group[i].point = rand() % 101; } printf("整列前:\n"); display(group, SIZE); for (i = 0; i < SIZE; i++) { max = i; for (j = i + 1; j < SIZE; j++) if (group[j].point > group[max].point) max = j; w = group[i]; group[i] = group[max]; group[max] = w; } printf("整列後:\n"); display(group, SIZE); return EXIT_SUCCESS; } void display(struct student x[], int n) { int i; for (i = 0; i < n; i++) printf("%-15s%4d\n", x[i].name, x[i].point); } void gen_str(char s[], int n) { int i; for (i = 0; i < n; i++) s[i] = rand() % 26 + 'a'; s[i] = '\0'; } ■8.2 #include #include #include char *mystrstr(const char *s, const char *t); int main(void) { if (mystrstr("ABCDEFGHIJ", "EFG")) printf("ABCDEFGHIJ の中に EFG があります.\n"); else printf("ABCDEFGHIJ の中に EFG はありません.\n"); if (mystrstr("ABCDEFGHIJ", "EFX")) printf("ABCDEFGHIJ の中に EFX があります.\n"); else printf("ABCDEFGHIJ の中に EFX はありません.\n"); return EXIT_SUCCESS; } char *mystrstr(const char *s, const char *t) { char *ss, *tt; while (*s) { ss = s; tt = t; while (*tt && *ss == *tt) { ss++; tt++; } if (*tt == '\0') return s; s++; } return NULL; } ■8.3 #include #include #include void insert(char *s, char *p, const char *t); void reverse(char *s, char *t); int main(void) { char s[50], *p; strcpy(s, "ABCDEFG"); p = strchr(s, 'D'); insert(s, p, "XYZ"); printf("ABCDEFG の D の前に XYZ を挿入すると %s となります.\n", s); return EXIT_SUCCESS; } void insert(char *s, char *p, const char *t) { int ls, lt; ls = strlen(s); lt = strlen(t); strcat(s, t); reverse(p, (s += ls) - 1); reverse(s, s + lt - 1); reverse(p, s + lt - 1); } void reverse(char *s, char *t) { int c; while (s < t) { c = *s; *s++ = *t; *t-- = c; } } ■8.4 #include #include #include struct item { struct item *next; int value; }; void output(struct item *p); int main(void) { int n, i; struct item *root, *p, *q, *t; printf("データ数を入力して下さい:"); scanf("%d", &n); assert(n > 0); root = NULL; for (i = 0; i < n; i++) { t = malloc(sizeof(struct item)); /* 要素の割り付け */ t->value = rand(); q = NULL; p = root; /* qとpの間に挿入する */ while (p != NULL && t->value > p->value) { q = p; p = p->next; } t->next = p; if (q != NULL) q->next = t; else root = t; } output(root); return EXIT_SUCCESS; } void output(struct item *p) { if (p) { output(p->next); /* @ */ printf("%8d\n", p->value); /* A */ } /* @,Aを逆にすると昇順になょ?*/ } =====第9章 練習問題===== ■9.1 #include #include #include #include #define SIZE 100 int main(int argc, char *argv[]) { FILE *f; char buf[SIZE + 1]; int lcnt; assert(argc == 3); f = fopen(argv[2], "r"); if (f == NULL) { printf("ファイル%sがオープンできません.\n", argv[2]); return EXIT_FAILURE; } lcnt = 0; while (fgets(buf, SIZE, f)) { lcnt++; if (strstr(buf, argv[1])) printf("%6d: %s", lcnt, buf); } fclose(f); return EXIT_SUCCESS; } ■9.2 #include #include #include #include enum {false, true}; int main(int argc, char *argv[]) { FILE *f; int c, ccnt, lcnt, wcnt, wflag; assert(argc == 2); f = fopen(argv[1], "r"); if (f == NULL) { printf("ファイル%sがオープンできません.\n", argv[1]); return EXIT_FAILURE; } ccnt = lcnt = wcnt = 0; wflag = false; while ((c = fgetc(f)) != EOF) { ccnt++; if (isspace(c)) { wflag = false; if (c == '\n') lcnt++; } else if (!wflag) { wflag = true; wcnt++; } } fclose(f); printf("ファイル %s に含まれる\n", argv[1]); printf(" 文字数は %d,行数は %d,単語数は %d\n", ccnt, lcnt, wcnt); printf("です.\n"); return EXIT_SUCCESS; } ■9.3 #include #include #include #include void output(FILE *f, int c); int main(int argc, char *argv[]) { FILE *inf, *outf; int c; assert(argc == 3); inf = fopen(argv[1], "rb"); if (inf == NULL) { printf("ファイル%sがオープンできません.\n", argv[1]); return EXIT_FAILURE; } outf = fopen(argv[2], "w"); if (outf == NULL) { printf("ファイル%sがオープンできません.\n", argv[2]); return EXIT_FAILURE; } do { c = fgetc(inf); output(outf, c); } while (c != EOF); fclose(inf); fclose(outf); return EXIT_SUCCESS; } void output(FILE *f, int c) { static int shift = 2, cc = 0; static char ch[] = "0123456789abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ+-"; cc <<= 8; if (c != EOF) { cc |= c; fputc(ch[(cc >> shift) & 0x3F], f); shift += 2; if (shift == 8) { fputc(ch[cc & 0x3F], f); shift = 2; } } else { if (shift != 2) fputc(ch[(cc >> shift) & 0x3F], f); fputc('*', f); } } ■9.4 #include #include #include #include void output(FILE *f, int c); int main(int argc, char *argv[]) { FILE *inf, *outf; int c; assert(argc == 3); inf = fopen(argv[1], "r"); if (inf == NULL) { printf("ファイル%sがオープンできません.\n", argv[1]); return EXIT_FAILURE; } outf = fopen(argv[2], "wb"); if (outf == NULL) { printf("ファイル%sがオープンできません.\n", argv[2]); return EXIT_FAILURE; } while ((c = fgetc(inf)) != '*') output(outf, c); fclose(inf); fclose(outf); return EXIT_SUCCESS; } void output(FILE *f, int c) { static int shift = 6, cc = 0; static char ch[] = "0123456789abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ+-"; int i; for (i = 0; i < 64 && c != ch[i]; i++) ; if (i == 64) { printf("正しいファイルではありません.\n"); exit(EXIT_FAILURE); } cc = (cc << 6) | i; if (shift < 6) fputc((cc >> shift) & 0xFF, f); shift -= 2; if (shift < 0) shift = 6; } =====第10章 練習問題===== ■10.1 #include #include #include #define PTFILE "point.cnt" #define MAX 5 struct record { long point; int year, month, day; }; int main(void) { struct record bests[MAX + 1], w; int cnt, i, k; FILE *f; time_t t; struct tm *pt; cnt = 0; f = fopen(PTFILE, "r"); if (f) { while (fscanf(f, "%ld%d%d%d", &w.point, &w.year, &w.month, &w.day) == 4) { bests[0] = w; /* 番兵 */ for (k = cnt; w.point > bests[k].point; k--) ; if (k < MAX) { if (cnt < MAX) cnt++; for (i = cnt - 1; i > k; i--) bests[i + 1] = bests[i]; bests[k + 1] = w; } } printf("現在までのベスト%d:\n", cnt); for (i = 1; i <= cnt; i++) printf("%2d: %ld 点(%d年%d月%d日)\n", i, bests[i].point, bests[i].year, bests[i].month, bests[i].day); } else printf("今回が最初です.\n"); fclose(f); printf("\n今回の得点:"); scanf("%ld", &w.point); time(&t); pt = localtime(&t); f = fopen(PTFILE, "a"); if (f == NULL) { printf("ファイルがオープンできません.\n"); exit(EXIT_FAILURE); } fprintf(f, "%ld %d %d %d\n", w.point, pt->tm_year + 1900, pt->tm_mon + 1, pt->tm_mday); fclose(f); return EXIT_SUCCESS; } ■10.2 #include #include #include #include char *concat(char *s, int n, ...); int main(void) { char s[100]; concat(s, 3, "abc", "12345", "+-*/"); printf("%s\n", s); return EXIT_SUCCESS; } char *concat(char *s, int n, ...) { va_list ap; va_start(ap, n); *s = '\0'; while (n--) { strcat(s, va_arg(ap, char *)); } va_end(ap); return s; } ■10.3 #include #include #include #include long fib_a(int n); long elapse(void); enum {false, true}; int main(void) { int n; long f; printf("0以上45以下の整数を入力して下さい:"); scanf("%d", &n); assert(0 <= n && n <=45) elapse(); f = fib_a(n); printf("fib(%d) = %ldで,計算所要時間は%ld秒です.\n", n, f, elapse()); return EXIT_SUCCESS; } long fib_a(int n) { return n < 2 ? 1 : fib_a(n - 1) + fib_a(n - 2); } long elapse(void) { static int first = true; static time_t t0, t1; if (first) t1 = time(&t0); else time(&t1); first = !first; return (long)difftime(t1, t0); } ■10.4 #include #include #include #define M 10 #define N 25 int mesh[M + 2][M + 2]; void island(int i, int j); int main(void) { int i, j, k, cnt; srand(time(NULL)); for (i = 0; i < M + 2; i++) for (j = 0; j < M + 2; j++) mesh[i][j] = 0; k = 0; while (k < N) { i = rand() % M + 1; j = rand() % M + 1; if (!mesh[i][j]) { mesh[i][j] = 1; k++; } } cnt = 0; for (i = 1; i <= M; i++) { for (j = 1; j <= M; j++) printf("%s", mesh[i][j] == 0 ? "□" : "■"); printf("\n"); } for (i = 1; i <= M; i++) for (j = 1; j <= M; j++) if (mesh[i][j] > 0) { cnt++; island(i, j); } printf("\n島の個数は%dです.\n", cnt); return EXIT_SUCCESS; } void island(int i, int j) { static int v[4] = {1, 0, -1, 0}, h[4] = {0, 1, 0, -1}; int k, i1, j1; mesh[i][j] = -mesh[i][j]; for (k = 0; k < 4; k++) { i1 = i + v[k]; j1 = j + h[k]; if (mesh[i1][j1] > 0) island(i1, j1); } }