Test Struct cpp Test Struct cpp include iostream

  • Slides: 64
Download presentation

範例程式 檔案 Test. Struct. cpp // Test. Struct. cpp #include <iostream> using namespace std; struct

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

int main() { Employee. Ea= {"Ann", "02384125", 105}; Employee Eb = {"Joanne", "03544132", 106};

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; system("PAUSE"); return 0; } 11/64

struct陣列各欄位的資料 cout << Officer[8]. Name << endl; cout << Officer[12]. Phone << endl; cout

struct陣列各欄位的資料 cout << Officer[8]. Name << endl; cout << Officer[12]. Phone << endl; cout << Officer[40]. Id << endl; 16/64

範例程式 檔案 Struct. Array. cpp // Struct. Array. cpp #include <iostream> using namespace std; const

範例程式 檔案 Struct. Array. cpp // Struct. Array. cpp #include <iostream> using namespace std; 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"; 18/64

for (int i=0; i<Size; i++) { cout << "請輸入 Officer[" << I << "] 的姓名:

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'; } system("PAUSE"); return 0; } 19/64

程式執行結果 共 2個Officers: 請輸入Officer[0] 的姓名: Alan John 電話號碼: 03 -4521234 請輸入 Officer[1] 的姓名: Peter

程式執行結果 共 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/64

使用傳參照 (pass by reference) 改變 struct實例的內容 n 例如: Change. Name(Ea, “Jackson”); n 對應的「被呼叫函數」則定義成: void

使用傳參照 (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/64

範例程式 檔案 Struct. Fnc. cpp // Struct. Fnc. cpp #include <iostream> #include <cstring> using namespace

範例程式 檔案 Struct. Fnc. cpp // Struct. Fnc. cpp #include <iostream> #include <cstring> using namespace std; struct Employee { char Name[20]; char Phone[10]; int Id; }; void Show. Member(Employee A) { cout << "資料的詳細內容是:n" << "姓名 : " << A. Name << 'n' << "電話號碼: " << A. Phone << 'n' << "編號 : " << A. Id << endl; return; 24/64 }

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; } // ======= 主程式 ============ 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); system("PAUSE"); return 0; } 25/64

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

使用指標改變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/64

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

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

範例程式 檔案 Struct. Fnc 2. cpp // Struct. Fnc 2. cpp #include <iostream> #include <cstring>

範例程式 檔案 Struct. Fnc 2. cpp // Struct. Fnc 2. cpp #include <iostream> #include <cstring> 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 31/64 } << 'n' << endl; return;

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; } 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); system("PAUSE"); return 0; } 32/64

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

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

範例程式 檔案 Dyn. Struct. cpp // Dyn. Struct. cpp #include <iostream> using namespace std; struct

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

int main() { int Size; cout << "請輸入 Employee 的數目:n"; cin >> Size; Employee*

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

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

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

動態產生任意數目的Element (各實 例的值在此為 0, 2, 4, 6, …): cout << "請輸入 Element 的數目:n"; cin

動態產生任意數目的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/64

顯示現有串列元素 Element* p. Show; for (p. Show = p. E; p. Show != NULL;

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

以while迴圈顯示現有串列元素 Element* p. Show=p. E; while (p. Show != NULL) { cout << p.

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

將顯示串列內容的功能封裝到函數中 void Show. Element(Element* p. Show) { while (p. Show != NULL) { cout

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

範例程式 檔案 List. Struct. cpp // List. Struct. cpp #include <iostream> using namespace std; struct

範例程式 檔案 List. Struct. cpp // 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; } } 50/64

// ---主程式------------int main() { int Size; cout << "請輸入 Element 的數目: n"; cin >>

// ---主程式------------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; system("PAUSE"); return 0; } 51/64

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

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

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

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/64

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

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

範例程式 檔案 Test. Enum. cpp // Test. Enum. cpp #include <iostream> using namespace std; enum

範例程式 檔案 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/64

case Down: cout << "Moving Down!n"; break; case Left: cout << "Moving Left!n"; break;

case Down: cout << "Moving Down!n"; break; case Left: cout << "Moving Left!n"; break; case Right: cout << "Moving Right!n"; break; default: cout << "Staticn"; } system("PAUSE"); return 0; } 63/64

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

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