C プログラミング 演習 プログラミング基礎 II 演習 II 2012 年 12 月 10 日 第 11 回
今後の予定 2012/12/10 C プログラミング及びプログラミング演習 II 2 2012/12/17 期末試験 (60 分間 ) 場所 :A0542 時間 :4 時限 (15:00~16:00) 2012/12/16 課題の最終提出締切
課題の提出方法 電子メールで提出 提出先 ( 宛先 ) - hcilab@rs.kagu.tus.ac.jp メールの題名 ( 件名 ) - 後期第 回課題学籍番号例 ) 後期第 11 回課題 J123456 Jは大文字 ハイフン (-) はいらない 第 回の は及び学籍番号は半角数字 メールの本文 - 課題のプログラムをそのまま記述 2012/12/10 Cプログラミング及びプログラミング演習 II 3
2012/12/10 C プログラミング及びプログラミング演習 II 4 プログラミングの習得 プログラミング言語を学ぶ唯一の方法は プログラムを書いてみること 練習課題, 提出課題の実行 習うより慣れろ というのが経験的な習得方法
2012/12/10 Cプログラミング及びプログラミング演習 II 5 C プログラミング 演習 プログラミング基礎 II 演習 II 2012 年 12 月 10 日 第 11 回
2012/12/10 C プログラミング及びプログラミング演習 II 6 #include < stdio.h > #include < math.h > struct point int x; int y; ; double length(struct point a1, struct point a2) double xx, yy; xx = (a1.x - a2.x) * (a1.x - a2.x); yy = (a1.y - a2.y) * (a1.y - a2.y); return sqrt(xx + yy); // sqrt(x) は x の平方根を求める void main() struct point p1, p2; double a; 実行後の a の値を示せ. p1.x = 2; p1.y = 1; p2.x = 5; p2.y = -3; a = length(p1, p2); これまでの復習 1 #include < stdio.h > struct exam int a; double b; ; 2 #include < stdio.h > struct bunsu int m; int n; ; 3 struct exam set(int x, int y) struct exam result; result.a = x; result.b = (double)y/x; return result; void main() double a; struct exam hensu; hensu = set(50, 120); a = hensu.b; struct bunsu seki(struct bunsu X, struct bunsu Y) struct bunsu ans; ans.m = X.m * Y.m; ans.n = X.n * Y.n; return ans; void main() struct bunsu A = 10, 3; struct bunsu B = 6, 5; struct bunsu C = 2, 7; struct bunsu D; int a; D = seki(a, seki(b, C)); a = D.n;
2012/12/10 C プログラミング及びプログラミング演習 II 7 今日の課題の URL http://www015.upp.so-net.ne.jp /class/sec_ex11.html 復習構造体について
2012/12/10 C プログラミング及びプログラミング演習 II 8 構造体 構造体とは複数の ( 異なる型の ) 変数を1つの変数に格納することが可能な変数例 ) 構造体 health は以下の全ての情報を格納する 名前 (char 型 ) 年齢 (int 型 ) health 身長 (double 型 ) 名前 年齢 身長 体重 体重 (double 型 )
構造体の宣言 main 関数の前に構造体を宣言する struct 構造体の名前 ( 構造体タグ ) ; 構造体内部で使う変数 ( メンバ ) の宣言 例 ) struct health ; char name[100];// 名前 int age; // 年齢 double length; // 身長 double weight;// 体重 構造体 health char int double double name age length weight 2012/12/10 C プログラミング及びプログラミング演習 II 9
構造体変数の宣言 <stdio.h> struct health char name[100]; int age; double length; double weight; ; // 名前 // 年齢 // 身長 // 体重 void main() struct health konishi; struct health yamada; struct health taro, hanako;... struct 構造体タグ変数名 ; 2012/12/10 C プログラミング及びプログラミング演習 II 10
2012/12/10 C プログラミング及びプログラミング演習 II 11 構造体への値の代入と参照 struct health taro; taro.id= 13066; taro.age = 20; taro.length = 180.0; taro.weight = 70.0; struct health int id; int age; double length; double weight; ; // 学籍番号 // 年齢 // 身長 // 体重 printf( 学籍番号 :%d, 年齢 :%d n, taro.id, taro.age); printf( 身長 :%f, 体重 %f n, taro.length, taro.weight); 変数名 (taro). メンバ名 (id, age, length, weight) で参照と代入ができる
2012/12/10 C プログラミング及びプログラミング演習 II 12 構造体変数の宣言 2 struct health int id; int age; double length; double weight; ; // 学籍番号 // 年齢 // 身長 // 体重 struct health taro= 13066, 20, 180.0, 70.0 ; struct health int name[100]; int age; double length; double weight; ; // 名前 // 年齢 // 身長 // 体重 struct health taro= Taro YAMADA, 20, 180.0, 70.0 ;
構造体の配列 struct score int math; int english; double average; ; main() struct score student[100]; student[0].math = 100; student[0].english = 10; student[1].math = 50;... 宣言 :struct 構造体タグ配列変数名 [ 個数 ]; 参照及び代入 : 配列変数名 [ 番号 ]. メンバ 2012/12/10 C プログラミング及びプログラミング演習 II 13
2012/12/10 C プログラミング及びプログラミング演習 II 14 復習 1 #include < stdio.h > #include < math.h > 1 point struct point int x; int y; ; double length(struct point a1, struct point a2) double xx, yy; x y xx = (a1.x - a2.x) * (a1.x - a2.x); yy = (a1.y - a2.y) * (a1.y - a2.y); return sqrt(xx + yy); // sqrt(x) は x の平方根を求める void main() struct point p1, p2; double a; p1.x = 2; p1.y = 1; p2.x = 5; p2.y = -3; a = length(p1, p2); y p1 (2, 1) a p2 (5, -3) x a = 5
2012/12/10 C プログラミング及びプログラミング演習 II 15 復習 2 #include < stdio.h > struct exam int a; double b; ; struct exam set(int x, int y) struct exam result; 2 構造体を返す exam a b result.a = x; result.b = (double)y/x; return result; void main() double a; set(50, 120) hensu a struct exam hensu; hensu = set(50, 120); a = hensu.b; b a = hensu.b a = 2.4
2012/12/10 C プログラミング及びプログラミング演習 II 16 復習 3 #include < stdio.h > struct bunsu int m; int n; ; struct bunsu seki(struct bunsu X, struct bunsu Y) struct bunsu ans; ans.m = X.m * Y.m; ans.n = X.n * Y.n; return ans; 3 構造体を引数 構造体を返す bunsu m n A B C void main() struct bunsu A = 10, 3; struct bunsu B = 6, 5; struct bunsu C = 2, 7; struct bunsu D; int a; 10 3 6 5 2 7 D = seki(a, seki(b, C)); a = D.n; D = A (B C) a = D.n a = 105
Kadai11.txt からデータを構造体配列 UEFA[] に読み込み, 試合数 (match) や得点 (score) の平均を求める関数 UEFA_ave(), 試合数や得点の最大値及び最小値を求める関数 UEFA_MaxMin() を完成させよ 名前 (name) をアルファベットの昇順 (A Z) に並べ替える関数 UEFA_sort1(), チーム名 (team) をアルファベットの降順 (Z A) に並べ替える関数 UEFA_sort2() を完成させよ 各結果をファイル (Result11.txt) 及び通常出力するプログラムを完成させよ Kadai11.txt は名前, チーム, 試合数, 得点の順で 6 名分のデータとなっている 表示例 Messi Barcelona 14 21 Ronaldo RealMadrid 14 13 Uchida Schalke 10 1 Nagatomo InterMilan 14 0 Ibrahimovic PSG 12 13 Pirlo Juventus 13 3 提出期限 :2012/12/14( 金 )24:00 提出先 :hcilab@rs.kagu.tus.ac.jp 提出課題 平均試合数 =??? 最大値 =??, 最小値 =?? 平均得点 =??? 最大値 =??, 最小値 =?? 名前の並べ替え Ibrahimovic PSG 12 13... Uchida Schalke 10 1 チーム名の並べ替え Messi Barcelona 14 21... Uchida Schalke 10 1 2012/12/10 Cプログラミング及びプログラミング演習 II 17
課題の提出に関して 電子メールで提出 提出先 ( 宛先 ) - hcilab@rs.kagu.tus.ac.jp メールの題名 ( 件名 ) - 後期第 回課題学籍番号例 ) 後期第 11 回課題 J123456 Jは大文字 ハイフン (-) はいらない 第 回の は及び学籍番号は半角数字 メールの本文 - 課題のプログラムをそのまま記述 2012/12/10 Cプログラミング及びプログラミング演習 II 18