Structure Union Structure Programming Structure structname type name1

  • Slides: 63
Download presentation
Structure และ Union Structure Programming

Structure และ Union Structure Programming

การสราง Structure struct-name } type name-1; type name-2; . . type name-n; ; {

การสราง Structure struct-name } type name-1; type name-2; . . type name-n; ; { ตวอยาง struct VDO{ char vdo. Code[5]; char vdo. Name[30]; float price; char date[6]; }; VDO vdo. Code Structure Programming vdo. Name price date 7

ตวอยาง : ข�อมล รายละเอยดวชา ประกอบด�วย รหสวชา , ชอวชา านวนหน�วยกต , ผSUBJECT{ �สอน struct char

ตวอยาง : ข�อมล รายละเอยดวชา ประกอบด�วย รหสวชา , ชอวชา านวนหน�วยกต , ผSUBJECT{ �สอน struct char int char , จ sbj. Code[5]; sbj. Name[30]; sbj. Unit; tch. Name[30]; }; SUBJECT sbj. Code Structure Programming sbj. Name sbj. Unit tch. Name 9

การสรางและการเขาถงตวแ ปร struct STD { char code[9]; char name[30]; int midterm; int final; char

การสรางและการเขาถงตวแ ปร struct STD { char code[9]; char name[30]; int midterm; int final; char grade; } student, stud; }char code[9; [ char name[30; [ float midterm; float final; char grade; หรอ {; struct STD student, stud; STD ชอตวแปรโครงสราง std. Code Structure Programming std. Name ชอโครงสราง std. Age ชอตวแปรโครงสรา std. Birth. Date 10

#include <stdio. h< #include <conio. h< struct stdstruct }char code[9; [ char name[50; [

#include <stdio. h< #include <conio. h< struct stdstruct }char code[9; [ char name[50; [ float midterm; float final; {student; void input; () void display; () void main} () input ; () display; () { หรอ struct stdstruct }char code[9; [ char name[50; [ float midterm; float final; ; { struct stdstruct student; ชอโครงสราง ชอตวแปรโครงสร stdstruct code Structure Programming name midterm final 13

void input() { printf(“Enter Data : n”); printf(“Code : ”); scanf(“%s”, student. code); printf(“Name

void input() { printf(“Enter Data : n”); printf(“Code : ”); scanf(“%s”, student. code); printf(“Name : ”); scanf(“%s”, student. name); printf(“Midterm : ”); scanf(“%f”, &student. midterm); printf(“Final : ”); scanf(“%f”, &student. final); } void Display() { printf(“nn. Your Information n”); printf(“Code : %s”, student. code); printf(“Name : %s”, student. name); printf(“Midterm : %. 2 f”, student. midterm); printf(“Final : %. 2 f”, student. final); } Structure Programming 14

structure ซอน structure ขอมลบาง field อาจจะประกอบดวยขอมลหลายประเภท (เปน structure( ประกอบด�วย struct datestruct { int day;

structure ซอน structure ขอมลบาง field อาจจะประกอบดวยขอมลหลายประเภท (เปน structure( ประกอบด�วย struct datestruct { int day; char month[30]; int year; }; struct stdstruct { char code[9]; char name[50]; struct datestruct birthdate; } student; Structure Programming -รหสนกศกษา - ชอนกศกษ า code - วนเดอนปเก name ด day birthdate month year 15

#include <stdio. h> #include <conio. h> struct datestruct { int day; char month[30]; int

#include <stdio. h> #include <conio. h> struct datestruct { int day; char month[30]; int year; }; struct stdstruct { char code[9]; char name[50]; struct datestruct birthdate; } student; void input(); void display(); void main() { input(); display(); } Structure Programming code name birthdate day month year 16

void input() { printf("Enter Data : n"); printf("Code : "); scanf("%s", student. code); printf("Name

void input() { printf("Enter Data : n"); printf("Code : "); scanf("%s", student. code); printf("Name : "); scanf("%s", student. name); printf("Birthdate : n"); printf("date : "); scanf("%d", &student. birthdate. day); printf("month : "); scanf("%s", student. birthdate. month); printf("year : "); scanf("%d", &student. birthdate. year); } void display() { printf("nn. Your Information n"); printf("Code : %sn", student. code); printf("Name : %sn", student. name); printf("Birthdate : n"); printf("date : %dn", student. birthdate. day); printf("month : %sn", student. birthdate. month); printf("year : %dn", student. birthdate. year); } Structure Programming 17

ขอมลของนกศกษา 1 คน ทมขอมล รหสนกศกษา ชอ -สกล และวนเดอนปเก struct stdstruct { char code[9]; char

ขอมลของนกศกษา 1 คน ทมขอมล รหสนกศกษา ชอ -สกล และวนเดอนปเก struct stdstruct { char code[9]; char name[50]; struct datestruct birthdate; } student; struct stdstruct { char code[9]; char name[50]; struct datestruct birthdate; }student[10]; code name birthdate ตวอยางการอางถง student. name ตวอยางการอางถง student[0]. nam code name birthdate student[0] student[1] student[2] ขอมลของนกศกษา 10 คน ทแตละคน. มขอมล รหสนกศกษา ชอ -สกล. . และวนเดอนปเกด. student[9] Structure Programming 19

code #include<stdio. h> #include<conio. h> student[0] struct datestruct student[1] { int day; char month[30];

code #include<stdio. h> #include<conio. h> student[0] struct datestruct student[1] { int day; char month[30]; student[2] int year; . }; . struct stdstruct. . { char code[9]; student[9] char name[50]; struct datestruct birthdate; } student[10]; void input(); void display(); void main() { input(); display(); } Structure Programming name birthdate day month year 20

void input() { printf("Enter Data : n"); for (int i = 0; i <

void input() { printf("Enter Data : n"); for (int i = 0; i < 10; ++i) { printf("n. Student NO. %d n", i+1); printf("Code : "); scanf("%s", student[i]. code); printf("Name : "); scanf("%s", student[i]. name); printf("Birthdate : n"); printf("date : "); scanf("%d", &student[i]. birthdate. day); printf("month : "); scanf("%s", student[i]. birthdate. month); printf("year : "); scanf("%d", &student[i]. birthdate. year); } } Structure Programming 21

void display() { printf("nn. Student Information n"); for (int i = 0; i <

void display() { printf("nn. Student Information n"); for (int i = 0; i < 10; ++i) { printf("Student NO. %d n", i+1); printf("Code : %sn", student[i]. code); printf("Name : %sn", student[i]. name); printf("Birthdate : n"); printf("date : %dn", student[i]. birthdate. day); printf("month : %sn", student[i]. birthdate. month); printf("year : %dnnn", student[i]. birthdate. year); } } Structure Programming 22

struct gradestruct { char subjectcode[8]; char grade[2]; }; struct stdstruc { char code[9]; char

struct gradestruct { char subjectcode[8]; char grade[2]; }; struct stdstruc { char code[9]; char name[50]; struct gradestruct subj_grd[3]; } student[10]; Structure Programming 24

code name subj_grade subjectcode student[0] grade. subj_grd[0]. subj_grd[1]. subj_grd[2] student[1] . subj_grd[0] . .

code name subj_grade subjectcode student[0] grade. subj_grd[0]. subj_grd[1]. subj_grd[2] student[1] . subj_grd[0] . . . . subj_grd[1] student[9] . subj_grd[2] . subj_grd[0]. subj_grd[1]. subj_grd[2] Structure Programming 25

#include <stdio. h> #include <conio. h> struct gradestruct { char subjectcode[8]; char grade[2]; };

#include <stdio. h> #include <conio. h> struct gradestruct { char subjectcode[8]; char grade[2]; }; struct stdstruc { char code[9]; char name[50]; struct gradestruct subj_grd[3]; } student[10]; void input(); void display(); void main() { input(); display(); } Structure Programming 26

void input() { printf("Enter Data : n"); for (int i = 0; i <

void input() { printf("Enter Data : n"); for (int i = 0; i < 10; ++i) { printf("n. Student NO. %d n", i+1); printf("Code : "); scanf("%s", student[i]. code); printf("Name : "); scanf("%s", student[i]. name); printf("Grade Information: n"); for (int j = 0; j < 3; ++j) { printf("-------------------n"); printf("subjectcode : "); scanf("%s", student[i]. subj_grade[j]. subjectcode); printf("grade : "); scanf("%s", student[i]. subj_grade[j]. grade); printf("-------------------n"); } //for j printf("+++++++++++++++++++n"); } //for i } Structure Programming 27

void display() { printf("nn. Student Information n"); for (int i = 0; i <

void display() { printf("nn. Student Information n"); for (int i = 0; i < 10; ++i) { printf("Student NO. %d n", i+1); printf("Code : %sn", student[i]. code); printf("Name : %sn", student[i]. name); printf("Grade Information: n"); for (int j = 0; j < 3; ++j) { printf("-------------------n"); printf("subjectcode : %sn", student[i]. subj_grade[j]. subjectcode); printf("grade : %sn", student[i]. subj_grade[j]. grade); printf("-------------------n"); } //for j printf("++++++++++++++++++n"); }//for i } Structure Programming 28

#include <stdio. h> struct history{ int id; char name[20]; int age; }; void display(int

#include <stdio. h> struct history{ int id; char name[20]; int age; }; void display(int , int); void main() { struct history student; //input printf(“Enter id, name, age; (“ : scanf(“%d”, &student. id; ( scanf(“%s”, student. name; ( scanf(“%d”, &student. age; ( // output display(student. id, student. age; ( Structure Programming 32

printf(“n++++ After Call function ++++ n"); printf("ID = %dn", student. id); printf("Name = %sn",

printf(“n++++ After Call function ++++ n"); printf("ID = %dn", student. id); printf("Name = %sn", student. name); printf("Age = %dn", student. age); } void display)int id, int age} ( age += 10; printf("n +++In function +++n; (" printf("ID = %dn , "id; ( printf("Age = %dn , "age; ( { Structure Programming 33

OUTPUT Structure Programming 34

OUTPUT Structure Programming 34

#include <stdio. h> #include <conio. h> struct history{ int id; char name[20]; int age;

#include <stdio. h> #include <conio. h> struct history{ int id; char name[20]; int age; }; void display(int *, char *, int *); void main() { struct history student; //input printf("Enter id, name, age : "); scanf("%d", &student. id); scanf("%s", student. name); scanf("%d", &student. age); //output display(&student. id, student. name, &student. age); Structure Programming 36

printf")n ++++After Call function ++++ n; (" printf("ID = %dn , "student. id; (

printf")n ++++After Call function ++++ n; (" printf("ID = %dn , "student. id; ( printf("Name = %sn , "student. name; ( printf("Age = %dn , "student. age; ( { void display)int *id, char *name, int *age} ( * age += 10; printf("n +++In function +++n; (" printf("ID = %dn* , "id; ( printf("Name = %sn* , "name; ( printf("Age = %dn* , "age; ( { Structure Programming 37

OUTPUT Structure Programming 38

OUTPUT Structure Programming 38

#include <stdio. h> #include <conio. h> struct history{ int id; char name[20]; int age;

#include <stdio. h> #include <conio. h> struct history{ int id; char name[20]; int age; }; void display(struct history st); void main() { struct history student; //input printf("Enter id, name, age : "); scanf("%d", &student. id); scanf("%s", student. name); scanf("%d", &student. age); //output display(student); Structure Programming 41

printf(“n++++ After Call function ++++ n"); printf("ID = %dn", student. id); printf("Name = %sn",

printf(“n++++ After Call function ++++ n"); printf("ID = %dn", student. id); printf("Name = %sn", student. name); printf("Age = %dn", student. age); } void display)struct history st} ( st. age += 10; printf("n +++In function +++n; (" printf("ID = %dn , "st. id; ( printf("Name = %sn , "st. name; ( printf("Age = %dn , "st. age; ( { Structure Programming 42

OUTPUT Structure Programming 43

OUTPUT Structure Programming 43

#include<stdio. h< #include<string. h< struct num_string} int i; char st[10; [ ; { void

#include<stdio. h< #include<string. h< struct num_string} int i; char st[10; [ ; { void output(struct num_string; ( void main() } struct num_string x; x. i = 1000; strcpy(x. st, "Hahaha; (" output(x; ( { void output(struct num_string a( } printf("x. i = %dn", a. i; ( printf("x. st = %sn", a. st; ( { Structure Programming

#include<stdio. h< void add)struct data plus_num( #include<string. h< } struct data} printf("In function addn;

#include<stdio. h< void add)struct data plus_num( #include<string. h< } struct data} printf("In function addn; (" int i, j, k; printf("i = %dn", plus_num. i; ( { add_data; printf("j = %dn", plus_num. j; ( void add(struct data; ( plus_num. k = plus_num. i + void main() plus_num. j; } printf("k = %dn", plus_num. k; ( add_data. i = 20; { add_data. j = 40; add_data. k = 2; add(add_data; ( printf("In function mainn; (" printf("i = %dn", add_data. i; ( printf("j = %dn", add_data. j; ( printf("k = %dn", add_data. k; ( { Structure Programming

#include<stdio. h< #include<string. h< struct data} int i, j, k; { add_data; struct data

#include<stdio. h< #include<string. h< struct data} int i, j, k; { add_data; struct data 1} int x, y, z; { add_data 1; void add(struct data, struct data 1; ( main() } add_data. i = 20; add_data. j = 40; add_data. k = 2; add_data 1. x = 10; add_data 1. y = 20; add_data 1. z = 1; add(add_data, add_data 1; ( printf("In function mainn ; (" printf("i-x = %dn", add_data. i-add_data 1. x; ( printf("j-y = %dn", add_data. j-add_data 1. y; ( printf("k-z = %dn", add_data. k-add_data 1. z; ( { void add)struct data plus_num, struct data 1 plus_num 1( } printf("In function addn; (" printf("i+x = %dn", plus_num. i+plus_num 1. x; ( printf("j+y = %dn", plus_num. j+plus_num 1. y; ( printf("k+z = %dn", plus_num. k+plus_num 1. z; ( { Structure Programming

#include <stdio. h> #include <conio. h> struct history{ int id; char name[20]; int age;

#include <stdio. h> #include <conio. h> struct history{ int id; char name[20]; int age; }; void display(struct history *st); void main() { struct history student; //input printf("Enter id, name, age : "); scanf("%d", &student. id); scanf("%s", student. name); scanf("%d", &student. age); //output display(&student); Structure Programming 48

printf(“n++++ After Call function ++++ n"); printf("ID = %dn", student. id); printf("Name = %sn",

printf(“n++++ After Call function ++++ n"); printf("ID = %dn", student. id); printf("Name = %sn", student. name); printf("Age = %dn", student. age); } void display)struct history *st} ( st->age ; 10 =+ printf("n +++In function +++n; (" printf("ID = %dn , "st->id; ( printf("Name = %sn , "st->name; ( printf("Age = %dn , "st->age; ( { Structure Programming 49

OUTPUT Structure Programming 50

OUTPUT Structure Programming 50

#include<stdio. h< #include<string. h< struct data} int i, j, k; ; { void add(struct

#include<stdio. h< #include<string. h< struct data} int i, j, k; ; { void add(struct data; (* void main() } struct data add_data; add_data. i = 20; add_data. j = 40; add_data. k = 2; add(&add_data; ( printf("In function mainn; (" printf("i = %dn", add_data. i; ( printf("j = %dn", add_data. j; ( printf("k = %dn", add_data. k; ( { void add)struct data *p_num( } printf("In function addn; (" printf("i = %dn", (*p_num). i; ( printf("j = %dn", (*p_num). j; ( *) p_num). k = (*p_num). i +(*p_num). j; printf("k = %dn", (*p_num). k; ( { Structure Programming

#include<stdio. h< #include<string. h< struct data} int i, j, k; ; { void add(struct

#include<stdio. h< #include<string. h< struct data} int i, j, k; ; { void add(struct data; (* void main() } struct data add_data; add_data. i = 20; add_data. j = 40; add_data. k = 2; add(&add_data; ( printf("In function mainn; (" printf("i = %dn", add_data. i; ( printf("j = %dn", add_data. j; ( printf("k = %dn", add_data. k; ( { void add)struct data *p_num( } printf("In function addn; (" printf("i = %dn", p_num->i; ( printf("j = %dn", p_num->j; ( p_num->k = p_num->i + p_num->j; printf("k = %dn", p_num->k; ( { Structure Programming

#include<stdio. h< void add)struct data *p_num( #include<string. h< } struct data} printf("In function addn;

#include<stdio. h< void add)struct data *p_num( #include<string. h< } struct data} printf("In function addn; (" int i, j, k; for(i=0; i<=1; i(++ { add_data[2; [ } int i; printf("i[%d] = %dn", i, p_num[i]. i; ( void add(struct data; (* printf("j[%d] = %dn", i, p_num[i]. j; ( void main() p_num[i]. k = p_num[i]. i + p_num[i]. j; } printf("k[%d] = %dn", i, p_num[i]. k; ( add_data[0]. i = 20; { add_data[0]. j = 40; { add_data[0]. k = 2; add_data[1]. i = 10; add_data[1]. j = 20; add_data[1]. k = 1; add(add_data; ( printf("In function mainn; (" for(i=0; i<=1; i(++ } printf("i[%d] = %dn", i, add_data[i]. i; ( printf("j[%d] = %dn", i, add_data[i]. j; ( printf("k[%d] = %dn", i, add_data[i]. k; ( { { Structure Programming

#include<stdio. h< #include<string. h< struct num_string} int i; char st[15; [ ; { void

#include<stdio. h< #include<string. h< struct num_string} int i; char st[15; [ ; { void main() } struct num_string x; struct num_string *pt; pt = &x; strcpy(pt->st, "C programming; (" pt->i = 200; printf("%sn", pt->st; ( printf("%dn", pt->i; ( { Structure Programming

#include<stdio. h< #include<conio. h< void main() } struct bookshop} char pro_id[10; [ char pro_name[20;

#include<stdio. h< #include<conio. h< void main() } struct bookshop} char pro_id[10; [ char pro_name[20; [ int pro_price; ; { struct bookshop store = {"001", "Java Programming", 600; { struct bookshop *pt; pt = &store; printf("Product id : %sn", pt->pro_id ; ( // printf("Product id : %sn", (*pt). pro_id ; ( printf("Product name : %sn", pt->pro_name; ( printf("Product price : %dn", pt->pro_price; ( { Structure Programming

#include<stdio. h< #include<conio. h< void discount(struct bookshop; (* struct bookshop} char pro_id[10; [ char

#include<stdio. h< #include<conio. h< void discount(struct bookshop; (* struct bookshop} char pro_id[10; [ char pro_name[20; [ int pro_price; ; { void main() } struct bookshop store = {"001", "Java programming", 600; { struct bookshop *pt; pt = &store; discount(pt; ( printf("Product id : %sn", (*pt). pro_id; ( printf("Product name : %sn", (*pt). pro_name; ( printf("Product price : %dn", (*pt). pro_price; ( { void discount)struct bookshop *q( } *) q). pro_price = (*q). pro_price - ((*q). pro_price * 0. 10; ( printf("Product id : %sn", (*q). pro_id; ( printf("Product name : %sn", (*q). pro_name; ( printf("Product price : %dn", (*q). pro_price; ( { Structure Programming

����union my. Union { int i; float x; ; { union myunion u 1;

����union my. Union { int i; float x; ; { union myunion u 1; my. Union i x ดงนน u 1 ������� 4 ���� ถาเปลยน u 1 เปน structure จะ������� 6 ���� Structure Programming 61

#include “stdio. h” #include “conio. h” union grade { int s; //integer float m;

#include “stdio. h” #include “conio. h” union grade { int s; //integer float m; //float char c; //character } gr; void main() { printf(“Display grade”); gr. s = 4; printf (“Grade = %dn”, gr. s); gr. m = 3. 5; printf (“Grade = %1. 1 fn”, gr. m); gr. c = ‘A’; printf (“Grade = %cn”, gr. c); getch(); } Structure Programming Display Grade = grade 4 3. 5 A 63