当日のOHP資料です。
Download : sample-001.c ( SJIS 版 )
/*
* 2013/09/20 sample-001.c
*/
/*
* Hello, World
*
* いつでも、始まりはこれから..
*
* 利用方法
* コンパイル
* cc -Ic:\usr\c\include -o sample-001.exe sample-001.c
* 実行
* sample-001
*/
#include <stdio.h>
/*
* main
*/
int main( double argc, char *argv[] )
{
printf ( "Hello, World\n" );
return 0;
}
C:\usr\c>sample-001 Hello, World C:\usr\c>
Download : sample-002.c ( SJIS 版 )
/*
* 2013/09/20 sample-002.c
*/
/*
* n^2 の計算を足し算だけで実現する
*
* 利用方法
* コンパイル
* cc -Ic:\usr\c\include -o sample-002.exe sample-002.c
* 実行
* sample-002
*/
#include <stdio.h>
#include "s_input.h"
#include "s_print.h"
/*
* 四角数 http://ja.wikipedia.org/wiki/%E5%B9%B3%E6%96%B9%E6%95%B0
*/
/*
* main
*/
int main ( int argc, char *argv[] ) {
int n = 13; /* n=13 として n^2 = n*n = 13*13 = 169 を計算する */
int i = 0; /* i は、0 から n-1 まで動き、足し算の回数を示す */
int sum = 0; /* sum には、個々の odd の値が足しこまれる。最初は 0 */
int odd = 1; /* odd は、最初は 1 で、以後 2 ずつふえる(奇数) */
while ( i < n ) { /* i が n より小さい間 */
sum = sum + odd; /* sum に現在の奇数を加える */
odd = odd + 2; /* 次の奇数は、今の奇数に 2 を加えればよい */
i = i + 1; /* 何回加えたかを計数し、繰返し回数を確認する */
}
s_print_int ( sum ); /* 加えた結果が表示される n^2 になっているはず.. */
s_print_newline();
return 0;
}
C:\usr\c>sample-002 169 C:\usr\c>
Download : sample-003.c ( SJIS 版 )
/*
* 2013/09/20 sample-003.c
*/
/*
* printf の書式付き出力
*
* 利用方法
* コンパイル
* cc -Ic:\usr\c\include -o sample-003.exe sample-003.c
* 実行
* sample-003
*/
#include <stdio.h>
/*
* main
*/
int main( int argc, char *argv[] )
{
printf ( "5 + 4 = %d\n", 5 + 4 );
/* 文字列の中の「%d」の所に 9 ( = 5 + 4 ) が入る */
return 0;
}
C:\usr\c>sample-003 5 + 4 = 9 C:\usr\c>
Download : sample-004.c ( SJIS 版 )
/*
* 2013/09/20 sample-004.c
*/
/*
* printf の書式付き出力(その他色々)
*
* 利用方法
* コンパイル
* cc -Ic:\usr\c\include -o sample-004.exe sample-004.c
* 実行
* sample-004
*/
#include <stdio.h>
/*
* main
*/
int main( int argc, char *argv[] )
{
printf ( "%d + %d = %d\n", 5, 4, 5 + 4 );
/*
文字列の中の「%d」の所に
順番に数値を表す文字列が埋め込まれる */
return 0;
}
C:\usr\c>sample-004 5 + 4 = 9 C:\usr\c>
Download : sample-005.c ( SJIS 版 )
/*
* 2013/09/20 sample-005.c
*/
/*
* printf の書式付き出力(その他色々)
*
* 利用方法
* コンパイル
* cc -Ic:\usr\c\include -o sample-005.exe sample-005.c
* 実行
* sample-005
*/
#include <stdio.h>
/*
* main
*/
int main( int argc, char *argv[] )
{
printf ( "整数型の出力は %%d を使って「%d」と出せる。\n",
123 );
printf ( "浮動小数点数型の出力は %%f を使って「%f」と出せる。\n",
123.456 );
printf ( "文字型の出力は %%c を使って「%c」と出せる。\n",
'Z' );
printf ( "文字列の出力は %%s を使って「%s」と出せる。\n",
"pqr" );
return 0;
}
C:\usr\c>sample-005 整数型の出力は %d を使って「123」と出せる。 浮動小数点数型の出力は %f を使って「123.456000」と出せる。 文字型の出力は %c を使って「Z」と出せる。 文字列の出力は %s を使って「pqr」と出せる。 C:\usr\c>
Download : sample-006.c ( SJIS 版 )
/*
* 2013/09/20 sample-006.c
*/
/*
* printf の書式付き出力(その他色々)
*
* 利用方法
* コンパイル
* cc -Ic:\usr\c\include -o sample-006.exe sample-006.c
* 実行
* sample-006
*/
#include <stdio.h>
/*
* main
*/
int main( int argc, char *argv[] )
{
printf ( "書式は、混在させても出せる\n" );
printf ( "%d, %f, %c, %s\n", 10, 2.3, 'x', "abc" );
return 0;
}
C:\usr\c>sample-006 書式は、混在させても出せる 10, 2.300000, x, abc C:\usr\c>
Download : sample-007.c ( SJIS 版 )
/*
* 2013/09/20 sample-007.c
*/
/*
* scanf の書式付き入力
*
* 利用方法
* コンパイル
* cc -Ic:\usr\c\include -o sample-007.exe sample-007.c
* 実行
* sample-007
*/
#include <stdio.h>
/*
* main
*/
int main( int argc, char *argv[] )
{
int i;
printf ( "整数値を入力してください : " );
scanf ( "%d", &i );
/* 整数型なので「%d」の書式を付ける */
/* 変数 i の前に「&」を付ける(今回は「お呪い」考えてください)*/
printf ( "整数値 %d が入力されました。\n", i );
return 0;
}
123
C:\usr\c>sample-007< sample-007.in 整数値を入力してください : 123 整数値 123 が入力されました。 C:\usr\c>
Download : sample-008.c ( SJIS 版 )
/*
* 2013/09/20 sample-008.c
*/
/*
* scanf の書式付き入力(色々)
*
* 利用方法
* コンパイル
* cc -Ic:\usr\c\include -o sample-008.exe sample-008.c
* 実行
* sample-008
*/
#include <stdio.h>
/*
* main
*/
int main( int argc, char *argv[] )
{
int i;
double x;
/* 整数 */
printf ( "整数値を入力してください : " );
scanf ( "%d", &i );
/* 整数型なので「%d」の書式を付ける */
/* 変数 i の前に「&」を付ける(今回は「お呪い」と考える)*/
printf ( "整数値 %d が入力されました。\n", i );
/* 浮動小数点数 */
printf ( "浮動小数点数値を入力してください : " );
scanf ( "%lf", &x );
/* 浮動小数点数型なので「%lf」の書式を付ける */
/* 変数 x の前に「&」を付ける(今回は「お呪い」と考える)*/
printf ( "浮動小数点数値 %f が入力されました。\n", x );
return 0;
}
123 456.789
C:\usr\c>sample-008< sample-008.in 整数値を入力してください : 123 整数値 123 が入力されました。 浮動小数点数値を入力してください : 456.789000 浮動小数点数値 456.789000 が入力されました。 C:\usr\c>
課題プログラム内の「/*名前:ここ*/」の部分を書き換えたり、「/*この部分を完成させなさい*/」の部分にプログラムを追加して、プログラムを完成させます。
なお「名前(P,Q,R,..)」の部分が同じ所には同じものが入ります。
Download : 20130920-01.c ( SJIS 版 )
/*
* 課題 20130920-01
*
* 2013/09/20 20130920-01-QQQQ.c
*
* Hello, World (3 度目)
*/
#include <stdio.h>
/*
* main
*/
int main( int argc, char *argv[] )
{
/*
** この部分を完成させなさい
*/
return 0;
}
C:\usr\c\> 20130920-01-QQQQ Hello, World C:\usr\c\>
Download : 20130920-02.c ( SJIS 版 )
/*
* 課題 20130920-02
*
* 2013/09/20 DATE-02-QQQQ.c
*
* printf の書式指定
*
*/
#include <stdio.h>
/*
*
*/
/*
* main
*/
int main( int argc, char *argv[] )
{
int ia = 7;
int ib = 15;
double da = 3.14;
double db = 1.59;
printf ( "/* o:ここ */ + /* o:ここ */ = /* o:ここ */\n", /* p:ここ */, /* q:ここ */, \
/* p:ここ */ + /* q:ここ */ ); /* 整数型の和 */
printf ( "/* r:ここ */ / /* r:ここ */ = /* r:ここ */\n", /* s:ここ */, /* u:ここ */, \
/* s:ここ */ / /* u:ここ */ ); /* 浮動小数点数の商 */
return 0;
}
C:\usr\c\> 20130920-02-QQQQ 7 + 15 = 22 3.140000 / 1.590000 = 1.974843 C:\usr\c\>
Download : 20130920-03.c ( SJIS 版 )
/*
* 課題 20130920-03
*
* 2013/09/20 DATE-03-QQQQ.c
*
* scanf の書式指定
*
*/
#include <stdio.h>
/*
*
*/
/*
* main
*/
int main( int argc, char *argv[] )
{
int i; /* キーボードから入力された整数値を保持する整数型変数 */
double d; /* キーボードから入力された浮動小数点数値を保持する浮動小数点数型変数 */
printf ( "整数値をキーボードから入力します : " );
scanf ( "/* p:ここ */", /* q:ここ */ );
printf ( "入力された整数値は %d でした。\n", i );
printf ( "浮動小数点数値をキーボードから入力します : " );
scanf ( "/* r:ここ */", /* s:ここ */ );
printf ( "入力された浮動小数点数値は %f でした。\n", d );
return 0;
}
123 456.789
C:\usr\c\> 20130920-03-QQQQ 整数値をキーボードから入力します : 123 入力された整数値は 123 でした。 浮動小数点数値をキーボードから入力します : 456.789000 入力された浮動小数点数値は 456.789000 でした。 C:\usr\c\>