introduction to c++ · enumerated, structure & union in c/c++ language ตัวอย...

20
Enumerated, Structure & Union in C/C++ Language Introduction to C++ .. 1972 Dennis M. Ritchie และ Ken Thompson ไดสรางภาษา C เพื่อเพิ่มประสิทธิภาพ ภาษา B ใหดียิ่งขึ้นจากเริ่มแรกในป .. 1970 มี การพัฒนาภาษา B โดย Ken Thompson ซึ่งทํางาน บนเครื่อง DEC PDP-7 ซึ่ง ทํางานบนเครื่องไมโครคอมพิวเตอรไมได และยังมีขอจํากัดในการใช งานอยู (ภาษา B สืบทอดมาจาก ภาษา BCPL ซึ่งเขียนโดย Marth Richards) .. 1983 Bjarne Stroustrup แหงหองปฏิบัติการเบล (Bell Laboratories) ไดพัฒนาภาษา C++ ขึ้นรายละเอียดและความสามารถของ C++ มีสวนขยายเพิ่มจาก C ที่สําคัญ ไดแก แนวความคิดของการ เขียนโปรแกรมแบบ OOP (Object Oriented Programming) ซึ่งเปนแนว การเขียนโปรแกรมทีเหมาะกับการพัฒนาโปรแกรมขนาดใหญ ที่มีความสลับซับซอน มีขอมูลที่ใช ใน โปรแกรมจํานวนมาก ปจจุบันจึงนิยมใชเทคนิคของการเขียนโปรแกรมแบบ OOP ในการ พัฒนาโปรแกรม โครงสรางของโปรแกรมภาษา C++ // Myfirst.cpp 1 Comments or Remark { cout << “Myname is Urachart Kokaew”; cin.get(); } 322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 1

Upload: others

Post on 09-Feb-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to C++ · Enumerated, Structure & Union in C/C++ Language ตัวอย างโปรแกรม Boolean.CPP แสดงการกําหนดค าของข

Enumerated, Structure & Union in C/C++ Language

Introduction to C++

ค.ศ. 1972 Dennis M. Ritchie และ Ken Thompson ไดสรางภาษา C เพื่อเพิ่มประสิทธิภาพ ภาษา B ใหดียิ่งข้ึนจากเร่ิมแรกในป ค.ศ. 1970 มี การพัฒนาภาษา B โดย Ken Thompson ซ่ึงทํางานบนเคร่ือง DEC PDP-7 ซ่ึง ทํางานบนเคร่ืองไมโครคอมพิวเตอรไมได และยังมีขอจํากัดในการใชงานอยู (ภาษา B สืบทอดมาจาก ภาษา BCPL ซ่ึงเขียนโดย Marth Richards)

ค.ศ. 1983 Bjarne Stroustrup แหงหองปฏิบัติการเบล (Bell Laboratories) ไดพัฒนาภาษา C++ ข้ึนรายละเอียดและความสามารถของ C++ มีสวนขยายเพิ่มจาก C ท่ีสําคัญ ๆ ไดแก แนวความคิดของการ เขียนโปรแกรมแบบ OOP (Object Oriented Programming) ซ่ึงเปนแนว การเขียนโปรแกรมที่เหมาะกับการพัฒนาโปรแกรมขนาดใหญท่ีมีความสลับซับซอน มีขอมูลท่ีใชใน โปรแกรมจํานวนมาก ปจจุบันจึงนิยมใชเทคนิคของการเขียนโปรแกรมแบบ OOP ในการพัฒนาโปรแกรม

โครงสรางของโปรแกรมภาษา C++

// Myfirst.cpp 1 Comments or Remark

{

cout << “Myname is Urachart Kokaew”; cin.get(); }

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 1

Page 2: Introduction to C++ · Enumerated, Structure & Union in C/C++ Language ตัวอย างโปรแกรม Boolean.CPP แสดงการกําหนดค าของข

Enumerated, Structure & Union in C/C++ Language

ชนิดขอมูลกําหนดเอง (enumerated) ความหมายของขอมูลชนดิ enumerated

