---8.1--- #include #include int main(void) { char *p; gets(p); printf("%s\n", p); return EXIT_SUCCESS; } 修正部分 char s[100]; /* @ */ p = s; /* A */ ---8.2--- #include #include int main(void) { char *p = "STRING"; /* (1) */ char s[] = "STRING"; /* (2) */ printf("#1: %c %c %c\n", *p, *p + 2, *(p + 4)); /* (3) */ printf("#2: %c %c %c\n", p[0], p[0] + 2, p[4]); /* (4) */ p++; /* (5) */ printf("#3: %c %c %c\n", *p, *p + 2, *(p + 4)); /* (6) */ printf("#4: %c %c %c\n", *s, *s + 2, *(s + 4)); /* (7) */ printf("#5: %c %c %c\n", s[0], s[0] + 2, s[4]); /* (8) */ s++; /* (9) */ printf("#6: %c %c %c\n", *s, *s + 2, *(s + 4)); /* (10) */ return EXIT_SUCCESS; } 修正部分 p = s + 1; /* (9) */ printf("#6: %c %c %c\n", *p, *p + 2, *(p + 4)); /* (10) */ ---8.3--- #include #include #include void reverse(char s[]); int main(void) { char str[100]; printf("文字列を入力して下さい:\n"); gets(str); reverse(str); printf("逆転文字列:\n%s\n", str); return EXIT_SUCCESS; } void reverse(char *s) { int w; char *p, *q; for (q = s; *q; q++) ; for (p = s; p < --q; p++) { w = *p; *p = *q; *q = w; } } ---8.4--- #include #include #include void maxmin(int x[], int size, int *max, int *min); int main() { int a[100]; int i, n, max, min; printf("1以上100以下の整数を入力して下さい:"); scanf("%d", &n); assert(1 <= n && n <= 100); for (i = 0; i < n; i++) a[i] = rand(); for (i = 0; i < n; i++) { printf("%6d", a[i]); if ((i + 1) % 10 == 0) printf("\n"); } printf("\n"); maxmin(a, n, &max, &min); printf("最大値は%d,最小値は%dです.\n", max, min); return EXIT_SUCCESS; } void maxmin(int x[], int size, int *max, int *min) { int i, m, n; m = n = x[0]; for (i = 1; i < size; i++) { if (x[i] > m) m = x[i]; else if (x[i] < n) n = x[i]; } *max = m; *min = n; } ---8.5--- #include #include #include int main(int argc, char *argv[]) { int s, i; assert(argc > 1); s = 0; for (i = 1; i < argc; i++) s += atoi(argv[i]); printf("コマンド行引数の和は%dです.\n", s); return EXIT_SUCCESS; } ---8.6--- #include #include #include struct item { struct item *next; int value; }; 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; } for (p = root; p != NULL; p = p->next) /* 全部の出力 */ printf("%8d\n", p->value); return EXIT_SUCCESS; } ---8.7--- #include #include double trapezoid(double (*func)(double), double a, double b, int n); double sample(double x); int main(void) { printf("関数 4 / (1 + x * x) の区間[0, 1]における積分の近似値:\n"); printf("%f\n", trapezoid(&sample, 0.0, 1.0, 10)); return EXIT_SUCCESS; } double trapezoid(double (*func)(double), double a, double b, int n) { double s, w; int i; s = (func(a) + func(b)) / 2.0; w = b - a; for (i = 1; i < n; i++) s += func(a + w * i / n); return s * w / n; } double sample(double x) { return 4.0 / (1.0 + x * x); }