You are on page 1of 4

721'8&7+$1*81,9(56,7<

 )DFXOW\RI,QIRUPDWLRQ7HFKQRORJ\


PROGRAMMING METHODOLOGY
Lab 8: Abstract Datatype STRUCT

 ,QWURGXFWLRQ

,QWKLVODEWXWRULDOZH OOFRQVLGHUDQDEVWUDFWGDWDW\SHFDOOHGVWUXFW6WUXFWXUHV±VRPHWLPHV
UHIHUUHGWRDVDJJUHJDWHV±DUHFROOHFWLRQVRIUHODWHGYDULDEOHVXQGHURQHQDPH6WUXFWXUHVPD\
FRQWDLQYDULDEOHVRIPDQ\GLIIHUHQWGDWDW\SHV±LQFRQWUDVWWRDUUD\VZKLFKFRQWDLQRQO\HOHPHQWV
RIWKHVDPHGDWDW\SH

 6WUXFWXUH'HILQLWLRQ

6WUXFWXUHVDUHGHULYHGGDWDW\SHV±WKH\ UHFRQVWUXFWHGXVLQJREMHFWVRIRWKHUW\SHV7RGHILQH
DVWUXFWXUH\RXPXVWXVHWKHVWUXFWVWDWHPHQW7KHVWUXFWVWDWHPHQWGHILQHVDQHZGDWDW\SHZLWK
PRUHWKDQRQHPHPEHU7KHIRUPDWRIWKHVWUXFWVWDWHPHQWLVDVIROORZV

struct [structure_tag] {
datatype definition;
datatype definition;

datatype definition;
} [one_or_more_structure_variables];

7KHVWUXFWXUHWDJLVRSWLRQDODQGHDFKPHPEHUGHILQLWLRQLVDQRUPDOYDULDEOHGHILQLWLRQRUDQ\
RWKHUYDOLGYDULDEOHGHILQLWLRQ$WWKHHQGRIWKHVWUXFWXUH VGHILQLWLRQEHIRUHWKHILQDOVHPLFRORQ
\RXFDQVSHFLI\RQHRUPRUHVWUXFWXUHYDULDEOHVEXWLWLVRSWLRQDO)RUH[DPSOHGHFODULQJD%RRN
VWUXFWXUHDVLQWKHIROORZLQJILJXUH
định nghĩa struct
struct Book { typedef struct {
char title[100]; char title[100];
char author[100];
char author[100]; int book_id;
int book_id; } Book;

} book1; Book b1, b2;


khai báo biến struct

Phuc Duong – http://it.tdt.edu.vn/~dhphuc  



721'8&7+$1*81,9(56,7<
 )DFXOW\RI,QIRUPDWLRQ7HFKQRORJ\

$FFHVVLQJ6WUXFWXUH0HPEHUV

7RDFFHVVDQ\PHPEHURIDVWUXFWXUHZHXVHWKHPHPEHUDFFHVVRSHUDWRU  7KHPHPEHUDFFHVV
RSHUDWRULVFRGHGDVDSHULRGEHWZHHQWKHVWUXFWXUHYDULDEOHQDPHDQGWKHVWUXFWXUHPHPEHUWKDW
ZHZLVKWRDFFHVV<RXZRXOGXVHWKHNH\ZRUGVWUXFWWRGHILQHYDULDEOHVRIVWUXFWXUHW\SH

7KHSURJUDPEHORZZLOOLOOXVWUDWHKRZWRGHFODUHDQGXVHVWUXFW
1 #include <stdio.h>
2 #include <string.h>
3 #define SIZE 100
4
5 struct Book {
6 char title[SIZE];
7 char author[SIZE];
8 int book_id;
9 };
10
11 int main(int argc, char const *argv[])
12 {
13 struct Book book1, book2;
14
15 // book 1 specification
16 strcpy(book1.title, "C Programming Language");
17 strcpy(book1.author, "Brian W. Kernighan");
18 book1.book_id = 1001;
19
20 // book 2 specification
21 strcpy(book2.title, "C: How to Program");
22 strcpy(book2.author, "Paul Deitel");
23 book2.book_id = 1002;
24
25 // print book1 info
26 printf( "Book1 title : %s\n", book1.title);
27 printf( "Book1 author : %s\n", book1.author);
28 printf( "Book1 book_id : %d\n", book1.book_id);
29
30 // print book2 info
31 printf( "Book2 title : %s\n", book2.title);
32 printf( "Book2 author : %s\n", book2.author);
33 printf( "Book2 book_id : %d\n", book2.book_id);
34
35 return 0;
36 }