enumerated หมายถึง ประเภทขอมูลท่ีประกอบดวยสมาชิก(member) จํานวนหน่ึงซ่ึงมีคาคงท่ีและลําดับท่ีอยูของรายการขอมูลแนนอนและมีความสัมพันธกัน เชน วันในหน่ึงสัปดาห ประกอบดวย สมาชิก อาทิตย จันทร อังคาร พุธ พฤหัสบดี ศุกร และเสาร

วิธีกําหนดขอมูลใหเปนประเภท enumerated การกําหนดหรือประกาศใหขอมูลเปนชนิด enumerate มีรูปแบบดังน้ี

enum type_name{member1,member2,member3,…,member n); โดยท่ี

enum เปนคียเวิรด ตองเขียนทุกครั้งเม่ือกําหนด enumerated type_name คือ ช่ือประเภท enumerated ท่ีผูใชกําหนดเอง

member1,… คือ ช่ือสมาชิกและจํานวนสมาชิก กําหนดไวในเครื่องหมาย [ ] มีมากกวา 1 ตัวใช , แบงรายการสมาชิก สมาชิกทุกตัวของอินิวเมอเรดเต็ดถือเปน constant จึง เรียกอีกอยางหน่ึงวา enumerated constant คาของอินิวเมอเรดเต็ดเม่ือเรากําหนดขอมูลแบบ enumerated ในโปรแกรม C++ จะกําหนดใหสมาชิกตัวแรกมีคาเทากับจํานวนเต็ม(integer) 0 และกําหนดคา 1,2,3,…ใหแกสมาชิกท่ีอยูในลําดับตอไปตามลําดับ เชน enum DayOfWeek{sun,mon,tue,wen,thu,fri,sat}; คาของสมาชิกแตละลําดับเปนดังน้ี sun=0, mon=1,tue=2,wen=3,thu=4,fri=5,sat=6 ถาตองการกําหนดใหสมาชิกตัวแรกเริ่มท่ี 1 สามารถเขียนไดดังน้ี

enum DayOfWeek{sun=1,mon,tue,wen,thu,fri,sat}; คาของ sun=1, mon=2,tue=3,wen=4,thu=5,fri=6,sat=7 enum DayOfWeek{sun=1,mon,tue,wen=50,thu,fri,sat}; คาของ sun=1, mon=2,tue=3,wen=50,thu=51,fri=52,sat=53

การกําหนดคาของสมาชิก enumerated สามารถกําหนดคาใหเปนอักษรได เชน enum FileMenu{FileOpen='O', FileClose='F',FileSave='S',Exit='X'}; หมายเหตุ

คาของสมาชิก enumerated ปกติจะเปน integer เริ่มต้ังแต 0,1,2… เวนแตผูใชจะ กําหนดใหเปนอยางอื่นตามท่ีกลาวมา

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 2

Page 3: Introduction to C++ · Enumerated, Structure & Union in C/C++ Language ตัวอย างโปรแกรม Boolean.CPP แสดงการกําหนดค าของข

Enumerated, Structure & Union in C/C++ Language

การประกาศตัวแปรประเภท enumerated และการกําหนดคา ขอมูลประเภท enumerated เมื่อสรางช่ือประเภทขอมูลข้ึนแลว สามารถนําไปใชในการประกาศ

หรือกําหนดตัวแปรชนิดนั้นได เชน enum DayOfWeek {sun,mon,tue,wen,thu,fri,sat}; //กําหนดช่ือประเภท enumerated DayOfWeek day,newday; //day และ newday เปนตัวแปรประเภท DayOfWeek ซึ่ง

//เปนประเภทขอมูล enumerated ท่ีผูใชกําหนดข้ึนใหม จากตัวอยาง ตัวแปร day และ newday เปนตัวแปรชนิด DayOfWeek ซึ่งเปนประเภท enumerated ดังน้ัน ตัวแปร day และ newday สามารถมีคาหรือกําหนดคาท่ีเปนไปได คือ sun,mon,tue,wen,thu,fri,sat คาใดคาหน่ึงเทาน้ัน ไมสามารถเก็บคาคงท่ีชนิดอื่น ๆ ได เชน day=sun; //sun = 1 newday=sat; //sat = 6 คาของ day คือ 0 สวนคาของ newday = 6 ตัวอยางโปรแกรม enum1.cpp แสดงการสรางขอมูลประเภท enumerated และการ ประกาศตัวแปร รวมถึงการดําเนินการกับตัวแปรประเภท enumerated /*Program : enum1.cpp Process : create and manage enumerated data type*/ #include <iostream.h> #include <conio.h> void main() { enum DayOfWeek{sun,mon,tue,wen,thu,fri,sat};

