close
C++結構陣列和巢狀結構
結構是很多語言中非常之重要的資料組織方式,讓變數成為更有意義的資訊。
想聊解他從實際例子下手比較容易體會,而巢狀陣列就是結構裡使用其他結構名稱宣告成員變數。(◕‿‿◕。)
 
結構變數.成員 and 結構變數.成員[索引值]
 
假設寫計算學生成績的程式,資料有:姓名、國文、英文、數學與三科平均~~
班上30人,各項資料儲存在宣告陣列裡頭。
  1. struct Scores{  
  2.     char Name[10];  
  3.     int chi;  
  4.     int eng;  
  5.     int mat;  
  6.     float avg;  
  7. };  
  8. int main()  
  9. {  
  10.     struct Scores cir;  
  11.     cout << "輸入姓名:";cin >> cir.Name;  
  12.     cout << "輸入國文:";cin >> cir.chi;  
  13.     cout << "輸入英文:";cin >> cir.eng;  
  14.     cout << "輸入數學:";cin >> cir.mat;  
  15.     cir.avg = (cir.chi+cir.eng+cir.mat)/3.0f;  
  16.     cout << cir.Name <<"的平均成績為"<<cir.avg;  
  17. }
其實主程式裏頭的    struct Scores cir;  可以不用寫,只要在結構外寫上變數名稱就好 ! !
  1. struct Scores{  
  2.     char Name[10];  
  3.     int chi;  
  4.     int eng;  
  5.     int mat;  
  6.     float avg;  
  7. }cir;
超方便的喔~~
By the way,如果你是用C寫,在scanf裡,要寫成scanf("%d",&cir.chi); ,而不是 cir.&chi 要注意! !(。・ˇ_ˇ・。)
 
  1. struct grade{  
  2.     char Name[10]; //姓名  
  3.     int score[3];  //國英數  
  4.     float avg;     //平均成績  
  5. }cirl[3];           //宣告結構陣列cirl  
  6. int main()  
  7. {  
  8.     for(int i=0;i<3;i++)  
  9.     {  
  10.         cout << "輸入姓名:";cin >> cirl[i].Name;  
  11.         cout << "輸入國文:";cin >> cirl[i].score[0];  
  12.         cout << "輸入英文:";cin >> cirl[i].score[1];  
  13.         cout << "輸入數學:";cin >> cirl[i].score[2];  
  14.         cirl[i].avg = (cirl[i].score[0]+cirl[i].score[1]+cirl[i].score[2])/3.0f;  
  15.     }  
  16.     for(int i=0;i<3;i++)  
  17.         cout << cirl[i].Name << "的平均成績為" << cirl[i].avg << endl;  
  18. }
陣列運用看程式碼可能還是"母撒撒"(  ̄(エ) ̄),看圖吧~~
邊看圖在看程式碼,是阿呆也看得懂在幹嘛\(^o^)/
很多新手可能不習慣這個,都是一鼓腦兒把東西放在主程式,搞得亂78糟Σ(´д ';)
 
然而巢狀結構則是結構裡有結構的概念~~
那幹嘛不用一個就好啦,向陣列一樣(・A・)
舉例比較好懂,今天除了學生,在外加老師好了,
學生要打學號和年級;老師則是編號和年資~~
 
  1. struct Teacher{  //老師  
  2.     int TID;  
  3.     int Years;  
  4. };  
  5. struct Student{  //學生  
  6.     int SID;  
  7.     int SDe;  
  8. };  
  9. struct PersonalInfo{  
  10.     char Name[10];  
  11.     int Type;    //身分:t代表老師,s代表學生  
  12.     union{       //同時具備老師學生的結構內容  
  13.         struct Teacher tea;  
  14.         struct Student stu;  
  15.     };  
  16. }person; //宣告  
  17. int main()  
  18. {  
  19.     char it;  
  20.     cout << "輸入t老師、s學生:";cin >> it;  
  21.     person.Type = it;  
  22.     if(it=='t')  
  23.     {  
  24.         cout << "老師名字:"; cin >> person.Name;  
  25.         cout << "老師編號:"; cin >> person.tea.TID;  
  26.         cout << "老師年資:"; cin >> person.tea.Years;  
  27.     }  
  28.     else  
  29.     {  
  30.         cout << "學生名字:"; cin >> person.Name;  
  31.         cout << "學生學號:"; cin >> person.stu.SID;  
  32.         cout << "年級:"; cin >> person.stu.SDe;  
  33.     }  
  34.     if(person.Type=='t')  
  35.         cout << person.Name << "老師的編號:" << person.tea.TID  
  36.              << "\t服務年資:" << person.tea.Years <<endl;  
  37.     else  
  38.         cout << person.Name << "學生的學號:" << person.stu.SID  
  39.              << "\t年級:" << person.stu.SDe <<endl;  
於是乎,不用動腦想太多東西, 分類好就可以了
程式裡的union一定要學起來,它是用來儲存其他結構內的內容,到你想要的結構內,老師和學生的儲存空間共用地
 
END ლ(◉◞౪◟◉ )ლ 
 
 
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 佑佑 的頭像
    佑佑

    佑佑的語言

    佑佑 發表在 痞客邦 留言(0) 人氣()