1 / 25

CHƯƠNG 8 STRUCTURES – ENUM - typedef

CHƯƠNG 8 STRUCTURES – ENUM - typedef. 1. Giới thiệu. Có 5 cách để tạo kiểu dữ liệu tùy biến ( custom data types ). S tructure bit-field Union Enumeration typedef. 2. Struct ( cấu trúc ).

Télécharger la présentation

CHƯƠNG 8 STRUCTURES – ENUM - typedef

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHƯƠNG 8STRUCTURES – ENUM - typedef

  2. 1. Giớithiệu Có 5 cáchđểtạokiểudữliệutùybiến (custom data types). • Structure • bit-field • Union • Enumeration • typedef

  3. 2. Struct (cấutrúc) • Mộtcấutrúclàmộttậpcácbiếnđượcthamchiếuthông qua mộttênchung. Nhữngbiếntạonêncấutrúcđượcgọilàcácthànhviên (members). • Sựkhácbiệtgiữakiểucấutrúcvàkiểumãnglà: cácphầntửcủamãnglàcùngkiểucòncácphầntửcủakiểucấutrúccóthểcókiểukhácnhau.

  4. 2.1. KhaibáoStruct structtructureName { type member1; type member2; ... type memberN; .. . } varNames; • structureName: Têncủacấutrúc • type: Kiểudữliệucủathànhviêntươngứng • Member1,..., memberN: Têncácbiếnthànhviêncủacấutrúc • varNames: Têncácbiếncấutrúcphâncáchnhaubằngdấuphẩy.

  5. Vídụ: • Tạomộtstructsinhviêngồmcácthông tin: mãsốsinhviên, họtên, nămsinh, địachỉ. structsinhvien { char *MaSv; char *TenSv; intNamsinh; char *Diachi; }; • Khaibáobiếnkiểusinhvien: sinhvien sv1, sv2;

  6. 2.2.Truy cậpbiếncấutrúc • Dùngtoántửdấuchấm (dot operator) đểtruycậpcácthànhviêncủamộtbiếncấutrúc. • Cúpháp: varNames.memberName Vídụ: sv1.MaSv= “a001”; sv1.TenSv=“Nguyen van A”; sv2.Namsinh=1977;

  7. 2.3. Lệnhgáncấutrúc • Dùnglệnhgánđểgánnội dung trong 1 biếncấutrúcchomộtbiếncấutrúckháccócùngkiểu • Vídụ: structcoordXY { int x; int y; } diemA, diemB; • diemA.x = 100; diemA.y = 200;

  8. 2.3. Lệnhgáncấutrúc • Gánnội dung biếncấutrúcdiemAchobiếndiemB: diemB = diemA ; • Hoặccóthểsaochéptừngthànhviên: pointB.x = pointA.x; pointB.y = pointA.y;

  9. 2.4. Mảngcáccấutrúc • Mảngcấutrúclàmộtmảngmàmỗiphầntửlàmộtbiếnkiểucấutrúc. • Đểkhaibáomộtmảngcáccấutrúc, trướchếtphảikhaibáocấutrúc, sauđókhaibáomộtmảngcủacấutrúcđó. Vídụ: structds{ char hoten[25]; float toan,ly,hoa; }; dsbangdiem[50];//mảng 50 phầntửkiểuds

  10. 2.4. Mảngcáccấutrúc • Đểtruycậpđếntừngthànhviêncủatừngphầntửcủamảng, tadùngchỉmụccủaphầntửvàtoántửthànhviên (.). Vídụ: for(inti=0; i<n; i++) { cout <<"Ho va ten: "; gets(bangdiem[i].hoten) cout <<"Diem toan: "; cin >> bangdiem[i].toan; cout <<"Diem ly: "; cin >> bangdiem[i].ly; cout <<"Diem hoa "; cin >> bangdiem[i].hoa; }

  11. 2.5. Truyềnthamsốkiểucấutrúc a. Truyềnthànhviêncủabiếncấutrúcvàohàm • Có 2 cáchtruyềnthànhviêncủabiếncấutrúcvàohàm: • Truyềnthamtrị • Truyềnthambiến

  12. Vídụ: Truyềnthamtrị struct diem { int x; int y; }; double khcach(int x1, int y1, int x2, int y2) { double kc; kc=sqrt(pow((x2-x1),2)+pow((y2-y1),2)); return kc; }

  13. Vídụ: Truyềnthamtrị void main() { diem a,b; double kcach; cout<<"\ntoa do diem a:"; cout<<"\nx1=";cin>>a.x; cout<<"\ny1=";cin>>a.y; cout<<"\ntoa do diem b:"; cout<<"\nx2=";cin>>b.x; cout<<"\ny2=";cin>>b.y; cout<<"\nKhoangcachgiua a diem:"; kcach=khcach(a.x, a.y,b.x, b.y); cout<<kcach; }

  14. Vídụ: truyềnthamchiếu • Đểtruyềnđịachỉcủathànhviêncủacấutrúcvàohàm ta dùngtoántử & đặttrướctênbiếncấutrúc void doitoado(int &x, int& y, int a, int b) { x=x-a; y=y-b; } void main() { diem a,b; doitoado(a.x, a.y, 10, 10); cout<<"\nx="<<a.x; cout<<"\ny="<<a.y; }

  15. 2.5. Truyềnthamsốkiểucấutrúc b. Truyềntoànbộbiếncấutrúcđếnhàm Khimộtcấutrúcđượcdùngnhưmộtđốisốcủamộthàm, toànbộcấutrúcđượctruyềnvàothamsốhìnhthức. Cóhaicáchtruyền • Truyềnthamtrị. double khcach(diem a,diem b) { double kc; kc=sqrt(pow((b.x-a.x),2)+pow((b.y-a.y),2)); return kc; }

  16. 2.5. Truyềnthamsốkiểucấutrúc • Truyềnthamchiếu void doitoado(diem &a,int n, int m) { a.x=a.x-n; a.y=a.y-m; } void main() { diem a,b; doitoado(a, 10, 10); cout<<« x="<<a.x<<« y="<<a.y; }

  17. 2.6. Con trỏđếncấutrúc • Mộtbiến con trỏcóthểtrỏđếnmộtbiếnkiểucấutrúc. • Cúphápkhaibáomột con trỏcấutrúc structureName *structurePointers; Vídụ: struct diem { int x; int y; }; diem *p;//p là con trỏcấutrúc.

  18. 2.7. Sửdụng con trỏcấutrúc • Đểthamchiếuđếnthànhviêncủamộtcấutrúcđượctrỏđếnbởimột con trỏ, tadùngtoántử-> (toántửthamchiếugồmmộtdấutrừvàmộtdấulớnhơn). Vídụ: P->x=100; P->y=150;

  19. 2.7. Sửdụng con trỏcấutrúc void main() { struct diem { int x; int y; }; diem *p, a; p=&a; p->x=100; p->y=120; cout<<a.x; cout<<a.y; }

  20. 2.7. Sửdụng con trỏcấutrúc • Lưu ý: • Đểtruycậpđếnthànhviêncủamộtcấutrúc: • Nếudùngbiếncấutrúcthìdùngtoántửchấm(dot operator). • Nếudùngbiến con trỏthìdùngtoántử-> (arrow operator). • Truyềnthamsốlà con trỏcấutrúcthìmặcđịnhlàtruyềnthamchiếu

  21. 3. Kiểuliệtkê (Enumerations, enum) • Mộtenumlàmộttậpcủacáctênhằngnguyênxácđịnhtấtcảcácgiátrịhợplệmàmộtbiếncủakiểuđócóthểcó. • Cúpháp: enumenumName {enumList} enumVars; • enum: từkhóađểkhaibáoenum • enumName: Têncủaenum • enumList: Danhsáchcáctênhằngnguyênphâncáchnhaubởidấuphẩy • enumVars: Têncácbiếnkiểuenum.

  22. 3. Kiểuliệtkê (Enumerations, enum) • Vídụ: enumcolor {red, orange, yellow, green, blue, indigo}; color c1 = indigo; if( c1 == indigo ) { cout << "c1 is indigo" << endl; }

  23. 3. Kiểuliệtkê (Enumerations, enum) • Mỗimộttêntrongdanhsáchenumtượngtrưngchomộtgiátrịnguyên. Giátrịcủatênthứnhấttrongenumlà 0, kếtiếplà 1, ... • Ta cóthểgángiátrịkhácchomỗitênhằngnguyên

  24. 3. typedef • Từkhóatypedefdùngđểđịnhnghĩamộttênmớichomộtkiểudữliệuđãcó. • Dạngtổngquátcủadùngtypedeflà typedefexistingTypenewType; • existingType: làkiểudữliệunàođãtồntại • newType: tênmớicủakiểudữliệu

  25. 3. typedef • Vídụ: Tạomộttênmớichokiểudữliệuint typedefint int2bytes; typedef long int4bytes; Saukhicáclệnhtrênthựchiệnthìlệnh int n1; long n2; tươngđương int2bytes n1; Int4bytes n2;

More Related