DayOfWeek day,newday; //declare variable day,newday are DayOfWeek type //defind value of day and newday variable

day=mon; newday=fri; clrscr(); cout<< "Display value of member of DayOfWeek"<<endl; cout<< "sun = "<<sun<<endl; cout<< "mon = "<<mon<<endl; cout<< "tue = "<<tue<<endl; cout<< "wen = "<<wen<<endl; cout<< "thu = "<<thu<<endl; cout<< "fri = "<<fri<<endl; cout<< "sat = "<<sat<<endl; cout<< "day = mon is : "<<day<<endl; cout<< "newday =fri is : "<<newday<<endl; cout<< "Value between newday and day is : " <<newday-day<<endl; cout<< "Value between newday+day is : " <<newday+day; getch(); }

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 3

Page 4: Introduction to C++ · Enumerated, Structure & Union in C/C++ Language ตัวอย างโปรแกรม Boolean.CPP แสดงการกําหนดค าของข

Enumerated, Structure & Union in C/C++ Language

ตัวอยางโปรแกรม Boolean.CPP แสดงการกําหนดคาของขอมูลไว 2 คาคือ false และ true /*Program:Boolean.Cpp Process: use enumerated type of data */ #include <iostream.h> #include <ctype.h> #include <conio.h> void main() { enum Boolean { False, True};

Boolean Quit=False; char ch; int count=0; do{

clrscr(); ++count; cout<<"\n Count = "<<count; cout<<"\n Continue <y/n> "; ch=getch(); if(toupper(ch)=='N') Quit=True;

}while(!Quit); cout<<"\n end of program";getch();

}

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 4

Page 5: Introduction to C++ · Enumerated, Structure & Union in C/C++ Language ตัวอย างโปรแกรม Boolean.CPP แสดงการกําหนดค าของข

Enumerated, Structure & Union in C/C++ Language

ขอมูลแบบมีโครงสราง structure และ union ในภาษา C/C++

1. ความหมายของ structure

structure หมายถึง กลุมขอมูลซึ่งประกอบดวยขอมูลชนิดเดียวกันหรือหลายชนิดก็ได ทําใหสามารถแยกโปรแกรมออกเปนหนวย (Modular Programming) ซึ่งแกไขไดงายเพราะสามารถแยกฟงกชันและตัวแปรออกเปนหนวยๆ ซึ่งก็เปนประโยชนในการสรางฐานขอมูลดวย

ขอมูล แตละรายการใน structure เรียกวา สมาชิกของสตรักเจอร (members of structure) ซึ่งตางชนิดกันไดในแตละรายการ ตัวอยาง ขอมูลท่ีสามารถนํามาเก็บเปนชนิด structure ไดแกขอมูลท่ีมีรายละเอียดภายในแตละรายการเปนขอมูลตางชนิด (Data Type) กัน ดังเชน

- ขอมูลประวัตินักศึกษา ประกอบดวย รหัสนกัศึกษา, ช่ือนักศึกษา, อายุ, วันเดือนปเกิด - ขอมูลรายละเอียดวิชา ประกอบดวย รหัสวชิา, ช่ือวิชา, จํานวนหนวยกิต, ผูสอน - ขอมูลประวัติสินคา ประกอบดวย รหัสสินคา, คําอธิบายรายการ, หนวยนบั, ราคาตอหนวย

2. วิธีสรางขอมลูประเภท structure และการนําไปใช

กอนท่ีจะนําขอมูลประเภท structure มาใชในโปรแกรม มี 2 ข้ันตอน คือ 1. สรางช่ือประเภทขอมูลใหเปนประเภท structure กอนโดยผูใชกําหนดช่ือ structure จาก keyword คําวา struct 2. จากนั้นจากนํา ช่ือประเภทขอมูล structure ไปประกาศตัวแปรท่ีตองการสรางข้ึนใชภายใน โปรแกรม

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 5

Page 6: Introduction to C++ · Enumerated, Structure & Union in C/C++ Language ตัวอย างโปรแกรม Boolean.CPP แสดงการกําหนดค าของข

