STRUCTURE,UNION,ENUMERATION


Types of data created by the user( Structure,Union,Enumeration)

C++ language မ်ားတြင္ standard data type မ်ားစြာ ပါ၀င္ေသာ္လည္း တစ္ခါတစ္ရံတြင္ အသံုးၿပဳမွဳ႕ ပိုမိုလြယ္ကူေစ၇န္အတြက္ ထို programming language တြင္အဆင္သင့္ပါလာေသာ data type မ်ားထက္ variable မ်ားအား ပိုမိုရွဳပ္ေထြးေသာ structure(design) အၿဖစ္သို႕ေပါင္းစည္းနိဳင္သည္။ ထိုသို႕ၿပဳလုပ္ရန္အတြက္ C++ language တြင္ programmer မ်ားအား သူတို႕၏ ကိုယ္ပိုင္ data type မ်ားအား တည္ေဆာက္ရန္အတြက္ special tools မ်ားပါ၀င္ေပသည္။
၄င္းတို႕မွာ ေအာက္ပါအတိုင္းၿဖစ္သည္။
Structure (структуры) 
Union(combination) (объединения) 
Enumeration (перечисления)

Structure(структуры)
Structure ဆိုသည္မွာ တူညီေသာ (သို႕) ကြဲၿပားၿခားနားေသာ data type မ်ားအား အသံုးၿပဳမွဳ႕လြယ္ကူေစရန္ အတြက္ single language တစ္ခုအၿဖစ္သို႕ အမည္နာတစ္ခုၿဖင့္ ေပါင္းစည္းလိုက္ေသာ variable (သို႕) variables မ်ားၿဖစ္သည္။


Structure တစ္ခု declare(ေၾကၿငာၿခင္း) ၿပဳလုပ္ရန္အတြက္ struct ဟူေသာ keyword အားအသံုးၿပဳရသည္။ထို struct ဟူေသာ စကားလံုး၏ေနာက္တြင္ structure type တစ္၏ အမည္ၿဖစ္ေသာ dentifier(နာမည္)တစ္ခုေရးသားရမည္ၿဖစ္ၿပီး ထို႕ေနာက္၏ brackets တစ္ခုလံုး၏အတြင္းတြင္ structure’s members အားလံုး(သို႕) field list အားေရးသားရမည္ၿဖစ္သည္။ေၾကၿငာ လိုက္ေသာ structure ၏ members မ်ားသည္ တစ္ခုနွင့္တစ္ခု semicolon ၿဖင့္ ပိုင္းၿခားမည္ၿဖစ္သည္။ထို႕အတူ structure တစ္ခုလံုးၿပီးဆံုးသြားလွ်င္လည္း semicolon ၿဖင့္ အဆံုးသတ္ရမည္ၿဖစ္သည္။
struct name_struct {  data_type field_name;
                              data_type field_name;                                                                    
                               . . .
                            };

For example,
struct date {int day;
                 int month;
                };
ထိုသို႕ structure type ေၾကၿငာလိုက္သည္နွင့္ complier သည္ own(ကိုယ္ပိုင္) data type တစ္ခုအား တည္ေဆာက္လိုက္မည္ၿဖစ္သည္။သို႕ေသာ္လည္း ထို structure data type အတြက္ variable တစ္ခု မေၾကၿငာမခ်င္း ထို struct type အားအသံုးၿပဳ၍ရလိမ့္မည္မဟုတ္ေပ။

