2002 年度「応用数理C3」「計算数学1」 2002-04-17 (水) 岩瀬順一 == mystrcmp.c ================================================================= #include int mystrcmp(char *s, char *t); main() { printf("%d\n", mystrcmp("abc", "abb")); return 0; } /* 文字列 s が t より小(resp. 大)なら負(resp. 正)の数を返す (K&R2 p.130) */ int mystrcmp(char *s, char *t) { for ( ; *s == *t; s++, t++) { if (*s == '\0') { return 0; } } return *s - *t; } =============================================================================== 以下は http://wwwedu.ipc.kanazawa-u.ac.jp/%7Eiwase/mirror/96comp/010 http://www5a.biglobe.ne.jp/%7Eiwase47/96comp/010 および http://wwwedu.ipc.kanazawa-u.ac.jp/%7Eiwase/mirror/96comp/011 http://www5a.biglobe.ne.jp/%7Eiwase47/96comp/011 からの抜粋です。 == cmdline.c ================================================================== #include main(int argc, char *argv[]) { int i; printf("argc は %d です.\n", argc); for (i = 0; i <= argc; i++) { printf("第 %d 番目の引数は \"%s\" です.\n", i, argv[i]); } } =============================================================================== IPE2050{iwase}60% ./a.out -3 abc def "uvw xyz" argc は 5 です. 第 0 番目の引数は "./a.out" です. 第 1 番目の引数は "-3" です. 第 2 番目の引数は "abc" です. 第 3 番目の引数は "def" です. 第 4 番目の引数は "uvw xyz" です. 第 5 番目の引数は "(null)" です. == complex.c ================================================================== #include struct complex { /* 新しい型 struct complex を定義 */ double re; double im; }; /* ←このセミコロンを忘れやすいので注意 */ typedef struct complex Complex; /* それを以下 Complex と呼ぶ */ void print(Complex z); /* 複素数を出力 */ Complex add(Complex z1, Complex z2); /* 複素数の足し算 */ void print(Complex z) { printf("(%f + %f i)", z.re, z.im); } Complex add(Complex z1, Complex z2) { Complex tmp; tmp.re = z1.re + z2.re; tmp.im = z1.im + z2.im; return tmp; } int main() { Complex z1 = { 3.0, 7.0 }; /* 構造体の初期化 */ Complex z2 = { 4.0, -2.0 }; print(z1); printf(" + "); print(z2); printf(" = "); print(add(z1, z2)); putchar('\n'); return 0; } =============================================================================== == mycopy1.c ================================================================== #include int main() { FILE *in, *out; int c; in = fopen("test.txt", "rt"); /* ファイルを読み出し用にオープン */ if (in == NULL) { fprintf(stderr, "test.txt をオープンできません.\n"); return 1; } out = fopen("test._o_", "wt"); /* ファイルを書き込み用にオープン */ if (out == NULL) { fprintf(stderr, "test._o_ をオープンできません.\n"); fclose(in); return 1; } while ((c = fgetc(in)) != EOF) { /* ファイルから1バイト読み出す */ if (fputc(c, out) == EOF) { /* ファイル に 1バイト書き込む */ fprintf(stderr, "書き込みエラーです.\n"); fclose(in); fclose(out); return 1; } } fclose(in); fclose(out); /* ファイルをクローズ */ return 0; } =============================================================================== == mycopy2.c ================================================================== #include char buf[128+1]; int main() { FILE *in; FILE *out; if ((in = fopen("test.txt", "rt")) == NULL) { /* 読み出し用にオープン */ fprintf(stderr, "test.txt をオープンできません.\n"); return 1; } if ((out = fopen("test._o_", "wt")) == NULL) { /* 書き込み用にオープン */ fprintf(stderr, "test._o_ をオープンできません.\n"); fclose(in); return 1; } while (fgets(buf, 128+1, in) != NULL) { /* 一行読み出す */ if (fputs(buf, out) == EOF) { /* 一行書き込む */ fprintf(stderr, "書き込みエラーです.\n"); fclose(in); fclose(out); return 1; } } fclose(in); fclose(out); /* ファイルをクローズ */ return 0; } ===============================================================================  ※ コマンドライン引数でファイル名を指定したい場合は, int main(int argc, char *argv[]) { /* ... */ in = fopen(argv[1], "rt"); out = fopen(argv[2], "wt"); /* ... */ }    のように書くことになる。