Test Struct cpp Test Struct cpp include iostream















![struct陣列各欄位的資料 cout 16 << << << Officer[8]. Name << endl; Officer[12]. Phone << endl; struct陣列各欄位的資料 cout 16 << << << Officer[8]. Name << endl; Officer[12]. Phone << endl;](https://slidetodoc.com/presentation_image_h/7c6b955ae7c3fbea12160b17d7d750fa/image-16.jpg)


![for (int i=0; i<Size; i++) { cout << "請輸入 Officer[" << I << "] 的姓名: for (int i=0; i<Size; i++) { cout << "請輸入 Officer[" << I << "] 的姓名:](https://slidetodoc.com/presentation_image_h/7c6b955ae7c3fbea12160b17d7d750fa/image-19.jpg)
![執行結果 共 2個Officers: 請輸入Officer[0] 的姓名: Alan John 電話號碼: 03 -4521234 請輸入 Officer[1] 的姓名: Peter 執行結果 共 2個Officers: 請輸入Officer[0] 的姓名: Alan John 電話號碼: 03 -4521234 請輸入 Officer[1] 的姓名: Peter](https://slidetodoc.com/presentation_image_h/7c6b955ae7c3fbea12160b17d7d750fa/image-20.jpg)




![void Change. Name (Employee& A, char New. Name[]) { strcpy(A. Name, New. Name); return; void Change. Name (Employee& A, char New. Name[]) { strcpy(A. Name, New. Name); return;](https://slidetodoc.com/presentation_image_h/7c6b955ae7c3fbea12160b17d7d750fa/image-25.jpg)






![32 void Change. Name (Employee& A, char New. Name[]) { strcpy(A. Name, New. Name); 32 void Change. Name (Employee& A, char New. Name[]) { strcpy(A. Name, New. Name);](https://slidetodoc.com/presentation_image_h/7c6b955ae7c3fbea12160b17d7d750fa/image-32.jpg)
































- Slides: 64










範例程式 檔案 Test. Struct. cpp // Test. Struct. cpp #include <iostream> using namespace std; struct Employee { char Name[20]; char Phone[10]; int Id; }; // ----- 主程式 --------------10

int main() { Employee. Ea= {"Ann", "02384125", 105}; Employee Eb = {"Joanne", "03544132", 106}; cout << "Ea 的資料是:n" << "姓名 : " << Ea. Name << 'n' << "電話號碼: " << Ea. Phone << 'n' << "編號 : " << Ea. Id << endl; cout << "Eb 的資料是:n" << "姓名 : " << Eb. Name << 'n' << "電話號碼: " << Eb. Phone << 'n' << "編號 : " << Eb. Id << endl; return 0; } 11




![struct陣列各欄位的資料 cout 16 Officer8 Name endl Officer12 Phone endl struct陣列各欄位的資料 cout 16 << << << Officer[8]. Name << endl; Officer[12]. Phone << endl;](https://slidetodoc.com/presentation_image_h/7c6b955ae7c3fbea12160b17d7d750fa/image-16.jpg)
struct陣列各欄位的資料 cout 16 << << << Officer[8]. Name << endl; Officer[12]. Phone << endl; Officer[40]. Id << endl;


範例程式 檔案 Struct. Array. cpp // Struct. Array. cpp #include <iostream> using namespace std; 18 const int Name. Size = 20; const int Phone. Size = 10; struct Employee { char Name[Name. Size]; char Phone[Phone. Size]; }; int main() { const int Size = 2; Employee Officer[Size]; cout << "共 " << Size << " 個 Officers:n";
![for int i0 iSize i cout 請輸入 Officer I 的姓名 for (int i=0; i<Size; i++) { cout << "請輸入 Officer[" << I << "] 的姓名:](https://slidetodoc.com/presentation_image_h/7c6b955ae7c3fbea12160b17d7d750fa/image-19.jpg)
for (int i=0; i<Size; i++) { cout << "請輸入 Officer[" << I << "] 的姓名: "; cin. getline(Officer[i]. Name, Name. Size, 'n'); cout << "電話號碼: "; cin. getline(Officer[i]. Phone, Phone. Size, 'n'); } for (int i=0; i<Size; i++) { cout << "Officer[" << i << "] 的資料是:n" << "姓名 : " << Officer[i]. Name << 'n' << "電話號碼: " << Officer[i]. Phone << 'n'; } return 0; 19 }
![執行結果 共 2個Officers 請輸入Officer0 的姓名 Alan John 電話號碼 03 4521234 請輸入 Officer1 的姓名 Peter 執行結果 共 2個Officers: 請輸入Officer[0] 的姓名: Alan John 電話號碼: 03 -4521234 請輸入 Officer[1] 的姓名: Peter](https://slidetodoc.com/presentation_image_h/7c6b955ae7c3fbea12160b17d7d750fa/image-20.jpg)
執行結果 共 2個Officers: 請輸入Officer[0] 的姓名: Alan John 電話號碼: 03 -4521234 請輸入 Officer[1] 的姓名: Peter Pan 電話號碼: 02 -4354512 Officer[0] 的資料是: 姓名 : Alan John 電話號碼: 03 -4521234 Officer[1] 的資料是: 姓名 : Peter Pan 電話號碼: 02 -4354512 20



使用傳參照 (pass by reference) 改變struct 實例的內容 n 例如: Change. Name(Ea, “Jackson”); n 對應的「被呼叫函數」則定義成: void Change. Name (Employee& A, char New. Name[]) { strcpy(A. Name, New. Name); return; } 23

範例程式 檔案 Struct. Fnc. cpp // Struct. Fnc. cpp #include <iostream> using namespace std; struct Employee { char Name[20]; char Phone[10]; int Id; }; 24 void Show. Member(Employee A) { cout << "資料的詳細內容是:n" << "姓名 : " << A. Name << 'n' << "電話號碼: " << A. Phone << 'n' << "編號 : " << A. Id << endl; return; }
![void Change Name Employee A char New Name strcpyA Name New Name return void Change. Name (Employee& A, char New. Name[]) { strcpy(A. Name, New. Name); return;](https://slidetodoc.com/presentation_image_h/7c6b955ae7c3fbea12160b17d7d750fa/image-25.jpg)
void Change. Name (Employee& A, char New. Name[]) { strcpy(A. Name, New. Name); return; } 25 // ======= 主程式 ============ int main() { Employee Ea = {"Ann", "02384125", 105}; Employee Eb = {"Joanne", "03544132", 106}; Show. Member(Ea); Show. Member(Eb); Change. Name(Ea, "Jackson"); cout << "執行 Change. Name() 後:n"; Show. Member(Ea); return 0; }


使用指標改變struct實例的內容 n 使用傳址 (pass-by-address) 來達到使用參照的 目的: Change. Id(&Ea, n 00128); 「被呼叫函數」則定義為 void Change. Id(Employee* p. E, int New. Id) { (*p. E). Id=New. Id; return; } 27



進一步改寫 Change. Id() void Change. Id(Employee* p. E, { p. E->Id = New. Id; return; } 30 int New. Id)

範例程式 檔案 Struct. Fnc 2. cpp 31 // Struct. Fnc 2. cpp #include <iostream> using namespace std; struct Employee { char Name[20]; char Phone[10]; int Id; }; void Show. Member(Employee A) { cout << "資料的詳細內容是:n" << "姓名 : " << A. Name << "電話號碼: " << A. Phone << "編號 : " << A. Id } << 'n' << endl; return;
![32 void Change Name Employee A char New Name strcpyA Name New Name 32 void Change. Name (Employee& A, char New. Name[]) { strcpy(A. Name, New. Name);](https://slidetodoc.com/presentation_image_h/7c6b955ae7c3fbea12160b17d7d750fa/image-32.jpg)
32 void Change. Name (Employee& A, char New. Name[]) { strcpy(A. Name, New. Name); return; } void Change. Id(Employee* p. E, int New. Id) { p. E->Id = New. Id; return; } // ===== 主程式 ============ int main() { Employee Ea = {"Ann", "02384125", 105}; Employee Eb = {"Joanne", "03544132", 106}; Show. Member(Ea); Show. Member(Eb); Change. Id(&Ea, 208); cout << "執行 Change. Id() 後:n"; Show. Member(Ea); return 0; }


struct實例的動態宣告 亦即struct實例的動態記憶體配置 (dynamic memory allocation)。 n 下列敘述則可以在執行時才臨時決定陣 列的大小: n int Size; cin >> Size; Employee* p. E = new Employee[Size]; 34




範例程式 檔案 Dyn. Struct. cpp // Dyn. Struct. cpp #include <iostream> using std::cin; using std::cout; struct Employee { char Name[20]; char Phone[10]; int Id; }; // ----- 主程式 ------------ 38

int main() { int Size; cout << "請輸入 Employee 的數目:n"; cin >> Size; Employee* p. E = new Employee[Size]; delete [] p. E; return 0; } 39






一個串列的範例 n 假設使用struct宣告了一個名叫 Element 的自訂資料形態: struct Element { int Value; Element* Next; }; 45

動態產生任意數目的Element (各實例的 值在此為 0, 2, 4, 6, …): cout << "請輸入 Element 的數目:n"; cin >> Size; Element* p. E = new Element[Size]; for (int i=0; i<(Size-1); i++) p. E[i]. Next = p. E + i +1; p. E[Size-1]. Next = NULL; for (int i=0; i<(Size); i++) p. E[i]. Value = i*2; 46

顯示現有串列元素 Element* p. Show; for (p. Show = p. E; p. Show != NULL; p. Show=p. Show->Next) cout << p. Show->Value << ' '; n 47 不斷更換指標使它指向下一個元素的位址。

以while迴圈顯示現有串列元素 Element* p. Show=p. E; while (p. Show != NULL) { cout << p. Show->Value << ' '; p. Show = p. Show->Next; } 48

將顯示串列內容的功能封裝到函數中 void Show. Element(Element* p. Show) { while (p. Show != NULL) { cout << p. Show->Value << ' '; p. Show = p. Show->Next; } } 49

範例程式 檔案 List. Struct. cpp 50 // List. Struct. cpp #include <iostream> using namespace std; struct Element { int Value; Element* Next; }; void Show. Element(Element* p. Show) { while (p. Show != NULL) { cout << p. Show->Value << ' '; p. Show = p. Show->Next; } }

51 // ---主程式------------int main() { int Size; cout << "請輸入 Element 的數目: n"; cin >> Size; Element* p. E = new Element[Size]; for (int i=0; i<(Size-1); i++) p. E[i]. Next = p. E + i +1; p. E[Size-1]. Next = NULL; for (int i=0; i<(Size); i++) p. E[i]. Value = i*2; cout << "Element 的內容是: n"; Show. Element(p. E); delete [] p. E; return 0; }



「雙向鏈結串列」的宣告 struct Node { int Value; Node* Previous; Node* Next; }; 54






enum資料型態 n 使用enum型態的目的是為了增進程式的可讀性,常 與switch和if等判斷式結合使用。例如: int N; cin >> N; switch (N) { case Up: cout << “Moving Up!n”; break; case Down: cout << “Moving Down!n”; break; case Left: cout << “Moving Left!n”; break; 60

case Right: cout << “Moving Right!n”; break; default: cout << “Staticn”; } 61

範例程式 檔案 Test. Enum. cpp // Test. Enum. cpp #include <iostream> using namespace std; enum Direction {Up, Down, Left, Right}; // ---- 主程式 ------------int main() { int N; cout << "請輸入期望的運動方向n"; cout << "(0=Up, 1=Down, 2=Left, 3=Right): n"; cin >> N; switch (N) { case Up: cout << "Moving Up!n"; break; 62

case Down: cout << "Moving Down!n"; break; case Left: cout << "Moving Left!n"; break; case Right: cout << "Moving Right!n"; break; default: cout << "Staticn"; } return 0; } 63

執行結果 請輸入期望的運動方向 (0=Up, 1=Down, 2=Left, 3=Right): 2 Moving Left! 64