当日のOHP資料です。
Download : sample-001.c ( SJIS 版 )
/* * 2011/10/07 sample-001.c */ /* * */ #include <stdio.h> #include "s_print.h" /* * sum ( n ) : 1 〜 n までの整数の和を計算する * * 素直な実装 */ int sum ( int n ) { if ( n == 1 ) { /* n が 1 の時は.. */ return 1; /* sum ( n ) = sum ( 1 ) = 1 である */ } else { /* n > 1 の時は.. */ return sum ( n - 1 ) + n; /* 再帰呼び出しを利用して、一つ前を求める */ } } /* * */ int main ( void ) { /* * sum ( 5 ) の結果を出力 */ s_print_string ( "sum ( 5 ) = " ); s_print_int ( sum ( 5 ) ); s_print_newline(); /* * sum ( 100 ) の結果を出力 */ s_print_string ( "sum ( 100 ) = " ); s_print_int ( sum ( 100 ) ); s_print_newline(); return 0; } /* * */
C:\usr\c\> sample-001 sum ( 5 ) = 15 sum ( 100 ) = 5050 C:\usr\c\>
Download : sample-002.c ( SJIS 版 )
/* * 2011/10/07 sample-002.c */ /* * */ #include <stdio.h> #include "s_print.h" /* * sum ( n ) : 1 〜 n までの整数の和を計算する * * 蓄積型 : 頭からやる */ int sum_i_to_n ( int sum, int i, int n ) { if ( i <= n ) { /* まだ加える物がある */ return sum_i_to_n ( sum + i, i + 1, n ); /* 残りを加える */ } else { /* もう、全てて片付けた */ return sum; /* これまでの和が答 */ } } int sum ( int n ) { return sum_i_to_n ( 0, 1, n ); /* 最初は 0 で、これに 1 〜 n を加える ) */ } /* * */ int main ( void ) { /* * sum ( 5 ) の結果を出力 */ s_print_string ( "sum ( 5 ) = " ); s_print_int ( sum ( 5 ) ); s_print_newline(); /* * sum ( 100 ) の結果を出力 */ s_print_string ( "sum ( 100 ) = " ); s_print_int ( sum ( 100 ) ); s_print_newline(); return 0; } /* * */
C:\usr\c\> sample-002 sum ( 5 ) = 15 sum ( 100 ) = 5050 C:\usr\c\>
Download : sample-003.c ( SJIS 版 )
/* * 2011/10/07 sample-003.c */ /* * */ #include <stdio.h> #include "s_print.h" /* * add ( a, b ) : a+b を計算するだけだが、プリントアウトも行う */ int add ( int a, int b ) { s_print_string ( " add ( " ); s_print_int ( a ); s_print_string ( ", " ); s_print_int ( b ); s_print_string ( " ) = " ); s_print_int ( a + b ); s_print_newline(); return a + b; } /* * sum ( n ) : 1 〜 n までの整数の和を計算する * * 素直な実装 */ int sum ( int n ) { s_print_string ( "sum ( " ); s_print_int ( n ); s_print_string ( " )\n" ); if ( n == 1 ) { /* n が 1 の時は.. */ return 1; /* sum ( n ) = sum ( 1 ) = 1 である */ } else { /* n > 1 の時は.. */ return add ( sum ( n - 1 ), n ); /* 再帰呼び出しを利用して、一つ前を求める */ } } /* * */ int main ( void ) { /* * sum ( 5 ) の結果を出力 */ s_print_string ( "sum ( 5 ) = " ); s_print_int ( sum ( 5 ) ); s_print_newline(); return 0; } /* * */
C:\usr\c\> sample-003 sum ( 5 ) = sum ( 5 ) sum ( 4 ) sum ( 3 ) sum ( 2 ) sum ( 1 ) add ( 1, 2 ) = 3 add ( 3, 3 ) = 6 add ( 6, 4 ) = 10 add ( 10, 5 ) = 15 15 C:\usr\c\>
Download : sample-004.c ( SJIS 版 )
/* * 2011/10/07 sample-004.c */ /* * */ #include <stdio.h> #include "s_print.h" /* * add ( a, b ) : a+b を計算するだけだが、プリントアウトも行う */ int add ( int a, int b ) { s_print_string ( " add ( " ); s_print_int ( a ); s_print_string ( ", " ); s_print_int ( b ); s_print_string ( " ) = " ); s_print_int ( a + b ); s_print_newline(); return a + b; } /* * sum ( n ) : 1 〜 n までの整数の和を計算する * * 蓄積型 : 頭からやる */ int sum_i_to_n ( int sum, int i, int n ) { s_print_string ( "sum_i_to_n ( " ); s_print_int ( sum ); s_print_string ( ", " ); s_print_int ( i ); s_print_string ( ", " ); s_print_int ( n ); s_print_string ( " )\n" ); if ( i <= n ) { /* まだ加える物がある */ return sum_i_to_n ( add ( sum, i ), i + 1, n ); /* 残りを加える */ } else { /* もう、全てて片付けた */ return sum; /* これまでの和が答 */ } } int sum ( int n ) { return sum_i_to_n ( 0, 1, n ); /* 最初は 0 で、これに 1 〜 n を加える ) */ } /* * */ int main ( void ) { /* * sum ( 5 ) の結果を出力 */ s_print_string ( "sum ( 5 ) = " ); s_print_int ( sum ( 5 ) ); s_print_newline(); return 0; } /* * */
C:\usr\c\> sample-004 sum ( 5 ) = sum_i_to_n ( 0, 1, 5 ) add ( 0, 1 ) = 1 sum_i_to_n ( 1, 2, 5 ) add ( 1, 2 ) = 3 sum_i_to_n ( 3, 3, 5 ) add ( 3, 3 ) = 6 sum_i_to_n ( 6, 4, 5 ) add ( 6, 4 ) = 10 sum_i_to_n ( 10, 5, 5 ) add ( 10, 5 ) = 15 sum_i_to_n ( 15, 6, 5 ) 15 C:\usr\c\>
Download : sample-005.c ( SJIS 版 )
/* * 2011/10/07 sample-005.c */ #include <stdio.h> #include "s_print.h" /* * */ #define EOS '\0' /* * */ void output_stars ( char *stars ) { s_print_string ( stars ); /* 先ず出力してから .. */ s_print_newline (); } /* * piramid_before */ void piramid_before ( char *stars ) { if ( *stars != EOS ) { /* 空文字でなければ.. */ output_stars ( stars ); /* 先ず出力してから .. */ piramid_before ( stars + 1 ); /* 再帰呼び出し */ } } /* * piramid_after */ void piramid_after ( char *stars ) { if ( *stars != EOS ) { /* 空文字でなければ.. */ piramid_after ( stars + 1 ); /* 再帰呼び出してから .. */ output_stars ( stars ); /* 後から出力 */ } } /* * */ int main ( void ) { s_print_string ( "after\n" ); piramid_after ( "*****" ); s_print_newline(); s_print_string ( "before\n" ); piramid_before ( "*****" ); s_print_newline(); return 0; } /* * */
C:\usr\c\> sample-005 after * ** *** **** ***** before ***** **** *** ** * C:\usr\c\>
Download : sample-006.c ( SJIS 版 )
/* * 2011/10/07 sample-006.c */ #include <stdio.h> #include "s_print.h" /* * */ #define EOS '\0' /* * */ void output_stars ( char *stars ) { s_print_string ( " output_stars ( " ); s_print_string ( stars ); s_print_string ( " )\n" ); s_print_string ( stars ); /* 先ず出力してから .. */ s_print_newline (); } /* * piramid_before */ void piramid_before ( char *stars ) { s_print_string ( " piramid_before ( " ); s_print_string ( stars ); s_print_string ( " )\n" ); if ( *stars != EOS ) { /* 空文字でなければ.. */ output_stars ( stars ); /* 先ず出力してから .. */ piramid_before ( stars + 1 ); /* 再帰呼び出し */ } } /* * piramid_after */ void piramid_after ( char *stars ) { s_print_string ( " piramid_after ( " ); s_print_string ( stars ); s_print_string ( " )\n" ); if ( *stars != EOS ) { /* 空文字でなければ.. */ piramid_after ( stars + 1 ); /* 再帰呼び出してから .. */ output_stars ( stars ); /* 後から出力 */ } } /* * */ int main ( void ) { s_print_string ( "main()\n" ); s_print_string ( "after\n" ); piramid_after ( "*****" ); s_print_newline(); s_print_string ( "before\n" ); piramid_before ( "*****" ); s_print_newline(); return 0; } /* * */
C:\usr\c\> sample-006 main() after piramid_after ( ***** ) piramid_after ( **** ) piramid_after ( *** ) piramid_after ( ** ) piramid_after ( * ) piramid_after ( ) output_stars ( * ) * output_stars ( ** ) ** output_stars ( *** ) *** output_stars ( **** ) **** output_stars ( ***** ) ***** before piramid_before ( ***** ) output_stars ( ***** ) ***** piramid_before ( **** ) output_stars ( **** ) **** piramid_before ( *** ) output_stars ( *** ) *** piramid_before ( ** ) output_stars ( ** ) ** piramid_before ( * ) output_stars ( * ) * piramid_before ( ) C:\usr\c\>
Download : sample-007.c ( SJIS 版 )
/* * 2011/10/07 sample-007.c */ /* * */ #include <stdio.h> #include "s_print.h" /* * */ int assign ( int n ) { /* n の値を参照 */ s_print_string ( "n = " ); s_print_int ( n ); s_print_newline(); n = 100; /* n に 100 を代入 */ s_print_string ( "n = " ); s_print_int ( n ); s_print_newline(); n = 1 + 2 + 3; /* n に 1 + 2 + 3 を代入 */ s_print_string ( "n = " ); s_print_int ( n ); s_print_newline(); } /* * */ int main ( void ) { /* * assign ( 5 ) */ s_print_string ( "assgin ( 5 )\n" ); assign ( 5 ); /* * */ s_print_newline(); /* * assign ( 5 ) */ s_print_string ( "assgin ( 10 )\n" ); assign ( 10 ); return 0; } /* * */
C:\usr\c\> sample-007 assgin ( 5 ) n = 5 n = 100 n = 6 assgin ( 10 ) n = 10 n = 100 n = 6 C:\usr\c\>
Download : sample-008.c ( SJIS 版 )
/* * 2011/10/07 sample-008.c */ /* * */ #include <stdio.h> #include "s_print.h" /* * なにやら複雑な計算をしたとする.. ( 二乗の計算しかしていないが.. ) */ int any_complex_calculus ( int n ) { s_print_string ( "Function Called !!\n" ); return n * n; } /* * */ int assign_and_refer ( int n, int m ) { /* n の値を参照 */ s_print_string ( "n = " ); s_print_int ( n ); s_print_newline(); n = any_complex_calculus ( m ); s_print_string ( "any_complex_calculus ( " ); s_print_int ( m ); s_print_string ( " ) = " ); s_print_int ( n ); s_print_newline(); s_print_string ( "any_complex_calculus ( " ); s_print_int ( m ); s_print_string ( " ) = " ); s_print_int ( n ); s_print_newline(); } /* * */ int function_call_again ( int n, int m ) { /* n の値を参照 */ s_print_string ( "n = " ); s_print_int ( n ); s_print_newline(); s_print_string ( "any_complex_calculus ( " ); s_print_int ( m ); s_print_string ( " ) = " ); s_print_int ( any_complex_calculus ( m ) ); s_print_newline(); s_print_string ( "any_complex_calculus ( " ); s_print_int ( m ); s_print_string ( " ) = " ); s_print_int ( any_complex_calculus ( m ) ); s_print_newline(); } /* * */ int main ( void ) { /* * assign_and_refer ( 10, 5 ); */ s_print_string ( "assign_and_refer ( 10, 5 )\n" ); assign_and_refer ( 10, 5 ); /* * */ s_print_newline(); /* * function_call_again ( 10, 5 ); */ s_print_string ( "function_call_again ( 10, 5 )\n" ); function_call_again ( 10, 5 ); /* * */ return 0; } /* * */
C:\usr\c\> sample-008 assign_and_refer ( 10, 5 ) n = 10 Function Called !! any_complex_calculus ( 5 ) = 25 any_complex_calculus ( 5 ) = 25 function_call_again ( 10, 5 ) n = 10 any_complex_calculus ( 5 ) = Function Called !! 25 any_complex_calculus ( 5 ) = Function Called !! 25 C:\usr\c\>
Download : sample-009.c ( SJIS 版 )
/* * 2011/10/07 sample-009.c */ /* * */ #include <stdio.h> #include "s_print.h" /* * */ void modify_with_self ( int n ) { /* n の値を参照 */ s_print_string ( "n = " ); s_print_int ( n ); s_print_newline(); n = n * 2; /* 自分の値を利用して次の自分の値を求める事ができる */ s_print_string ( "n = " ); s_print_int ( n ); s_print_newline(); n = n - 10; /* 自分の値を利用して次の自分の値を求める事ができる */ s_print_string ( "n = " ); s_print_int ( n ); s_print_newline(); } /* * */ int main ( void ) { /* * modify_with_self ( 5 ) */ s_print_string ( "modify_with_self ( 5 )\n" ); modify_with_self ( 5 ); /* * */ s_print_newline(); /* * modify_with_self ( -8 ) */ s_print_string ( "modify_with_self ( -8 )\n" ); modify_with_self ( -8 ); /* * */ return 0; } /* * */
C:\usr\c\> sample-009 modify_with_self ( 5 ) n = 5 n = 10 n = 0 modify_with_self ( -8 ) n = -8 n = -16 n = -26 C:\usr\c\>
Download : sample-010.c ( SJIS 版 )
/* * 2011/10/07 sample-010.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-010 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-011.c ( SJIS 版 )
/* * 2011/10/07 sample-011.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-011 n = 10, m = 15 swap n = 15, m = 15 C:\usr\c\>
Download : sample-012.c ( SJIS 版 )
/* * 2011/10/07 sample-012.c */ /* * */ #include <stdio.h> #include "s_print.h" /* * swap * * 値の交換 : 成功 */ int swap ( int n, int m, int w ) { s_print_string ( "n = " ); s_print_int ( n ); s_print_string ( ", m = " ); s_print_int ( m ); s_print_string ( ", w = " ); s_print_int ( w ); s_print_newline(); s_print_string ( "swap\n" ); w = n; /* この時点で w の値は失われるし.. */ n = m; /* n の値も消えてしまうが.. */ m = w; /* w にのこっているので、m,n の交換は可能.. */ s_print_string ( "n = " ); s_print_int ( n ); s_print_string ( ", m = " ); s_print_int ( m ); s_print_string ( ", w = " ); s_print_int ( w ); s_print_newline(); } /* * */ int main ( void ) { /* * swap ( 10, 15, 0 ) */ swap ( 10, 15, 0 ); return 0; } /* * */
C:\usr\c\> sample-012 n = 10, m = 15, w = 0 swap n = 15, m = 10, w = 10 C:\usr\c\>
Download : sample-013.c ( SJIS 版 )
/* * 2011/10/07 sample-013.c */ /* * */ #include <stdio.h> #include "s_print.h" /* * */ void function_argument ( int n ) { /* n の値を参照 */ s_print_string ( "(pre) n = " ); s_print_int ( n ); s_print_newline(); if ( n > 0 ) { function_argument ( n - 1 ); } s_print_string ( "(pos) n = " ); s_print_int ( n ); s_print_newline(); } /* * */ int main ( void ) { /* * function_argument ( 5 ); */ s_print_string ( "function_argument ( 5 )\n" ); function_argument ( 5 ); /* * */ return 0; } /* * */
C:\usr\c\> sample-013 function_argument ( 5 ) (pre) n = 5 (pre) n = 4 (pre) n = 3 (pre) n = 2 (pre) n = 1 (pre) n = 0 (pos) n = 0 (pos) n = 1 (pos) n = 2 (pos) n = 3 (pos) n = 4 (pos) n = 5 C:\usr\c\>
Download : sample-014.c ( SJIS 版 )
/* * 2011/10/07 sample-014.c */ /* * */ #include <stdio.h> #include "s_print.h" /* * */ int assign_variable ( int n ) { /* n の値を参照 */ s_print_string ( "n = " ); s_print_int ( n ); s_print_string ( ", &n = " ); s_print_hex ( &n ); s_print_newline(); n = n - 1; /* 代入 */ s_print_string ( "n = " ); s_print_int ( n ); s_print_string ( ", &n = " ); s_print_hex ( &n ); s_print_newline(); n = n - 1; /* 代入 */ s_print_string ( "n = " ); s_print_int ( n ); s_print_string ( ", &n = " ); s_print_hex ( &n ); s_print_newline(); n = n - 1; /* 代入 */ s_print_string ( "n = " ); s_print_int ( n ); s_print_string ( ", &n = " ); s_print_hex ( &n ); s_print_newline(); n = n - 1; /* 代入 */ s_print_string ( "n = " ); s_print_int ( n ); s_print_string ( ", &n = " ); s_print_hex ( &n ); s_print_newline(); n = n - 1; /* 代入 */ s_print_string ( "n = " ); s_print_int ( n ); s_print_string ( ", &n = " ); s_print_hex ( &n ); s_print_newline(); } /* * */ void function_argument ( int n ) { /* n の値を参照 */ s_print_string ( "n = " ); s_print_int ( n ); s_print_string ( ", &n = " ); s_print_hex ( &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-014 assign_variable ( 5 ) n = 5, &n = bfe43670 n = 4, &n = bfe43670 n = 3, &n = bfe43670 n = 2, &n = bfe43670 n = 1, &n = bfe43670 n = 0, &n = bfe43670 function_argument ( 5 ) n = 5, &n = bfe43670 n = 4, &n = bfe43650 n = 3, &n = bfe43630 n = 2, &n = bfe43610 n = 1, &n = bfe435f0 n = 0, &n = bfe435d0 C:\usr\c\>
Download : sample-015.c ( SJIS 版 )
/* * 2011/10/07 sample-015.c */ /* * */ #include <stdio.h> #include "s_print.h" /* * */ void function_argument ( int n ) { /* n の値を参照 */ s_print_string ( "(pre) n = " ); s_print_int ( n ); s_print_string ( ", &n = " ); s_print_hex ( &n ); s_print_newline(); if ( n > 0 ) { function_argument ( n - 1 ); } s_print_string ( "(post) n = " ); s_print_int ( n ); s_print_string ( ", &n = " ); s_print_hex ( &n ); s_print_newline(); } /* * */ int main ( void ) { /* * function_argument ( 5 ); */ s_print_string ( "function_argument ( 5 )\n" ); function_argument ( 5 ); /* * */ return 0; } /* * */
C:\usr\c\> sample-015 function_argument ( 5 ) (pre) n = 5, &n = bfacb040 (pre) n = 4, &n = bfacb020 (pre) n = 3, &n = bfacb000 (pre) n = 2, &n = bfacafe0 (pre) n = 1, &n = bfacafc0 (pre) n = 0, &n = bfacafa0 (post) n = 0, &n = bfacafa0 (post) n = 1, &n = bfacafc0 (post) n = 2, &n = bfacafe0 (post) n = 3, &n = bfacb000 (post) n = 4, &n = bfacb020 (post) n = 5, &n = bfacb040 C:\usr\c\>
Download : sample-016.c ( SJIS 版 )
/* * 2011/10/07 sample-016.c */ /* * */ #include <stdio.h> #include "s_print.h" /* * swap_sub * * 引数上で値の交換 */ void swap_sub ( int n, int m ) { s_print_string ( "swap_sub " ); s_print_string ( "n = " ); s_print_int ( n ); s_print_string ( ", m = " ); s_print_int ( m ); s_print_newline(); } /* * swap * * 値の交換 */ int swap ( int n, int m ) { s_print_string ( "swap " ); s_print_string ( "n = " ); s_print_int ( n ); s_print_string ( ", m = " ); s_print_int ( m ); s_print_newline(); swap_sub ( m, n ); /* 引数の上では交換可能 */ } /* * */ int main ( void ) { /* * swap ( 10, 15 ) */ swap ( 10, 15 ); return 0; } /* * */
C:\usr\c\> sample-016 swap n = 10, m = 15 swap_sub n = 15, m = 10 C:\usr\c\>
Download : sample-017.c ( SJIS 版 )
/* * 2011/10/07 sample-017.c */ /* * */ #include <stdio.h> #include "s_print.h" /* * swap * * 値の交換 : 局所変数の利用 */ int swap ( int n, int m ) { int w; /* 局所変数 w の宣言 (w には何がはいっているか解らない..) */ s_print_string ( "n = " ); s_print_int ( n ); s_print_string ( ", m = " ); s_print_int ( m ); s_print_newline(); s_print_string ( "swap\n" ); w = n; /* この時点で w の値が初めて意味を持つ */ n = m; /* n の値も消えてしまうが.. w にコピーがある */ m = w; /* w にのこっているので、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-017 n = 10, m = 15 swap n = 15, m = 10 C:\usr\c\>
Download : sample-018.c ( SJIS 版 )
/* * 2011/10/07 sample-018.c */ /* * */ #include <stdio.h> #include "s_print.h" /* * */ int main ( void ) { int w; /* 変数を宣言するだけでは値が定まならない */ s_print_string ( "w = " ); s_print_int ( w ); /* どんな値になるかは神のみ知るぞ.. */ s_print_newline(); return 0; } /* * */
C:\usr\c\> sample-018 w = 1075757044 C:\usr\c\>
Download : sample-019.c ( SJIS 版 )
/* * 2011/10/07 sample-018.c */ /* * */ #include <stdio.h> #include "s_print.h" /* * */ int main ( void ) { int w = 15; /* 初期代入をする */ s_print_string ( "w = " ); s_print_int ( w ); /* 15 である事が保証される */ s_print_newline(); return 0; } /* * */
C:\usr\c\> sample-019 w = 15 C:\usr\c\>
Download : sample-020.c ( SJIS 版 )
/* * 2011/10/07 sample-020.c */ #include <stdio.h> #include "s_print.h" #include "s_input.h" /* * */ int main ( int argc, char *argv[] ) { int m; /* 変数 m の宣言 */ int n; /* 変数 n の宣言 */ int wa; /* 変数 wa の宣言 */ /* * m,n の値の入力 */ s_print_string ( "imput m = " ); m = s_input_int(); s_print_string ( "imput n = " ); n = s_input_int(); /* * wa の計算 */ wa = m + n; /* * wa の結果出力 */ s_print_int ( m ); s_print_string ( " と " ); s_print_int ( n ); s_print_string ( " の和は " ); s_print_int ( result ); s_print_string ( " です。\n" ); return 0; } /* * */
13 4
C:\usr\c\> sample-020< sample-020.in w = 15 C:\usr\c\>
Download : sample-021.c ( SJIS 版 )
/* * 2011/10/07 sample-021.c * * 演習問題と同じ内容を、代入文なしで作った場合 */ #include <stdio.h> #include "s_print.h" #include "s_input.h" /* * */ int prompt ( char *var_name ) { s_print_string ( "imput " ); s_print_string ( var_name ); s_print_string ( " = " ); return s_input_int(); } /* * */ void print_result ( int m, int n, char *calc, int result ) { s_print_int ( m ); s_print_string ( " と " ); s_print_int ( n ); s_print_string ( " の" ); s_print_string ( calc ); s_print_string ( "は " ); s_print_int ( result ); s_print_string ( " です。\n" ); } /* * */ void four_calc_sub ( int m, int n ) { print_result ( m, n, "和", m + n ); print_result ( m, n, "差", m - n ); print_result ( m, n, "積", m * n ); print_result ( m, n, "商", m / n ); print_result ( m, n, "余り", m % n ); } /* * */ void four_calc ( int m ) { four_calc_sub ( m, prompt ( "n" ) ); } /* * */ int main ( int argc, char *argv[] ) { four_calc ( prompt ( "m" ) ); return 0; } /* * */
13 4
C:\usr\c\> sample-021< sample-021.in imput m = 13 imput n = 4 13 と 4 の和は 17 です。 13 と 4 の差は 9 です。 13 と 4 の積は 52 です。 13 と 4 の商は 3 です。 13 と 4 の余りは 1 です。 C:\usr\c\>
Download : sample-022.c ( SJIS 版 )
/* * 2011/10/07 sample-022.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 = n + m; m = n - m; n = n - m; 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-022 n = 10, m = 15 swap n = 15, m = 10 C:\usr\c\>
Download : 20111007-01.c ( SJIS 版 )
/* * 20111007-1-QQQQ.c * 二つの整数型の変数 m,n を宣言し、それにキーボードから値を入力する * 五つの整数型の変数 wa, sa, seki, sho, amari を宣言し、それに m と n の * 和、差、積、商、余りを計算した結果を代入する。 * 変数 wa, sa, seki, sho, amari の結果を出力する */ #include <stdio.h> #include "s_print.h" #include "s_input.h" /* * */ void print_result ( int m, int n, char *calc, int result ) { s_print_int ( m ); s_print_string ( " と " ); s_print_int ( n ); s_print_string ( " の" ); s_print_string ( calc ); s_print_string ( "は " ); s_print_int ( result ); s_print_string ( " です。\n" ); } /* * */ int main ( int argc, char *argv[] ) { int m; /* 変数 m の宣言 */ int n; /* 変数 n の宣言 */ /* ** この部分を完成させなさい */ /* * m,n の値の入力 */ s_print_string ( "imput m = " ); m = s_input_int(); /* ** この部分を完成させなさい */ /* * wa, sa, seki, sho, amari の計算 */ wa = m + n; sa = m - n; /* ** この部分を完成させなさい */ amari = m % n; /* * wa, sa, seki, sho, amari の結果出力 */ print_result ( m, n, "和", wa ); print_result ( m, n, "差", sa ); /* ** この部分を完成させなさい */ return 0; } /* * */
13 4
C:\usr\c\> 01< 01.in imput m = 13 imput n = 4 13 と 4 の和は 17 です。 13 と 4 の差は 9 です。 13 と 4 の積は 52 です。 13 と 4 の商は 3 です。 13 と 4 の余りは 1 です。 C:\usr\c\>