Enumerated, Structure & Union in C/C++ Language

การสรางประเภทขอมูลใหเปน structure มีรปูแบบโครงสราง ดังน้ี struct ช่ือสตรักเจอร

{ ชนิดขอมูล ช่ือรายการ; ชนิดขอมูล ช่ือรายการ; ชนิดขอมูล ช่ือรายการ; ชนิดขอมูล ช่ือรายการ;

} ; ตัวอยางเชน ถาตองการสรางขอมูลแบบ structure เพื่อเก็บขอมูล รหัสนักศึกษา, ช่ือนักศึกษา, คะแนนสอบระหวางภาค, คะแนนสอบปลายภาค และเกรด กําหนดรายละเอียดใน structure ดังน้ี struct STD //(ช่ือ structure กําหนดโดยผูใช ) { char code[9]; // (ช่ือ ฟลด(Field) ในโครงสราง )

char name[31]; int midterm; int final; char grade;

}; STD คือช่ือของประเภท structure ท่ีมีสมาชิกหรือฟลด(Field) 5 รายการคือ code, name, midterm, final และ grade โดยท่ีสมาชิกแตละรายการมีชนิดขอมูลท่ีแตกตางกันได

3.การประกาศตัวแปร เมื่อตองการนําช่ือ STD ไปใชประกาศตัวแปร สามารถประกาศตัวแปรไดตามรูปแบบ การประกาศตัวแปรประเภทอื่น ๆ ทําได 3 วิธี คือ 3.1. การใช คําสั่ง struct สรางตัวแปรชนิด student โดยตรง คือ ประกาศตัวแปรไปพรอมกับการสราง structure struct STD { char code[9];

char name[31]; int midterm; int final; char grade;

} student;

struct product { int weight; float price; } apple, banana, melon;

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 6

Page 7: Introduction to C++ · Enumerated, Structure & Union in C/C++ Language ตัวอย างโปรแกรม Boolean.CPP แสดงการกําหนดค าของข

Enumerated, Structure & Union in C/C++ Language

3.2 การใช คําสั่ง struct สรางตัวแปรชนิด student ผานตัวแปร STD คือ ประกาศตัวแปร หลังจากสราง structure แลว struct STD { char code[9];

char name[31]; int midterm; int final; char grade;

}; STD student; // ช่ือตัวแปร Student เปนประเภท STD 3.3 การใช คําสั่ง typedef struct เพ่ือสรางตัวแปรชนิด student ผานตัวแปร STD typedef struct { char code[9];

char name[31]; int midterm; int final; char grade;

} STD; STD student; #define MAX_LEN 12 /* Length of name */ typedef struct { char name[MAX_LEN]; int id; float gpa; char major[10]; } product; product s; product stu1, stu2, stu3;

struct product { int weight; float price; }; product apple; product banana, melon;

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 7

Page 8: Introduction to C++ · Enumerated, Structure & Union in C/C++ Language ตัวอย างโปรแกรม Boolean.CPP แสดงการกําหนดค าของข

Enumerated, Structure & Union in C/C++ Language

4. การอางถึง structure และสมาชิกของ structure การอางถึงสมาชิกใน structure ไมสามารถจะอางถึงช่ือรายละเอียดใน structure โดยตรงแต

จะตองกําหนดตัวแปร(variable) ท่ีมีชนิดเปน structure ข้ึนมากอน เชน จากตัวอยางท่ีสราง structure ช่ือ STD ข้ึน เราสามารถประกาศตัวแปร student และ stud ใหเปนประเภท STD ไดดังน้ี

STD student,stud; //ช่ือตัวแปรคือ student และ stud หรือ

อาจประกาศในขณะสราง structure ก็ได มีรูปแบบดังน้ี struct STD {

char code[9]; char name[31]; int midterm; int final; char grade;

}student,stud;

เมื่อเรากําหนดตัวแปร student และ stud ใหเปนชนิด STD ซึ่งเปน structure ไดแลว เรา สามารถอางถึงสมาชิกของ STD ไดโดยการเขียนช่ือตัวแปรเครื่องหมายจุด(.)และตามดวยช่ือรายการ สมาชิกหรือฟลด(Field)ของ STD ดังน้ี

