Cプログラミング・演習
プログラミング基礎II・演習II
2012年12月10日
今後の予定
■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
プログラミングの習得
■プログラミング言語を学ぶ唯一の方法は
「プログラムを書いてみること」
→ 練習課題,提出課題の実行
Cプログラミング・演習
プログラミング基礎II・演習II
2012年12月10日
第11回
#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; p1.x = 2; p1.y = 1; p2.x = 5; p2.y = -3; a = length(p1, p2); }
これまでの復習
#include < stdio.h > struct exam { int a; double b; };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; } #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; } 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; }
■実行後のaの値を示せ.
1
2
3
2012/12/10
Cプログラミング及びプログラミング演習II
7
http://www015.upp.so-net.ne.jp
/class/sec_ex11.html
復習
構造体について
今日の課題のURL
■構造体とは
複数の(異なる型の)変数を1つの変数に格納すること
が可能な変数
例)
構造体 health は以下の全ての情報を格納する
名前 (char型)
年齢 (int型)
身長 (double型)
構造体
health
名
前
年
齢
身
長
体
重
2012/12/10
Cプログラミング及びプログラミング演習II
9
■main関数の前に構造体を宣言する
struct 構造体の名前(
構造体タグ
)
{
構造体内部で使う変数(
メンバ
)の宣言
} ;
例)
struct health
{
char name[100];// 名前
int age;
// 年齢
double length; // 身長
double weight;// 体重
} ;
構造体 health
char
name
int
age
double length
double weight
構造体の宣言
<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
11
struct health
taro
;
taro.id
= 13066;
taro.age
= 20;
taro.length
= 180.0;
taro.weight
= 70.0;
printf(“学籍番号:%d, 年齢:%d ¥n”,
taro.id
,
taro.age
);
printf(“身長:%f, 体重%f ¥n”,
taro.length
,
taro.weight
);
struct health
{
int id;
// 学籍番号
int age;
// 年齢
double length;
// 身長
double weight;
// 体重
} ;
変数名 (taro) . メンバ名 (id, age, length, weight) で
参照と代入ができる
struct health
{
int id;
// 学籍番号
int age;
// 年齢
double length;
// 身長
double weight;
// 体重
} ;
struct health
{
int name[100];
// 名前
int age;
// 年齢
double length;
// 身長
double weight;
// 体重
} ;
struct health taro={ 13066, 20, 180.0, 70.0 };
2012/12/10
Cプログラミング及びプログラミング演習II
13
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 構造体タグ 配列変数名[個数];
参照及び代入:配列変数名[番号].メンバ
復習1
#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; p1.x = 2; p1.y = 1; p2.x = 5; p2.y = -3; a = length(p1, p2); }
1
x
y
point
p1 (2, 1)
p2 (5, -3)
a
x
y
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; result.a = x; result.b = (double)y/x; return result; } void main() { double a;
struct exam hensu; hensu = set(50, 120); a = hensu.b; }
2
a
b
exam
a
b
hensu
set(50, 120)
a = hensu.b
a = 2.4
構造体を返す
#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; } 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; }