問題描述及要求:
定義一個 TeacherList 類
可以實作簡單的增加教師
可以實作簡單的查找教師
可以實作簡單的洗掉教師
要求用二進制檔案的讀寫操作
單#include<iostream>#include<cstring>#include<fstream>using namespace std;class employee{public: employee() { } employee(charna[50], char sex[50], int num, char tel[20], char off[50], char pos[50]):m_number(num) { strcpy(m_name,na); strcpy(m_sex,sex); strcpy(m_telephone,tel); strcpy(m_office,off); strcpy(m_posting,pos); } intgetnum() { returnm_number; } char*getna() // { returnm_name; } char*getoff() { returnm_office; } char*getposting() // { returnm_posting; } friendostream& operator <<(ostream &os, const employee & s) { os<< "姓名:" << s.m_name << " 性別:" << s.m_sex << " 工號:" << s.m_number << " 電話:" << s.m_telephone << " 所在系部:" << s.m_office << " 職稱:" << s.m_posting << endl; returnos; } friendistream& operator >>(istream &is, employee &s) { is>> s.m_name >> s.m_sex >> s.m_number >> s.m_telephone>> s.m_office >> s.m_posting; returnis; } ~employee() { }private: charm_name[50]; charm_sex[50]; intm_number; charm_telephone[20]; charm_office[50]; charm_posting[50];}; voidmenu() //1)選單功能選擇 { cout<< "************************************* 教師資訊管理系統 ***************************************" << endl; cout<< "**----------------------------------------------------------------------------------------------**"<< endl; cout<< "** ----------------------------- **"<< endl; cout<< "** | * 1.錄入人員資訊并保存 | **"<< endl; cout<< "** | * 2.按工號查找人員 | **"<< endl; cout<< "** | * 3.按工號修改人員資訊 | **"<< endl; cout<< "** | * 4.按工號洗掉人員資訊 | **"<< endl; cout<< "** | * 5.查看所有人員資訊 | **" << endl; cout<< "** | * 0.關閉系統 | **"<< endl; cout<< "** ----------------------------- **" <<endl; cout<< "**----------------------------------------------------------------------------------------------**"<< endl; cout<< "**請輸入序號使用對應的功能:(0,1,2,3,4,5) **" << endl; cout<< "**************************************************************************************************"<< endl; }void input() //2)輸入保存 { fstream fs; int len; cout << "請輸入教師人數:" << endl; cin >> len; employee *emp = new employee[len]; //開辟空間,存教職工資料 fs.open("guanli.dat",ios::out | ios::app | ios::binary); if (!fs) cout << "Openfailed." << endl; else { cout << "Opensucceedly." << endl; cout << "請輸入教師的姓名,性別,工號,電話,所在系部,職稱:" <<endl; for (int i = 0; i<len; i++) { cin>> emp[i]; fs.write((char *)&emp[i], sizeof(emp[i])); //fs<<emp[i]<<endl;二進制 } } fs.close(); delete[] emp; }void find1() //3)查詢---能夠根據工號精確查詢職工資訊; { int num; cout << "請輸入教師的工號:" << endl; cin>> num; fstream fs; fs.open("guanli.dat", ios::in| ios::binary); fs.seekg(0, ios::end); //檔案調到末尾 int s = fs.tellg(); //告訴檔案大小 int n = s / sizeof(employee); //計算職工人數 fs.seekg(ios::beg); //檔案指標調到檔案開頭 employee*emp = new employee[n]; for (int i = 0; i<n; i++) fs.read((char *)& emp[i],sizeof(emp[i])); //讀入到記憶體 fs.close(); int a = -100; for (int i = 0; i<n; i++) { if (num == emp[i].getnum()) { cout << emp[i]; a = i; } } if (a == -100) cout << "工號不正確!無此人!" << endl; delete[] emp; }void modify() //5)根據工號修改職工資訊{ fstreamfs; fs.open("guanli.dat",ios::in | ios::out | ios::binary); fs.seekg(0,ios::end); ints = fs.tellg(); intn = s / sizeof(employee); fs.seekg(ios::beg); employee*emp = new employee[n]; for(int i = 0; i<n; i++) fs.read((char*)&emp[i], sizeof(emp[i])); intnum; cout<< "請輸入所修改的職工號:" << endl; cin>> num; inta=-100; for(int i = 0; i < n; i++) { if(num == emp[i].getnum()) { fs.seekp(sizeof(employee)*i); employeee; cout<< "請輸入要修改的教師的姓名,性別,工號,電話,所屬系部,職稱:" << endl; cin>> e; fs.write((char*)&e, sizeof(employee)); cout<< "教師資訊修改成功!" << endl; a= i; } } if(a==-100) cout<< "工號不正確!無此人!" << endl; fs.close(); delete[]emp; }void del() // 6)根據工號洗掉職工資訊{ fstreamfs; fs.open("guanli.dat",ios::in | ios::out | ios::binary); fs.seekg(0,ios::end); ints = fs.tellg(); intn = s / sizeof(employee); fs.seekg(ios::beg); employee*emp = new employee[n]; for(int i = 0; i<n; i++) { fs.read((char*)&emp[i], sizeof(emp[i])); } fs.close(); intnum; cout<< "請輸入要洗掉的教師號:" << endl; cin>> num; inta=-100; for( int i = 0; i<n; i++) { if(num == emp[i].getnum()) a= i; } if(a==-100) cout<< "工號不正確!無此人!" << endl; fs.open("guanli.dat",ios::out | ios::binary); if(!fs) cout<< "Open failed." << endl; else { cout<< "Open succeedly." << endl; for(int i = 0; i<n; i++) { if(i == a) continue; else fs.write((char*)& emp[i], sizeof(emp[i])); } } if(a >= 0 && a <= n) cout<< "洗掉成功!" << endl; else cout<< "洗掉失敗!" << endl; fs.close(); fs.clear(); delete[]emp;}void show() //8.查看所有人員資訊 { fstream fs; fs.open("guanli.dat", ios::in| ios::binary); fs.seekg(0, ios::end); int s = fs.tellg(); int n = s / sizeof(employee); fs.seekg(ios::beg); employee *emp = new employee[n]; for (int i = 0; i<n; i++) { fs.read((char *)&emp[i],sizeof(emp[i])); cout << emp[i]; } fs.close(); delete[] emp; }int main() { char flag = 'n'; while (flag == 'n' || flag == 'N') //由y/n控制回圈 { menu(); int judge; cin >> judge; if (judge >= 0 &&judge <= 8) { switch (judge) { case 0: cout <<"是否退出系統(y/n):" << endl; cin >>flag; break; case 1: input(); break; case 2: find1(); break; case 3: modify(); break; case 4: del(); break; case 5: show(); break; default: break; } } else cout << "輸入錯誤,請重新輸入!" << endl; cout <<"------------------------------------------Press any key tocontinue!------------------------------" << endl; getchar(); getchar(); system("cls"); } return 0;}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/83680.html
標籤:新技術前沿
上一篇:以物件作為回傳值的方法
下一篇:C語言