student.code หมายถึง รหัสนักศึกษา student.name หมายถึง ช่ือนักศึกษา student.midterm หมายถึง คะแนนสอบระหวางภาค student.final หมายถึง คะแนนสอบปลายภาค student.grade หมายถึง เกรด หรือ stud.code หมายถึง รหัสนักศึกษา stud.name หมายถึง ช่ือนักศึกษา stud.midterm หมายถึง คะแนนสอบระหวางภาค stud.final หมายถึง คะแนนสอบปลายภาค stud.grade หมายถึง เกรด

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 8

Page 9: Introduction to C++ · Enumerated, Structure & Union in C/C++ Language ตัวอย างโปรแกรม Boolean.CPP แสดงการกําหนดค าของข

Enumerated, Structure & Union in C/C++ Language

สรุปหลักการเขาถึงตัวแปรรูปแบบท่ัวไป struct: VarName.FieldName เชน

typedef struct { int month, day, year; } DATE; void main() { DATE d1; d1.month = 12; d1.day = 2; d1.year = 1970; }

การกําหนดคาเร่ิมตนใหกับตัวแปร เราไมสามารถกําหนดคาเริ่มตนใหกับตัวแปรไปพรอมกับการประกาศตัวแปรได ดังน้ันหากเรากําหนดดังตัวอยางขางลางนี้จะเกิดขอผิดพลาดข้ึน

struct CAR { char color[10] = “Black”; long price = 40000; } mycar; [C++ Error] ……….cpp(6): E2233 Cannot initialize a class member here struct CAR { char color[10] = “Red”; long price = 30000; } hiscar; [C++ Error] ……….cpp(6): E2233 Cannot initialize a class member here

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 9

Page 10: Introduction to C++ · Enumerated, Structure & Union in C/C++ Language ตัวอย างโปรแกรม Boolean.CPP แสดงการกําหนดค าของข

Enumerated, Structure & Union in C/C++ Language

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 10

Page 11: Introduction to C++ · Enumerated, Structure & Union in C/C++ Language ตัวอย างโปรแกรม Boolean.CPP แสดงการกําหนดค าของข

Enumerated, Structure & Union in C/C++ Language

5. การกําหนดคาคงที่ใหกับสมาชิกของ structure struct STD { char code[9];

char name[31]; int midterm; int final; char grade;

}student,stud; จากโครงสรางของ STD และการกําหนดตัวแปร student , stud สามารถกําหนดคาใหกับตัว แปรไดดังนี้ 5.1. กําหนดคาคงท่ีใหกับ structure เขียนไดดังน้ี

strcpy(student.code,"40214514"); //เนื่องจากเปน string ตองใช strcpy() strcpy(student.name, "Somsak"); student.midterm = 45; student.final = 30; student.grade=75;

5.2. การกําหนดคาของตัวแปรใหกับ structure เขียนไดดังน้ี char co[9]=”10015210”; strcpy(student.code,co); int x = 25; student.midterm = x;

6. การรับขอมูลจากคียบอรดเกบ็ในสมาชิกของ sturcture การรับคาคงท่ีจากคียบอรดไปเก็บไวใน structure ทําไดโดยใช cin>> ดังน้ี

cin>>student.code; cin>>student.name; cin>>student.midterm; cin>>student.final; cin>>student.grade;

7. การแสดงขอมูลจากสมาชกิของ structure การแสดงผลขอมูลจาก structure ทําไดโดยใช cout<< ดังน้ี

cout<<student.code; cout<<student.name; cout<<student.midterm; cout<<student.final; cout<<student.grade;

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 11

Page 12: Introduction to C++ · Enumerated, Structure & Union in C/C++ Language ตัวอย างโปรแกรม Boolean.CPP แสดงการกําหนดค าของข

Enumerated, Structure & Union in C/C++ Language

ตัวอยางโปรแกรมที่ 1 stru_exp1.cpp แสดงการกําหนดโครงสราง structure การประกาศตัว แปร การรับคาทางคียบอรด และแสดงคาใน structure เพื่อเก็บรายละเอียดขอมูลคะแนน สอบนักศึกษา จํานวน 1 คน /*Program : stru_ex1.cpp Process : create structure and manage structure*/ # include <iostream.h> #include <conio.h> struct STD //declaration structure STD { char code[9]; char name[31]; int midterm; int final; char grade; }student; //declaration student is global variable has structure typeSTD student; void display(); //declaration prototype function void input(); void main() //begin main program { clrscr(); input(); clrscr(); display(); getch(); } //end main program void input() //function enter data { clrscr();

