結構(struct)、列舉(enum)、聯合(union)與自訂型態(typedef)

結構型態

結構型態定義之語法(syntax):

typedef struct
{  
    <資料型態1>:<欄位名稱1>;
    <資料型態2>:<欄位名稱2>;
           .
           .
           .
    <資料型態n>:<欄位名稱n>;
   } <結構型態名稱>;

結構型態型態定義之範例: studtype

typedef sturct 
{
 char name[20];
 long int id;
 double score;
 char grade;
} studtype;

/*
studtype是結構型態的名稱
Name,  id,  score,  grade 是欄位名稱
*/

結構型態之變項宣稱與初值設定

//範例
studtype stu1, stu2 = {“David Tai”, 12345, 86, ‘A’};

/*
利用自訂結構型態studtype宣稱結構變項stu1,stu2等
並設定結構變項stu2各欄位之初值
*/

結構型態變項之操作

利用指定敘述以操作結構變項之內容

 //範例:
   stu1.name[] =”John Miller”;
   stu1.id = 96123;
   stu1.score = 75;

   if (stu1.score >= 80)
     stu1.grade = ‘A’;
   else if ((stu1.score >= 70) &&(stu1.score < 79))
     stu1.grade = ‘B’;
   else if ((stu1.score >= 60) &&(stu1.score < 69))
     stu1.grade = ‘C’;
   else stu1.grade = ‘F’; 

   int sum;
   sum = stu1.score + stu2.score;

利用輸入及輸出指令以操作結構變項之內容

//輸入範例:
for (int j = 0; j <= 19; j++)
cin >> stu1.name[j];

   cin >> stu1.id;
   cin >> stu1.score;
   cin >> stu1.grade;

//輸出範例:
   for (int i= 0; i < 19; i++)
     cout<<stu1.name[i];

   cout<<stu1.id;
   cout<<stu1.score;
   cout<<stu1.grade;

結構陣列之宣稱與操作

結構陣列之宣稱

//範例:
studtype students[100];

結構陣列之操作

//範例:
     for (int i = 0; i <= 99; i++) {
       for (int j = 0; j <= 19; j++)cin >> students[i].name[j];
       cin >> students[i].id;
       cin >> students[i].score;
       if (students[i].score >= 80)
         students[i].grade = ‘A’;
       else if ((students[i].score >= 70) &&( students[i].score < 79))
        students[i].grade = ‘B’;
       else if ((students[i].score >= 60) &&( students[i].score < 69))
         students[i].grade = ‘C’;
       else students[i].grade = ‘F’; 
     }

巢狀結構

巢狀結構型態之定義

//範例:
   typedef struct { 
     float eng;
     float chi;
     float math;
   } scoretype;

   typedef struct {
     char enggrade;
     char chigrade;
     char mathgrade;
   } gradetype;

   typedef struct {
     char name[20];
     long int id;
     scoretype score;
     gradetype grade;
     float sumpoint;
     float gpa;  
   } stutype1;

巢狀結構變項之宣稱

//範例:
    stutype1 stu1, stu2;

巢狀結構變項之操作

//範例:
//利用指定敘述 將資料存入結構變項之欄位中
    stu2.name[] = “david tai”;
    stu2.id = 123456;
    stu1.score.eng = 95;

//利用輸入指令 將資料存入結構變項之欄位中
    For (int i = 0 ; i<=19;i++) {
      cin >> stu1.name[];
      cin >> stu1.id;
      cin >> stu1.score.eng;
      cin >> stu1.score.chi;
      cin >> stu1.score.math;
    }

//比較結構變項中欄位之值
    if (stu1.score.eng >= 80)
       stu1.grade.enggrade = ‘A’;
    else if (stu1.score.eng >= 70) && (stu1.score.eng < 80))
       stud1.grade.enggrade = ‘B’;
    else stud1.grade.enggrade = ‘C’;

    if (stu1.score.chi >= 80)
       stu1.grade.chigrade = ‘A’;
    else if (stu1.score.chi >= 70) && (stu1.score.chi < 80))
       stu1.grade.chigrade = ‘B’;
    else stu1.grade.chigrade = ‘C’;

    if (stu1.score.math >= 80)
       stu1.grade.mathgrade = ‘A’;
    else if (stu1.score.math >= 70) && (stu1.score.math < 80))
       stu1.grade.mathgrade = ‘B’;
    else stu1.grade.mathgrade = ‘C’;


