Transcript Structure

Structure Programming
การเขียนโปรแกรม
คอมพิวเตอร ์
สั ปดาหที
์ ่ 10
ตัวแปรโครงสรางข
้
้อมูล(Structure)
Outline
1
Objective
2p
Structure
3
Array of Structure
4
Typedef (Type definition)
5
Assignment
objectives
เพือ
่ ให้นิสิตรูจั
วแปรโครงสร้าง
้ กและเขาใจตั
้
ข้อมูลในภาษาซี
สามารถเขียนโปรแกรมภาษาซีโดยใช้ตัวแปร
โครงสรางข
อมู
้
้ ลได้
สามารถนาความรูเรื
่ งตัวแปรโครงสรางข
อมู
้ อ
้
้ ลไป
ประยุกตเพื
่ แกไขโจทย
ปั
ิ ประจาวัน
์ อ
้
์ ญหาในชีวต
ไดได
กตองเหมาะสม
้ อย
้ างถู
่
้
Outline
1
Objective
2p
Structure
3
Array of Structure
4
Typedef (Type definition)
5
Assignment
Why structure variable is essential?
การเก็บข้อมูลเราอาจจาเป็ นต้องใช้ข้อมูลทีห
่ ลากหลาย
รูปแบบแตเป็
่ ส
ี ่ วนรวมกั
น
่ นกลุมข
่ ้อมูลทีม
่
• รายละเอีย ดของนัก ศึ กษาอาจจะมีข้ อมู ล รหัส ชื่อ
สกุ ล อายุ เกรดเฉลี่ย ของนั ก ศึ กษา ซึ่ ง เรา
จะต้ องสร้ างตัว แปรขึ้น มาใช้ ในการเก็ บ ข้ อมูล ของ
นักศึ กษา 5 ตัวแปรดวยกั
น
้
ทัง้ ทีจ
่ ริง แล้ว ข้อมูล ทัง้ หมดมีส่วนเกีย
่ วข้อง คือเป็ น
ของนักศึ กษาคนเดียวกัน หากมีจานวนนักศึ กษามากๆ
การสร้างตัวแปรและใช้งานตัวแปร อาจจะเกิดความ
สั บสนขึ้นได้ ดังนั้นเราจึง ควรนาโครงสร้างข้ อมูลมา
ช่วยในการจัดการกับข้อมูลเหลานี
่ ้
Structure Variable
โครงสร้างข้อมูล (Structure) เป็ นการกาหนดชนิด
ข้อมูลใหมขึ
้ มาใช้งาน โดยชนิดข้อมูลนี้เกิดจากการ
่ น
รวมกั น ของ ชนิ ด ข้ อมู ล พื้ น ฐา นที่ อ า จ จ ะเป็ นชนิ ด
เดียวกันหรือตางชนิ
ดกันก็ได้
่
ตัวอยาง
อมู
่ วา่ “Student”
่ : โครงสรางข
้
้ ลนักศึ กษาให้ชือ
ประกอบดวย
้
ชือ
่ เป็ นชนิด char
นามสกุลเป็ นชนิด char
อายุเป็ นชนิด int
เพศเป็ น char
เกรดเฉลีย
่ เป็ น float
Structure Variable Declaration
struct structure_name {
type variable_1;
type variable_2;
type variable_3;
………
}
คือ บ่งบอกว่าเป็ นตัวแปรชนิดโครงสร้าง(Structure)
struct_name คือ ชื่อของตัวแปรชนิ ดโครงสร้าง
type variable_1-n คือ ตัวแปรที่เกี่ยวข้องกับตัวแปรโครงสร้างที่
ต้องการเก็บ
struct
Structure VS. Array
•
•
•
อะเรย ์ (array)
เก็บข้อมูลจานวนมากๆ ได
int้ point[5]
เข้าถึงข้อมูลแตละตั
วไดโดยง
าย
่
้
่
ข้อจากัด
:
ข้อมูลเหลานั
่ ้ นต้องเป็ นชนิด
เดียวกันเทานั
่ ้น
โครงสร้างข้อมูล (Structure)
struct ช
student
{ น ก็ ไ ด้
•
ข้อมูล แต่ละตัวในกลุมสามารถมี
นิดต่างกั
่
char
เช่น ข้อมูลผลการเรียนของนั
กศึ กstdName[20];
ษาแตละคน
่
•
char stdLastName[20];
เทียบเทากั
บ
Record
ในภาษาอื
น
่
่
}
int age;
float grade;
Example of Information
student
นายวีกจ
ิ นางสาวนิน
วีกจ
ิ
มุนินทร ์
รักเป็ นไทย
จงสวัสดิ ์
16
17
char sex[7];
ชาย
หญิง
float grade;
2.95
3.25
char name[20];
char surname[30];
int
age;
Example of Variable Declaration
struct student {
char
char
int
char
float
name[20];
surname[30];
age;
sex[7];
grade;
int
day, month, year;
char
char
day[3], month[3];
year[5];
};
struct date {
};
struct sdate {
};
พิจ ารณาสมาชิกในโครงสร้ างข้อมูล เราเรียกข้อมูล
ยอยเหล
านี
่
่ ้วา่ “ฟิ ลด ์ (Field)”
struct address
{
char name[30];
char detail[50];
int age;
char telephone[10];
};
struct student
{
char name[30];
char surname[50];
int Id;
};
ชือ
่ ของ Structure
สมาชิกขอมู
้ ลภายในมี 4 ฟิ ลด ์
(4 fields)
Memory Usage
struct book {
char
float
int
}
code
6 bytes
code[6];
price;
year;
price
4 bytes
year
2 bytes
Member Accessing
การอ้ างถึง สมาชิก ในตัว แปรชนิ ด โครงสร้ าง ท าได้
โดยบอกชือ
่ ตัวแปรโครงสร้างตามดวยจุ
ด และตอด
้
่ ้วย
ชือ
่ สมาชิกของโครงสร้างนั้นๆ
• มีรป
ู แบบดังนี้
structure_name.member;
student1.name อ้างถึงขอมู
่ ของตัว
้ ลชือ
แปร student1
student1.surname อ้างถึงขอมู
้ ลสกุลของ
ตัวแปร student1
birthday.day
อ้างถึงขอมู
้ ลวันของตัว
Declaration & Assignment Value
#include <stdio.h>
struct income {
float
salary, bonus;
int
age;
};
void main(void)
{
struct income somsri;
somsri.salary = 18000.00;
somsri.bonus = 30000.00;
somsri.age
= 23;
}
Example 1
#include <stdio.h>
#include <string.h>
struct database {
int id_number;
int age;
float salary;
};
int main()
{
database employee;
has modifiable
//There is now an employee variable that
// variables inside it.
employee.age = 22;
employee.id_number = 1;
employee.salary = 12000.21;
return 0;
}
Declaration & Assignment Value (cont.)
สามารถก าหนดค่ าทั้ง หมดในตัว แปรได้ โดยใช้
เครื่ อ งหมาย{ } คร่ อมค่ าเริ่ ม ต้ นทั้ง หมด และใช้
เครื่ อ งหมาย , คั่น ระหว่ างค่ าของสมาชิ ก แต่ ละตัว
ตัวอยางเช
่
่น
struct
{
int
int
int
} ;
struct
d1
date
day;
month;
year;
date d1 = {10,5,1992};
day
10
month
5
year
1992
String Assignment
ถ้าสมาชิกของโครงสร้างเป็ นข้อความ เราจะกาหนด
ตรงๆ เหมือนสมาชิกทีเ่ ป็ นชนิดอืน
่ ไมได
่ ้
วิธก
ี ารกาหนดข้อมูลชนิดข้อความ ให้ทาการกาหนด
ผานค
าสั่ ง strcpy หรือรับคาจากคี
ยบอร
ดด
่
่
์
์ ้วยคาสั่ ง
gets หรือ scanf ก็ได้
#include <stdio.h>
#include <string.h>
struct letter {
char
name[20];
char
address[30];
char
message[40];
};
void main(void)
{
struct letter first;
printf("Enter name :");
gets(first.name);
printf("Enter address :");
scanf("%s",first.address);
strcpy(first.message, "How r u?");
printf("\nNAME is %s",first.name);
printf("\nAddress is %s",first.address);
printf("\nMessage : %s",first.message);
}
Initial Value to Member
การกาหนดคาเริ
่ ต้นให้ตัวแปรขอความ
่ ม
้
struct subject
{
char name[20];
int credit;
char grade;
} s1 = {"Physics I”, 3, 'A'};
หรือ
struct subject
s1
name
Physics I
credit
3
grade
A
{
char name[20];
int credit;
char grade;
};
struct subject s1 = {"Physics I”, 3, 'A'};
Copy Value in Same Structure
การคัดลอก (Copy)
ค่าของตัว แปรโครงสร้างชนิ ด
เดียวกัน สามารถทาไดโดยใช
่ งหมาย " = " เช่น
้
้เครือ
struct date {
int day,month,year;
} d1, d2;
แทนทีจ
่ ะกาหนดคาสมาชิ
กทีละตัว
่
d2.day = d1.day;
d2.month = d1.month;
d2.year = d1.year;
สามารถกาหนดคาเป็
เลยเช
่ นตัวแปรโครงสรางได
้
้
่ น d2
d1;
=
Comparison of Structure
ในการเปรีย บเทีย บค่ าของตัว แปรโครงสร้ างนั้ น ให้
เปรียบเทียบคาของสมาชิ
กแตละตั
ว
่
่
น าตัว แปรโครงสร้ างมาเปรีย บเทีย บกัน โดยตรงไม่ ได้
เช่น หากมีการตัวแปรชนิด struct date ชือ
่ วา่ d1
และ d2
struct date {
int day,month,year
} d1,d2;
จะนา d1 และ d2 มาเปรียบเทียบกันโดยตรงไมได
่ ้
if(d1 == d2)
printf(“d1 is the same date as d2”);
Outline
1
Objective
2p
Structure
3
Array of Structure
4
Typedef (Type definition)
5
Assignment
Array of Structure
ปกติเราจะใช้ข้อมูลชนิดโครงสร้างมากกวาหนึ
่ง
่
ข้ อมู ล เช่ นโครงสร้ างนั ก เรีย นประกอบด้ วย
รหัส, ชื่อ, เพศ, และอายุ โดยส่วนมากเราจะมี
ข้อมูลของนักเรียนหลายคน ดังนั้นเราสามารถ
สร้ างตัว แปรของนัก เรีย นให้ เป็ นอาเรย ของตั
ว
์
แปรโครงสร้ าง เพื่ อ ท าการเก็ บ ข้ อมู ล แบบ
โครงสรางไว
เป็
่ วข้องกัน เช่น
้
้ นกลุมข
่ ้อมูลทีเ่ กีย
โครงสร้างนัก เรีย น ซึ่ง โรงเรีย นหนึ่ ง จะต้องมี
นักเรียนหลายคน จะต้องเก็บข้อมูลของนักเรียน
่ นามาประมวลผลตอไป
เหลานี
่
่ ้เพือ
Array of Structure (cont.)
ข้อมูลโครงสราง
1 ตัว ก็คอ
ื ขอมู
้
้ ล 1 รายการ (1
record) เช่น ประกาศขอมู
ด student
้ ลโครงสรางชนิ
้
struct student {
char code[9];
char name[41];
float
mid, lab, fin;
char
grade;
} stu1;
stu1 เป็ นตัวแปรโครงสราง
้ สามารถเก็บขอมู
้ ลนักศึ กษาได้
1 คน
ต้องการเก็บขอมู
้ ลนักศึ กษาหลายๆ คนนั้น ไมต
่ ้องประกาศ
ตัวแปรโครงสรางหลายๆ
ตัว
้
โดยสามารถสรางอาเรย
ของโครงสร
าง
้
้ (Arrays of
์
structures) ได้
Array of Structure (cont.)
การประกาศอาเรยของข
อมู
รป
ู แบบ
้ ลชนิดโครงสรางมี
้
์
ดังนี้ struct structure_name member
เช่น
struct
student
[size];
{
char code[9];
char name[41];
float
mid, lab, fin;
char
grade;
};
struct
student
stu[3];
ไดตั
่ ส
ี มาชิก 3 ตัว (เพือ
่ เก็บขอมู
้ วแปรอาเรยที
้ ล
์ ม
น.ศ 3 คน)
สมาชิกแตละตั
วในอาเรย ์ คือตัวแปรโครงสราง
่
้
Memory Usage
#include <stdio.h>
main() {
struct profile {
char name[19];
int
age;
char grade;
};
struct profile student[3];
}
nam
e
student[0]
student[1]
student[2]
20 bytes
age
2 1
grad
e
Assignment Value to Array of Structure
struct structure_name member
[size] = {
{ value_list 1},
{ value_list 2},
...
{ value_list n}
};
ตัวอยางเช
่
่น
struct
student
stu[100]
={
30, ‘C’} ,
};
{“61007332”, “Somchai”, 20, 15,
{“61017332”, “Decha”, 25, 18, 40, ‘A’} ,
{“61027332”, “Tanee”, 12, 15, 25, ‘D’}
Example 3
#include <stdio.h>
#include <string.h>
main() {
struct profile {
char name[20];
int age;
char grade;
}student[10];
//Assignment value to each field of member
strcpy(student[0].name, “Manee”);
student[2].age = 12;
student[4].grade = ‘A’;
//show some field of each member
printf(“%s\n”,student[0].name);
printf(“%d\n”,student[2].age);
printf(“%c\n”,student[4].grade);
}
Example 4
1.#include <stdio.h>
2.#include <conio.h>
3.void main()
4.{
5. struct
6. {
7.
char id[10];
8.
char name[30];
9.
int age;
10. } std[4];
11. int i;
12.*
13. printf("Please <Enter> for Input 5 record: \n");
14. for(i=0;i<4;i++)
15.
{
16.
printf("-------------------------------------------\n");
17.
printf("RECORD # %d \n",i+1);
18.*
19.
printf("\tCODE : ");
20.
scanf("%s",std[i].id);
Example 4 (cont.)
21. printf("\tNAME : ");
22. scanf("%s",std[i].name);
23. printf("\tAGE:");
24. scanf("%d",&std[i].age);
25. }
26. //show the output to user
27. printf("\n------ OUT PUT ------ \n");
28. printf(" CODE
NAME
AGE\n");
29. //For-loop for show information of each user
30. for(i=0; i<4; i++)
31. {
32.
printf("%s %s %d \n",std[i].id ,std[i].name,std[i].age);
33. }
34. }
Result
/* The mechanism of a structure */
/* Define a type "person" to include firstname,surname, age
and child count */
#include <stdio.h>
#include <string.h>
struct {
char firstname[20];
char surname[20];
float age;
int childcount; } person;
int main () {
/* Create two persons */
person jimmy, flossie;
float avage;
/* Fill up each of the persons with information */
strcpy(jimmy.firstname,"James");
strcpy(jimmy.surname,"Conway");
jimmy.age = 32.0;
jimmy.childcount = 2;
strcpy(flossie.firstname,"Florence");
strcpy(flossie.surname,"Conway");
flossie.age = 21.0;
flossie.childcount = 2;
/* Calculate the average age of the persons */
avage = (flossie.age + jimmy.age) / 2.0;
/* Print out the persons names and the average age */
printf ("About %s and %s ...n",flossie.firstname,jimmy.forename);
printf ("Their average age is %.2fn",avage);
return 0;
}
Structure of Structure
struct date
{
strcpy(p1.name,"Somchai");
int day;
p1.birthday.day = 10;
int month;
p1.birthday.month = 3;
int year;
p1.birthday.year = 1992;
};
struct person
{
char name[30];
struct date birthday;
};
struct person p1;
Structure of Structure
struct date
{
struct node
int day;
{
int month;
char data;
int year;
struct node next;
};
};
struct person
{
char name[30];
struct date birthday;
};
ห้ ามซ้ อนข้ อมูลชนิด
โครงสร้ างแบบเดียวกันใน
ตัวเอง
Outline
1
Objective
2p
Structure
3
Array of Structure
4
Typedef (Type definition)
5
Assignment
Type definition
ในภาษาซี สามารถกาหนดชนิดตัวแปรขึน
้ มา
ใหมได
่ ้ โดยใช้คาสั่ ง
typedef (type definition)
รูปแบบ: typedef ชนิดตัวแปรทีม
่ อ
ี ยูแล
้
่ ว
ตัวแปรใหม;่ เช่น
typedef int my_int;
บการนิยาม
รูปแบบการใช้ typedef รวมกั
่
structure:
typedef struct{
type variable 1;
type variable 2;
ชนิด
Type definition (cont.)
ตัวอย่าง
typedef struct {
int day,month,year;
} date;
ในการประกาศตัวแปรก็สามารถใช้ ชนิดตัวแปรใหม่ ได้ เลย เช่ น
date d1,d2;
ข้อสั งเกต เมือ
่ กาหนดตัวแปรหลังจากการกาหนดดวย
้
ไมต
่ ้องมีคาวา่ struct นาหน้าชนิดขอมู
้ ลอีก
How to use structure variable?
struct info
{
char firstName[20];
char lastName[20];
int age;
};
typedef struct
{
char firstName[20];
char lastName[20];
int age;
} info;
info j;
struct info i1, i2;
struct info k;
info j;
Typedef of Typedef
typedef struct {
char firstName[20];
char lastName[20];
int age;
} InfoT;
typedef struct {
InfoT info;
double salary;
} EmployeeT;
EmployeeT e1;
e1.info.age = 21;
Example 5 (Regular Form)
#include<stdio.h>
typedef struct telephone
{
char *name;
int number;
}TELEPHONE;
int main()
{
TELEPHONE index;
index.name = "Jane Doe";
index.number = 12345;
printf("Name: %s\n", index.name);
printf("Telephone number: %d\n", index.number);
return 0;
}
Outline
1
Objective
2p
Structure
3
Array of Structure
4
Typedef (Type definition)
5
Assignment
Assignment
1. ให้นิสิตสรางข
อมู
น
้ มา เพือ
่
้
้ ลชนิดโครงสรางขึ
้
ใช้เก็บขอมู
้ ลดังนี้
1. รหัสนิสิต (ID)
2. เกรดเฉลีย
่ (ตัวแปรแบบทศนิยม)
3. รับข้อมูลจากแป้นพิมพ ์ มาเก็บไว้ โดยจะ
เก็บขอมู
้ ลนิสิตจานวน 5 คน
4. ทาการเรียงขอมู
่ เี กรดน้อยสุด
้ ลของนิสิตทีม
ไปหาเกรดมากสุดจากนั้นแสดงรหัสนิสิตที่
เรียงตามเกรดเฉลีย
่ พรอมกั
บเกรดทีเ่ รียงดวย
้
้