gotoxy(30,2); cout<< " Please enter data : "; gotoxy(5,4); cout<< "Code : "; gotoxy(5,5); cout<< "Name : "; gotoxy(5,6); cout<< "Midterm : "; gotoxy(5,7); cout<< "Final : "; gotoxy(5,8); cout<< "Grade : "; gotoxy(15,4); cin >>student.code; //input from keyboardgotoxy gotoxy(15,5); cin>>student.name;

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 12

Page 13: Introduction to C++ · Enumerated, Structure & Union in C/C++ Language ตัวอย างโปรแกรม Boolean.CPP แสดงการกําหนดค าของข

Enumerated, Structure & Union in C/C++ Language

gotoxy(15,6); cin>>student.midterm; gotoxy(15,7); cin>>student.final; gotoxy(15,8); cin>>student.grade;

} void display() //function display data {

gotoxy(30,2); cout<< " Your Information : \t"; gotoxy(5,4); cout<< "Code : "<<student.code; gotoxy(5,5); cout<< "Name : "<<student.name; gotoxy(5,6); cout<< "Midterm : "<<student.midterm; gotoxy(5,7); cout<< "Final : "<<student.final; gotoxy(5,8); cout<< "Grade : "<<student.grade;

} อารเรยของ structure การกําหนดอารเรยของ structure มีวิธีการเชนเดียวกับการกําหนดอารเรย 1

มิติปกติ เพราะถือ วา structure เปนขอมูล 1 รายการในอารเรย ดังน้ันการอางอิงถึงขอมูลท่ีเปนอารเรยชนิดโครงสราง จึงตองอางถึงตําแหนงรายการของสมาชิกในอารเรยรวมกับการอางถึงสมาชิกใน structure struct STD //สราง structure ช่ือ STD {

char code[9]; char name[31]; int midterm; int final; char grade;

}; STD student[5]; //ประกาศตัวแปรช่ือ student เปนอารเรยรายการ 5

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 13

Page 14: Introduction to C++ · Enumerated, Structure & Union in C/C++ Language ตัวอย างโปรแกรม Boolean.CPP แสดงการกําหนดค าของข

Enumerated, Structure & Union in C/C++ Language

ดังน้ัน การอางถึงสมาชิกของอาเรยตองใช [index] ตามหลังช่ือตัวแปรและอางถึงสมาชิกของ

structure ตองใช ตามดวยจุด (.) และช่ือรายการสมาชิกใน structure ดังน้ี student[0].midterm=30; student[1].final=50; strcpy(student[0].code,"10012512"); cin>>student[4].code; cout<<student[4].code; ตัวอยางโปรแกรมที่ 2 movie.cpp // example about structures #include <iostream.h> #include <string.h> struct movies_t { char title[30]; int year; } mine, yours; void printmovie (movies_t movie); int main () { strcpy(mine.title,"2001 A Space Odyssey"); mine.year = 1968; cout << "Enter title: "; cin >> yours.title;

ผลลัพธหนาจอ Enter title: Alien Enter year: 1979 My favorite movie is: 2001 A Space Odyssey (1968) And yours is: Alien (1979)

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 14

Page 15: Introduction to C++ · Enumerated, Structure & Union in C/C++ Language ตัวอย างโปรแกรม Boolean.CPP แสดงการกําหนดค าของข

Enumerated, Structure & Union in C/C++ Language

cout << "Enter year: "; cin >> yours.year; cout << "My favorite movie is:\n "; printmovie (mine); cout << "And yours is:\n "; printmovie (yours); cin.get( ); return 0; } void printmovie (movies_t movie) { cout << movie.title; cout << " (" << movie.year << ")\n"; }