//利用指定敘述與運算式 將資料存入結構變項之欄位中
//若上述各科之學分數為:英文= 3學分, 國文= 4學分, 數學=5學分 
     stu1.sumpoint = 0;
     if (stu1.grade.enggrade == ‘A’)
       stu1.sumpoint = stu1.sumpoint + (4 * 3) ;
     else if (stu1.grade.enggrade == ‘B’)
       stu1.sumpoint += (3 * 3);
     else stu1.sumpoint += (2 * 3);

     if (stu1.grade.chigrade == ‘A’)
       stu1.sumpoint = stu1.sumpoint + (4 * 4) ;
     else if (stu1.grade.chigrade == ‘B’)
       stu1.sumpoint += (3 * 4);
     else sumpoint += (2 * 4);

     if (stu1.grade.mathgrade == ‘A’)
       stu1.sumpoint = sumpoint + (4 * 5) ;
     else if (stu1.grade.mathgrade == ‘B’)
       stu1.sumpoint += (3 * 5);
     else stu1.sumpoint += (2 * 5);

     stu1.gpa = sumpoint / (3 + 4 + 5);

結構型態變項作為參數

//整數指標與結構指標範例:

int n1,n2;
int *pn1,*pn2;
pn1 = &n1;
stutype1 stu1, stu2;
stutype1 *p1, *p2;
p1 = &stu1;


//結構型態變項作為參數範例1:

void pr1(stutype1*ps,stutype1 s1){
  *ps->gpa = 3.40;
  *ps->score.eng = 15;
  *ps->id = 12345;
}

int main(){
  stutype1 stu1, stu2, *p1, *p2;
  (略)
  pr1(&stu1, stu2);
  stu2 = stu1;
}

//結構型態變項作為參數範例 2:
void pr1(stutype1 *pr; stutype s){
  stutype1 stu1, stu2;
  cin >> *pr->name[];
  cin >> *pr->id;
  cin >> *pr->score.eng;
  cin >> *pr->score.chi;
  cin >> *pr->score.math; 
  stu2 = s;
}

int main(){
  stutype1 s1, s2;
 //結構變項之傳址呼叫與傳值呼叫     
 pr1(&s1, s2);
      .
}

聯合 Union

聯合型態定義語法(syntax):

typedef union
   {<資料型態1>:<欄位名稱1>;
    <資料型態2>:<欄位名稱2>;
           .
           .
           .
    <資料型態n>:<欄位名稱n>;
   } <聯合型態名稱>;

聯合型態型態定義之範例: unitype

typedef union 
{
  long int id;
  long int SSNum;
  char MaleName[10];
  char FemaleName[10];
} unitype;

註:unitype是聯合型態的名稱 id, SSNum, MaleName, FemaleName 是欄位名稱

二、聯合型態變項的宣稱與操作

  unitype u1, u2;
  u1.id=961234;
  u2.SSNum=1234567;

註:聯合變項之各欄位共用一個儲存空間

列舉資料型態 Enum

列舉資料型態定義語法(syntax):

typedef enum
{ 
   <列舉值1>, 
   <列舉值2>,
           .
           .
           .
    <列舉值n>
 } <列舉資料型態名稱>;

### 列舉資料型態型態定義之範例:enutype
```c    
typedef enum 
{
  Sunday, Monday, Tuesday, Wedsday, Thursday, Friday, Saturday
} enutype;

註:enutype是列舉資料型態的名稱 Sunday, Monday, Tuesday, Wedsday, Thursday, Friday, Saturday 是列舉值

列舉資料型態變項的宣稱與操作

enutype enu1, enu2;
enu1=Sunday;
enu2=Friday;

註:列舉值不可直接輸出或輸入

範例:

char weekday[10];
//輸入範例
cin>>weekday[];
if(weekday[]=="sunday")
   enu1=Sunday;
else if (weekday==”Monday”)
   enu1=Monday;

//輸出範例:
if(enu1==sunday)
   cout<<”sunday”;
else if(enu1==monday)
   cout<<”monday”;
else if(enu1==tuesday)
   cout<<”tuesday”;
else if(enu1==monday)
   cout<<”monday”;

results matching ""

    No results matching ""