Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
ตวัแปรโครงสรา้ง
(Structure)
ดร.ธีระยุทธ ทองเครอื
ภาควิชาวิทยาการคอมพิวเตอร ์ คณะวิทยาศาสตร์
มหาวิทยาลัยขอนแก่น
บทท่ี 10
การส่งข้อมูลหลายค่าไปยงัฟังก์ชัน#include <stdio.h>
void catalog(char name[], char species[], int teeth, int age) {printf("%s is a %s with %d teeth. He is %d\n\n", name,
species, teeth, age);}
void label(char name[], char species[], int teeth, int age) {printf("Name:%s\nSpecies:%s\n%d years old, %d teeth\n", name,
species, teeth, age);}
void main() {catalog("Snappy", "Piranha", 69, 4);label("Snappy", "Piranha", 69, 4);
}
ส่งขอ้มูลปลาตวัเดียวกนั
ไปยงั 2 ฟังกช์นั
มีปลาตวัเดียว แต่ตอ้งส่ง argument ไปหลายตวั
2
Structure
Structure คือ ตวัแปรชนิดโครงสร้าง ท่ีสามารถเกบ็มูลไดห้ลาย
ค่าเหมือนกบั array
ตวัแปร Structure ต่างจาก array คือ สมาชิกทุกตวัไม่จาํเป็นตอ้ง
เป็นชนิดขอ้มูลเดียวกนักไ็ด้
3
ตวัแปรชนิดโครงสร้าง
อาร์เรย์
ตวัแปรชนิดโครงสร้าง หรือ Structure
-54.514.012.02.58.06.012.016.0
x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7]
Snappy
69
4
name
teeth
age
Fish
Index หรอื Subscript
ช่ือฟิลด์ (Field)
Piranhaspecies
4
ทาํไมต้องใช้ Structure
การส่ง argument ท่ีมีขอ้มูลเก่ียวกบัเร่ืองเดียวกนั เช่น ขอ้มูล
นกัศึกษา ขอ้มูลสตัวเ์ล้ียง ไปยงั function เม่ือมีการเปล่ียนแปลง
หรือเพ่ิมตวัแปร จะตอ้งตามไปแกไ้ขทุก function
หากสามารถเกบ็ตวัแปรหลายชนิดเป็นกอ้นเดียวกนั และส่งไป
ยงัฟังกช์นัเหล่าน้ีได ้จะง่ายต่อการพฒันาโปรแกรมมากข้ึน
5
การสร้าง Structure
รูปแบบ
typedef struct {ชนิดข้อมูล ตวัแปรท่ี 1;
ชนิดข้อมูล ตวัแปรท่ี 2;
...
ชนิดข้อมูล ตวัแปรท่ี n;
} ช่ือโครงสร้าง;
ตวัอยา่ง
typedef struct {
char name[20];
char species[10];
int teeth;
int age;
} Fish;
6
กจิกรรมจงออกแบบ Structure สาํหรับเกบ็ขอ้มูลรถยนต์
#include <stdio.h>
____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
7
การประกาศตวัแปร Structure
Structure ท่ีสร้างข้ึนเป็นเพียงโครงสร้างขอ้มูลเท่านั้น ยงัไม่ได้
ถูกจองบนหน่วยความจาํ การนาํไปใชจ้ะตอ้งประกาศเหมือนกบั
ตวัแปรตวัหน่ึง ซ่ึงมีรูปแบบดงัน้ี
ตวัอยา่ง
Fish snappy;
ช่ือStructure ช่ือตวัแปร;
8
การกาํหนดค่าเร่ิมต้น
รูปแบบการประกาศค่าเร่ิมตน้มีลกัษณะเดียวกบัอาร์เรย ์โดยจะ
กาํหนดค่าของแต่ละฟิลดใ์นเคร่ืองหมาย { }
ช่ือStructure ช่ือตวัแปร = { รายการค่าคัน่ด้วย , };
Fish snappy = {"Snappy", "Piranha", 69, 4};
9
ตวัอย่าง
#include <stdio.h>
typedef struct {char name[20];char species[10];int teeth;int age;
} Fish;
void main() {Fish snappy = {"Snappy", "Piranha", 69, 4};printf("%s\n", snappy.name);printf("%s\n", snappy.species);printf("%d\n", snappy.teeth);printf("%d\n", snappy.age);
}
SnappyPiranha694
10
กจิกรรมจาก Structure สาํหรับเกบ็ขอ้มูลรถยนตใ์นกิจกรรมก่อนหนา้น้ี จงประกาศใชต้วัแปร
กาํหนดค่าเร่ิมตน้ และพิมพค์่าในแต่ละฟิลดอ์อกทางหนา้จอ
#include <stdio.h>/* ละไวใ้นสว่น Structure ทีอ่อกแบบไปแลว้ */void main() {____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________}
11
การเข้าถงึและกาํหนดค่าให้กบั Structure
รูปแบบ
ตวัอยา่ง
snappy.age = 7;
scanf("%s", &snappy.name);
printf("%d\n", snappy.age);
ช่ือStructure.ช่ือฟิลด;์
12
ตวัอย่าง
Snappy Piranha 69 4
#include <stdio.h>
typedef struct {char name[20];char species[10];int teeth;int age;
} Fish;
void main() {Fish snappy;strcpy(snappy.name, "Snappy");strcpy(snappy.species, "Piranha");snappy.teeth = 69;snappy.age = 4;printf("%s %s %d %d\n", snappy.name, snappy.species,
snappy.teeth, snappy.age);}
13
การส่งตวัแปร structure ไปยงัฟังก์ชัน
#include <stdio.h>
void catalog(char name[], char species[], ...) {printf("...", name, species, teeth, age);
}
void label(char name[], char species[], ...) {printf("...", name, species, teeth, age);
}
void main() {catalog("Snappy", "Piranha", 69, 4);label("Snappy", "Piranha", 69, 4);
}
#include <stdio.h>
typedef struct {char name[20];char species[10];int teeth;int age;
} Fish;
void catalog(Fish f) {printf("...", f.name, f.species, f.teeth, f.age);
}
void label(Fish f) {printf("...", f.name, f.species, f.teeth, f.age);
}
void main() {Fish snappy = {"Snappy", "Piranha", 69, 4};catalog(snappy);label(snappy);
}
ส่งแบบหลายค่า ส่งตวัแปรเดยีว
14
อาร์เรย์ของ Structure
รูปแบบ
ตวัอยา่ง
Fish fishList[4];
ช่ือStructure ช่ือตวัแปร[ขนาด];
15
การกาํหนดค่าเร่ิมต้นFish fishList[4] = {
{"Snappy", "Piranha", 69, 4},{"Golfy", "Tuna", 6, 1},{"Nobita", "Shushi", 3, 2},{"Pikachu", "Mackerel", 6, 15}
};
name species teeth ageSnappy Piranha 69 4Golfy Tuna 6 1Nobita Shushi 3 2Pikachu Mackerel 6 15
fishList[0]
fishList[1]
fishList[2]fishList[3]
16
การกาํหนดค่าและเข้าถงึสมาชิก
// การกาํหนดค่า
strcpy(fishList[2].name, "Chinjang");fishList[2].age = 3;
// การรับค่า
scanf("%d", &fishList[2].teeth);
// การแสดงค่า
printf("%s\n", fishList[2].name);
17
ตวัอย่าง#include <stdio.h>
typedef struct {char name[20];char species[10];int teeth;int age;
} Fish;
Fish inputFish() {Fish f;scanf("%s", &f.name);scanf("%s", &f.species);scanf("%d", &f.teeth);scanf("%d", &f.age);return f;
}
void catalog(Fish f) {printf("%s is a %s with %d teeth. He is %d\n\n",
f.name, f.species, f.teeth, f.age);}
void main() {Fish fishList[4];
int i;for(i=0; i<4; i++) {
fishList[i] = inputFish();}
for(i=0; i<4; i++) {catalog(fishList[i]);
}}
18
การใช้ pointer กบั structure
รูปแบบ
ตวัอยา่งFish fisho;Fish *p;p = &fisho;scanf("%s", &p->name);scanf("%s", &p->species);
ช่ือพอยเตอร-์>ช่ือฟิลด;์
19
ตวัอย่าง
#include <stdio.h>
typedef struct {char name[20];char species[10];int teeth;int age;
} Fish;
void catalog(Fish f) {printf("%s is a %s with %d teeth. He is %d\n\n",
f.name, f.species, f.teeth, f.age);}
void main() {Fish fisho;Fish *p;p = &fisho;
scanf("%s", &p->name);scanf("%s", &p->species);scanf("%d", &p->teeth);scanf("%d", &p->age);
catalog(fisho);}
20
กจิกรรม
จงเขียนโปรแกรมคาํนวณระยะห่างระหวา่งจุด 2 จุด คือ P1(x1, y1) และ
P2(x2, y2) โดยใชสู้ตร
โดยสร้าง Structure ช่ือ Point เพื่อใชใ้นการเกบ็พิกดั และเขียนฟังกช์นัดงัน้ี
1. ฟังกช์นัรับขอ้มูลทางคียบ์อร์ด โดยเกบ็ขอ้มูลพิกดัลงตวัแปร Structure
Point getPoint();
2. ฟังกช์นัหาค่าระยะทางระหวา่งจุด 2 จุด
float distance(Point p1, Point p2);
𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑 = (𝑥𝑥2 − 𝑥𝑥1)2+(𝑦𝑦2 − 𝑦𝑦1)2
Enter P1 (x1,y1) (separate by space): 5 6 ↵Enter P2 (x2,y2) (separate by space): 2 3 ↵distance = 4.24
ตวัอยา่งหนา้จอโปรแกรม
21
Assignment#10ข้อ 1. จงเขียนโปรแกรมรับขอ้มูลรหสั
นกัศึกษา ช่ือนกัศึกษา และคะแนน 4 คน
โดยออกแบบ Structure และสร้างเป็น
อาร์เรยข์อง Structure เกบ็ขอ้มูลท่ีรับจากผูใ้ช ้
หลงัจากรับค่าเสร็จใหดึ้งค่าออกมาแสดง
พร้อมกบัหาค่าเฉล่ียของคะแนน
Student ID: 562030111-1 ↵Student Name: Jim ↵Score: 55 ↵
Student ID: 562030111-2 ↵Student Name: John ↵Score: 76 ↵
Student ID: 562030111-3 ↵Student Name: Joe ↵Score: 49 ↵
Student ID: 562030111-4 ↵Student Name: Jimmy ↵Score: 76 ↵
ID Name Score=====================================562030111-1 Jim 55562030111-2 John 76562030111-3 Joe 49562030111-4 Jimmy 76
Score Average = 64.00 ตวัอยา่งการทาํงาน
22
Assignment#10ข้อ 2. จงเขียนโปรแกรมรับคะแนนสอบวชิาการเขียนโปรแกรมภาษาซีของนกัเรียน n
คน เพ่ือคาํนวณหาเกรดท่ีจะได ้โดยแบ่งคะแนนออกเป็น คะแนนสอบ Lab 15 คะแนน
คะแนนสอบ Midterm และ Final 30 คะแนน คะแนนเขา้เรียน 10 คะแนน และคะแนน
งาน 15 คะแนน ใหอ้อกแบบ Structure และสร้างเป็นอาร์เรยข์อง Structure เกบ็ขอ้มูล
ท่ีรับจากผูใ้ช ้หลงัจากรับค่าเสร็จใหดึ้งค่าออกมาแสดง พร้อมกบัแสดงเกรดท่ีได้ ซ่ึงมี
เกณฑด์งัน้ี
ช่วงคะแนน เกรดทีไ่ด้
คะแนนมากกว่าหรือเท่ากบั 80 A
คะแนนมากกว่าหรือเท่ากบั 70 B
คะแนนมากกว่าหรือเท่ากบั 60 C
คะแนนมากกว่าหรือเท่ากบั 50 D
คะแนนน้อยกว่า 50 F
23
Assignment#10Enter amount : 3
Enter Student no.1>>> Enter student id : 1 ↵>>> Enter score Laboratory Test : 14 ↵>>> Enter score Midterm Test : 23 ↵>>> Enter score Homework : 11 ↵>>> Enter score Final Test : 20 ↵Enter Student no.2>>> Enter student id : 2 ↵>>> Enter score Laboratory Test : 2 ↵>>> Enter score Midterm Test : 10 ↵>>> Enter score Homework : 12 ↵>>> Enter score Final Test : 11 ↵Enter Student no.3>>> Enter student id : 3 ↵>>> Enter score Laboratory Test : 1 ↵>>> Enter score Midterm Test : 5 ↵>>> Enter score Homework : 15 ↵>>> Enter score Final Test : 10 ↵
ID Lab Test Midterm Homework Final Sum Grade1 14.00 23.00 11.00 20.00 68.00 C2 2.00 10.00 12.00 11.00 35.00 F3 1.00 5.00 15.00 10.00 31.00 F
ตวัอยา่งการทาํงาน
24
กาํหนดส่ง
ภาคปกติ
ส่งภายในวนัท่ี 14 พ.ย. 59 เวลา 23.00 น.
ภาคสมทบ
ส่งภายในวนัท่ี 16 พ.ย. 59 เวลา 23.00 น.
25