ตัวอยางโปรแกรมท่ี 3 stru_arr1.cpp แสดงการใชโครงสรางขอมูลอารเรยรวมกับโครงสรางขอมูลแบบ structure เพื่อเก็บรายละเอียดขอมูล รหัส, ช่ือ, คะแนนสอบระหวางภาค, ปลายภาค และเกรดของนักศึกษา จํานวน 5 คน /*Program : stru_arr1.cpp Process : create structure and manage structure*/ #include <iostream.h> #include <conio.h> struct STD //declaration structure STD { char code[9]; char name[31]; int midterm; int final; char grade; }; void input(); //declaration prototype function void display(); STD student[5]; //declaration array structure is global variable

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 15

Page 16: Introduction to C++ · Enumerated, Structure & Union in C/C++ Language ตัวอย างโปรแกรม Boolean.CPP แสดงการกําหนดค าของข

Enumerated, Structure & Union in C/C++ Language

void main() //begin main program {

clrscr(); input(); clrscr(); display();

} //end main program void input() //function enter data { int i;

for(i=0;i<=4;++i) { clrscr();

gotoxy(15,2);cout<< " Please enter data of student No.# \t"<<i+1; gotoxy(5,4);cout<< "Code : "; gotoxy(5,5);cout<< "Name : "; gotoxy(5,6);cout<< "Midterm : "; gotoxy(5,7);cout<< "Final : "; gotoxy(5,8);cout<< "Grade : "; gotoxy(15,4);cin>>student[i].code; gotoxy(15,5);cin>>student[i].name; gotoxy(15,6);cin>>student[i].midterm; gotoxy(15,7);cin>>student[i].final; gotoxy(15,8);cin>>student[i].grade;

} } void display() //function display data {

int i; for(i=0;i<=4;++i) { clrscr();

gotoxy(30,2);cout<< " Your Information : studen#\t"<<i+1; gotoxy(5,4);cout<< "Code : "<<student[i].code; gotoxy(5,5);cout<< "Name : "<<student[i].name;

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 16

Page 17: Introduction to C++ · Enumerated, Structure & Union in C/C++ Language ตัวอย างโปรแกรม Boolean.CPP แสดงการกําหนดค าของข

Enumerated, Structure & Union in C/C++ Language

gotoxy(5,6);cout<< "Midterm : "<<student[i].midterm; gotoxy(5,7);cout<< "Final : "<<student[i].final; gotoxy(5,8);cout<< "Grade : "<<student[i].grade; cout<<endl<<endl<< "press any key to continue..."; getch();

} } ตัวอยางโปรแกรมที่ 4 movie_array.cpp // array of structures #include <iostream.h> #define N_MOVIES 3 struct movies_t { char title[30]; int year; } films [N_MOVIES]; void printmovie (movies_t movie); int main () { int n; for (n=0; n<N_MOVIES; n++) { cout << "Enter title: "; cin >> films[n].title; cout << "Enter year: "; cin >> films[n].year; } cout << "\nYou have entered these movies:\n"; for (n=0; n<N_MOVIES; n++) printmovie (films[n]); cin.get(); cin.get(); return 0; } void printmovie (movies_t movie) { cout << movie.title; cout << " (" << movie.year << ")\n"; }

ผลลัพธหนาจอ Enter title: Blade Runner Enter year: 1982 Enter title: Matrix Enter year: 1999 Enter title: Taxi Driver Enter year: 1976 You have entered these movies: Blade Runner (1982) Matrix (1999) Taxi Driver (1976)

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 17

Page 18: Introduction to C++ · Enumerated, Structure & Union in C/C++ Language ตัวอย างโปรแกรม Boolean.CPP แสดงการกําหนดค าของข

Enumerated, Structure & Union in C/C++ Language

8. ขอมูลชนิด union ยูเนียน(union) เปนกลุมขอมูลท่ีมีโครงสรางเชนเดียวกับ structure แตมีความแตกตางกันใน ดาน

