Download : sample-001.c
/*
* 2018/11/09 sample-001.c
*/
/*
* 色々な if 文 : if 文の基本
*
* 利用方法
* コンパイル
* cc -I ~/c/include -o sample-001.exe sample-001.c
* 実行
* sample-001
*/
#include <stdio.h>
/*
* void isPositive ( int value )
*
* value が正なら「正」と出力し、そうでなければ「正でない」と出力する関数
*/
void isPositive ( int value ) {
if ( value > 0 ) { /* value が 0 より大きい場合.. */
printf ( "正\n" );
} else { /* そうでない場合 */
printf ( "正でない\n" );
}
}
/*
* main
*
*/
int main( int argc, char *argv[] )
{
isPositive ( 3 ); /* 「正」と出るはず */
isPositive ( -5 ); /* 「正でない」と出るはず */
isPositive ( 0 ); /* 「正でない」と出るはず */
return 0;
}
$ ./sample-001.exe 正 正でない 正でない $
Download : sample-002.c
/*
* 2018/11/09 sample-002.c
*/
/*
* 色々な if 文 : else 節の内容が何もない場合
*
* 利用方法
* コンパイル
* cc -I ~/c/include -o sample-002.exe sample-002.c
* 実行
* sample-002
*/
#include <stdio.h>
/*
* void isPositive ( int value )
*
* value が正なら「正」と出力し、そうでなければ何もしない
*/
void isPositive ( int value ) {
if ( value > 0 ) { /* value が 0 より大きい場合.. */
printf ( "正\n" );
} else { /* そうでない場合 */
/* 何もしなくてよいので、中身は空っぽ */
}
}
/*
* main
*
*/
int main( int argc, char *argv[] )
{
isPositive ( 3 ); /* 「正」と出るはず */
isPositive ( -5 ); /* 何もでない */
isPositive ( 0 ); /* 何もでない */
return 0;
}
$ ./sample-002.exe 正 $
Download : sample-003.c
/*
* 2018/11/09 sample-003.c
*/
/*
* 色々な if 文 : else 節の省略
*
* 利用方法
* コンパイル
* cc -I ~/c/include -o sample-003.exe sample-003.c
* 実行
* sample-003
*/
#include <stdio.h>
/*
* void isPositive ( int value )
*
* value が正なら「正」と出力し、そうでなければ何もしない
*/
void isPositive ( int value ) {
if ( value > 0 ) { /* value が 0 より大きい場合.. */
printf ( "正\n" );
}
/* 条件が不成立の時の文が何もなければ else 以下(else 節)を省略可能 */
}
/*
* main
*
*/
int main( int argc, char *argv[] )
{
isPositive ( 3 ); /* 「正」と出るはず */
isPositive ( -5 ); /* 何もでない */
isPositive ( 0 ); /* 何もでない */
return 0;
}
$ ./sample-003.exe 正 $
Download : sample-004.c
/*
* 2018/11/09 sample-004.c
*/
/*
* 色々な if 文 : 単文の場合 「{」,「}」も省略可能
*
* 利用方法
* コンパイル
* cc -I ~/c/include -o sample-004.exe sample-004.c
* 実行
* sample-004
*/
#include <stdio.h>
/*
* void isPositive ( int value )
*
* value が正なら「正」と出力し、そうでなければ何もしない
*/
void isPositive ( int value ) {
if ( value > 0 )
printf ( "正\n" );
/* 単文の場合は、 「{」,「}」が省略可能 */
/* 注意 : 省略は *絶対に* お勧めしない !!
* if 文では常に「{」,「}」を付ける習慣を身に付ける(「行儀」の問題)
* ただし、「行儀の悪い人」もいるので、「知っている」必要がある。
*/
}
/*
* main
*
*/
int main( int argc, char *argv[] )
{
isPositive ( 3 ); /* 「正」と出るはず */
isPositive ( -5 ); /* 何もでない */
isPositive ( 0 ); /* 何もでない */
return 0;
}
$ ./sample-004.exe 正 $
Download : sample-005.c
/*
* 2018/11/09 sample-005.c
*/
/*
* 条件式
* (C 言語での)「条件」は、「(整数の)値」を持つ
* 1 : 真(条件が成り立つ場合)の時
* 0 : 偽(条件が成り立たない場合)の時
* 逆に、整数値は、常に「条件(値)」と見做す事ができる
* 「条件」が要求された場合に、「(条件の)値」は、次のように「解釈」される
* 「偽」: 0 の時
* 「真」: それ (0) 以外の時 ( 1 でなくても良い )
*
* 利用方法
* コンパイル
* cc -I ~/c/include -o sample-005.exe sample-005.c
* 実行
* sample-005
*/
#include <stdio.h>
/*
* main
*
*/
int main( int argc, char *argv[] )
{
/* 「条件(式)」は「(整数)値」を持つ */
printf ( "1 < 2 は成立するので %d になる。\n", 1 < 2 );
printf ( "3 < 2 は成立しないので %d になる。\n", 3 < 2 );
if ( 1 < 2 ) { /* この条件は常に成立するので.. */
printf ( "1 < 2 は成立しました。\n" ); /* こちらを実行 */
} else {
printf ( "1 < 2 は成立しませんでした。\n" );
}
if ( 3 < 2 ) { /* この条件は常に成立しないので.. */
printf ( "3 < 2 は成立しました。\n" );
} else {
printf ( "3 < 2 は成立しませんでした。\n" ); /* こちらを実行 */
}
if ( 1 ) { /* 1 は 0 でないので、「真」と見做される */
printf ( " 1 は「真」と見做されました。\n" ); /* こちらを実行 */
} else {
printf ( " 1 は「偽」と見做されました。\n" );
}
if ( 0 ) { /* 0 は、「偽」と見做される */
printf ( " 0 は「真」と見做されました。\n" );
} else {
printf ( " 0 は「偽」と見做されました。\n" ); /* こちらを実行 */
}
if ( 3 ) { /* 3 は 0 でないので、「真」と見做される */
printf ( " 3 は「真」と見做されました。\n" ); /* こちらを実行 */
} else {
printf ( " 3 は「偽」と見做されました。\n" );
}
if ( -10 ) { /* -10 は 0 でないので、「真」と見做される */
printf ( " -10 は「真」と見做されました。\n" ); /* こちらを実行 */
} else {
printf ( " -10 は「偽」と見做されました。\n" );
}
return 0;
}
$ ./sample-005.exe 1 < 2 は成立するので 1 になる。 3 < 2 は成立しないので 0 になる。 1 < 2 は成立しました。 3 < 2 は成立しませんでした。 1 は「真」と見做されました。 0 は「偽」と見做されました。 3 は「真」と見做されました。 -10 は「真」と見做されました。 $
Download : sample-006.c
/*
* 2018/11/09 sample-006.c
*/
/*
* 条件式
* (C 言語での)「条件」は、「(整数の)値」を持つ
* 「条件」を「引数」に渡す事ができる。
*
* 利用方法
* コンパイル
* cc -I ~/c/include -o sample-006.exe sample-006.c
* 実行
* sample-006
*/
#include <stdio.h>
/*
* void condition_check ( int condition )
* int condition 条件(値)
* 条件が真なら「真と見做される」、偽なら「偽と見做される」と表示
*/
void condition_check ( int condition ) {
if ( condition ) {
printf ( " %d は「真」と見做されました。\n", condition );
} else {
printf ( " %d は「偽」と見做されました。\n", condition );
}
}
/*
* main
*
*/
int main( int argc, char *argv[] )
{
/* 「整数値」は、「条件値」として利用できる */
/* 整数値 */
printf ( "整数値\n" );
condition_check ( 1 );
condition_check ( 0 );
condition_check ( 3 );
condition_check ( -10 );
/* 不等号 */
printf ( "---\n" );
printf ( "不等号\n" );
/* 「条件式」は、評価されて「条件値」になるが、これは「整数値」になる */
printf ( "条件式「 1 < 2 」を引数に指定\n" );
condition_check ( 1 < 2 ); /* 引数には 1 < 2 とあるが、これは評価されて 1 になってから渡される */
printf ( "条件式「 3 < 2 」を引数に指定\n" );
condition_check ( 3 < 2 ); /* 引数には 3 < 2 とあるが、これは評価された 0 になってから渡される */
/* 等号 */
printf ( "---\n" );
printf ( "等号\n" );
printf ( "条件式「 1 == 1 」を引数に指定\n" );
condition_check ( 1 == 1 );
printf ( "条件式「 1 != 1 」を引数に指定\n" );
condition_check ( 1 != 1 );
printf ( "条件式「 1 == 0 」を引数に指定\n" );
condition_check ( 1 == 0 );
printf ( "条件式「 1 != 0 」を引数に指定\n" );
condition_check ( 1 != 0 );
return 0;
}
$ ./sample-006.exe 整数値 1 は「真」と見做されました。 0 は「偽」と見做されました。 3 は「真」と見做されました。 -10 は「真」と見做されました。 --- 不等号 条件式「 1 < 2 」を引数に指定 1 は「真」と見做されました。 条件式「 3 < 2 」を引数に指定 0 は「偽」と見做されました。 --- 等号 条件式「 1 == 1 」を引数に指定 1 は「真」と見做されました。 条件式「 1 != 1 」を引数に指定 0 は「偽」と見做されました。 条件式「 1 == 0 」を引数に指定 0 は「偽」と見做されました。 条件式「 1 != 0 」を引数に指定 1 は「真」と見做されました。 $
Download : sample-007.c
/*
* 2018/11/09 sample-007.c
*/
/*
* 「条件値」の計算
* 「条件値」は、「整数値」なので計算できる
*
* 利用方法
* コンパイル
* cc -I ~/c/include -o sample-007.exe sample-007.c
* 実行
* sample-007
*/
#include <stdio.h>
/*
* void condition_check ( int condition )
* int condition 条件(値)
* 条件が真なら「真と見做される」、偽なら「偽と見做される」と表示
*/
void condition_check ( int condition ) {
if ( condition ) {
printf ( " %d は「真」と見做されました。\n", condition );
} else {
printf ( " %d は「偽」と見做されました。\n", condition );
}
}
/*
* main
*
*/
int main( int argc, char *argv[] )
{
/* 条件値の計算 */
printf ( "条件値の計算\n" );
printf ( "(1<2) + (2<3) = %d + %d = %d\n", (1<2), (2<3), (1<2) + (2<3) );
printf ( "(1<2) * (2<3) = %d * %d = %d\n", (1<2), (2<3), (1<2) * (2<3) );
printf ( "(1<2) - (2<3) = %d - %d = %d\n", (1<2), (2<3), (1<2) - (2<3) );
printf ( "(1<2) / (2<3) = %d / %d = %d\n", (1<2), (2<3), (1<2) / (2<3) );
printf ( "---\n" );
/* (1<2)「真」と (2<3)「真」の場合 */
printf ( "(1<2) + (2<3) = %d\n", (1<2) + (2<3) );
printf ( "(1<2) * (2<3) = %d\n", (1<2) * (2<3) );
/* (1<2)「真」と (3<2)「偽」の場合 */
printf ( "(1<2) + (3<2) = %d\n", (1<2) + (3<2) );
printf ( "(1<2) * (3<2) = %d\n", (1<2) * (3<2) );
/* (2<1)「偽」と (2<3)「真」の場合 */
printf ( "(2<1) + (2<3) = %d\n", (2<1) + (2<3) );
printf ( "(2<1) * (2<3) = %d\n", (2<1) * (2<3) );
/* (2<1)「偽」と (3<2)「偽」の場合 */
printf ( "(2<1) + (3<2) = %d\n", (2<1) + (3<2) );
printf ( "(2<1) * (3<2) = %d\n", (2<1) * (3<2) );
return 0;
}
$ ./sample-007.exe 条件値の計算 (1<2) + (2<3) = 1 + 1 = 2 (1<2) * (2<3) = 1 * 1 = 1 (1<2) - (2<3) = 1 - 1 = 0 (1<2) / (2<3) = 1 / 1 = 1 --- (1<2) + (2<3) = 2 (1<2) * (2<3) = 1 (1<2) + (3<2) = 1 (1<2) * (3<2) = 0 (2<1) + (2<3) = 1 (2<1) * (2<3) = 0 (2<1) + (3<2) = 0 (2<1) * (3<2) = 0 $
Download : sample-008.c
/*
* 2018/11/09 sample-008.c
*/
/*
* 「条件値」の計算
* 「条件値」は、「整数値」なので計算できる
*
* 利用方法
* コンパイル
* cc -I ~/c/include -o sample-008.exe sample-008.c
* 実行
* sample-008
*/
#include <stdio.h>
/*
* void condition_print ( int condition )
* int condition 条件(値)
* 条件が真なら「真」、偽なら「偽」と表示
*/
void condition_print ( int condition ) {
if ( condition ) {
printf ( "「真(%d)」", condition );
} else {
printf ( "「偽(%d)」", condition );
}
}
/*
* void condition_add ( int c1, int c2 )
* int c1; 条件値 1
* int c2; 条件値 2
* 二つの条件値の「和」の振る舞いを表示
*/
void condition_add ( int c1, int c2 ) {
condition_print ( c1 );
printf ( "と" );
condition_print ( c2 );
printf ( "の和は" );
condition_print ( c1 + c2 );
printf ( "になります。\n" );
}
/*
* void condition_mul ( int c1, int c2 )
* int c1; 条件値 1
* int c2; 条件値 2
* 二つの条件値の「積」の振る舞いを表示
*/
void condition_mul ( int c1, int c2 ) {
condition_print ( c1 );
printf ( "と" );
condition_print ( c2 );
printf ( "の積は" );
condition_print ( c1 * c2 );
printf ( "になります。\n" );
}
/*
* main
*
*/
int main( int argc, char *argv[] )
{
/* (1<2)「真」と (2<3)「真」の場合 */
printf ( "(1<2)「真」と (2<3)「真」の場合\n" );
condition_add ( (1<2), (2<3) );
condition_mul ( (1<2), (2<3) );
printf ( "(1<2)「真」と (3<2)「偽」の場合\n" );
condition_add ( (1<2), (3<2) );
condition_mul ( (1<2), (3<2) );
printf ( "(2<1)「偽」と (2<3)「真」の場合\n" );
condition_add ( (2<1), (2<3) );
condition_mul ( (2<1), (2<3) );
printf ( "(2<1)「偽」と (3<2)「偽」の場合\n" );
condition_add ( (2<1), (3<2) );
condition_mul ( (2<1), (3<2) );
printf ( "1「真」と 1「真」の場合\n" );
condition_add ( 1, 1 );
condition_mul ( 1, 1 );
printf ( "1「真」と -1「真」の場合\n" );
condition_add ( 1, -1 );
condition_mul ( 1, -1 );
return 0;
}
$ ./sample-008.exe (1<2)「真」と (2<3)「真」の場合 「真(1)」と「真(1)」の和は「真(2)」になります。 「真(1)」と「真(1)」の積は「真(1)」になります。 (1<2)「真」と (3<2)「偽」の場合 「真(1)」と「偽(0)」の和は「真(1)」になります。 「真(1)」と「偽(0)」の積は「偽(0)」になります。 (2<1)「偽」と (2<3)「真」の場合 「偽(0)」と「真(1)」の和は「真(1)」になります。 「偽(0)」と「真(1)」の積は「偽(0)」になります。 (2<1)「偽」と (3<2)「偽」の場合 「偽(0)」と「偽(0)」の和は「偽(0)」になります。 「偽(0)」と「偽(0)」の積は「偽(0)」になります。 1「真」と 1「真」の場合 「真(1)」と「真(1)」の和は「真(2)」になります。 「真(1)」と「真(1)」の積は「真(1)」になります。 1「真」と -1「真」の場合 「真(1)」と「真(-1)」の和は「偽(0)」になります。 「真(1)」と「真(-1)」の積は「真(-1)」になります。 $
Download : sample-009.c
/*
* 2018/11/09 sample-009.c
*/
/*
* 「論理値」の計算
* 演算子 「&&」 : 論理積を計算する
* a && b : a と b のどちらか一方でも 0(偽) なら 0(偽)、それ以外は 1(真)
* 演算子 「||」 : 論理和を計算する
* a || b : a と b の両方が 0(偽) なら 0(偽)、それ以外は 1(真)
* 演算子 「!」 : 否定を計算する
* !a : a が 0(偽) なら 1(偽)、それ以外は 0(偽)
*
* 利用方法
* コンパイル
* cc -I ~/c/include -o sample-009.exe sample-009.c
* 実行
* sample-009
*/
#include <stdio.h>
/*
* void condition_print ( int condition )
* int condition 条件(値)
* 条件が真なら「真」、偽なら「偽」と表示
*/
void condition_print ( int condition ) {
if ( condition ) {
printf ( "「真(%d)」", condition );
} else {
printf ( "「偽(%d)」", condition );
}
}
/*
* void condition_or ( int c1, int c2 )
* int c1; 条件値 1
* int c2; 条件値 2
* 二つの条件値の「論理和」の振る舞いを表示
*/
void condition_or ( int c1, int c2 ) {
condition_print ( c1 );
printf ( "と" );
condition_print ( c2 );
printf ( "の論理和は" );
condition_print ( c1 || c2 ); /* 演算子「||」は「論理和」を表す */
printf ( "になります。\n" );
}
/*
* void condition_and ( int c1, int c2 )
* int c1; 条件値 1
* int c2; 条件値 2
* 二つの条件値の「論理積」の振る舞いを表示
*/
void condition_and ( int c1, int c2 ) {
condition_print ( c1 );
printf ( "と" );
condition_print ( c2 );
printf ( "の論理積は" );
condition_print ( c1 && c2 ); /* 演算子「&&」は「論理積」を表す */
printf ( "になります。\n" );
}
/*
* void condition_not ( int c )
* int c; 条件値
* 一つの条件値の「否定」の振る舞いを表示
*/
void condition_not ( int c ) {
condition_print ( c );
printf ( "の否定は" );
condition_print ( !c ); /* 演算子「!」は「否定」を表す */
printf ( "になります。\n" );
}
/*
* main
*
*/
int main( int argc, char *argv[] )
{
/* (1<2)「真」と (2<3)「真」の場合 */
printf ( "(1<2)「真」と (2<3)「真」の場合\n" );
condition_or ( (1<2), (2<3) );
condition_and ( (1<2), (2<3) );
printf ( "(1<2)「真」と (3<2)「偽」の場合\n" );
condition_or ( (1<2), (3<2) );
condition_and ( (1<2), (3<2) );
printf ( "(2<1)「偽」と (2<3)「真」の場合\n" );
condition_or ( (2<1), (2<3) );
condition_and ( (2<1), (2<3) );
printf ( "(2<1)「偽」と (3<2)「偽」の場合\n" );
condition_or ( (2<1), (3<2) );
condition_and ( (2<1), (3<2) );
printf ( "1「真」と 1「偽」の場合\n" );
condition_or ( 1, 1 );
condition_and ( 1, 1 );
printf ( "1「真」と -1「真」の場合\n" );
condition_or ( 1, -1 );
condition_and ( 1, -1 );
printf ( "1 の否定\n" );
condition_not ( 1 );
printf ( "0 の否定\n" );
condition_not ( 0 );
printf ( "-1 の否定\n" );
condition_not ( -1 );
return 0;
}
$ ./sample-009.exe (1<2)「真」と (2<3)「真」の場合 「真(1)」と「真(1)」の論理和は「真(1)」になります。 「真(1)」と「真(1)」の論理積は「真(1)」になります。 (1<2)「真」と (3<2)「偽」の場合 「真(1)」と「偽(0)」の論理和は「真(1)」になります。 「真(1)」と「偽(0)」の論理積は「偽(0)」になります。 (2<1)「偽」と (2<3)「真」の場合 「偽(0)」と「真(1)」の論理和は「真(1)」になります。 「偽(0)」と「真(1)」の論理積は「偽(0)」になります。 (2<1)「偽」と (3<2)「偽」の場合 「偽(0)」と「偽(0)」の論理和は「偽(0)」になります。 「偽(0)」と「偽(0)」の論理積は「偽(0)」になります。 1「真」と 1「偽」の場合 「真(1)」と「真(1)」の論理和は「真(1)」になります。 「真(1)」と「真(1)」の論理積は「真(1)」になります。 1「真」と -1「真」の場合 「真(1)」と「真(-1)」の論理和は「真(1)」になります。 「真(1)」と「真(-1)」の論理積は「真(1)」になります。 1 の否定 「真(1)」の否定は「偽(0)」になります。 0 の否定 「偽(0)」の否定は「真(1)」になります。 -1 の否定 「真(-1)」の否定は「偽(0)」になります。 $
Download : sample-010.c
/*
* 2018/11/09 sample-010.c
*/
/*
* 「論理計算」と if 文
*
* 利用方法
* コンパイル
* cc -I ~/c/include -o sample-010.exe sample-010.c
* 実行
* sample-010
*/
#include <stdio.h>
/*
* main
*
*/
int main( int argc, char *argv[] )
{
/* 二つの条件が成立する場合 */
if ( 2 > 1 ) {
if ( 3 > 2 ) {
printf ( "(2>1) かつ (3>2)\n" );
} else {
} else {
}
return 0;
}
$ ./sample-010.exe (1<2)「真」と (2<3)「真」の場合 「真(1)」と「真(1)」の論理和は「真(1)」になります。 「真(1)」と「真(1)」の論理積は「真(1)」になります。 (1<2)「真」と (3<2)「偽」の場合 「真(1)」と「偽(0)」の論理和は「真(1)」になります。 「真(1)」と「偽(0)」の論理積は「偽(0)」になります。 (2<1)「偽」と (2<3)「真」の場合 「偽(0)」と「真(1)」の論理和は「真(1)」になります。 「偽(0)」と「真(1)」の論理積は「偽(0)」になります。 (2<1)「偽」と (3<2)「偽」の場合 「偽(0)」と「偽(0)」の論理和は「偽(0)」になります。 「偽(0)」と「偽(0)」の論理積は「偽(0)」になります。 1「真」と 1「偽」の場合 「真(1)」と「真(1)」の論理和は「真(1)」になります。 「真(1)」と「真(1)」の論理積は「真(1)」になります。 1「真」と -1「真」の場合 「真(1)」と「真(-1)」の論理和は「真(1)」になります。 「真(1)」と「真(-1)」の論理積は「真(1)」になります。 1 の否定 「真(1)」の否定は「偽(0)」になります。 0 の否定 「偽(0)」の否定は「真(1)」になります。 -1 の否定 「真(-1)」の否定は「偽(0)」になります。 $
Download : sample-011.c
/*
* 2018/11/09 sample-011.c
*/
/*
* 複数条件の分岐
*
* 利用方法
* コンパイル
* cc -I ~/c/include -o sample-011.exe sample-011.c
* 実行
* sample-011
*/
#include <stdio.h>
/*
* trump
* トランプの番号から、その名前を表示する
*/
void trump ( int number ) {
printf ( "数値 %d の表すカードは、", number );
if ( number == 1 ) { /* 番号が 1 なら エース (A) */
printf ( "エース" );
} else if ( number == 11 ) { /* 番号が 11 なら ジャック (J) */
printf ( "ジャック" );
} else if ( number == 12 ) { /* 番号が 12 なら クイーン (Q) */
printf ( "クイーン" );
} else if ( number == 13 ) { /* 番号が 13 なら キング (K) */
printf ( "キング" );
} else if ( number == 0 ) { /* 番号が 0 の場合は例外的に ジョーカー ($) */
printf ( "ジョーカー" );
} else { /* それ以外 */
if ( number < 0 ) { /* 番号が負の場合は.. */
printf ( "範囲外" );
} else if ( 13 < number ) { /* 番号が 13 より大きい場合は.. */
printf ( "範囲外" );
} else { /* それ以外は.. */
printf ( "%d の数カード", number );
}
}
printf ( "です。\n" );
}
/*
* main
*
*/
int main( int argc, char *argv[] )
{
trump ( -1 );
trump ( 0 );
trump ( 1 );
trump ( 7 );
trump ( 10 );
trump ( 13 );
trump ( 20 );
return 0;
}
$ ./sample-011.exe 数値 -1 の表すカードは、範囲外です。 数値 0 の表すカードは、ジョーカーです。 数値 1 の表すカードは、エースです。 数値 7 の表すカードは、7 の数カードです。 数値 10 の表すカードは、10 の数カードです。 数値 13 の表すカードは、キングです。 数値 20 の表すカードは、範囲外です。 $
Download : sample-012.c
/*
* 2018/11/09 sample-012.c
*/
/*
* 複数条件の分岐(論理演算子を利用した場合)
*
* 利用方法
* コンパイル
* cc -I ~/c/include -o sample-012.exe sample-012.c
* 実行
* sample-012
*/
#include <stdio.h>
/*
* trump
* トランプの番号から、その名前を表示する
*/
void trump ( int number ) {
printf ( "数値 %d の表すカードは、", number );
if ( number == 1 ) { /* 番号が 1 なら エース (A) */
printf ( "エース" );
} else if ( number == 11 ) { /* 番号が 11 なら ジャック (J) */
printf ( "ジャック" );
} else if ( number == 12 ) { /* 番号が 12 なら クイーン (Q) */
printf ( "クイーン" );
} else if ( number == 13 ) { /* 番号が 13 なら キング (K) */
printf ( "キング" );
} else if ( number == 0 ) { /* 番号が 0 の場合は例外的に ジョーカー ($) */
printf ( "ジョーカー" );
} else if ( 1 < number && number < 11 ) { /* 番号が 2 〜 10 の時 */
printf ( "%d の数カード", number );
} else { /* それ以外は.. */
printf ( "範囲外" );
}
printf ( "です。\n" );
}
/*
* main
*
*/
int main( int argc, char *argv[] )
{
trump ( -1 );
trump ( 0 );
trump ( 1 );
trump ( 7 );
trump ( 10 );
trump ( 13 );
trump ( 20 );
return 0;
}
$ ./sample-012.exe 数値 -1 の表すカードは、範囲外です。 数値 0 の表すカードは、ジョーカーです。 数値 1 の表すカードは、エースです。 数値 7 の表すカードは、7 の数カードです。 数値 10 の表すカードは、10 の数カードです。 数値 13 の表すカードは、キングです。 数値 20 の表すカードは、範囲外です。 $
Download : sample-013.c
/*
* 2018/11/09 sample-013.c
*/
/*
* 複数条件の分岐(case 文を利用した場合)
*
* 利用方法
* コンパイル
* cc -I ~/c/include -o sample-013.exe sample-013.c
* 実行
* sample-013
*/
#include <stdio.h>
/*
* trump
* トランプの番号から、その名前を表示する
*/
void trump ( int number ) {
printf ( "数値 %d の表すカードは、", number );
switch ( number ) { /* 一つの式の値で分岐 */
case 1: /* 番号が 1 なら エース (A) { (開きブロック) */
printf ( "エース" );
break; /* ここで 1 の場合がおわる } (閉じブロック) */
case 11: /* 番号が 11 なら ジャック (J) */
printf ( "ジャック" );
break;
case 12: /* 番号が 12 なら クイーン (Q) */
printf ( "クイーン" );
break;
case 13: /* 番号が 13 なら キング (K) */
printf ( "キング" );
break;
case 0: /* 番号が 0 の場合は例外的に ジョーカー ($) */
printf ( "ジョーカー" );
break;
case 2: /* 範囲は指定できないので、全て列挙 */
/* 2 の場合は 3 の場合と同じなので break しない */
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
printf ( "%d の数カード", number );
break; /* 2 〜 10 の場合はここで、終了 */
default: /* それ以外は.. */
printf ( "範囲外" );
break; /* これは不要だが、つける習慣を (マナー) */
}
printf ( "です。\n" );
}
/*
* main
*
*/
int main( int argc, char *argv[] )
{
trump ( -1 );
trump ( 0 );
trump ( 1 );
trump ( 7 );
trump ( 10 );
trump ( 13 );
trump ( 20 );
return 0;
}
$ ./sample-013.exe 数値 -1 の表すカードは、範囲外です。 数値 0 の表すカードは、ジョーカーです。 数値 1 の表すカードは、エースです。 数値 7 の表すカードは、7 の数カードです。 数値 10 の表すカードは、10 の数カードです。 数値 13 の表すカードは、キングです。 数値 20 の表すカードは、範囲外です。 $
Download : sample-014.c
/*
* 2018/11/09 sample-014.c
*/
/*
* 複数条件の分岐(case 文と if 文の混合)
*
* 利用方法
* コンパイル
* cc -I ~/c/include -o sample-014.exe sample-014.c
* 実行
* sample-014
*/
#include <stdio.h>
/*
* trump
* トランプの番号から、その名前を表示する
*/
void trump ( int number ) {
printf ( "数値 %d の表すカードは、", number );
switch ( number ) { /* 一つの式の値で分岐 */
case 1: /* 番号が 1 なら エース (A) { (開きブロック) */
printf ( "エース" );
break; /* ここで 1 の場合がおわる } (閉じブロック) */
case 11: /* 番号が 11 なら ジャック (J) */
printf ( "ジャック" );
break;
case 12: /* 番号が 12 なら クイーン (Q) */
printf ( "クイーン" );
break;
case 13: /* 番号が 13 なら キング (K) */
printf ( "キング" );
break;
case 0: /* 番号が 0 の場合は例外的に ジョーカー ($) */
printf ( "ジョーカー" );
break;
default: /* それ以外は.. */
if ( 2 <= number && number <= 10 ) {
/* if 文ならば、「範囲」も指定できる */
printf ( "%d の数カード", number );
} else {
printf ( "範囲外" );
}
break; /* これは不要だが、つける習慣を (マナー) */
}
printf ( "です。\n" );
}
/*
* main
*
*/
int main( int argc, char *argv[] )
{
trump ( -1 );
trump ( 0 );
trump ( 1 );
trump ( 7 );
trump ( 10 );
trump ( 13 );
trump ( 20 );
return 0;
}
$ ./sample-014.exe 数値 -1 の表すカードは、範囲外です。 数値 0 の表すカードは、ジョーカーです。 数値 1 の表すカードは、エースです。 数値 7 の表すカードは、7 の数カードです。 数値 10 の表すカードは、10 の数カードです。 数値 13 の表すカードは、キングです。 数値 20 の表すカードは、範囲外です。 $
/*
* 課題 CNAME-02
*
* 20181026 20181026-02-QQQQ.c
*
* 0 〜 99 の偶数を出力する
*
*/
#include <stdio.h>
/*
* for 文を利用して、0 から 99 の偶数の数値
(そのニ) i が 0 〜 99 の間の偶数だけを動くようにし、毎回 i を出す
*/
int main(int ac, char *av[] ) {
int i; /* for 文の制御変数 */
for ( i = 0; i < 100; i = i + 2 ) {
/* i++ だと i = 0, 1, 2, .. */
/* ○○とすれば i = 0, 2, 4, .. */
printf ( "%d\n", i ); /* i を出力 */
}
return 0;
}
/*
* 課題 CNAME-02
*
* 20181026 20181026-02-QQQQ.c
*
* 0 〜 99 の偶数を出力する
*
*/
#include <stdio.h>
/*
* for 文を利用して、0 から 99 の偶数の数値
(そのニ) i が 0 〜 99 の間の偶数だけを動くようにし、毎回 i を出す
*/
int main(int ac, char *av[] ) {
int i; /* for 文の制御変数 */
/* 0 ? 99 までの偶数の個数は、(99-0+1)/2 個ある */
for ( i = 0; i < 100/2; i++ ) { /* i++ は i = i + 1 と同じ */
printf ( "%d\n", 2*i );
}
return 0;
}
#include <stdio.h>
/*
count = xx ( xx = 0 ? 9 が入る )
を xx の部分を 0 から 9 まで、出力(10 回出力)
*/
int main(int argc, char *argv[]) {
int count; /* パラメータ変数の宣言 */
count = 0; /* パラメータ変数の初期化 */
while ( count < 10 ) { /* パラメータ変数の条件判定 */
printf ( "count = %d\n", count );
count = count + 1; /* パラメータ変数の更新 */
}
/* 「パラメータ」:何かを制御するような値を持つ変数 */
/* ここでは、 while 構文の振る舞い(繰り返しを続けるかどうか)を
制御している変数が、count なので、count を while 構文の
パラメータと呼んでいる */
return 0;
}
#include <stdio.h>
/*
count = xx ( xx = 0 ? 9 が入る )
を xx の部分を 0 から 9 まで、出力(10 回出力)
*/
int main(int argc, char *argv[]) {
int count = 0;
/*
while ( count < 10 ) {
printf ( "count = %d\n", count );
count = count + 1;
}
*/
/*
while 構文は if 構文が無限にあると考える事ができる
*/
/* 0 番目(1つ目) */
if ( count < 10 ) {
printf ( "count = %d\n", count );
count = count + 1;
}
/* 1 番目(2つ目) */
if ( count < 10 ) {
printf ( "count = %d\n", count );
count = count + 1;
}
if ( count < 10 ) {
printf ( "count = %d\n", count );
count = count + 1;
}
if ( count < 10 ) {
printf ( "count = %d\n", count );
count = count + 1;
}
if ( count < 10 ) {
printf ( "count = %d\n", count );
count = count + 1;
}
if ( count < 10 ) {
printf ( "count = %d\n", count );
count = count + 1;
}
if ( count < 10 ) {
printf ( "count = %d\n", count );
count = count + 1;
}
if ( count < 10 ) {
printf ( "count = %d\n", count );
count = count + 1;
}
if ( count < 10 ) {
printf ( "count = %d\n", count );
count = count + 1;
}
/* 9 番目(10つ目) */
if ( count < 10 ) {
printf ( "count = %d\n", count );
count = count + 1;
}
/* count の値は 10 になっている */
/* 10 番目(11つ目) */
if ( count < 10 ) {
printf ( "count = %d\n", count );
count = count + 1;
}
/* .. 無限に copy があるものと思う */
/* しかし、これ以後は条件が成立しないので、あっても無意味 */
return 0;
}
#include <stdio.h>
/*
count = xx ( xx = 0 ? 9 が入る )
を xx の部分を 0 から 9 まで、出力(10 回出力)
*/
int main(int argc, char *argv[]) {
int count = 0;
/*
while ( count < 10 ) {
printf ( "count = %d\n", count );
count = count + 1;
}
*/
/*
while 構文は if 構文が無限にあると考える事ができる
if 構文が無限にあっても、
結局は、繰り返し回数分以後の条件は不成立なので、
あってもなくても同じ(だから、さぼってよい)
逆に、意味のある if 構文は、条件が必ず成立
=> if 構文の形にする必要はない
*/
/* 0 番目(1つ目) */
printf ( "count = %d\n", count );
count = count + 1;
/* 1 番目(2つ目) */
printf ( "count = %d\n", count );
count = count + 1;
printf ( "count = %d\n", count );
count = count + 1;
printf ( "count = %d\n", count );
count = count + 1;
printf ( "count = %d\n", count );
count = count + 1;
printf ( "count = %d\n", count );
count = count + 1;
printf ( "count = %d\n", count );
count = count + 1;
printf ( "count = %d\n", count );
count = count + 1;
printf ( "count = %d\n", count );
count = count + 1;
/* 9 番目(10つ目) */
printf ( "count = %d\n", count );
count = count + 1;
return 0;
}
#include <stdio.h>
/*
while 構文は繰り返しが目的
繰り返しが目的の機能として、再帰呼び出しがある
=> while 構文と再帰呼び出しの関係
*/
void f_while ( int count ) {
if ( count < 10 ) { /* while ( count < 10 ) */
printf ( "count = %d\n", count ); /* 繰り返す文 */
f_while ( count + 1 ); /* count = count + 1 */
} else {
}
}
int main(int argc, char *argv[]) {
/*
int count = 0;
while ( count < 10 ) {
printf ( "count = %d\n", count );
count = count + 1;
}
*/
f_while ( 0 ); /* count = 0 */
return 0;
}
#include <stdio.h>
/*
count = xx ( xx = 0 ? 9 が入る )
を xx の部分を 0 から 9 まで、出力(10 回出力)
*/
int main(int argc, char *argv[]) {
int count; /* パラメータ変数の宣言 */
count = 0; /* パラメータ変数の初期化 */
while ( count < 10 ) { /* パラメータ変数の条件判定 */
printf ( "count = %d\n", count );
count = count + 1; /* パラメータ変数の更新 */
}
/* 「パラメータ」:何かを制御するような値を持つ変数 */
/* ここでは、 while 構文の振る舞い(繰り返しを続けるかどうか)を
制御している変数が、count なので、count を while 構文の
パラメータと呼んでいる */
return 0;
}
#include <stdio.h>
/*
count = xx ( xx = 0 ? 9 が入る )
を xx の部分を 0 から 9 まで、出力(10 回出力)
*/
int main(int argc, char *argv[]) {
int count; /* パラメータ変数の宣言 */
/*
count = 0; /* パラメータ変数の初期化
while ( count < 10 ) { /* パラメータ変数の条件判定
printf ( "count = %d\n", count );
count = count + 1; /* パラメータ変数の更新
}
*/
/*
while 構文から for 構文への変換
0. while 構文をコピー
1. while を for
2. 初期化を、for の前の所、更新を後ろの所にもってゆく
*/
for ( count = 0; count < 10; count = count + 1 ) {
printf ( "count = %d\n", count );
}
return 0;
}
#include <stdio.h>
int main(int argc, char *argv[]) {
int count; /* パラメータ変数の宣言 */
/*
count = xx を xx を 0 ? 9 で 10 回出力
*/
for ( count = 0; count < 10; count = count + 1 ) {
printf ( "count = %d\n", count );
}
/*
count = xx を xx を 1 ? 10 で 10 回出力
*/
for ( count = 1; count <= 10; count = count + 1 ) {
printf ( "count = %d\n", count );
}
/*
count = xx を xx を 1 ? 10 で 10 回出力
*/
for ( count = 0; count < 10; count = count + 1 ) {
int vcount = count + 1;
printf ( "count = %d\n", vcount );
printf ( "count = %d\n", vcount );
printf ( "count = %d\n", vcount );
printf ( "count = %d\n", vcount );
printf ( "count = %d\n", vcount );
}
/*
\sum_{i=0}^9 (i+1) = \sum_{i=1}^10 i
*/
return 0;
}
#include <stdio.h>
/*
fib ????
0 ( n = 0 ??? )
fib(n)= { 1 ( n = 1 ??? )
fib(n-1)+fib(n-2) ( ????? )
*/
int fib ( int n ) {
switch ( n ) {
case 0:
return 0;
break;
case 1:
return 1;
break;
default:
return fib(n-1)+fib(n-2);
break;
}
/*
if ( n == 0 ) {
return 0;
} else if ( n == 1 ) {
return 1;
} else {
return fib(n-1)+fib(n-2);
}
*/
}
#include <stdio.h>
void printn ( int n ) {
switch ( n ) {
default:
case 3:
printf ( "Hello, World\n" );
case 2:
printf ( "Hello, World\n" );
case 1:
printf ( "Hello, World\n" );
case 0:
break;
}
}
int main(int argc, char *argv[]) {
printf ( "n=2 ???\n" );
printn( 2 );
printf ( "n=0 ???\n" );
printn( 0 );
printf ( "n=10 ???\n" );
printn( 10 );
return 0;
}
#include <stdio.h>
void printn ( int n ) {
switch ( n ) {
default:
case 3:
printf ( "Hello, World\n" );
printf ( "Hello, World\n" );
printf ( "Hello, World\n" );
break;
case 2:
printf ( "Hello, World\n" );
printf ( "Hello, World\n" );
break;
case 1:
printf ( "Hello, World\n" );
break;
case 0:
break;
}
}
int main(int argc, char *argv[]) {
printf ( "n=2 ???\n" );
printn( 2 );
printf ( "n=0 ???\n" );
printn( 0 );
printf ( "n=10 ???\n" );
printn( 10 );
return 0;
}
[前回の復習]
while 構文
一つの文を繰り返す場合に、その文を while 構文の中で利用する
例:
int count = 1;
while ( count < 10 ) {
printf ( "%d\n", count ); /* count を出力 */
count = count + 1;
}
while によって、
{
printf ( "%d\n", count ); /* count を出力 */
count = count + 1;
}
が繰り返し実行されている
# while 構文の対象は、「一つの」文
# => 複数の文を対象にしたい場合は、ブロックにする
# => 繰り返しの命令、状態の変更命令の二つがペアなら..
# => 結局、いつでも、ブロックにした方がよい
# cf. if 構文の時も、ブロックを使わずに、「ぶら下がり構文」を
# 利用してもよいが、「この講義」では、「禁止」
# => 「構文の時には、必ず、ブロックにする」という
「マナー」を適用する(ルールではない)
printf : 書式付き出力関数 ( PRINT wiht Format )
printf ( 書式文字列, 追加の引数... )
書式文字列 : 基本は文字列だが、中に、書式指定が含まれる
書式指定 : %+英文字
printf は、書式文字列の書式以外の部分の文字はそのまま、
書式のある部分は、追加で指定された値を、その場所に埋め込む
例:
printf ( "%d+%d=%d\n", 123, 456, 123+456 );
=> %d %d %d
123+456=578
画面に情報を(文字列の形で表現して)出力するときに、大変便利
! これまでは、「書式指定の無い書式文字列」の例のみやった
! => 書式指定がないので、書式文字列がそのまま出力される
! => 文字列の出力関数
! [注意] printf は、非常に変な関数
! 変 : 引数の個数も型も固定されていない
! 普通の関数とは毛色が異なる(まだ、説明しない)
!!! 便利なので、積極的に使おう
scanf ( 書式指定, &付の変数名を並べる );
例:
int n; /* 入力した値を保持する変数 */
scanf ( "%d", &n ); /* キーボードから「123」と入力すると.. */
/* 変数 n に 整数値 123 が代入される */
[注意]
書式には、複数の書式指定ができる(追加引数にも複数の&+変数がかける)
=> 「マナー」
一つの scanf では、一つの変数への入力だけにする
書式文字列には、書式指定のみを記述する
[疑問]
printf と同じように、変な引数
書式文字列に書式指定以外のものを指定すると、どうなる ?
変数の前につける & はなに ?
=> 後回し
! scanf の利用は、必要最小限度(単独の変数に入力をおこないたい場合の簡便法)で使う
! printf と scanf は、書式指定の記述形式が同じなので、一緒に覚える
! 当分必要なのは % の後ろの英文字の意味
! 詳しくは google
!
! 書式指定 扱う値 型
! d 整数値 int
! f (lf) 浮動小数点数 double
! c 文字 char
! s (出力) 文字列 char *
[今日の話]
while 構文の続き
while 構文の働きを、これまで学んだ内容で解釈する
(その 1) if 構文が無限にあると思う
while ( 条件 ) {
文
}
=>
if ( 条件 ) {
文
}
if ( 条件 ) {
文
}
... (無限にならんでいる)
(その 2) 再帰呼び出しだと思う
変数=初期値
while ( 条件 ) {
文
変数=変数の新しい値(変数更新)
}
=>
関数呼び出し
関数(初期値)
関数定義(再帰呼び出しを利用して定義)
void 関数名(変数宣言){
if ( 条件 ) {
文
関数(変数の新しい値)
} else {}
}
! while 構文は、if 構文の並びあるいは、再帰呼び出しの関数の別表現として、解釈可能
!! while 構文は常に再帰関数に変換可能(逆も可能だが面倒)
[for 構文]
形式 :
for ( 変数の初期化; 条件; 変数の更新 ) {
文
}
意味 :
変数の初期化
while ( 条件 ) {
文
変数の初期化
}
while 構文と同様な機能をもつ(ほぼ同じ)
=> for 構文だと、パラメータ変数の記述が一か所にまとまる
# 繰り返し記述においては、
# 繰り返したい命令
# と
# 繰り返しを実現するための命令(パラメータの処理)
# の二つの要素からなる
C 言語では、「i++」(後置インクリメント演算)という形で、変数名の後ろに ++ を付ける事ができる
これの意味は、 ほぼ「i=i+1」と同じ
for 構文では、「i=i+1」とかく代わりに「i++」と書く事が多い
switch 構文
形式 : switch ( 式 ) {
「ラベル : 命令列」の繰り返し
}
ラベル : case 式 | default
命令列 : 命令の繰り返し
命令 : ふつうの文と break
意味:
まず、式が計算され、その値がもとめられる。
もし、その阿知と、ラベル式の値のなかで一致するものが
あれば、そのラベルの後ろから命令が実行される
そうでなければ、default の後ろ
ちなみに、default がない場合は、命令は一切実行されない
命令は、ラベルの所から、上から下へ、実行されるが、途中で、
break 命令に会うと、switch 構文を終了する(抜ける)
switch 構文は、「効率が良いが制限された」if 構文の塊と解釈できる
switch ( 式 ) {
case 式1: 命令1 ; break;
case 式2: 命令2 ; break;
..
case 式n: 命令n ; break;
default: 命令n+1l break;
}
if ( 式 == 式1 ) {
命令1
} else if ( 式 == 式2 ) {
命令2
} ...
} else if ( 式 == 式n ) {
命令n
} else {
命令n+1
}
一つの評価(式)に対して、複数の場合分けが必要な場合(多重分岐)は、
if 構文を組み合わせるより、switch 構文の方が、わかりやすい
「プログラムは思った通りに動かない、書いた通りに動く」
[まとめ]
新しい構文
for 構文
switch 構文
基本は、これまでの機能の別表現
1. 知らなくても、できる事は減らない(使わなくてもよい)
2. ほかの人がつかうかもしれないので、知っている必要
3. 「表現」の違いなので、「他の人間(三日後の自分を含む)」に、わかりやすくするために、利用する事ができる
[squash]
反射神経ゲーム
時間が刻々と進んでにつれて、様相が変化してゆく
それに対して、ゲームのプレイヤーが干渉する形になっている
MVC
Model 状況を表す情報(変数の値の組)
View 状況を(変数の値の組を利用して)表現機能
Control 状況表す兵法を入力によって変換させる機能
課題プログラム内の「/*名前:ここ*/」の部分を書き換え「/*この部分を完成させなさい*/」の部分にプログラムを追加して、プログラムを完成させます。
Download : 20181109-01.c
/*
* 20181109-01-QQQQ.c
*
* curses を利用して画面に、「回」の文字を書く
*
* コンパイル : curses を利用するのでリンク時に「-lncursesw」が必要
* cc -c 20181109-01-QQQQ.c
* cc -o 20181109-01-QQQQ.exe 20181109-01-QQQQ.o -lncursesw
* 実行 :
* ./20181109-01-QQQQ.exe
*
*/
#include <stdio.h>
#include <ncursesw/ncurses.h> /* ncurses の利用 */
#include <unistd.h>
/*
*
*/
#define OUT_X_POS 20 /* 「回」の外側の左上の x 座標 ( 1 〜 80 ) */
#define OUT_Y_POS 5 /* 「回」の外側の左上の y 座標 ( 1 〜 25 ) */
#define OUT_Y_SIZE 10 /* 「回」の外側の縦の長さ */
#define OUT_X_SIZE (OUT_Y_SIZE*2) /* 「回」の外側の横の長さ */
#define IN_Y_SIZE 6 /* 「回」の内側の縦の長さ */
#define IN_X_SIZE (IN_Y_SIZE*2) /* 「回」の内側の横の長さ */
#define IN_X_POS (OUT_X_POS+(OUT_X_SIZE-IN_X_SIZE)/2) /* 「回」の内側の左上の x 座標 */
#define IN_Y_POS (OUT_Y_POS+(OUT_Y_SIZE-IN_Y_SIZE)/2) /* 「回」の内側の左上の y 座標 */
/*
* draw_rect ( int px, int py, int xs, int ys )
* (px, py) の位置 に xs * ys の矩形を書く
*/
void draw_rect ( int px, int py, int xs, int ys ) {
int x; /* カーソルの x 座標を表す変数 */
int y; /* カーソルの y 座標を表す変数 */
/* 矩形の上下の線を書く */
for ( x = px; x < px + xs; x++ ) { /* 横方向に移動 */
move ( py, x ); addch ( '*' ); /* 上 */
move ( py + ys - 1, x ); addch ( '*' ); /* 下 */
}
/* 矩形の左右の線を書く */
for ( y = py; y < py + ys; y++ ) { /* 縦方向に移動 */
move ( y, px ); addch ( '*' ); /* 左 */
/*
** この部分を完成させなさい
*/
}
}
/*
* main
*/
int main( void )
{
/* ncurses の初期化 */
initscr(); /* 画面を初期化 (クリアされる) */
cbreak(); /* 文字の入力をリアルタイムにする */
noecho(); /* 文字の入力をエコーバックしない */
/* 画面に「回」を書く */
/* 外の四角 */
draw_rect ( OUT_X_POS, OUT_Y_POS, OUT_X_SIZE, OUT_Y_SIZE );
/* 内の四角 */
/*
** この部分を完成させなさい
*/
/* 描画結果の反映 */
refresh(); /* 書き換えた内容を画面に描画 */
/* 何かキーが押されるまで末 */
getch(); /* 文字の入力待ち */
/* 後始末 */
endwin(); /* curses の後始末 */
return 0;
}
123 987 456
********************
* *
* ************ *
* * * *
* * * *
* * * *
* * * *
* ************ *
* *
********************