Structure struct strucname type name1 type name2 type

  • Slides: 41
Download presentation

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

การสราง Structure struct struc-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 vdo. Name price date 6

การสรางและการเขาถงตวแ ปร 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; struct STD }char code[9; [ char name[50; [ หรอ float midterm; float final; char grade; {; struct STD student; 9

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

#include <stdio. 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; ชอโครงสราง ชอตวแปรโครงสร 12

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); } 13

structure ซอน structure ขอมลบาง field อาจจะประกอบดวยขอมลหลายประเภท (เปน structure( ประกอบด�วย struct stdstruct { char code[9];

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

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

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

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); } 16

struct stdstruct { char code[9]; char name[50]; struct datestruct birthdate; } student; ขอมลของนกศกษา 1

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

#include<stdio. h> code name Birthdate struct datestruct day month Year { int day; student[0]

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

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); } } 20

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); } } 21

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]; 23

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] 24

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

#include <stdio. 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(); } 25

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 } 26

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 } 27

#include <stdio. h> void display(int , int); void main() { struct history{ int id;

#include <stdio. h> void display(int , int); void main() { struct history{ int id; char name[20]; int age; }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; ( 31

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; ( { 32

OUTPUT 33

OUTPUT 33

#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; }student; void display(struct history st); void main() { //input printf("Enter id, name, age : "); scanf("%d", &student. id); scanf("%s", &student. name); scanf("%d", &student. age); //output display(student); 36

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; ( { 37

OUTPUT 38

OUTPUT 38

#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; ( {

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

#include<stdio. h< void add)struct data plus_num( #include<string. h< } printf("In function addn; (" struct data} printf("i = %dn", plus_num. i; ( int i, j, k; printf("j = %dn", plus_num. j; ( { add_data; plus_num. k = plus_num. i + void add(struct data; ( plus_num. j; void main() 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; ( {

#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; (" 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; ( { 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; ( {