Download : sample-001.c ( utf8 版 )
/*
* 2016/07/08 sample-001.c
*/
/*
* 型の違い
*
* 利用方法
* コンパイル
* cc -I ~/c/include -o sample-001.exe sample-001.c
* 実行
* ./sample-001.exe
*/
#include <stdio.h>
#include "s_input.h"
#include "s_print.h"
/*
* main 関数
*/
int main ( void ) {
/*
* 型が違っても表示は同じ
*/
s_print_string ( "整数 : " );
s_print_int ( 9 );
s_print_newline();
s_print_string ( "文字 : " );
s_print_char ( '9' );
s_print_newline();
s_print_string ( "文字列 : " );
s_print_string ( "9" );
s_print_newline();
/*
* 整数の場合の +1
*/
s_print_string ( "整数の計算 : 9 + 1 = " );
s_print_int ( 9 + 1 );
s_print_newline();
/*
* 文字の場合の +1
*/
s_print_string ( "文字の計算 : '9' + 1 = " );
s_print_char ( '9' + 1 );
s_print_newline();
/*
* 文字列の場合の +1
*/
s_print_string ( "文字の計算 : \"9\" + 1 = " );
s_print_string ( "9" + 1 );
s_print_newline();
/*
* 型によって、計算結果が異る
*/
return 0;
}
$ ./sample-001.exe 整数 : 9 文字 : 9 文字列 : 9 整数の計算 : 9 + 1 = 10 文字の計算 : '9' + 1 = : 文字の計算 : "9" + 1 = $
Download : sample-002.c ( utf8 版 )
/*
* 2016/07/08 sample-002.c
*/
/*
* 整数の四則計算式
*
* 利用方法
* コンパイル
* cc -I ~/c/include -o sample-002.exe sample-002.c
* 実行
* sample-002
*/
#include <stdio.h>
#include "s_input.h"
#include "s_print.h"
/*
* main 関数
*/
int main ( void ) {
/*
* 加法(足し算)
*/
s_print_string ( "12 + 5 = " );
s_print_int ( 12 + 5 ); /* 整数の 12 と 5 を加える */
s_print_newline();
/*
* 減法(引き算)
*/
s_print_string ( "12 - 5 = " );
s_print_int ( 12 - 5 ); /* 整数の 12 から 5 を引く */
s_print_newline();
/*
* 乗法(かけ算)
*/
s_print_string ( "12 * 5 = " );
s_print_int ( 12 * 5 ); /* 整数の 12 に 5 をかける */
s_print_newline();
/*
* 商法(割り算)
*/
s_print_string ( "12 / 5 = " );
s_print_int ( 12 / 5 ); /* 整数の 12 を 5 で割る */
s_print_newline();
/*
* 式の優先順位やかっこも利用可能
*/
s_print_string ( "1 + 2 * 3 = " );
s_print_int ( 1 + 2 * 3 ); /* かけ算が優先される */
s_print_newline();
s_print_string ( "(1 + 2) * 3 = " );
s_print_int ( (1 + 2) * 3 ); /* かっこの中の計算が優先される */
s_print_newline();
return 0;
}
$ ./sample-002.exe 12 + 5 = 17 12 - 5 = 7 12 * 5 = 60 12 / 5 = 2 1 + 2 * 3 = 7 (1 + 2) * 3 = 9 $
Download : sample-003.c ( utf8 版 )
/*
* 2016/07/08 sample-003.c
*/
/*
* 文字列の演算式
*
* 利用方法
* コンパイル
* cc -I ~/c/include -o sample-003.exe sample-003.c
* 実行
* sample-003
*/
#include <stdio.h>
#include "s_input.h"
#include "s_print.h"
/*
* main 関数
*/
int main ( void ) {
/*
* 文字列への加法
*/
s_print_string ( "\"abc\" + 1 = " );
s_print_string ( "abc" + 1 ); /* 文字列が短くなる */
s_print_newline();
/*
* 先頭の文字の取出し
*/
s_print_string ( "*\"abc\" = " );
s_print_char ( *"abc" ); /* 先頭の文字が出て来る */
s_print_newline();
return 0;
}
$ ./sample-003.exe "abc" + 1 = bc *"abc" = a $
Download : sample-004.c ( utf8 版 )
/*
* 2016/07/08 sample-004.c
*/
/*
* 関数呼出しを含む形の「式」
*
* 利用方法
* コンパイル
* cc -I ~/c/include -o sample-004.exe sample-004.c
* 実行
* sample-004
*/
#include <stdio.h>
#include "s_input.h"
#include "s_print.h"
/*
* main 関数
*/
int main ( void ) {
/*
* 加法(足し算)
*/
s_print_string ( "12 + 5 = " );
s_print_int ( 12 + 5 ); /* 整数の 12 と 5 を加える */
s_print_newline();
/*
* 減法(引き算)
*/
s_print_string ( "12 - 5 = " );
s_print_int ( 12 - 5 ); /* 整数の 12 から 5 を引く */
s_print_newline();
/*
* 乗法(かけ算)
*/
s_print_string ( "12 * 5 = " );
s_print_int ( 12 * 5 ); /* 整数の 12 に 5 をかける */
s_print_newline();
/*
* 商法(割り算)
*/
s_print_string ( "12 / 5 = " );
s_print_int ( 12 / 5 ); /* 整数の 12 を 5 で割る */
s_print_newline();
return 0;
}
$ ./sample-004.exe 12 + 5 = 17 12 - 5 = 7 12 * 5 = 60 12 / 5 = 2 $
Download : sample-005.c ( utf8 版 )
/*
* 2016/07/08 sample-005.c
*/
#include <stdio.h>
#include "s_print.h"
#include "s_input.h"
/*
正の整数をいくつかキーボードから入力し、
その総和を出力する
ただし、最後に、0 以下の数を入力する
入力は、関数を呼び出す前に行われ、引数に渡される
目的 : 入力(したデータ)を、
終了のためのチェック
加えるデータ
の二ヶ所で利用したい
入力したデータ二ヶ所で利用したいは関数に引数として
わたして、その引数変数を経由して利用するパターンを使う
(注意) 後で、「代入」を学ぶと、もっと簡単になる
引数 :
int pre_input
関数を呼び出すまえに入力されたデータ
int total
これまで、入力されたデータの総和が入っている
入力の例
1 2 3 4 -1
-> 結果の総和として 10 が表示される
*/
void input_sum ( int pre_input, int total ) {
if ( pre_input <= 0 ) { /* 加えるデータはもうない */
/* お仕舞いなので.. */
s_print_string ( "総和は " );
s_print_int ( total );
s_print_string ( " です。\n" );
} else { /* 入力が正なので、まだやる可能性がある */
input_sum ( s_input_int(), total + pre_input );
/*
total にはこれまでの和
pre_input には、今回の入力
なので
ここまでの総和は
total + pre_input
になる
*/
}
}
int main(void) {
input_sum( s_input_int(), 0 );
return 0;
}
12 83 40 58 42 0
$ ./sample-005.exe < sample-005.in 12 83 40 58 42 0 総和は 235 です。 $
Download : sample-006.c ( utf8 版 )
/*
* 2016/07/08 sample-006.c
*/
#include <stdio.h>
#include "s_print.h"
void print_n_random ( int n ) {
if ( n <= 0 ) {
} else {
/* ここから */
s_print_int ( rand() );
s_print_char ( '\n' );
/* ここまでが、 n 回繰り返される */
print_n_random ( n - 1 );
}
}
int main(void) {
print_n_random ( 10 ); /* 10 個の乱数を出力 */
return 0;
}
$ ./sample-006.exe 1804289383 846930886 1681692777 1714636915 1957747793 424238335 719885386 1649760492 596516649 1189641421 $
Download : sample-007.c ( utf8 版 )
/*
* 2016/07/08 sample-007.c
*/
#include <stdio.h>
#include "s_print.h"
#include "s_input.h"
int main(void) {
s_print_string ( "乱数の seed(種) を入力してください : " );
srand(s_input_int()); /* キーボードから seed を入力 */
s_print_string ( "1回目 : " );
s_print_int ( rand() );
s_print_char ( '\n' );
s_print_string ( "2回目 : " );
s_print_int ( rand() );
s_print_char ( '\n' );
s_print_string ( "3回目 : " );
s_print_int ( rand() );
s_print_char ( '\n' );
return 0;
}
1234
$ ./sample-007.exe < sample-007.in 乱数の seed(種) を入力してください : 1234 1回目 : 479142414 2回目 : 465566339 3回目 : 961126155 $
Download : sample-008.c ( utf8 版 )
/*
* 2016/07/08 sample-008.c
*/
#include <stdio.h>
#include "s_print.h"
#include "s_input.h"
int main(void) {
srand(time(NULL)); /* プログラムの実行開始時間を種にする */
//srand(0); /* プログラムをチェックする時は乱数列を固定 */
s_print_string ( "1回目 : " );
s_print_int ( rand() );
s_print_char ( '\n' );
s_print_string ( "2回目 : " );
s_print_int ( rand() );
s_print_char ( '\n' );
s_print_string ( "3回目 : " );
s_print_int ( rand() );
s_print_char ( '\n' );
return 0;
}
$ ./sample-008.exe 1回目 : 1836925604 2回目 : 841705533 3回目 : 1804436748 $
Download : sample-009.c ( utf8 版 )
/*
* 2016/07/08 sample-009.c
*/
#include <stdio.h>
#include "s_input.h"
#include "s_print.h"
/*
プログラムが考えた数 ( 1 〜 100 の乱数 ) を
人間が、予想して、当てるというゲームプログラム
入力する個数は、当るまでなので、何度も入力する必要がある
-> input loop を使う
問題を作るのに乱数を利用する
*/
void game( int input, int question ) {
if ( input == question ) { /* 予想が当っている */
s_print_string ( "おめでとう、ございます。当たりです\n" );
} else {
if ( input > question ) {
s_print_string ( "大きすぎます。\n" );
} else {
s_print_string ( "ちいさすぎます。\n" );
}
s_print_string ( "もう一度予想してください : " );
game( s_input_int(), question );
}
}
int main(void) {
srand ( time(NULL) ); /* 現在の時刻を利用して乱数を初期化*/
/* 毎回異る問題になる */
s_print_string ( "コンピュータが考えた 1 〜 100 の数を予想して入力してください : " );
game ( s_input_int(), (rand()%100)+1 );
}
コンピュータが考えた 1 〜 100 の数を予想して入力してください : 40 40 ちいさすぎます。 もう一度予想してください : 60 60 ちいさすぎます。 もう一度予想してください : 80 80 大きすぎます。 もう一度予想してください : 70 70 大きすぎます。 もう一度予想してください : 65 65 ちいさすぎます。 もう一度予想してください : 68 68 大きすぎます。 もう一度予想してください : 67 67 大きすぎます。 もう一度予想してください : 66 66 おめでとう、ございます。当たりです
Download : sample-010.c ( utf8 版 )
/*
* 2016/07/08 sample-010.c
*/
#include <stdio.h>
#include "s_input.h"
#include "s_print.h"
/*
sample-009.c とは逆に、
人間が数を考え、
コンピュータが当てる
*/
/*
考えかた
答えが、区間 [a,b] の中にあるとして、
答の案として x = (a+b)/2 を言う
もし、x が大きい場合は、答えは
区間 [a,x-1] の中にある
逆に、そうでない場合は
区間 [x+1,b] の中にある
にある事がわかる
これをヒントに考える
ルール
コンピュータの予想がただしい時は 0
コンピュータの予想が大きいときは 1
コンピュータの予想が小さいときは 2
を答える
*/
void game ( int ans, int a, int b, int x ) {
if ( ans == 0 ) { /* 当たり */
s_print_string ( "やっぱり " );
s_print_int ( x );
s_print_string ( " でしたか.. 当って嬉しいです\n" );
} else {
if ( ans == 1 ) { /* 予想が大きい */
/* 答えは、区間 [a, x-1] にある.. */
s_print_string ( "私の予想は " );
s_print_int ( (a+(x-1))/2 );
s_print_string ( " です。いかがでしょうか : " );
game(s_input_int(), a, x-1, (a+(x-1))/2 );
} else { /* ans == 2 のはず.. 予想が小さい */
/* 答えは、区間 [x+1,b] にある.. */
s_print_string ( "私の予想は " );
s_print_int ( ((x+1)+b)/2 );
s_print_string ( " です。いかがでしょうか : " );
game(s_input_int(), x+1, b, ((x+1)+b)/2 );
}
}
}
int main(void) {
s_print_string ( "1 〜 100 数を考えてください\n" );
s_print_string ( "私が、その数を予想します\n" );
s_print_string ( "私の予想は 50 です。いかがでしょうか : " );
game ( s_input_int(), 1, 100, 50 );
}
1 2 1 0
$ ./sample-010.exe < sample-010.in 1 〜 100 数を考えてください 私が、その数を予想します 私の予想は 50 です。いかがでしょうか : 1 私の予想は 25 です。いかがでしょうか : 2 私の予想は 37 です。いかがでしょうか : 1 私の予想は 31 です。いかがでしょうか : 0 やっぱり 31 でしたか.. 当って嬉しいです $
Download : sample-011.c ( utf8 版 )
/*
* 2016/07/08 sample-011.c
*/
#include <stdio.h>
#include "s_print.h"
#include "s_input.h"
/*
2 のべき乗を順に n 個出力する
*/
void power ( int n, int b ) {
if ( n <= 0 ) {
} else {
s_print_int ( b );
s_print_char ( '\n' );
power ( n - 1, b * 2 );
}
}
int main(void ) {
power ( s_input_int(), 2 );
return 0;
}
10
$ ./sample-011.exe < sample-011.in 10 2 4 8 16 32 64 128 256 512 1024 $
Download : sample-012.c ( utf8 版 )
/*
* 2016/07/08 sample-012.c
*/
#include <stdio.h>
#include "s_input.h"
#include "s_print.h"
int main(void) {
s_print_double ( 1.234 ); /* 小数点を含む数が扱える */
s_print_char ( '\n' );
return 0;
}
$ ./sample-012.exe 1.234000 $
Download : sample-013.c ( utf8 版 )
/*
* 2016/07/08 sample-013.c
*/
#include <stdio.h>
#include "s_input.h"
#include "s_print.h"
int main(void) {
s_print_double ( s_input_double() );
/* 小数点を含む数を入力して、そのまま出力 */
/* 整数の時には s_print_int ( s_input_int() ); だった */
s_print_char ( '\n' );
return 0;
}
12.34
$ ./sample-013.exe < sample-013.in 12.340000 12.340000 $
Download : sample-014.c ( utf8 版 )
#include <stdio.h>
#include "s_input.h"
#include "s_print.h"
int main(void) {
s_print_double ( s_input_double() * 1.08 );
/* 小数点を含む数を入力して、消費税込みの値段に.. */
s_print_char ( '\n' );
return 0;
}
123
$ ./sample-014.exe < sample-014.in 123.000000 132.840000 $
Download : sample-015.c ( utf8 版 )
/*
* 2016/07/08 sample-015.c
*/
#include <stdio.h>
#include "s_print.h"
#include "s_input.h"
#include "s_random.h"
int main(void) {
s_init_random(); /* 時計を使って、乱数列を初期化 */
s_print_string ( "1回目 : " );
s_print_int ( s_dice() );
s_print_char ( '\n' );
s_print_string ( "2回目 : " );
s_print_int ( s_dice() );
s_print_char ( '\n' );
s_print_string ( "3回目 : " );
s_print_int ( s_dice() );
s_print_char ( '\n' );
return 0;
}
$ ./sample-015.exe 1回目 : 3 2回目 : 5 3回目 : 2 $
/*
* CDATE FILENAME
*
* 二つの整数値をキーボードから入力し、その四則並びに余りを出力する
*/
#include <stdio.h>
#include "s_input.h"
#include "s_print.h"
/*
* print_int_result
*
* print_int_result ( "和", 123, 456, '+', 579 )
* => 「和 : 123 + 456 = 679」
*/
void print_int_result ( char *name, int a, int b, char op, int value ) {
s_print_string ( name ); /* 和 */
s_print_string ( " : " ); /* : */
s_print_int ( a ); /* 123 */
s_print_char( op ); /* + */
s_print_int( b ); /* 456 */
s_print_char ( '=' ); /* = */
s_print_int ( value ); /* 579 */
s_print_newline();
}
/*
* print_int_calc
* 四則(和、差、積、商)、余り
*/
void print_int_calc ( int a, int b ) {
print_int_result ( "和", a, b, '+', a + b );
print_int_result ( "差", a, b, '-', a - b );
print_int_result ( "積", a, b, '*', a / b );
/* 商 */
print_int_result ( "余り", a, b, '%', a % b );
}
/*
* print_int_calc_1
*/
void print_int_calc_1 ( int a ) {
print_int_calc ( a, s_input_int() );
}
/*
* main
*/
int main( int argc, char *argv[] )
{
print_int_calc_1 ( s_input_int() );
/*
print_int_calc ( s_input_int(), s_input_int() ):
*/
return 0;
}
/*
* CDATE FILENAME
*
* 二つの浮動小数点数値をキーボードから入力し、その四則を出力する
*/
#include <stdio.h>
#include "s_input.h"
#include "s_print.h"
/*
* print_double_result
*/
void print_double_result ( char *name, double a, double b, char op, double value ) {
s_print_string ( name );
s_print_string ( " : " );
s_print_double ( a );
/*
** この部分を完成させなさい
*/
s_print_char ( '=' );
s_print_double ( value );
s_print_newline();
}
/*
* print_double_calc
*/
void print_double_calc ( double a, double b ) {
print_double_result ( "和", a, b, '+', a + b );
print_double_result ( "差", a, b, '-', a - b );
/*
** この部分を完成させなさい
*/
}
/*
* print_double_calc_1
*/
void print_double_calc_1 ( double a ) {
print_double_calc ( a, s_input_double() );
}
/*
* main
*/
int main( double argc, char *argv[] )
{
print_double_calc_1 ( s_input_double() );
return 0;
}
#include <stdio.h>
#include <stdlib.h> /* rand 関数を利用する場合 */
#include "s_print.h" /* 整数値を出力したい場合 */
int main ( int argc, char *argv[] ) {
s_print_int ( rand() ); /* rand() 関数で、乱数を一つ生成し、出力 */
s_print_newline(); /* 改行 */
s_print_int ( rand() );
s_print_newline();
s_print_int ( rand() );
s_print_newline();
return 0;
}
#include <stdio.h>
#include <stdlib.h> /* rand 関数を利用する場合 */
#include "s_print.h" /* 整数値を出力したい場合 */
/*
引数 times で指定した回数だけ、
乱数を表示する
*/
void print_random ( int times ) {
if ( times <= 0 ) {
/* なにもしない */
} else {
s_print_int ( rand() );
s_print_newline();
print_random ( times - 1 );
}
}
int main ( int argc, char *argv[] ) {
print_random ( 100 ); /* 100 回数繰り返す */
return 0;
}
#include <stdio.h>
#include <stdlib.h> /* rand 関数を利用する場合 */
#include "s_print.h" /* 整数値を出力したい場合 */
#include "s_random.h" /* 乱数の色々な関数を利用する場合 */
int main ( int argc, char *argv[] ) {
s_init_random(); /* 乱数の初期値を、時間で初期化 */
s_print_int ( rand() ); /* rand() 関数で、乱数を一つ生成し、出力 */
s_print_newline(); /* 改行 */
s_print_int ( rand() );
s_print_newline();
s_print_int ( rand() );
s_print_newline();
return 0;
}
#include <stdio.h>
#include <stdlib.h> /* rand 関数を利用する場合 */
#include "s_print.h" /* 整数値を出力したい場合 */
#include "s_random.h" /* 乱数の色々な関数を利用する場合 */
int main ( int argc, char *argv[] ) {
s_init_random(); /* 乱数の初期値を、時間で初期化 */
s_print_int ( s_n_random( 10 ) ); /* s_n_random(10) で、0 ? ) の乱数を一つ生成し、出力 */
s_print_newline(); /* 改行 */
s_print_int ( s_n_random( 10 ) );
s_print_newline();
s_print_int ( s_n_random( 10 ) );
s_print_newline();
return 0;
}
#include <stdio.h>
void uta(char *X) {
printf ( "歌を忘れたかなりやは\n" );
printf ( X );
printf ( "色々\n" );
}
int main(int argc, char *argv[] ) {
uta ( "裏の山に捨てましょか\n" );
uta ( "海のなんちゃら\n" );
return 0;
}
#include <stdio.h>
#include "s_input.h"
#include "s_print.h"
int main(int argc, char *argv[] ) {
s_print_string ( "一つの整数値を入力してください。その数の二倍を出力します : " );
s_print_int ( s_input_int() * 2 );
s_print_newline ();
return 0;
}
#include <stdio.h>
#include "s_input.h"
#include "s_print.h"
int main(int argc, char *argv[] ) {
s_print_string ( "一つの文字列を入力してください。その文字列を二文字短くしたものを出力します : " );
s_print_string ( s_input_string() + 2 );
s_print_newline ();
return 0;
}
#include <stdio.h>
#include "s_input.h"
#include "s_print.h"
void print_p2 ( int times, int value ) {
if ( times <= 0 ) {
/* do nothing */
} else {
s_print_int ( value );
s_print_newline();
print_p2 ( times - 1, value * 2 );
}
}
int main(int argc, char *argv[] ) {
print_p2 ( 35, 1 );
return 0;
}
#include <stdio.h>
#include "s_input.h"
#include "s_print.h"
int main(int argc, char *argv[] ) {
s_print_string ( "3.0/2.0 = " );
s_print_double ( 3.0/2.0 );
s_print_newline ();
s_print_string ( "3/2 = " );
s_print_int ( 3/2 );
s_print_newline ();
return 0;
}
Download : 20160708-01.c ( utf8 版 )
/*
* CDATE FILENAME
*
* 二つの整数値をキーボードから入力し、その四則並びに余りを出力する
*/
#include <stdio.h>
#include "s_input.h"
#include "s_print.h"
/*
* print_int_result
*/
void print_int_result ( char *name, int a, int b, char op, int value ) {
s_print_string ( name );
s_print_string ( " : " );
s_print_int ( a );
/*
** この部分を完成させなさい
*/
s_print_char ( '=' );
s_print_int ( value );
s_print_newline();
}
/*
* print_int_calc
*/
void print_int_calc ( int a, int b ) {
print_int_result ( "和", a, b, '+', a + b );
print_int_result ( "差", a, b, '-', a - b );
/*
** この部分を完成させなさい
*/
print_int_result ( "余り", a, b, '%', a % b );
}
/*
* print_int_calc_1
*/
void print_int_calc_1 ( int a ) {
print_int_calc ( a, s_input_int() );
}
/*
* main
*/
int main( int argc, char *argv[] )
{
print_int_calc_1 ( s_input_int() );
return 0;
}
175 51
$ ./20160708-01-QQQQ.exe 175 51 和 : 175+51=226 差 : 175-51=124 積 : 175*51=8925 商 : 175/51=3 余り : 175%51=22 $
Download : 20160708-02.c ( utf8 版 )
/*
* CDATE FILENAME
*
* 二つの浮動小数点数値をキーボードから入力し、その四則を出力する
*/
#include <stdio.h>
#include "s_input.h"
#include "s_print.h"
/*
* print_double_result
*/
void print_double_result ( char *name, double a, double b, char op, double value ) {
s_print_string ( name );
s_print_string ( " : " );
s_print_double ( a );
/*
** この部分を完成させなさい
*/
s_print_char ( '=' );
s_print_double ( value );
s_print_newline();
}
/*
* print_double_calc
*/
void print_double_calc ( double a, double b ) {
print_double_result ( "和", a, b, '+', a + b );
print_double_result ( "差", a, b, '-', a - b );
/*
** この部分を完成させなさい
*/
}
/*
* print_double_calc_1
*/
void print_double_calc_1 ( double a ) {
print_double_calc ( a, s_input_double() );
}
/*
* main
*/
int main( double argc, char *argv[] )
{
print_double_calc_1 ( s_input_double() );
return 0;
}
123.456 789.012
$ ./20160708-02-QQQQ.exe 123.456000 789.012000 和 : 123.456000+789.012000=912.468000 差 : 123.456000-789.012000=-665.556000 積 : 123.456000*789.012000=97408.265472 商 : 123.456000/789.012000=0.156469 $