Chapter 6 Structure Reference Department of Computer Engineering

  • Slides: 39
Download presentation
Chapter 6: Structure ตวแปรแบบโครงสราง Reference Department of Computer Engineering, PSU. 2012. Structure. Available on

Chapter 6: Structure ตวแปรแบบโครงสราง Reference Department of Computer Engineering, PSU. 2012. Structure. Available on : http: //fivedots. coe. psu. ac. th/. . . Structure/Chapter 6 -Structucture

ทำไมตองศกษาตวแปรลกษณะโครงสราง Student name = “Jenny” surname = “Angel” age = 18 gender = “Female

ทำไมตองศกษาตวแปรลกษณะโครงสราง Student name = “Jenny” surname = “Angel” age = 18 gender = “Female name = “Tommy” surname = “Lee” age = 17 gender = “Male” name = “Will” surname = “Young” age = 18 gender = “Male”

ตวอยางท 2. การใหคากบสมาชก arrayแบบ char struct employ { char name]25[; int age; int pay;

ตวอยางท 2. การใหคากบสมาชก arrayแบบ char struct employ { char name]25[; int age; int pay; } employee; การกำหนดคา employee. age=32; employee. name]2 = ['X'; 14

ตวอยางท struct { 3. การใหคาแบบ string //exam 7_3. c address Printf(“Name : ”); scanf(“%s”,

ตวอยางท struct { 3. การใหคาแบบ string //exam 7_3. c address Printf(“Name : ”); scanf(“%s”, input. name); char name[30]; char detail[50]; Printf(“detail : ”); scanf(“%s”, input. detail); int age; char telephone[10]; }; struct address input; Printf(“age : ”); scanf(“%d”, &input. age); Printf(“telephone : ”); scanf(“%s”, input. teleph strcpy(input. name, "User Example"); strcpy(input. detail, "1 ratutid Haddyai Songkhla"); input. age=20; strcpy(input. telephone, "212895; (" 15

แบบฝกหด จงเขยนผลลพธของโปรแกรมตอไปน #include <stdio. h< int main() 16 x } re struct complex} 4

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

แบบฝกหด จงเตมสวนทขาดหายไปเพอใหไดผลลพธตามทก #include <stdio. h< 17 #include <string. h< ำหนด int main() { ผลลพธ

แบบฝกหด จงเตมสวนทขาดหายไปเพอใหไดผลลพธตามทก #include <stdio. h< 17 #include <string. h< ำหนด int main() { ผลลพธ struct person { Pongsit Kampee, 36 years old. char name[30]; Telephone: 074212895 int age; char tel[10]; { p 1; _ _ _strcpy(p 1. name, "Pongsit _ _ _Kampee; (" _ _ _. 1 // _ = _ 36; _ _ _. 2 // _ _p 1. age _ _ 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;

แบบฝกหด จงเขยนผลลพธสวนทเหลอ (ขอความสแดงคออนพตจากผใช #include <stdio. h> #include <string. h> int main() { ) ผลลพธ

แบบฝกหด จงเขยนผลลพธสวนทเหลอ (ขอความสแดงคออนพตจากผใช #include <stdio. h> #include <string. h> int main() { ) ผลลพธ 19 Enter subject name: Physics I Enter credit: 3 Physics I, credit: 4 struct subject { char name[30]; s 1 int credit; name Physics I } s 1, s 2; 3 printf("Enter subject name: "); credit gets("%s", s 1. name); s 2 printf("Enter credit: "); name Physics I scanf("%d", &s 1. credit); credit 34 s 2 = s 1; s 2. credit = s 2. credit + 1; printf("%s, credit: %dn", s 2. name, s 2. credit); return 0;

การกำหนดคาเรมตนใหกบตวแปรสตรคเจอร (ตอ ( 20 struct subject { char name[20]; int credit; char grade; }

การกำหนดคาเรมตนใหกบตวแปรสตรคเจอร (ตอ ( 20 struct subject { char name[20]; int credit; char grade; } s 1 = {"Physics I", 3, 'A'}; s 1 name Physics I credit 3 grade A

ตวอยางท 7. การใช typedef กบ struct info { char first. Name[20]; char last. Name[20];

ตวอยางท 7. การใช typedef กบ struct info { char first. Name[20]; char last. Name[20]; int age; }; struct info i 1, i 2; info j; 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; 26

ตวอยางท 8. typedef ทมสมาชกเปน typedef struct { 27 char first. Name[20]; char last. Name[20];

ตวอยางท 8. typedef ทมสมาชกเปน typedef struct { 27 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;

ตวอยางท 10. จงเขยนผลลพธของโปรแกรมตอไปน #include <stdio. h> //exam 7_10. c typedef struct { 30 double

ตวอยางท 10. จงเขยนผลลพธของโปรแกรมตอไปน #include <stdio. h> //exam 7_10. c typedef struct { 30 double re; double im; x } complex; re 1 void display(complex a); int main() { im 0. 75 complex x; x. re = 1; x. im = 0. 75; a display(x); return 0; 1 re } im 0. 75 void display(complex a) { printf("%. 2 f + %. 2 fin", a. re, a. im); } ผลลพธ 1. 00 + 0. 75 i

ร ( #include <stdio. h> typedef struct { cnum 32 double re, im; re

ร ( #include <stdio. h> typedef struct { cnum 32 double re, im; re 2 } complex; complex cconst(double a, double b); im 0. 5 int main() { double x = 2, y = 0. 5; ผลลพธ complex cnum; 2. 00 + 0. 50 i cnum = cconst(x, y); printf("%. 2 f + %. 2 fin", cnum. re, cnum. im); num return 0; 2 } re complex cconst(double a, double b) { im 0. 5 complex num; num. re = a; num. im = b; return num; }

ตวอยางท 13. )การรบคาและแสดงคาอารเรยของสตรคเจอร #include <stdio. h> int main (){ 35 typedef struct { char

ตวอยางท 13. )การรบคาและแสดงคาอารเรยของสตรคเจอร #include <stdio. h> int main (){ 35 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; } (

ผลลพธ 36 Enter name of student 1: John Enter age: 14 Enter name of

ผลลพธ 36 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

)ฟงกชนทมการรบคาเขาเปนอารเรยของสตรคเ จอร ( #include <stdio. h> typedef struct { 37 char name]30[; int salary;

)ฟงกชนทมการรบคาเขาเปนอารเรยของสตรคเ จอร ( #include <stdio. h> typedef struct { 37 char name]30[; int salary; } 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: "); ฟงกชน display รบคาเขาเปนอารเรยของ employee scanf("%d", &emp_list[i]. salary } display(emp_list); return 0; } );

void display)employee emps([] { 38 int i; printf-----------")n("; printf")Name Salaryn("; printf-----------")n("; for)i=0; i<3; i(++

void display)employee emps([] { 38 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(; }

ผลลพธ Enter name of Enter salary: ----------Name ----------Somsak Anan Somsri 39 employee 1: Somsak

ผลลพธ Enter name of Enter salary: ----------Name ----------Somsak Anan Somsri 39 employee 1: Somsak 12000 employee 2: Anan 15000 employee 3: Somsri 9000 Salary 12000 15000 9000