C++實現(xiàn)教職工信息管理系統(tǒng)課程設計
更新時間:2022年03月18日 11:04:41 作者:甜甜甜欣
這篇文章主要為大家詳細介紹了C++實現(xiàn)教職工信息管理系統(tǒng)課程設計,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C++實現(xiàn)教職工信息管理系統(tǒng)的具體代碼,供大家參考,具體內容如下
/*教職工信息管理 基本要求: 定義職工(employee )類,其中至少包括姓名、性別、工號、電話、所在系部和職稱。 功能要求: ? ? ? ?1、設計菜單實現(xiàn)功能選擇; ?? ? ? ? ?2、輸入功能:輸入職工信息,并保存到文件中; ? ? ? ?3、查詢功能: ? ? ? ? ? ?1)能夠根據(jù)工號精確查詢職工信息; ? ? ? ? ? ?2)能夠根據(jù)姓名、科室查詢職工信息 ?? ? ? ? ? 3)分系部進行職稱統(tǒng)計,計算各職稱的人數(shù) ? ? ? ?4、根據(jù)職工的職稱排序輸出 ? ? ? ?5、根據(jù)工號修改職工信息 ? ? ? ?6、根據(jù)工號刪除職工信息*/ #include<iostream> #include<cstring> #include<fstream> using namespace std; class employee { public: ?? ?employee() ?? ?{ ?? ?} ?? ?employee(char na[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); ?? ?} ?? ?int getnum() ?? ?{ ?? ??? ?return m_number; ?? ?} ?? ?char *getna() ?// ?? ?{ ?? ??? ?return m_name; ?? ?} ?? ?char *getoff() ?? ?{ ?? ??? ?return m_office; ?? ?} ?? ?char *getposting() ?// ?? ?{ ?? ??? ?return m_posting; ?? ?} ?? ?friend ostream& 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; ?? ??? ?return os; ?? ?} ?? ?friend istream& operator >>(istream &is, employee &s) ?? ?{ ?? ??? ?is >> s.m_name >> s.m_sex >> s.m_number >> s.m_telephone >> s.m_office >> s.m_posting; ?? ??? ?return is; ?? ?} ?? ?~employee() ?? ?{ ?? ?} private: ?? ?char m_name[50]; ?? ?char m_sex[50]; ?? ?int m_number; ?? ?char m_telephone[20]; ?? ?char m_office[50]; ?? ?char m_posting[50]; };
菜單功能選擇:
void menu() ? ? ? //1)菜單功能選擇 { ?? ?cout << "************************************* ? 教職工信息管理系統(tǒng) ? *************************************" << endl; ?? ?cout << "**----------------------------------------------------------------------------------------------**" << endl; ?? ?cout << "** ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ----------------------------- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?**" << endl; ?? ?cout << "** ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | * 1.錄入人員信息并保存 ? ?| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?**" << endl; ?? ?cout << "** ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | * 2.按工號查找人員 ? ? ? ?| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?**" << endl; ?? ?cout << "** ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | * 3.按姓名+科室查找人員 ? | ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?**" << endl; ?? ?cout << "** ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | * 4.分系部職稱人數(shù)統(tǒng)計 ? ?| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?**" << endl; ?? ?cout << "** ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | * 5.按職稱對人員排序 ? ? ?| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?**" << endl; ?? ?cout << "** ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | * 6.按工號修改人員信息 ? ?| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?**" << endl; ?? ?cout << "** ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | * 7.按工號刪除人員信息 ? ?| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?**" << endl; ?? ?cout << "** ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | * 8.查看所有人員信息 ? ? ?| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?**" << endl; ?? ?cout << "** ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | * 0.關閉系統(tǒng) ? ? ? ? ? ? ?| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?**" << endl; ?? ?cout << "** ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ----------------------------- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?**" << endl; ?? ?cout << "**----------------------------------------------------------------------------------------------**" << endl; ?? ?cout << "**請輸入序號使用對應的功能:(0,1,2,3,4,5,6,7,8) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?**" << endl; ?? ?cout << "**************************************************************************************************" << endl; }
輸入信息:
void input() ? ? ?//2)輸入保存 ? ? { ? ? ?? ?fstream fs; ? ? ?? ?int len; ? ? ?? ?cout << "請輸入教職工人數(shù):" << endl; ? ? ?? ?cin >> len; ? ? ?? ?employee *emp = new employee[len]; ? ? ?//開辟空間,存教職工數(shù)據(jù) ? ? ?? ?fs.open("guanli.dat", ios::out | ios::app | ios::binary); ? ? ?? ?if (!fs) ? ? ?? ??? ?cout << "Open failed." << endl; ? ? ?? ?else ? ? ?? ?{ ? ? ?? ??? ?cout << "Open succeedly." << 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; ? ? }
查詢功能(根據(jù)工號):
void find1() ? ? ? //3-1)查詢---能夠根據(jù)工號精確查詢職工信息; ? ? { ? ? ?? ?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); ? ? ?//計算職工人數(shù) ? ? ?? ?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 (i = 0; i<n; i++) ? ? ?? ?{ ? ? ?? ??? ?if (num == emp[i].getnum()) ? ? ?? ??? ?{ ? ? ?? ??? ??? ?cout << emp[i]; ? ? ?? ??? ??? ?a = i; ? ? ?? ??? ?} ? ? ?? ?} ? ? ?? ?if (a == -100) ? ? ?? ??? ?cout << "工號不正確!無此人!" << endl; ? ? ?? ?delete[] emp; ? ? }
查詢功能(根據(jù)姓名、科室):
void find2() ? ? ? //3-2)查詢---能夠根據(jù)姓名、科室查詢職工信息 { ?? ?char na[50]; ?? ?char off[50]; ?? ?cout << "請輸入教職工的姓名:" << endl; ?? ?cin >> na; ?? ?cout << "請輸入教職工所屬科室:" << endl; ?? ?cin >> off; ?? ?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 (i = 0; i<n; i++) ?? ?{ ?? ??? ?if (strcmp(na, emp[i].getna())==0 && strcmp(off, emp[i].getoff())==0) ?? ??? ?{ ?? ??? ??? ?cout << emp[i] << endl; ?? ??? ??? ?a = i; ?? ??? ?} ?? ?} ?? ?if (a == -100) ?? ??? ?cout << "名字或所屬科室不正確!無此人!" << endl; }
統(tǒng)計人數(shù):
void find3() ? ? ? //3-3)分系部進行職稱統(tǒng)計,計算各職稱的人數(shù) ? ? { ? ? ?? ?char off[50], posting[50]; ? ? ?? ?cout << "請輸入所查系部:" << endl; ? ? ?? ?cin >> off; ? ? ?? ?cout << "請輸入所查職稱:" << endl; ? ? ?? ?cin >> posting; ? ? ?? ?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 sum = 0; ? ? ?? ?for (i=0; i<n; i++) ? ? ?? ?{ ? ? ?? ??? ?if (strcmp(off, emp[i].getoff()) == 0 && strcmp(posting, emp[i].getposting()) == 0) ? ? ?? ??? ??? ?sum++; ? ? ?? ?} ? ? ?? ?cout << "該部門此職稱有" << sum << "人!" << endl; ? ? ?? ?delete[] emp; ? ? }
排序輸出:
void output() ? ? ?//4)根據(jù)職工的職稱排序輸出 ? ? { ? ? ?? ?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(); ? ? ?? ?employee temp; ? ? ?? ?for (int j = 0; j<n - 1; j++) ? ? ?? ?{ ? ? ?? ??? ?for (int k = 0; k<n - 1 - j; k++) ? ? ?? ??? ?{ ? ? ?? ??? ??? ?if (strcmp(emp[k].getposting(), emp[k + 1].getposting())>0) ? ? ?? ??? ??? ?{ ? ? ?? ??? ??? ??? ?temp = emp[k]; ? ? ?? ??? ??? ??? ?emp[k] = emp[k + 1]; ? ? ?? ??? ??? ??? ?emp[k + 1] = temp; ? ? ?? ??? ??? ?} ? ? ?? ??? ?} ? ? ?? ?} ? ? ?? ?for (i = 0; i<n; i++) ? ? ?? ??? ?cout << emp[i]; ? ? ?? ?delete[] emp; ? ? }
修改功能:
void modify() ? ? ?//5)根據(jù)工號修改職工信息 { ?? ?fstream fs; ?? ?fs.open("guanli.dat", ios::in | ios::out | 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])); ?? ?int num; ?? ?cout << "請輸入所修改的職工號:" << endl; ?? ?cin >> num; ?? ?int a=-100; ?? ?for (i = 0; i < n; i++) ?? ?{ ?? ??? ?if (num == emp[i].getnum()) ?? ??? ?{ ?? ??? ??? ?fs.seekp(sizeof(employee)*i); ?? ??? ??? ?employee e; ?? ??? ??? ?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)根據(jù)工號刪除職工信息 { ?? ?fstream fs; ?? ?fs.open("guanli.dat", ios::in | ios::out | 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 num; ?? ?cout << "請輸入要刪除的職工號:" << endl; ?? ?cin >> num; ?? ?int a=-100; ?? ?for ( 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; ? ? }
主函數(shù):
?int main() ? ? { ? ? ?? ?char flag = 'n'; ? ? ?? ?while (flag == 'n' || flag == 'N') ? ?//由y/n控制循環(huán) ? ? ?? ?{ ? ? ?? ??? ?menu(); ? ? ?? ??? ?int judge; ? ? ?? ??? ?cin >> judge; ? ? ?? ??? ?if (judge >= 0 && judge <= 8) ? ? ?? ??? ?{ ? ? ?? ??? ??? ?switch (judge) ? ? ?? ??? ??? ?{ ? ? ?? ??? ??? ?case 0: ? ? ?? ??? ??? ??? ?cout << "是否退出系統(tǒng)(y/n):" << endl; ? ? ?? ??? ??? ??? ?cin >> flag; ? ? ?? ??? ??? ??? ?break; ? ? ?? ??? ??? ?case 1: ? ? ?? ??? ??? ??? ?input(); ? ? ?? ??? ??? ??? ?break; ? ? ?? ??? ??? ?case 2: ? ? ?? ??? ??? ??? ?find1(); ? ? ?? ??? ??? ??? ?break; ? ? ?? ??? ??? ?case 3: ? ? ?? ??? ??? ??? ?find2(); ? ? ?? ??? ??? ??? ?break; ? ? ?? ??? ??? ?case 4: ? ? ?? ??? ??? ??? ?find3(); ? ? ?? ??? ??? ??? ?break; ? ? ?? ??? ??? ?case 5: ? ? ?? ??? ??? ??? ?output(); ? ? ?? ??? ??? ??? ?break; ? ? ?? ??? ??? ?case 6: ? ? ?? ??? ??? ??? ?modify(); ? ? ?? ??? ??? ??? ?break; ? ? ?? ??? ??? ?case 7: ? ? ?? ??? ??? ??? ?del(); ? ? ?? ??? ??? ??? ?break; ? ? ?? ??? ??? ?case 8: ? ? ?? ??? ??? ??? ?show(); ? ? ?? ??? ??? ??? ?break; ? ? ?? ??? ??? ?default: ? ? ?? ??? ??? ??? ?break; ? ? ?? ??? ??? ?} ? ? ?? ??? ?} ? ? ?? ??? ?else ? ? ?? ??? ??? ?cout << "輸入錯誤,請重新輸入!" << endl; ? ? ?? ??? ?cout << "------------------------------------------Press any key to continue!------------------------------" << endl; ? ? ?? ??? ?getchar(); ? ? ?? ??? ?getchar(); ? ? ?? ??? ?system("cls"); ? ? ?? ?} ? ? ?? ?return 0; ? ? }
運行主界面:(其余的圖就不圖啦)
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
深入C++中構造函數(shù)、拷貝構造函數(shù)、賦值操作符、析構函數(shù)的調用過程總結
本篇文章是對C++中構造函數(shù)、拷貝構造函數(shù)、賦值操作符、析構函數(shù)的調用過程進行了總結與分析,需要的朋友參考下2013-05-05c++?創(chuàng)建型設計模式工廠方法Factory?Method示例詳解
這篇文章主要為大家介紹了c++?創(chuàng)建型設計模式工廠方法Factory?Method示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09