当日のOHP資料です。
Download : sample-001.c ( SJIS 版 )
/*
* 2011/12/02 sample-001.c
*/
/*
*
*/
#include <stdio.h>
#include "s_print.h"
#include "s_input.h"
/*
* 二つの整数を入力し、その和を計算して出力する ( Input-Process-Output )
*/
int main ( void ) {
int a;
int b;
int wa;
/*
* データの入力 (Input)
*/
s_print_string ( "変数 a の値(整数値)を入力してください : " );
a = s_input_int();
s_print_string ( "変数 b の値(整数値)を入力してください : " );
b = s_input_int();
/*
* データの処理 (Process)
*/
wa = a + b;
/*
* データの出力 (Output)
*/
s_print_int ( a );
s_print_string ( "と、" );
s_print_int ( b );
s_print_string ( "の和は" );
s_print_int ( wa );
s_print_string ( "です。\n" );
/*
*
*/
return 0;
}
/*
*
*/
4 8
C:\usr\c\> sample-001< sample-001.in 変数 a の値(整数値)を入力してください : 4 変数 b の値(整数値)を入力してください : 8 4と、8の和は12です。 C:\usr\c\>
Download : sample-002.c ( SJIS 版 )
/*
* 2011/12/02 sample-002.c
*/
/*
*
*/
#include <stdio.h>
#include "s_print.h"
#include "s_input.h"
/*
* 整数値の入力
*/
int input_int_with_message ( char *name ) {
s_print_string ( "変数 " );
s_print_string ( name );
s_print_string ( "の値(整数値)を入力してください : " );
return s_input_int();
}
/*
* 和の出力 : あまり便利になっていない
*/
int output_wa ( int a, int b, int wa ) {
s_print_int ( a );
s_print_string ( "と、" );
s_print_int ( b );
s_print_string ( "の和は" );
s_print_int ( wa );
s_print_string ( "です。\n" );
}
/*
* 二つの整数を入力し、その和を計算して出力する ( Input-Process-Output )
*/
int main ( void ) {
int a;
int b;
int wa;
/*
* データの入力 (Input)
*/
a = input_int_with_message ( "a" );
b = input_int_with_message ( "b" );
/*
* データの処理 (Process)
*/
wa = a + b;
/*
* データの出力 (Output)
*/
output_wa ( a, b, wa );
/*
*
*/
return 0;
}
/*
*
*/
4 8
C:\usr\c\> sample-002< sample-002.in 変数 aの値(整数値)を入力してください : 4 変数 bの値(整数値)を入力してください : 8 4と、8の和は12です。 C:\usr\c\>
Download : sample-003.c ( SJIS 版 )
/*
* 2011/12/02 sample-003.c
*/
/*
*
*/
#include <stdio.h>
#include "s_print.h"
#include "s_input.h"
/*
*
*/
#define EOS '\0'
/*
* 文字列の中に整数値を埋め込んだ出力
*/
void print_string_with_one_int ( char msg[], int value ) {
int i;
for ( i = 0; msg[i] != EOS; i++ ) {
if ( msg[i] != '%' ) { /* 文字列の中に '%' があったら int にする */
s_print_char ( msg[i] );
} else {
s_print_int ( value );
}
}
}
/*
* 整数値の入力
*/
int input_int_with_message ( char *name ) {
s_print_string ( "変数 " );
s_print_string ( name );
s_print_string ( "の値(整数値)を入力してください : " );
return s_input_int();
}
/*
* 和の出力 : 少し簡便化
*/
int output_wa ( int a, int b, int wa ) {
print_string_with_one_int ( "% と、", a );
print_string_with_one_int ( "% の和は", b );
print_string_with_one_int ( "% です。\n", wa );
}
/*
* 二つの整数を入力し、その和を計算して出力する ( Input-Process-Output )
*/
int main ( void ) {
int a;
int b;
int wa;
/*
* データの入力 (Input)
*/
a = input_int_with_message ( "a" );
b = input_int_with_message ( "b" );
/*
* データの処理 (Process)
*/
wa = a + b;
/*
* データの出力 (Output)
*/
output_wa ( a, b, wa );
/*
*
*/
return 0;
}
/*
*
*/
4 8
C:\usr\c\> sample-003< sample-003.in 変数 aの値(整数値)を入力してください : 4 変数 bの値(整数値)を入力してください : 8 4 と、8 の和は12 です。 C:\usr\c\>
Download : sample-004.c ( SJIS 版 )
/*
* 2011/12/02 sample-004.c
*/
/*
*
*/
#include <stdio.h>
#include "s_print.h"
#include "s_input.h"
/*
*
*/
#define EOS '\0'
/*
* 複数の整数を文字列の中に埋め込む
*/
void print_string_with_ints ( char msg[], int value, ... ) {
int i;
int *pvalue = &value;
for ( i = 0; msg[i] != EOS; i++ ) {
if ( msg[i] != '%' ) { /* 文字列の中に '%' があったら int にする */
s_print_char ( msg[i] );
} else {
s_print_int ( *pvalue++ ); /* *pvalue を出力, pvalue = pvalue + 1 */
}
}
}
/*
* 整数値の入力
*/
int input_int_with_message ( char *name ) {
s_print_string ( "変数 " );
s_print_string ( name );
s_print_string ( "の値(整数値)を入力してください : " );
return s_input_int();
}
/*
* 和の出力 :
*/
int output_wa ( int a, int b, int wa ) {
print_string_with_ints ( "% と、% の和は% です。\n", a, b, wa );
}
/*
* 二つの整数を入力し、その和を計算して出力する ( Input-Process-Output )
*/
int main ( void ) {
int a;
int b;
int wa;
/*
* データの入力 (Input)
*/
a = input_int_with_message ( "a" );
b = input_int_with_message ( "b" );
/*
* データの処理 (Process)
*/
wa = a + b;
/*
* データの出力 (Output)
*/
output_wa ( a, b, wa );
/*
*
*/
return 0;
}
/*
*
*/
4 8
C:\usr\c\> sample-004< sample-004.in 変数 aの値(整数値)を入力してください : 4 変数 bの値(整数値)を入力してください : 8 4 と、8 の和は12 です。 C:\usr\c\>
Download : sample-005.c ( SJIS 版 )
/*
* 2011/12/02 sample-005.c
*/
/*
*
*/
#include <stdio.h>
#include "s_print.h"
#include "s_input.h"
/*
*
*/
#define EOS '\0'
/*
* 複数の整数を書式指定して文字列の中に埋め込む
*/
void print_ints_with_format ( char msg[], int value, ... ) {
int i;
int *pvalue = &value;
for ( i = 0; msg[i] != EOS; i++ ) {
if ( msg[i] == '%' ) { /* 文字列の中に '%' があったら特別処理する */
i++; /* 次の文字をみる */
switch ( msg[i] ) {
case 'd': /* 10 進数 */
s_print_int ( *pvalue++ );
break;
case 'o': /* 8 進数 */
s_print_oct ( *pvalue++ );
break;
case 'x': /* 16 進数 */
s_print_hex ( *pvalue++ );
break;
case '%': /* '%' が重なったら.. */
s_print_char ( '%' ); /* '%' を出力 */
break;
default: /* その他 : よくわからないので 10 進で.. */
s_print_int ( *pvalue++ );
break;
}
} else { /* そうでなけれ .. */
s_print_char ( msg[i] ); /* そのままその文字を出力 */
}
}
}
/*
* 整数値の入力
*/
int input_int_with_message ( char *name ) {
s_print_string ( "変数 " );
s_print_string ( name );
s_print_string ( "の値(整数値)を入力してください : " );
return s_input_int();
}
/*
* 整数を入力し、それを 10, 8, 16 進数で出力する
*/
int main ( void ) {
int a;
/*
* データの入力 (Input)
*/
a = input_int_with_message ( "a" );
/*
* データの出力 (Output)
*/
print_ints_with_format ( "%d は 8 進(%%o) だと %o になり、は 16 進(%%x) だと %x \
になります。\n", a, a, a );
/*
*
*/
return 0;
}
/*
*
*/
123
C:\usr\c\> sample-005< sample-005.in 変数 aの値(整数値)を入力してください : 123 123 は 8 進(%o) だと 173 になり、は 16 進(%x) だと 7b になります。 C:\usr\c\>
Download : sample-006.c ( SJIS 版 )
/*
* 2011/12/02 sample-006.c
*/
/*
*
*/
#include <stdio.h>
#include "s_print.h"
#include "s_input.h"
/*
*
*/
#define EOS '\0'
/*
* 複数の整数を書式指定して文字列の中に埋め込む
*/
void print_with_format ( char *msg, ... ) {
int i;
char *pvalue = (char *)&msg + sizeof( char * );
for ( i = 0; msg[i] != EOS; i++ ) {
if ( msg[i] == '%' ) { /* 文字列の中に '%' があったら特別処理する */
i++; /* 次の文字をみる */
switch ( msg[i] ) {
case 'd': /* 10 進数 */
s_print_int ( *((int *)pvalue) ); // s_print_int ( *pvalue++ );
pvalue += sizeof ( int );
break;
case 'o': /* 8 進数 */
s_print_int ( *((int *)pvalue) );
pvalue += sizeof ( int );
break;
case 'x': /* 16 進数 */
s_print_int ( *((int *)pvalue) );
pvalue += sizeof ( int );
break;
case 'c': /* 文字 */
s_print_char ( *((char *)pvalue) );
pvalue += sizeof ( int ); /* char は自動的に int にされる */
break;
case 's': /* 文字列 */
s_print_string ( *((char **)pvalue) );
pvalue += sizeof ( char * );
break;
case 'f': /* 浮動小数点数 */
s_print_double ( *((double *)pvalue) );
pvalue += sizeof ( double );
break;
case '%': /* '%' が重なったら.. */
s_print_char ( '%' ); /* '%' を出力 */
break;
default: /* その他 : よくわからないので読み飛ばす.. */
break;
}
} else { /* そうでなけれ .. */
s_print_char ( msg[i] ); /* そのままその文字を出力 */
}
}
}
/*
* 色々な数値の出力
*/
int main ( void ) {
/*
* データの出力 (Output)
*/
print_with_format (
"整数値(%%d) : %d, 文字(%%c) : '%c', 文字列(%%s) : \"%s\", 浮動小数点数(%%f) : \
%f\n",
123, (int)'a', "xyz", 1.23 );
/*
*
*/
return 0;
}
/*
*
*/
C:\usr\c\> sample-006
整数値(%d) : 123, 文字(%c) : 'a', 文字列(%s) : "xyz", 浮動小数点数(%f) : \
1.230000
C:\usr\c\>
Download : sample-007.c ( SJIS 版 )
/*
* 2011/12/02 sample-007.c
*/
/*
*
*/
#include <stdio.h>
/*
*
*/
/*
* 色々な数値の出力
*/
int main ( void ) {
int i;
char c;
char s[10];
double d;
/*
* データの入力 (Output)
*/
scanf ( "%d%c%s%lf", &i, &c, s, &d );
/*
* データの出力 (Output)
*/
printf (
"整数値(%%d) : %d, 文字(%%c) : '%c', 文字列(%%s) : \"%s\", 浮動小数点数(%%f) : \
%f\n",
i, c, s, d );
/*
*
*/
return 0;
}
/*
*
*/
123 a xyz 1.23
C:\usr\c\> sample-007< sample-007.in C:\usr\c\>
Download : sample-008.c ( SJIS 版 )
/*
* 2011/12/02 sample-008.c
*/
/*
*
*/
#include <stdio.h>
/*
*
*/
/*
* 色々な数値の出力
*/
int main ( void ) {
int i;
char c;
char s[10];
double d;
/*
* データの出力 (Input)
*/
scanf ( "%d", &i );
scanf ( "%c", &c );
scanf ( "%s", s );
scanf ( "%lf", &d );
/*
* データの出力 (Output)
*/
printf (
"整数値(%%d) : %d, 文字(%%c) : '%c', 文字列(%%s) : \"%s\", 浮動小数点数(%%f) : \
%f\n",
i, c, s, d );
/*
*
*/
return 0;
}
/*
*
*/
123axyz 1.23
C:\usr\c\> sample-008< sample-008.in
123
a
xyz
1.230000
整数値(%d) : 123, 文字(%c) : 'a', 文字列(%s) : "xyz", 浮動小数点数(%f) : \
1.230000
C:\usr\c\>
Download : 20111202-01.c ( SJIS 版 )
/*
* 20111202-1-QQQQ.c
*/
#include <stdio.h>
#include "s_print.h"
#include "s_input.h"
/*
*
*/
typedef struct {
int x;
int y;
} Point2D;
typedef struct {
Point2D sp;
Point2D ep;
} Line2D;
/*
* 複数の整数を書式指定して文字列の中に埋め込む
*/
void myprintf ( char *msg, ... ) {
int i;
char *pvalue = (char *)&msg + sizeof( char * );
Point2D pv;
Line2D lv;
for ( i = 0; msg[i] != EOS; i++ ) {
if ( msg[i] == '%' ) { /* 文字列の中に '%' があったら特別処理する */
i++; /* 次の文字をみる */
switch ( msg[i] ) {
case 'd': /* 10 進数 */
s_print_int ( *((int *)pvalue) );
pvalue += sizeof ( int );
break;
case 'o': /* 8 進数 */
s_print_int ( *((int *)pvalue) );
pvalue += sizeof ( int );
break;
case 'x': /* 16 進数 */
s_print_int ( *((int *)pvalue) );
pvalue += sizeof ( int );
break;
case 'c': /* 文字 */
s_print_char ( *((char *)pvalue) );
pvalue += sizeof ( int ); /* char は自動的に int にされる */
break;
case 's': /* 文字列 */
s_print_string ( *((char **)pvalue) );
pvalue += sizeof ( char * );
break;
case 'f': /* 浮動小数点数 */
s_print_double ( *((double *)pvalue) );
pvalue += sizeof ( double );
break;
case 'D': /* Point2D 型 */
pv = *((Point2D *)pvalue);
/* pv の値を (%d,%d) の形式で出力する */
myprintf ( "(%d,%d)", pv.x, pv.y );
/*
** この部分を完成させなさい
*/
/* 次のデータを処理するために pvalue 変更 */
pvalue = ...
/*
** この部分を完成させなさい
*/
break;
case '%': /* '%' が重なったら.. */
s_print_char ( '%' ); /* '%' を出力 */
break;
default: /* その他 : よくわからないので読み飛ばす.. */
break;
}
} else { /* そうでなけれ .. */
s_print_char ( msg[i] ); /* そのままその文字を出力 */
}
}
}
/*
* 色々な数値の出力
*/
int main ( void ) {
Point2D pv;
pv.x = 2;
pv.y = -3;
/*
* データの出力 (Output)
*/
myprintf (
"整数値(%%d) : %d, 文字(%%c) : '%c', 文字列(%%s) : \"%s\", 浮動小数点数(%%f) : %f, \
点の座標 (%%D) : %D\n",
123, (int)'a', "xyz" , 1.23, pv );
/*
*
*/
return 0;
}
/*
*
*/
1 2 3 4 5 6 7 8 9 0 0 1 0 1 0 1 0 0
C:\usr\c\> 01< 01.in
整数値(%d) : 123, 文字(%c) : 'a', 文字列(%s) : "xyz", 浮動小数点数(%f) : \
1.230000, 点の座標 (%D) : (2,-3)
C:\usr\c\>