当日のOHP資料です。
Download : sample-001.c ( SJIS 版 )
/* * 2012/07/13 sample-001.c */ /* * 最初のプログラムは "Hello, World" */ #include <stdio.h> int main ( void ) { printf ( "Hello, World\n" ); /* "Hello, World" という文字列と改行 ( "\n" ) を表示する */ return 0; }
C:\usr\c>sample-001 Hello, World C:\usr\c>
Download : sample-002.c ( SJIS 版 )
/* * 2012/07/13 sample-002.c */ /* * printf を並べれば、沢山の文字列が表示される */ #include <stdio.h> int main ( void ) { printf ( "おはよウナギ\n" ); printf ( "こんにちワン\n" ); printf ( "こんばんワニ\n" ); return 0; }
C:\usr\c>sample-002 おはよウナギ こんにちワン こんばんワニ C:\usr\c>
Download : sample-003.c ( SJIS 版 )
/* * 2012/07/13 sample-003.c */ #include <stdio.h> #include <string.h> /* * 木霊 */ void echo ( char *message ) { if ( !strcmp ( message, "" ) ) { /* もし、メッセージが空っぽならば.. */ printf ( "何か言わないと、言い返せないじゃないか.." ); } else { /* そうじゃなければ、そのまま答える */ printf ( message ); } } void chat ( char *message ) { /* こだまとお喋り */ printf ( "呼掛け : " ); printf ( message ); printf ( "\n" ); /* 呼掛けの言葉 */ printf ( "木霊 : " ); echo ( message ); printf ( "\n" ); /* 木霊の応答 */ } /* * main */ int main ( void ) { chat ( "ヤッホー" ); chat ( "オーイ" ); chat ( "" ); chat ( "生意気なやつだ" ); return 0; }
C:\usr\c>sample-003 呼掛け : ヤッホー 木霊 : ヤッホー 呼掛け : オーイ 木霊 : オーイ 呼掛け : 木霊 : 何か言わないと、言い返せないじゃないか.. 呼掛け : 生意気なやつだ 木霊 : 生意気なやつだ C:\usr\c>
Download : sample-004.c ( SJIS 版 )
/* * 2012/07/13 sample-004.c */ #include <stdio.h> #include <string.h> /* * main */ void recursive ( char *string ) { if ( !strcmp ( string, "" ) ) { /* もし空文字列 ( "" ) ならば */ printf ( "" ); /* これで御仕舞い */ } else { /* そうでなければ、 */ printf ( string ); recursive ( string + 1 ); /* 再帰呼び出しする */ /* +1 を忘れると悲惨な事に.. */ } } int main ( void ) { recursive ( "abcdefg\n" ); recursive ( "xyz\n" ); return 0; }
C:\usr\c>sample-004 abcdefg bcdefg cdefg defg efg fg g xyz yz z C:\usr\c>
Download : sample-005-01.c ( SJIS 版 )
/* * 2012/07/13 sample-005-01.c */ #include <stdio.h> /* * */ #include "ptr.h" /* ptr 関数の宣言が記載されている */ /* * */ void prt ( char *str ) { /* この関数は sample-005.c の main から呼出す */ printf ( str ); }
Download : sample-005.c ( SJIS 版 )
/* * 2012/07/13 sample-005.c */ #include <stdio.h> /* * */ #include "ptr.h" /* ptr 関数の宣言が記載されている */ /* * 別のファイルの関数を呼出す main 関数 */ int main () { prt ( "Hello, World\n" ); // sample-006.c の中に定義されている prt 関数を利用している return 0; }
C:\usr\c>sample-005 Hello, World C:\usr\c>
Download : sample-006.c ( SJIS 版 )
/* * 2012/07/13 sample-006.c */ #include <stdio.h> #include "s_print.h" /* * */ int assign_variable ( int n ) { /* n の値を参照 */ s_print_string ( "n = " ); s_print_int ( n ); s_print_newline(); n = n - 1; /* 代入 */ s_print_string ( "n = " ); s_print_int ( n ); s_print_newline(); n = n - 1; /* 代入 */ s_print_string ( "n = " ); s_print_int ( n ); s_print_newline(); n = n - 1; /* 代入 */ s_print_string ( "n = " ); s_print_int ( n ); s_print_newline(); n = n - 1; /* 代入 */ s_print_string ( "n = " ); s_print_int ( n ); s_print_newline(); n = n - 1; /* 代入 */ s_print_string ( "n = " ); s_print_int ( n ); s_print_newline(); } /* * */ void function_argument ( int n ) { /* n の値を参照 */ s_print_string ( "n = " ); s_print_int ( n ); s_print_newline(); if ( n > 0 ) { function_argument ( n - 1 ); } } /* * */ int main ( void ) { /* * assign_variable */ s_print_string ( "assign_variable ( 5 )\n" ); assign_variable ( 5 ); /* * */ s_print_newline(); /* * function_argument ( 5 ); */ s_print_string ( "function_argument ( 5 )\n" ); function_argument ( 5 ); /* * */ return 0; } /* * */
C:\usr\c>sample-006 assign_variable ( 5 ) n = 5 n = 4 n = 3 n = 2 n = 1 n = 0 function_argument ( 5 ) n = 5 n = 4 n = 3 n = 2 n = 1 n = 0 C:\usr\c>
Download : sample-007.c ( SJIS 版 )
/* * 2012/07/13 sample-007.c */ #include <stdio.h> #include "s_print.h" /* * swap * * 値の交換 : 失敗 */ int swap ( int n, int m ) { s_print_string ( "n = " ); s_print_int ( n ); s_print_string ( ", m = " ); s_print_int ( m ); s_print_newline(); s_print_string ( "swap\n" ); n = m; /* この時点で n の値は失われる.. */ m = n; /* 思った通りにはならない .. */ s_print_string ( "n = " ); s_print_int ( n ); s_print_string ( ", m = " ); s_print_int ( m ); s_print_newline(); } /* * */ int main ( void ) { /* * swap ( 10, 15 ) */ swap ( 10, 15 ); return 0; } /* * */
C:\usr\c>sample-007 n = 10, m = 15 swap n = 15, m = 15 C:\usr\c>
Download : sample-008.c ( SJIS 版 )
/* * 2012/07/13 sample-008.c */ #include <stdio.h> #include "s_print.h" /* * 2 進数 / 10 進数 / 16 進数 */ int main ( void ) { /* * 0 */ s_print_string ( "00000 " ); s_print_int ( 0 ); s_print_string ( " " ); s_print_hex ( 0 ); s_print_newline(); /* * 1 */ s_print_string ( "00001 " ); s_print_int ( 1 ); s_print_string ( " " ); s_print_hex ( 1 ); s_print_newline(); /* * 2 */ s_print_string ( "00010 " ); s_print_int ( 2 ); s_print_string ( " " ); s_print_hex ( 2 ); s_print_newline(); /* * 3 */ s_print_string ( "00011 " ); s_print_int ( 3 ); s_print_string ( " " ); s_print_hex ( 3 ); s_print_newline(); /* * 4 */ s_print_string ( "00100 " ); s_print_int ( 4 ); s_print_string ( " " ); s_print_hex ( 4 ); s_print_newline(); /* * 9 */ s_print_string ( "01001 " ); s_print_int ( 9 ); s_print_string ( " " ); s_print_hex ( 9 ); s_print_newline(); /* * 10 */ s_print_string ( "01010 " ); s_print_int ( 10 ); s_print_string ( " " ); s_print_hex ( 10 ); s_print_newline(); /* * 11 */ s_print_string ( "01011 " ); s_print_int ( 11 ); s_print_string ( " " ); s_print_hex ( 11 ); s_print_newline(); /* * 12 */ s_print_string ( "01100 " ); s_print_int ( 12 ); s_print_string ( " " ); s_print_hex ( 12 ); s_print_newline(); /* * 15 */ s_print_string ( "01111 " ); s_print_int ( 15 ); s_print_string ( " " ); s_print_hex ( 15 ); s_print_newline(); /* * 16 */ s_print_string ( "10000 " ); s_print_int ( 16 ); s_print_string ( " " ); s_print_hex ( 16 ); s_print_newline(); /* * 17 */ s_print_string ( "10001 " ); s_print_int ( 17 ); s_print_string ( " " ); s_print_hex ( 17 ); s_print_newline(); /* * 20 */ s_print_string ( "10100 " ); s_print_int ( 20 ); s_print_string ( " " ); s_print_hex ( 20 ); s_print_newline(); /* * 40 */ s_print_string ( "101000 " ); s_print_int ( 40 ); s_print_string ( " " ); s_print_hex ( 40 ); s_print_newline(); return 0; } /* * */
C:\usr\c>sample-008 00000 0 0 00001 1 1 00010 2 2 00011 3 3 00100 4 4 01001 9 9 01010 10 a 01011 11 b 01100 12 c 01111 15 f 10000 16 10 10001 17 11 10100 20 14 101000 40 28 C:\usr\c>
Download : sample-009.c ( SJIS 版 )
/* * 2012/07/13 sample-009.c */ #include <stdio.h> #include "s_print.h" /* * 10 進 to 2 進数 */ void bin_print ( int n ) { if ( n != 0 ) { bin_print ( n / 2 ); s_print_int ( n % 2 ); } } /* * */ void print_int_and_bin ( int n ) { s_print_int ( n ); s_print_string ( " = " ); bin_print ( n ); s_print_newline(); } /* * */ int main ( void ) { print_int_and_bin ( 1 ); print_int_and_bin ( 2 ); print_int_and_bin ( 3 ); print_int_and_bin ( 10 ); print_int_and_bin ( 100 ); return 0; } /* * */
C:\usr\c>sample-009 1 = 1 2 = 10 3 = 11 10 = 1010 100 = 1100100 C:\usr\c>
/* * 2012/07/13 ptr.h */ #ifndef __PTR_H__ #define __PTR_H__ 1 /* * */ extern void prt ( char *str ); /* * */ #endif
Download : 20120928-01.c ( SJIS 版 )
/* * DATE-DIR-QQQQ.c * 成績を入力して、ヒストグラムと順位を表示する */ /* * */ #define EOS '\0' #include <stdio.h> #include "s_print.h" #include "s_input.h" /* * */ extern void score ( char my, int cs, int ca, int cb, int cc, int cd ); /* * */ void print_bar ( int len ) { if ( len > 0 ) { s_print_char ( '*' ); print_bar ( len - 1 ); } } void print_hist ( int ch, int len ) { s_print_char ( ch ); s_print_char ( ':' ); print_bar ( len ); s_print_newline(); } /* * */ void print_report ( char my, int cs, int ca, int cb, int cc, int cd ) { print_hist ( 'S', cs ); print_hist ( 'A', ca ); print_hist ( 'B', cb ); print_hist ( 'C', cc ); print_hist ( 'D', cd ); switch ( my ) { case 'S': s_print_int ( 1 ); break; case 'A': s_print_int ( cs + 1 ); break; case 'B': s_print_int ( cs + ca + 1 ); break; case 'C': s_print_int ( cs + ca + cb + 1 ); break; case 'D': s_print_int ( cs + ca + cb + cc + 1 ); break; } s_print_string ( "位です。\n" ); } /* * */ void score_with_input ( char my, int cs, int ca, int cb, int cc, int cd, \ char *is ) { if ( is == NULL ) { /* もう入力はない */ print_report ( my, cs, ca, cb, cc, cd ); } else { switch ( *is ) { /* 成績をみる */ case 'S': score ( my, cs + 1, ca, cb, cc, cd ); break; case 'A': score ( my, cs, ca + 1, cb, cc, cd ); break; case 'B': score ( my, cs, ca, cb + 1, cc, cd ); break; case 'C': score ( my, cs, ca, cb, cc + 1, cd ); break; case 'D': score ( my, cs, ca, cb, cc, cd + 1 ); break; default: score ( my, cs, ca, cb, cc, cd ); break; } } } void score ( char my, int cs, int ca, int cb, int cc, int cd ) { score_with_input ( my, cs, ca, cb, cc, cd, s_input_string() ); } /* * */ int main ( int argc, char *argv[] ) { if ( argc == 2 ) { /* 引数が一つだけ指定されるものとする */ switch ( argv[1][0] ) { /* 引数の最初の文字だけみる */ case 'S': case 'A': case 'B': case 'C': case 'D': score ( argv[1][0], 0, 0, 0, 0, 0 ); /* ただしければ処理 */ break; default: printf ( "引数は S, A, B, C, D のいずれかです。\n" ); break; } } else { printf ( "引数が一つ必要です\n" ); } return 0; } /* * */
C:\usr\c\> 20120928-01 引数が一つ必要です C:\usr\c\>