Cプログラミング・演習
アルゴリズム基礎論・演習
今後の予定
■12/22(月) 期末試験(60分間) 場所:A1611 時間:16:20~17:20 ■課題の最終提出締切:12/19(金) これ以降の新規提出は評価されない 12/22までに最終状況を提示するので, 提出したのに△や×になってる人は自分の提出内容や 提出先を再確認した上で12/26までに問い合わせること■構造体とは 複数の(異なる型の)変数を1つの変数に格納すること が可能な変数 例) 構造体 health は以下の全ての情報を格納する 名前 (char型) 年齢 (int型) 身長 (double型) 体重 (double型)
構造体
health 名前 年齢 身長 体重■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 構造体タグ 変数名;
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 };
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 構造体タグ 配列変数名[個数]; 参照及び代入:配列変数名[番号].メンバ復習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 a = 5 x y
復習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; } 3
復習3
m n bunsu 構造体を引数 ↓ 構造体を返す 10 3 6 5 2 7 A B C a = D.n a = 105 D = A × (B × C)メールでの課題提出に関して
• 課題を2種類用意 • 練習課題: • 特に指定がない場合は, プログラムを提出する必要はない • 各課題の指示に従った内容をメール本文に記述 • 提出課題: • プログラムを提出(コメントは各自つけること) • ファイル名:第4回の場合 「sec04-J123456-final.cpp」 • 注意点: • 毎回,送り先のメールアドレスは変わるので注意 • 提出先を間違うと,課題が受理されないことがある■ファイル操作の関数 fopen():ファイルを開く(ファイル名の指定) fclose():ファイルを閉じる ■ファイル入出力関数 fprintf():ファイルへ値を書き込む fscanf():ファイルから値を読み込む
ファイル操作及び入出力関数
main()
{
FILE *fp;
fp = fopen(“data.txt”, “r”);
…….
…….
fp はファイルポインタ fopen(“ファイル名”, “モード”)fopen()とファイルポインタ
「どの」ファイルの「どこ」を読んでいるのかを示すポインタ
Space: The final frontier These are the voyages of the Starship, Enterprise
Its 5 year mission
To explore strange new worlds To seek out new life and new civilizations
To boldly go where no man has gone before このファイルの ここまで読んだ ポインタ fp で示す
ファイルポインタ
ファイルをオープンし,ファイルポインタを返す FILE *fp; fp = fopen(“ファイル名”, “モード”); モード 動作 ファイルがあるとき ファイルがないとき r 読み出し専用 正常 エラー NULLを返す w 書き込み専用 サイズを0にして上書き 新規作成 a 追加書き込み専用 最後に追加する 新規作成 r+ 読み込みと書き込み 正常 エラー NULLを返す w+ 書き込みと読み込み サイズを0にして上書き 新規作成 a+ 読み込みと追加書き込み 最後に追加する 新規作成
fopen()関数
■ファイルがオープンできなかった場合,ファイルポインタは NULLを返す -“r”(ファイル読み込み)でファイルが存在しない場合 -“w”(ファイル書き込み)でファイルが書き込み禁止になっ ている場合 FILE *fp; fp = fopen(“data.txt”,”r”); if ( fp == NULL ) { printf(“ファイルオープンエラー ¥n”); exit(1); // プログラム終了 }
fopen()のエラー処理
ファイル操作後にファイルを使わなくなった場合には fclose() 関数でファイルをクローズ
fclose(fp);
fprintf() fprintf( ファイルポインタ, “書き込む内容”, …); ファイルポインタを指定する以外は printfと同じ int x=1; FILE *fp; fp = fopen(“data.txt”,”w”); fprintf(fp,“書き込みテスト ¥n”); fprintf(fp,“変数xの値は%dです”, x); fclose(fp); 書き込みテスト 変数xの値は1です data.txt 書き込みモードでオープン
ファイルへの値の書き込み
fscanf() fscanf(ファイルポインタ, ”書式”, … ) ファイルポインタを指定する以外はfscanfと同じ int x1, x2, x3; FILE *fp; fp = fopen(“data.txt”, “r”); fscanf(fp,”%d”, &x1); fscanf(fp,”%d”, &x2); fscanf(fp,”%d”, &x3); fclose(fp); 10 15 3 17 19 data.txt 読み込みモードでオープン
x1 = 10
x2 = 15
x3 = 3
ファイルから値の読み込み
1
■ファイルの終わりとして,fscanf()はEOFを返す main() { int x; FILE *fp fp = fopen(“data.txt”,”r”); if ( fp == NULL ) exit(1);
while ( fscanf(fp, “%d”, &x) != EOF ) { printf(“x=%d ¥n”, x); } fclose(fp); } 10 15 3 17 19 data.txt EOF