วิธีการกําหนด และวิธีการจัดเก็บขอมูลในหนวยความจํา การจัดเก็บขอมูลในหนวยความจํา สมาชิกแตละรายการของ structure จะจองพ้ืนท่ีจัดเก็บ ขอมูลในหนวยความจําอิสระจากกัน แต union สมาชิกทุกตัวจะจองพื้นท่ีในหนวยความจําตําแหนง เดียวกัน และใชหนวยความจําตําแหนงน้ันรวมกัน สมาชิกแตละตัวจะเขาไปใชหนวยความจําพรอมกันไมได จะตองสลับกันใชหนวยความจําตําแหนงน้ัน จึงตองระมัดระวังความถูกตองของขอ มูลเม่ือจะอางถึงสมาชิกแตละตัวใน union มีประโยชนในเรื่องของการประหยัดหนวยความจํา ถาจําเปนตองใชตัวแปรหลายตัว แตเขาไปใชหนวยความจําไมพรอมกัน กลาวโดยสรุปคือตัวแปรแตละตัวในยูเนียนจะใชหนวยความจํารวมกันดังน้ันการท่ีขอมูลของตัวแปรตางชนิดกัน หนวยความจําจะถูกจัดสรรใหมีขนาดใหญใหพอท่ีขอมูลโตท่ีสุดจะใชได

การกําหนดขอมูลแบบ union มีรูปแบบดังน้ี union ช่ือยูเนียน { ชนิดขอมูล

ช่ือรายการสมาชิก; ชนิดขอมูล ช่ือรายการสมาชิก; ชนิดขอมูล ช่ือรายการสมาชิก; ชนิดขอมูล ช่ือรายการสมาชิก;

}; เชน

union STD2 { char code[9];

char name[31]; int midterm; int final; char grade;

}; typedef union

{ int x1; //2 ไบต float x2; // 4 ไบต

} TY1; TY1 k;

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 18

Page 19: Introduction to C++ · Enumerated, Structure & Union in C/C++ Language ตัวอย างโปรแกรม Boolean.CPP แสดงการกําหนดค าของข

Enumerated, Structure & Union in C/C++ Language

ตัวอยางโปรแกรมที่ 5 union.cpp แสดงการใชขอมูลประเภท structure เปรียบเทียบกับการใช union เนื่องจากสมาชิกของ structure ใชวิธีการเก็บขอมูลในหนวยความจําแยกกันเปน อิสระจึงสามารเก็บขอมูลพรอมกันได และนําสวน union มาใชไดถูกตอง สมาชิก ทุกรายการใชหนวยความจําตําแหนงเดียวกัน สมาชิกจะเขามาใชพรอมกันไมได ดังน้ันจึง แสดงขอมูลท่ีเก็บไวไดไมถูกตอง เมื่อกําหนดใหเก็บขอมูลพรอมกัน ถูกตองเฉพาะรายการ สุดทายท่ีเขาไปใชหนวยความจํา /*Program : union.cpp Process : create structure and union ,compare using memory*/ #include <iostream.h> #include <conio.h> #include <string.h> struct STD //declaration structure STD { char code[9];

char name[31]; int midterm; int final; char grade;

}; union STD2 //declare union STD2 { char code[9];

char name[31]; int midterm; int final; char grade;

}; //declaration global variable STD student; //type is structure STD2 stu; //type is union void main() //begin main program { clrscr(); //set value of student variabel...struct type

strcpy(student.code,"100001"); strcpy(student.name,"Urachart"); student.midterm=35; student.final=30; student.grade='A'; //set value of stu variable... union type

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 19

Page 20: Introduction to C++ · Enumerated, Structure & Union in C/C++ Language ตัวอย างโปรแกรม Boolean.CPP แสดงการกําหนดค าของข

Enumerated, Structure & Union in C/C++ Language

strcpy(stu.code,"100001"); strcpy(stu.name,"Urachart"); stu.midterm=35; stu.final=30; stu.grade='A'; cout<< "Display data from student variable of struct"<<endl;

cout<< "student.code= "<<student.code<<endl; //display data from student (struct) cout<< "student.name= "<<student.name<<endl; cout<< "student.midterm= "<<student.midterm<<endl; cout<< "student.final= "<<student.final<<endl; cout<< "student.grade= "<<student.grade<<endl; cout<< "*** correct value of student ***\t"<<endl<<endl; //display data from stu (union) cout<< "Display data from stu variable of union"<<endl; cout<< "stu.code= "<<stu.code<<endl; cout<< "stu.name= "<<stu.name<<endl; cout<< "stu.midterm= "<<stu.midterm<<endl; cout<< "stu.final= "<<stu.final<<endl; cout<< "stu.grade= "<<stu.grade<<endl; cout<< "*** error value of student ***\t"<<endl; getch(); }

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 20