Strucrure အားအသံုးၿပဳရန္အတြက္ ပထမဦးစြာ structure type ၏ variable အားေၾကၿငာရန္ လိုအပ္ေပသည္။ထိုသို႕ ေၾကၿငာရန္အတြက္ နည္းလမ္းနွစ္မ်ိဳး အားအသံုးၿပဳနိဳင္သည္။
1)       date today; / / structure type ၏ variable အား today ဟုေၾကၿငာၿခင္း
2)       struct date {  int day;
`                            int month;
                          } today;
C language တြင္ struct ဟူေသာ စကားလံုး၏ အေရွ႕တြင္ structure ၏ variable အား ေၾကၿငာၿခင္းသည္ မၿဖစ္မေနၿပဳလုပ္ရမည္ၿဖစ္သည္။အထက္ေဖာ္ၿပပါ ပထမနည္းတြင္ struct date အား today အၿဖစ္ ေၾကၿငာထားၿခင္းေတြ႕ၿမင္ရမည္ ၿဖစ္သည္။
ဒုတိယနည္းလမ္းတြင္ structure အား တစ္ခ်ိန္တည္းတစ္ၿပိဳင္တည္း ေၾကၿငာၿခင္းၿဖစ္ၿပီး memory တြင္လည္း  structure type ၏ variable အား တစ္ၿပိဳင္တည္း ေနရာသတ္မွတ္သြားမည္ၿဖစ္သည္။structure variable မ်ားစြာအား ေၾကၿငာလိုလွ်င္ structure တစ္ခုခ်င္းဆီအား comma ၿဖင့္ ပိုင္းၿခားရမည္ၿဖစ္သည္။
date d1,d2,d3;
Structure variable အား creating ၿပဳလုပ္ေနခ်ိန္တြင္ complier သည္ ကြန္ပ်ဴတာ၏ memory တြင္ ထို variable အတြက္ ေနရာသတ္မွတ္ေပးမည္ၿဖစ္သည္။ ထိုသို႕ ေနရာသတ္မွတ္ေပးရာတြင္ ပရိုဂမ္အလုပ္လုပ္ခ်ိန္တြင္ လိုအပ္မည့္ real object အေနၿဖင့္ သတ္မွတ္ေပးၿခင္းၿဖစ္သည္။

Structure members အား access ၿပဳလုပ္ရာတြင္ ေအာက္ပါအတိုင္းၿပဳလုပ္ရသည္။
name_struct_variable.field_name;
တစ္နည္းအားၿဖင့္ structure variable အား today ဟုေၾကၿငာခဲ့လွ်င္ ေအာက္ပါအတိုင္း ေရးသားနိဳင္သည္။
taday.day =12;
today.month =4;

structure မ်ားအား ၎၏ field member မ်ား (သို႕) နွစ္သက္ရာ variable member type မ်ားၿဖင့္ ဖြဲ႕စည္းတည္ေဆာက္နိဳင္သည္။ထို ႏွစ္သက္ရာ variable member type မ်ားဆိုရာတြင္ arrays မ်ား (သို႕) အၿခား structure မ်ားပါ၀င္နိဳင္သည္။
Example
struct bigstr { float f; / / field structure - a variable of type float
char st [80], / / field structure - an array of 80 characters
double d [25], / / field structure - an array of 25 doubel real number
       } BigStr;

Assignment values of structure variables
Structure variable ၏ fields မ်ားအား value assigned ၿပဳလုပ္ရာတြင္ ေအာက္ေဖာ္ၿပပါ နည္းလမ္းသံုးမ်ိဳးမွ တစ္ခုအားအသံုးၿပဳနိဳင္သည္။(၁)assignment operator အားအသံုးၿပဳၿခင္း(၂)သတ္မွတ္ထားေသာ ပံုစံအတိုင္း keyboard မွရိုက္ထည့္ၿခင္း(၃)ေၾကၿငာလိုက္ေသာ structure variable အား initialize ၿပဳလုပ္ၿခင္း တို႕ၿဖစ္သည္။

Example  Let describes the structure of Addr, containing the addresses of four components:town, street, house, apartment.(ၿမိဳ႕..လမ္း..အိမ္..တိုက္ခန္း ပါ၀င္ေသာ လိပ္စာတစ္ခု၏ structure အားေဖာ္ၿပၿခင္း)

struct Addr{ char town[10];
                     char street[10];
                     int block;
                     int flat;
                };
Addr Home;
// this is followed by assignment of values of member of structure
//assignment operator အားအသံုးၿပဳေသာနည္းလမ္း
Home.town =”Vilnius”;
Home.street =”Atejtes”;
Home.block = 35;
Home.flat = 305;
cout << "The address is" << Home.town << Home.street << Home.block << Home.flat;
//data input from the keybobard
// keyboard မွရိုက္ထည့္ၿခင္း
cout << "Town :";
cin >> Home.town;
cout << "Street :";
cin >> Home.street;
cout << "House :";
cin >> Home.block;
cout << "apartment :";
cin >> Home.flat;
cout << "The address is" << Home.town << Home.street << Home.block << Home.flat ;

အၿခား တစ္နည္းအားၿဖင့္ structure type ၏ variable ေၾကၿငာရာတြင္ တိုက္ရိုက္ initialize ၿပဳလုပ္နိဳင္သည္။
Addr Home = {“Vilnius”,”Atejtes”, 35, 305}; //initialize ၿပဳလုပ္ၿခင္း
cout << "The address is" << Home.town << Home.street << Home.block << Home.flat ;
သတိၿပဳ၇န္မွာ initializer(assignment values) ၿပဳလုပ္ေသာအခါ structure ၏ member မ်ားနွင့္ အစဥ္အလိုက္ ကိုက္ညီ၇န္လိုအပ္ေပသည္။

Array of structure
Structure ႏွင့္ array အားေပါင္းစပ္အသံုးၿပဳၿခင္းအားၿဖင့္ database ကဲ့သို႕ေသာ စုေပါင္းဖြဲ႕စည္းရန္အတြက္ အစြမ္းထက္ေသာ tool တစ္ခုအၿဖစ္အသံုးၿပဳနိဳင္သည္။အေၿခခံ ေပါင္းစည္းၿခင္း ႏွစ္မ်ိဳးတည္၇ွိသည္။
Array of structure
Structure with fields that are arrays.

Arrray structure အားေရးသားၿခင္းသည္ array အားနွစ္သက္ရာ အၿခား data type မ်ားနွင့္ ေရးသားၿခင္းနွင့္ မ်ားစြာ တူညီသည္။array element အားလံုးသည္ type အၿဖစ္ေၾကၿငာထားေသာ structure မ်ားပင္ၿဖစ္သည္။
Structure ၏ array element မ်ားအား definitation ၿပဳလုပ္ရန္အတြက္ ေအာက္ပါအတိုင္းေၾကၿငာရမည္ ၿဖစ္သည္။
Addr Homes[10];
Array ၏ index သည္ array structure type ဆီသို႕တိုက္ရိုက္ခ်ိတ္ဆက္လိမ့္မည္ၿဖစ္သည္။structure ၏ ေနာက္ဆံုးသုိ႕ သြားေရာက္ေရးသားၿခင္းမၿပဳလုပ္ရေပ။ဆိုလိုသည္မွာ ေအာက္ပါအတိုင္းၿဖစ္သည္။
Homes.town[2];       //wrong
Homes[3].Street;      //correct
တစ္ခ်ိန္တည္းမွာပဲ Home[2].street[4] ဟူ၍အသံုးၿပဳလိုလွ်င္လည္း complier ၏ record အမွတ္အသားၿဖစ္ေသာ ‘.’(spot) အားအသံုးၿပဳၿခင္းၿဖင့္လည္း အသံုးၿပဳ၍ရသည္။street သည္လည္း array တစ္ခုၿဖစ္ရမည္ၿဖစ္သည္။ အထက္ေဖာ္ၿပပါ ေရးသားခ်က္တြင္ Home ၏ သံုးလံုးေၿမာက္ element မွ street array ၏ 5 လံုးေၿမာက္ element အား access ၿပဳလုပ္ၿခင္းပင္ၿဖစ္သည္။ ထုိနည္းၿဖင့္ structure element မ်ားတြင္ array မ်ားအား အသံုးနိဳင္သည္။
Structure ၏ field မ်ားသည္ array လည္းၿဖစ္နိဳင္သကဲ့သို႕ အၿခား type လည္းၿဖစ္နိဳင္သည္။ထပ္မံ၍ array ၏ field အား အၿခား structure မ်ားအၿဖစ္ထပ္မံေၾကၿငာနိဳင္သည္ ။သို႕ေသာ္ ထိုသိုအသံုးၿပဳလိုလွ်င္ သတိထားစရာ အခ်က္အေနၿဖင့္ ထို array သည္ memory ပမာဏနွင့္ကိုက္ညီစြာအသံုးၿပဳသင့္သည္။ ထို႕အတူ array ၏ posotion သည္ moemory ပမာဏအား အေၾကာင္းမဲ့ကုန္ဆံုးေစမည့္ အၿဖစ္မ်ိဳးမၿဖစ္ေစရေပ။

Example .Given an array of structures containing information about the information of the group of 20 students. The structure contains the following information: name, student initiated , the four estimates, reflecting its current performance during the semester. Display a list of underachieving students (with at least one rating of "2").
#include <iostream>
using namespace std;
const int GROUP = 20;
void main ()
{        int i, flag;
struct gruppa {         char * FAM;
                             int inform [4];
                    } Gruppa [GROUP];
cout << "Enter data" << endl;                             / / Write the array data.
for (i=0; i<GROUP; i++)
     {        cout << "name:";
     cin >> Gruppa [i]. FAM;
         for (int j=0; j<4; j++)
                             {        cout << j+1 << "-I score";
                                      cin >> Gruppa [i]. inform [j];
                             }
}
/ / Now we can handle an array of typed h and recordkeeping.
for (i =0; i<GROUP; i++)
              {    flag = 0;
                for (int j = 0; j <4; j++)
                            if (Gruppa [i]. inform [j] == 2) flag = 1;
                            if (flag == 1) cout << Gruppa [i]. FAM << endl;
              }
}

Union
ကြဲၿပားၿခားနားေသာ components မ်ားအား အမ်ိဳးမ်ိဳးေသာ အခ်ိန္အတိုင္းအတာခဏတာတြင္ တူညီေသာ memory area တြင္ storage ၿပဳလုပ္နိဳင္လွ်င္ ၎သည္အလြန္အသံုး၀င္ေသာ tool တစ္ခုၿဖစ္လိမ့္မည္ၿဖစ္သည္။ ထိုသို႕ေသာ ၿပသနာအားေၿဖရွင္း၇န္အတြက္ C++ တြင္ union ဟုေခၚေသာ design အားအသံုးၿပဳနိဳင္သည္။

Union အသံုးၿပဳမွဳ႕အား ေၾကၿငာရန္အတြက္ union ဟူေသာ keyword အားအသံုးၿပဳရမည္ၿဖစ္ၿပီး Structure သကဲ့သို႕ several field မ်ားအားေပါင္းစပ္နိဳင္သည္။သို႕ေသာ္ structure မ်ားတြင္ field မ်ားသည္ တစ္ခုၿပီးမွတစ္ခု ကပ္လွ်ပ္စီထားၿခင္းၿဖစ္ၿပီး union တြင္ field မ်ားအား တစ္ခုၿပီးတစ္ခု ထပ္ဆင့္ထားၿခင္းၿဖစ္သည္။သို႕မွသာ တူညီေသာ address တစ္ခုထဲတြင္ တစ္ခုၿခင္းဆီအား ေနရာခ်ထားနိဳင္မည္ၿဖစ္သည္။
Syntax:
union name_unoin{   type    field_name;
                             type    field_name;
                             ………..
                      };
Complier သည္ union အတြက္ ၎၏ element မ်ားမွ အမ်ားဆံုးပမာဏအတြက္ လံုေလာက္စြာ moemory တြင္ ေနရာခ်ထားေပးလိမ့္မည္ၿဖစ္သည္။
ပံုမွန္အားၿဖင့္ union တြင္ structure နွင့္ ၎၏ element အား special define ၿပဳလုပ္ေပးခ်င္းမ်ားပါ၀င္သည္ ။ ထိုကဲ့သို႕ပါ၀င္မွဳ႕ေၾကာင့္ မည္သည့္ field သည္ association ၿပဳလုပ္ရာတြင္ မည္သည့္ အရာမ်ားနွင့္ ၿပဳလုပ္ရမည္အား တိက်စြာၿဖစ္ေပၚေစသည္။
Example.Declaration and using union
union un {     int i;
                    float f;
                    char c;
               } my_u;

Let be a character variable flag can be one of the three values ​​of 'i', 'f' or 'c'. (character varaible တစ္ခုၿဖစ္ေသာ flag သည္ ‘i’ , ‘f’, ‘c’ သံုးခုအနက္မွ တစ္ခုၿဖစ္သည္ဆိုပါဆို႕)။Then we can write:
if (flag == 'i') {        my_u.i = 25;
                              cout << my_u.i << endl;}
else if (flag == 'f') {  my_u.f = 37.82;
                                        cout << my_u.f << endl;}
else if (flag == 'c') {  my_u.c = '*';
                                                  cout << my_u.c << endl;}
                             else
                                                   cout << "Incorrect type entered" << endl;

union သည္လည္း တစ္ဖန္အားၿဖင့္ structure အထဲတြက္လည္းပါ၀င္အသံုးၿပဳနိဳင္သည္။
struct {           char * string;
                     char flag;
                     union un {    int i;
                                         flat f;
                                         char c;
                              } My_u;
              } my_s;
Field ဆီသို႕ access ၿပဳလုပ္ရာတြင္ my_s.my_u.f ဟူ၍ အသံုးၿပဳ access ၿပဳလုပ္ရမည္။
Structure ကဲ့သို႕ပင္ union သည္လည္း declaration(ေၾကၿငာၿခင္း) ၿပဳလုပ္ရာတြင္initialization ၿပဳလုပ္နိဳင္သည္။ သို႕ေသာ္ union member မ်ား၏ ပထမဆံုးတန္ဖိုးသာလွ်င္ define ၿပဳလုပ္နိဳင္မည္ၿဖစ္သည္။ unoin သည္ structure နွင့္ကြဲၿပားၿခားနားသည္မွာ structure သကဲ့သို႕ တန္ဖိုးအားလံုးအား ခ်က္ခ်င္း initialize ၿပဳလုပ္၍မရေပ။

Enumeration
Enumerable data type သည္ user မွ ဖန္တီးနိဳင္ေသာ data type တစ္ခုၿဖစ္ၿပီး ၎အား variable မ်ား၏တန္ဖိုးအား ၾကိဳတင္၍တိက်စြာသိရွိၿပီး ၎ တန္ဖိုးမ်ား၏ အေ၇အတြက္ ေသးငယ္ေသာအခါတြင္ အသံုးၿပဳေလ့ရွိသည္။
Enum အား declaration ၿပဳလုပ္ေသာ syntax သည္ေအာက္ပါအတိုင္းၿဖစ္သည္။
enum type_enum{ const_enum_type };
Enum variable type ၿဖင့္ေၾကၿငာလိုပါက ေအာက္ေဖာ္ၿပပါအတိုင္းအသံုးၿပဳနိဳင္သည္။
enum enum_type{ const_enum_type}list_variable;

Example.Declaring an enum type.
enum month {Jan,Feb,Mar,Apr,May,Jul,Aug,Sep,Oct,Nov,Dec} birthdays;

C++ language တြင္ enum identifiers မ်ားအား integers မ်ားနွင့္ ဆက္စပ္အသံုးၿပဳနိဳင္သည္။
enum cats {Cat = 1, Puma = 2, Lion = 3, Tiger = 10};
enum birds {stork = 1, lark, swallow, eagle};
အထက္ေဖာ္ၿပပါ ေၾကၿငာခ်က္တြင္ lark ၏ constant သည္ 2 ၿဖစ္လိမ့္မည္ၿဖစ္သည္။ထို႕အတူ swallow သည္ 3, eagle သည္ 4 ၿဖစ္လိမ့္မည္။အစမွစ၍ အကၡရာအစဥ္အလိုက္သတ္မွတ္သြားၿခင္းၿဖစ္သည္။

Example. When you enter a number of the month ,program must write birthdays of friends  for who were born in this month.(လ တစ္လ၏ နံပါတ္အားရုိက္ထည့္လိုက္ပါက ပရိုဂမ္မွ ထိုလအတြင္းေမြးေသာ သူငယ္ခ်င္းမ်ားအားလံုး၏ ေမြးေန႕ကိုတြက္ထုတ္ေပးရန္။)

#include <iostream>
using names pace std;
void main ()
{typedef enum month {Jan = 1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec}
Month;
Month birthdays;
int birth_mon;
cout << "Enter month:";
cin >> birth_mon;
birthdays = birth_mon;
switch (birthdays)
{        case Jan:       cout << "17 - Anna Voronova "<< endl;
 cout << "22 - Oleg Sedov" << endl;
                              cout << "30 - Inna Yaroshenko" << endl;
                               break;
case Feb:       cout << "2 - Stanislav Aleshkin "<< endl;
 break;
case Mar:       cout << "15 - Larissa Tkachenko "<< endl;
 cout << "16 - Lena Ivannikova" << endl;
                              break;
          case Apr:      cout << "I have no friends who were born in April" << endl;
                              break;
case May:      cout << "12 - Marina Andrianov "<< endl;
 break;
          case Jun:      cout << "1 - Igor Egorov "<< endl;
cout << "17 - Daina Yasinevichyute" << endl;
cout << "17 - Lyme Yasinevichyute" << endl;
break;
          case Jul:      cout << "I have no friends who were born in July" << endl;
                            break;
          case Aug:    cout << "I have no friends who were born in August" << endl;
                            break;
case Sep:     cout << "13 - Natasha Goncharov "<< endl;
                            cout << "19 - Alexander Andrianov" << endl;
                            cout << "30 - Alexander Ivannikov" << endl;
                            cout << "30 - Tatiana Afanasyeva" << endl;
                            break;
case Oct:     cout << "10 - Julia Korotaeva" << endl;  
                 cout << "21 - Dima Voloshin" << endl; 
                  break;
          case Nov:    cout << "I have no friends who were born in November" ;
                            break;
          case Dec:     cout << "I have no friends who were born in December";
                            break;
                            default: cout << "Invalid input." << endl;
        }
}


translated by zmk@miet51



No comments:

Post a Comment