6WUXFWXUHVDV)XQFWLRQ$UJXPHQWV

<RXFDQSDVVDVWUXFWXUHDVDIXQFWLRQDUJXPHQWLQWKHVDPHZD\DV\RXSDVVDQ\RWKHUYDULDEOHRU
SRLQWHU

 

Phuc Duong – http://it.tdt.edu.vn/~dhphuc  



721'8&7+$1*81,9(56,7<
 )DFXOW\RI,QIRUPDWLRQ7HFKQRORJ\

1 #include <stdio.h>
2 #include <string.h>
3 #define SIZE 100
4
5 struct Book {
6 char title[SIZE];
7 char author[SIZE];
8 int book_id;
9 };
10
11 void printInfo(struct Book); khai báo hàm
12
13 int main(int argc, char const *argv[])
14 {
15 struct Book book1, book2;
16
17 // book 1 specification
18 strcpy(book1.title, "C Programming Language");
19 strcpy(book1.author, "Brian W. Kernighan");
20 book1.book_id = 1001;
21
22 // // book 2 specification
23 strcpy(book2.title, "C: How to Program");
24 strcpy(book2.author, "Paul Deitel");
25 book2.book_id = 1002;
26
27 // print book1 info
28 printInfo(book1);
29
30 // print book2 info
31 printInfo(book2);
32
33 return 0;
34 }
35
36 void printInfo(struct Book book) định nghĩa hàm
37 {
38 printf( "Title : %s\n", book.title);
39 printf( "Author : %s\n", book.author);
40 printf( "book_id : %d\n", book.book_id);
41 }

 ([HUFLVHV

 'HILQHDQ(PSOR\HHVWUXFWXUHFRQWDLQVWKHIROORZLQJSURSHUWLHVLGQDPHVH[ELUWK\HDU
SKRQHQXPEHUVDODU\

 'HILQHDQDUUD\FRQWDLQVHPSOR\HHV typedef struct {


int id;
print1Emp(Employee nv)
char name[50];
 'HILQHDIXQFWLRQWRSULQWDOOHPSOR\HHLQIRUPDWLRQ
int sex; //0 nam, 1 nu
printListEmp(Employee nv[], int n) …
 )LQGDQHPSOR\HHE\SURYLGLQJKLVKHULG } Employee;
#define SIZE 10
 &RXQWWKHQXPEHURIPDOHIHPDOHHPSOR\HH int main () {
Employee nv[SIZE] = { {1, “ABC”, 1, …}, {...}, {…
 6RUWWKHHPSOR\HHDUUD\LQDVFHQGLQJRUGHURIELUWK\HDU int n=10;
}
int findEmp(Employee nv[], int n, int id) {
Phuc Duong – http://it.tdt.edu.vn/~dhphuc int i;  
 for (i=0; i<n; i++)
Thực hiện trong 30p
if (nv[i].id == id) return i;
return -1
}
721'8&7+$1*81,9(56,7<
 )DFXOW\RI,QIRUPDWLRQ7HFKQRORJ\

 6RUWWKHHPSOR\HHDUUD\LQDVFHQGLQJRUGHURIVDODU\
int findEmp(Employee nv[], int n) {
 )LQGDQHPSOR\HHKDVWKHPD[LPXPVDODU\ int i, max=0;
for (i=1; i<n; i++)
 )LQGD\RXQJHVWHPSOR\HH if (nv[i].salary > nv[max].salary)
max = i;
'HOHWHDVSHFLILFHPSOR\HH return max
}

 5HIHUHQFH

>@ %ULDQ:.HUQLJKDQ 'HQQLV5LWFKLH  &3URJUDPPLQJ/DQJXDJHQG(GLWLRQ


3UHQWLFH+DOO
>@ 3DXO'HLWHO +DUYH\'HLWHO  &+RZWR3URJUDPWK(GLWLRQ3UHQWLFH+DOO
>@ &3URJUDPPLQJ7XWRULDO  7XWRULDOV3RLQW
>@ &3URJUDPPLQJ  :LNLERRNV

Phuc Duong – http://it.tdt.edu.vn/~dhphuc  




You might also like