C++ 實驗1-2 學生通訊錄管理系統
阿新 • • 發佈:2019-01-07
一、注意:
Xcode:C++寫入資料的檔案為啥在專案下沒看到呢?
原來預設放在:/Users/username(這裡填入自己的使用者名稱)/Library/Developer/Xcode/DerivedData/專案
名/Build/Products/Debug/student.txt
二、實現程式:
1.student.h
#ifndef student_h #define student_h #include <iostream> #include <fstream> #include <string> using namespace std; struct Node { string id; // 學生的學號 string name; // 姓名 string birthday; // 出生日期:如1995-01-01 int sex; // 1:表示是男生,0:表示是女生 string phone; // 電話 string address; // 地址 Node *next; }; Node *Create(); // 新建同學錄 bool InsertStudent(Node *head); // 向學生通訊錄插入學生資訊 bool DeleteStudent(Node *head, string id); // 在通訊錄中刪除學生資訊 Node *ReadFromFile(); // 從檔案中讀取通訊錄資訊 bool WriteToFile(Node *head); // 向檔案寫入學生通訊錄資訊 bool FindStudent(Node *head, string id); // 在通訊錄中查詢學生資訊(按學號查詢) void PrintStudent(Node *head); // 在螢幕中輸出全部學生資訊 #endif /* student_h */
2.student.cpp
#include "student.h" //string id; // 學生的學號 //string name; // 姓名 //string birthday; // 出生日期:如1995-01-01 //int sex; // 1:表示是男生,0:表示是女生 //string phone; // 電話 //string address; // 地址 // 新建學生通訊錄資訊 // 如:20192101011 ChanJose 2000-01-01 1 15626453333 廣東廣州 Node *Create() { int i = 1; char ch = 'Y'; // 是否繼續輸入學生通訊錄資訊 Node *head = new Node(); // 頭結點,不存放資料 if(NULL == head) { cerr << "記憶體空間分配失敗" << endl; exit(1); } head->next = NULL; while(ch == 'Y') { cout << "請輸入第" << i << "位學生的學號、姓名、出生日期、姓別(1:表示是男生,0:表示是女生)、電話和地址(以空格隔開):" << endl; Node *newNode = new Node(); // 新建結點 if(NULL == newNode) { cerr << "記憶體空間分配失敗" << endl; exit(1); } cin >> newNode->id >> newNode->name >> newNode->birthday >> newNode->sex >> newNode->phone >> newNode->address; newNode->next = NULL; // 採用頭插入 newNode->next = head->next; // 新結點的next儲存首結點(頭結點的下一個結點) head->next = newNode; // 頭指標往前移 i++; cout << "是否繼續輸入學生的通訊錄資訊?(Y/N):"; cin >> ch; } return head; } // 向學生通訊錄插入學生資訊 bool InsertStudent(Node *head) { cout << "請輸入學生的學號、姓名、出生日期、姓別(1:表示是男生,0:表示是女生)、電話和地址(以空格隔開):" << endl; Node *newNode = new Node(); // 新建結點 if(NULL == newNode) { cerr << "記憶體空間分配失敗" << endl; exit(1); } cin >> newNode->id >> newNode->name >> newNode->birthday >> newNode->sex >> newNode->phone >> newNode->address; newNode->next = head->next; // 新結點的next儲存首結點(頭結點的下一個結點) head->next = newNode; // 頭指標往前移 return true; // 插入成功 } // 在通訊錄中刪除學生資訊 bool DeleteStudent(Node *head, string id) { Node *prev = head; Node *p = head->next; while(p != NULL && p->id != id) { // 也可以用strcmp,但不知道為什麼我引入了#include <cstring>還是發現找不到strcmp函式 prev = p; p = p->next; } if(p == NULL) return false; prev->next = p->next; delete p; p = NULL; return true; } // 從檔案中讀取通訊錄資訊 Node *ReadFromFile() { Node *head = new Node(); // 頭結點,不存放資料 if(NULL == head) { cerr << "記憶體空間分配失敗" << endl; exit(1); } ifstream inFile; inFile.open("student.txt", ios::in); // 以讀的方式開啟檔案 if(!inFile.is_open()) { cout << "Error: opening file fail" << endl; exit(1); } Node *newNode = new Node(); // 新建結點 if(NULL == newNode) { cerr << "記憶體空間分配失敗" << endl; exit(1); } while(!inFile.eof()) { // 當未到檔案尾 Node *newNode = new Node(); // 新建結點 if(NULL == newNode) { cerr << "記憶體空間分配失敗" << endl; exit(1); } // 從檔案中輸入資料 inFile >> newNode->id >> newNode->name >> newNode->birthday >> newNode->sex >> newNode->phone >> newNode->address; newNode->next = head->next; // 新結點的next儲存首結點(頭結點的下一個結點) head->next = newNode; // 頭指標往前移 } inFile.close(); // 關閉檔案 return head; } // 向檔案寫入學生通訊錄資訊 bool WriteToFile(Node *head) { Node *p = head->next; // 以寫模式開啟檔案 ofstream outFile; outFile.open("student.txt", ios::out); if(!outFile) { cout << "Error: opening file fail" << endl; exit(1); } while(p != NULL) { // 寫入檔案 outFile << p->id << " " << p->name << " " << p->birthday << " " << p->sex << " " << p->phone << " " << p->address << endl; p = p->next; // 往後移 } return true; } // 在通訊錄中查詢學生資訊(按學號查詢) bool FindStudent(Node *head, string id) { Node *p = head->next; while(p != NULL && p->id != id) { // 也可以用strcmp,但不知道為什麼我引入了#include <cstring>還是發現找不到strcmp函式 p = p->next; } if(p == NULL) return false; return true; } // 在螢幕中輸出全部學生資訊 void PrintStudent(Node *head) { Node *p = head->next; while(p != NULL) { cout << "學號:" << p->id << " 姓名:" << p->name << " 出生日期:" << p->birthday; if(p->sex == 1) cout << " 性別:男"; else cout << " 性別:女"; cout << " 電話:" << p->phone << " 地址:" << p->address << endl; p = p->next; } }
3.main.cpp
// 測試資料: 20192101011 ChanJose 2000-01-01 1 15626453333 廣東廣州 // 20192101012 juan 2000-01-02 0 15626453334 廣東廣州 // 20192101013 feng 2000-01-03 0 15626453335 廣東廣州 #include <iostream> #include "student.h" using namespace std; int main(int argc, const char * argv[]) { int choice; string id; bool finished = false; Node *head = NULL; while(!finished) { cout << "\n*********選單*********\n"; cout << "1:新建學生通訊錄\n"; cout << "2:向學生通訊錄插入學生資訊\n"; cout << "3:在通訊錄刪除學生資訊\n"; cout << "4:從檔案中讀取通訊錄資訊\n"; cout << "5:向檔案寫入通訊錄資訊\n"; cout << "6:在通訊錄中查詢學生資訊(按學號查詢)\n"; cout << "7:在螢幕中輸出全部學生資訊\n"; cout << "8:退出\n"; cout << "請輸入選擇(1-8):\n"; cin >> choice; switch(choice) { case 1: head = Create(); // 新建學生通訊錄 break; case 2: if(InsertStudent(head)) // 向學生通訊錄插入學生資訊 cout << "插入學生資訊成功!" << endl; else cout << "插入學生資訊失敗!" << endl; break; case 3: // 在通訊錄刪除學生資訊 cout << "請輸入要刪除的學生通訊錄資訊的學號:"; cin >> id; if(DeleteStudent(head, id)) cout << "刪除成功!" << endl; else cout << "刪除失敗!" << endl; break; case 4: // 從檔案中讀取通訊錄資訊 head = ReadFromFile(); break; case 5: // 向檔案寫入通訊錄資訊 WriteToFile(head); break; case 6: // 在通訊錄中查詢學生資訊(按學號查詢) cout << "請輸入要查詢的學生資訊的學號:"; cin >> id; if(FindStudent(head, id)) cout << "查詢成功!" << endl; else cout << "查詢失敗!" << endl; break; case 7: // 在螢幕中輸出全部學生資訊 PrintStudent(head); break; case 8: finished = true; break; default: cout << "輸入選擇錯誤,請重新輸入!" << endl; } } return 0; }