242 101 Introduction to Computer Programming 6 Structure

  • Slides: 48
Download presentation
242 -101 Introduction to Computer Programming บทท 6 สตรคเจอร Structure คณาจารยภาควชาวศวกรรมคอมพวเต อร introcom@coe. psu.

242 -101 Introduction to Computer Programming บทท 6 สตรคเจอร Structure คณาจารยภาควชาวศวกรรมคอมพวเต อร introcom@coe. psu. ac. th Department of Computer Engineering, PSU 1

242 -101 Introduction to Computer Programming ตวอยางการนยามโครงสรางขอมล ชอของกลมโครงสรางขอมล struct client { char name[30]; char

242 -101 Introduction to Computer Programming ตวอยางการนยามโครงสรางขอมล ชอของกลมโครงสรางขอมล struct client { char name[30]; char address[50]; int age; char telephone[10]; }; มสมาชกภายใน (4 fields) Department of Computer Engineering, PSU 4 ตว ชอของกลมโครงสรางขอมล struct student { char name[30]; char surname[50]; int id; }; มสมาชกภายใน (3 fields) 3 ตว 8

242 -101 Introduction to Computer Programming ตวอยางท 1. การเขาถงสมาชกในสตรคเจอร การนยามและประกาศตวแ ปรของกลมขอมลตวเล ขเชงซอน struct complex

242 -101 Introduction to Computer Programming ตวอยางท 1. การเขาถงสมาชกในสตรคเจอร การนยามและประกาศตวแ ปรของกลมขอมลตวเล ขเชงซอน struct complex { double real; double imag; } num 0 ; การกำหนดคา เชน num 1. real = 1; num 1. imag = 2. 5; num 0. real = num 1. real; num 0. imag = num 1. imag; struct complex num 1; Department of Computer Engineering, PSU 14

ตวอยางท 2. การเขาถงสมาชกแบบ ในสตรคเจอร struct employee { char name[25]; char surname[50]; int age; int

ตวอยางท 2. การเขาถงสมาชกแบบ ในสตรคเจอร struct employee { char name[25]; char surname[50]; int age; int pay; } emp; Department of Computer Engineering, PSU 242 -101 Introduction to Computer Programming การกำหนดคา array เชน emp. age = 32; strcpy(emp. name, ”David”); strcpy(emp. surname, ”Lee”); emp. name[4] = 'n'; 15

242 -101 Introduction to Computer Programming แบบฝกหด จงเขยนผลลพธของโปรแกรม #include <stdio. h< int main() {

242 -101 Introduction to Computer Programming แบบฝกหด จงเขยนผลลพธของโปรแกรม #include <stdio. h< int main() { struct complex { double real; double imag; } x, y; x. real = 4; x. imag = 0. 5; y. real = x. real + y. imag = x. imag + printf(“y= %. 2 f + return 0; } of Computer Engineering, PSU Department x re 4 im 0. 5 y re 5 ผลลพธ im 0. 75 y = 5. 00 + 0. 75 i 1; 0. 25; %. 2 fin, y. real, y. imag); 16

แบบฝกหด #include <stdio. h< #include <string. h< 242 -101 Introduction to Computer Programming จงเตมสวนทขาดหายไป

แบบฝกหด #include <stdio. h< #include <string. h< 242 -101 Introduction to Computer Programming จงเตมสวนทขาดหายไป int main() { ������� struct person { Andy Murray, 26 years old. char name[30]; Telephone: 074212895 int age; char tel[10]; } p 1; _ _ _ _ _ _ // 1. strcpy(p 1. name, “Andy Murray"); _ _p 1. age _ _ _ = _ _ _ _ // 2. 26; _ _strcpy(p 1. tel, "074212895"); _ _ _ _ _ // 3. printf("%s, %d years old. n", p 1. name, p 1. age); printf("Telephone: %sn", p 1. tel); return 0; } Department of Computer Engineering, PSU 17

แบบฝกหด 242 -101 Introduction to Computer Programming จงเขยนผลลพธสวนทเหลอ ผลลพธ #include <stdio. h> Enter subject

แบบฝกหด 242 -101 Introduction to Computer Programming จงเขยนผลลพธสวนทเหลอ ผลลพธ #include <stdio. h> Enter subject name: Physics I #include <string. h> Enter credit: 3 int main() { Physics I, credit: 4 struct subject { char name[30]; s 1 int credit; name Physics I } s 1, s 2; printf("Enter subject name: "); credit 3 gets("%s", s 1. name); s 2 printf("Enter credit: "); scanf("%d", &s 1. credit); name Physics I s 2 = s 1; credit 4 3 s 2. credit = s 2. credit + 1; printf("%s, credit: %dn", s 2. name, s 2. credit); return 0; } Department of Computer Engineering, PSU 19

242 -101 Introduction to Computer Programming สตรคเจอรทมสมาชกเปนสตรคเจอร struct date } int day; int month;

242 -101 Introduction to Computer Programming สตรคเจอรทมสมาชกเปนสตรคเจอร struct date } int day; int month; int year; }; struct person { char name[30]; struct date birthday; }; Department of Computer Engineering, PSU struct person p 1; strcpy(p 1. name, ”Somchai p 1. birthday. day = 10; p 1. birthday. month = 5; p 1. birthday. year = 1998; 20

242 -101 Introduction to Computer Programming การกำหนดคาเรมตนใหกบตวแปรสตรคเ จอร (ตอ ) struct subject { char

242 -101 Introduction to Computer Programming การกำหนดคาเรมตนใหกบตวแปรสตรคเ จอร (ตอ ) struct subject { char name[20]; int credit; char grade; } s 1 = {"Physics I", 3, 'A'}; s 1 name Physics I credit 3 grade A Department of Computer Engineering, PSU 22

แบบฝกหด 242 -101 Introduction to Computer Programming จงเตมสวนทขาดหายไป #include <stdio. h> int main() {

แบบฝกหด 242 -101 Introduction to Computer Programming จงเตมสวนทขาดหายไป #include <stdio. h> int main() { struct date { int day, month, year; ������� Warakorn Wannarat }; Birthday: 15/5/1998 struct student { char name[30]; struct date bday; }; struct student std = "}Warakorn _ _ _Wannarat", {15, 5, 1998}}; _ _ _ printf("%s n", std. name); printf("Birthday: %d/%d/%dn", std. bday. day, std. bday. month, std. bday. year); return 0; } Department of Computer Engineering, PSU 23

ตวอยางท 3. 242 -101 Introduction to Computer Programming #include <stdio. h> จงเตมสวนทขาดหายไปเพอใช int main()

ตวอยางท 3. 242 -101 Introduction to Computer Programming #include <stdio. h> จงเตมสวนทขาดหายไปเพอใช int main() ในการเปรยบเทยบวาจำนวนเช { struct complex{ งซอนทรบจากผใช (y) double re, im; วามคาเทากบ x หรอไม }; struct complex y, x = {1, 0. 25}; printf(“Enter complex number: "); scanf("%lf%lf", &y. re, &y. im); if((x. re _ _ _== _ y. re) _ _ _ && _ _(x. im _ _ _== _ y. im(( _ _ // 1. printf("x and y is the same complex. n"); _ _ _ // 2. else printf("x and y is not the same complex. n"); return 0; } Department of Computer Engineering, PSU 25

242 -101 Introduction to Computer Programming ตวอยางท 4. เปรยบเทยบการใช struct info { char first.

242 -101 Introduction to Computer Programming ตวอยางท 4. เปรยบเทยบการใช struct info { char first. Name[20]; char last. Name[20]; int age; }; struct info i 1, i 2; info j; typedef กบ st typedef struct { char first. Name[20]; char last. Name[20]; int age; } Info; Info j; struct Info k; typedef struct info. Type; info. Type i 3, i 4; Department of Computer Engineering, PSU 29

ตวอยางท 242 -101 Introduction to Computer Programming 5. typedef ทมสมาชกเปน typedef struct { char

ตวอยางท 242 -101 Introduction to Computer Programming 5. typedef ทมสมาชกเปน typedef struct { char first. Name[20]; char last. Name[20]; int age; } Info. T; typedef struct { Info. T info; double salary; } Employee. T; Employee. T e 1; e 1. info. age = 21; Department of Computer Engineering, PSU 30

แบบฝกหด 242 -101 Introduction to Computer Programming จงเขยนผลลพธของโปรแกรม x #include <stdio. h> re 1

แบบฝกหด 242 -101 Introduction to Computer Programming จงเขยนผลลพธของโปรแกรม x #include <stdio. h> re 1 typedef struct { double real; im 0. 75 double imag; } complex; a void display(complex a); int main() { 1 re complex x; im 0. 75 x. real = 1; x. imag = 0. 75; display(x); ������� return 0; 1. 00 + 0. 75 i } void display(complex a) { printf("%. 2 f + %. 2 fin", a. real, a. imag); } Department of Computer Engineering, PSU 33

242 -101 Introduction to Computer Programming แบบฝกหด จงเขยนผลลพธของโปรแกรม #include <stdio. h> typedef struct {

242 -101 Introduction to Computer Programming แบบฝกหด จงเขยนผลลพธของโปรแกรม #include <stdio. h> typedef struct { ������� int day, month, year; 12/9/2013 } date; void edit(date a); int main() { date d 1 = {12, 9, 2013}; edit(d 1); printf("%d/%d/%dn", d 1. day, d 1. month, d 1. year); return 0; การแกไขของพารามเตอรภายใน } void edit(date a) ฟงกชน { จะไมมผล ตอคาของตวแปรสต a. year = a. year + 10; รคเจอรทถกสงมาเปนอาร } Department of Computer Engineering, PSU กวเมนต 34

ตวอยางท 7. ฟงกชนทมการสงคากลบเปนสตรคเจอ #include <stdio. h> ร 242 -101 Introduction to Computer Programming cnum

ตวอยางท 7. ฟงกชนทมการสงคากลบเปนสตรคเจอ #include <stdio. h> ร 242 -101 Introduction to Computer Programming cnum typedef struct { re double re, im; 2 } complex; im 0. 5 complex cconst(double a, double b); int main() { ������� double x = 2, y = 0. 5; 2. 00 + 0. 50 i complex cnum; cnum = cconst(x, y); printf("%. 2 f + %. 2 fin", cnum. re, cnum. im); return 0; } num complex cconst(double a, double b){ re 2 complex num; im 0. 5 num. re = a; num. im = b; return num; } Department of Computer Engineering, PSU 35

ตวอยางท 8. 242 -101 Introduction to Computer Programming การรบคาและแสดงคาอารเรยของสตรคเจอร #include <stdio. h> int main()

ตวอยางท 8. 242 -101 Introduction to Computer Programming การรบคาและแสดงคาอารเรยของสตรคเจอร #include <stdio. h> int main() { typedef struct { char name[30]; int age; } student; student stds[3]; int i; for(i=0; i<3; i++) { printf("Enter name of student %d: ", i+1); scanf%")s", stds[i]. name(; printf("Enter age: "); scanf%")d", &stds[i]. age(; } printf("=========n"); printf("Name Agen"); printf("=========n"); for(i=0; i<3; i++) printf")%-15 s%dn", stds[i]. name, stds[i]. age(; return 0; Department of}Computer Engineering, PSU 38

242 -101 Introduction to Computer Programming ������� Enter name of student 1: John Enter

242 -101 Introduction to Computer Programming ������� Enter name of student 1: John Enter age: 14 Enter name of student 2: David Enter age: 15 Enter name of student 3: Angie Enter age: 12 ========= Name Age ========= John 14 David 15 Angie 12 Department of Computer Engineering, PSU 39

ตวอยางท 9. ฟงกชนทมการรบคาเขาเปนอารเรยของสต รคเจอร #include <stdio. h> 242 -101 Introduction to Computer Programming ฟงกชน

ตวอยางท 9. ฟงกชนทมการรบคาเขาเปนอารเรยของสต รคเจอร #include <stdio. h> 242 -101 Introduction to Computer Programming ฟงกชน display typedef struct { char name[30]; รบคาเขาเปนอารเรยขอ int salary; ง employee } employee; void display(employee emps[]); int main() { employee emp_list[3]; int i; for (i=0; i<3; i++ ) { printf(”Enter name of employee %d: ”, i+1); scanf(”%s”, emp_list[i]. name (; printf("Enter salary: "); scanf("%d", &emp_list[i]. salary ); } display(emp_list); return 0; } Department of Computer Engineering, PSU 40

242 -101 Introduction to Computer Programming void display (employee emps[] ) { int i;

242 -101 Introduction to Computer Programming void display (employee emps[] ) { int i; printf(“-----------n”); printf(”Name Salaryn”); printf(“-----------n”); for( i=0; i<3; i++) printf(”%-15 s%d n”, emps[i]. name, emps[i]. salary ); } Department of Computer Engineering, PSU 41

242 -101 Introduction to Computer Programming ������� Enter name of Enter salary: ----------Name ----------Somsak

242 -101 Introduction to Computer Programming ������� Enter name of Enter salary: ----------Name ----------Somsak Anan Somsri Department of Computer Engineering, PSU employee 1: Somsak 12000 employee 2: Anan 15000 employee 3: Somsri 9000 Salary 12000 15000 9000 42

242 -101 Introduction to Computer Programming แบบฝกหด จงเขยนผลลพธของโปรแกรม #include <stdio. h< int main() {

242 -101 Introduction to Computer Programming แบบฝกหด จงเขยนผลลพธของโปรแกรม #include <stdio. h< int main() { struct date { int day, month, year; } d 1; ������� size of d 1 = size of p 1 = 12 32 struct person { int age; char name[25]; } p 1; printf("size of d 1 = %dn", sizeof(d 1)); printf("size of p 1 = %dn", sizeof(p 1)); return 0; } Department of Computer Engineering, PSU 44

แบบฝกหด 242 -101 Introduction to Computer Programming จากตวอยางท 9 จงแกไขโปรแกรมใหผใชสามารถกำหนดจำนวนพน กงานได (สมมตวาจำนวนพนกงานไมเกน 10 �����������

แบบฝกหด 242 -101 Introduction to Computer Programming จากตวอยางท 9 จงแกไขโปรแกรมใหผใชสามารถกำหนดจำนวนพน กงานได (สมมตวาจำนวนพนกงานไมเกน 10 ����������� คน) Enter number of employees: 2 Enter name of employee 1: Sawat Enter salary : 20000 Enter name of employee 2: Samrit Enter salary : 16000 ----------Name Salary ----------Sawat 20000 Samrit 16000 Department of Computer Engineering, PSU 46

ตวอยางท 242 -101 Introduction to Computer Programming 9. –แกไข เพมเตม #include <stdio. h> ฟงกชน

ตวอยางท 242 -101 Introduction to Computer Programming 9. –แกไข เพมเตม #include <stdio. h> ฟงกชน display typedef struct { char name[30]; รบคาเขาเปนอารเรยขอ int salary; ง employee } employee; void display (employee emps[], int n); int main() { employee emp_list[10]; int i, n; printf(“Enter number of emp: ”); scanf(”%d”, &n); for (i=0; i<n; i++ ) { printf(“Enter name of employee %d: ” , i+1); scanf(“%s”, emp_list[i]. name (; printf("Enter salary: "); scanf("%d", &emp_list[i]. salary ); } display(emp_list, n); return 0; } Department of Computer Engineering, PSU 47

242 -101 Introduction to Computer Programming void display(employee emps[], int n ) { int

242 -101 Introduction to Computer Programming void display(employee emps[], int n ) { int i; printf(“-----------n”); printf(“Name Salaryn”); printf(“-----------n”); for (i=0; i<n; i++ ) printf("%-15 s %d n", emps[i]. name, emps[i]. salary ); } Department of Computer Engineering, PSU 48