C++實(shí)現(xiàn)宿舍管理查詢系統(tǒng)
更新時間:2022年03月16日 16:13:27 作者:DialogD
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)宿舍管理查詢系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了C++實(shí)現(xiàn)宿舍管理查詢系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
C++使用IO流關(guān)聯(lián).txt文件
各模塊之間的調(diào)用關(guān)系如下:
函數(shù)的調(diào)用關(guān)系反映了演示程序的層次結(jié)構(gòu):
代碼如下:
#include<iostream> #include<fstream> #include<iomanip> #include<cstdlib> #include<string> using namespace std; #define MAXSIZE 100 ? ? //順序表的最大長度 typedef struct { ? ? string name; ? ? ? ?//姓名 ? ? string id; ? ? ? ? ?//學(xué)號 ? ? string dormid; ? ? ?//宿舍號 }Student; typedef struct { ? ? Student r[MAXSIZE + 1]; ? ? //r[0]做單元哨兵 ? ? int length;//長度 }SqList; //用直接插入排序存入到student.txt文件中 void InsertSort(SqList &stu) { ? ? int i, j; ? ? for (i = 2; i <= stu.length; i++) ? ? ? ? if (stu.r[i].id < stu.r[i - 1].id) ? ? ? ? { ? ? ? ? ? ? stu.r[0] = stu.r[i]; ? ? ? ? ? ? stu.r[i] = stu.r[i - 1]; ? ? ? ? ? ? for (j = i - 2; stu.r[0].id < stu.r[j].id; j--) ? ? ? ? ? ? ? ? stu.r[j + 1] = stu.r[j]; ? ? ? ? ? ? stu.r[j + 1] = stu.r[0]; ? ? ? ? } ? ? ofstream outfile("student.txt", ios::out); ? ? if (!outfile) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? //如果文件打開失敗 ? ? ? ? cout << "文件打開失敗" << endl; ? ? ? ? exit(1); ? ? } ? ? //outfile << "學(xué)號" << setw(8) << "姓名" << setw(8) << "宿舍號" << endl; ? ? outfile << stu.length << endl; ? ? for (i = 1; i <= stu.length; i++) { ? ? ? ? outfile << stu.r[i].id << setw(8) << stu.r[i].name << setw(8) << stu.r[i].dormid << endl; ? ? } ? ? cout << "學(xué)生信息數(shù):" << stu.length << endl; ? ? outfile.close(); ? ? cout << stu.length; } //創(chuàng)建學(xué)生信息(只能創(chuàng)建一次,不然會被刷新) void InitList(SqList &stu) { ? ? int i; ? ? cout << "學(xué)號" << setw(8) << "姓名" << setw(8) << "宿舍號" << endl; ? ? for (i = 1; i <= stu.length; i++) { ? ? ? ? cin >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid; ? ? } ? ? InsertSort(stu); } //增加學(xué)生信息 void Addstudent(SqList &stu) { ? ? int n; ? ? int i = stu.length + 1; ? ? cout << "輸入增加學(xué)生人數(shù)" << endl; ? ? cin >> n; ? ? cout << "學(xué)號" << setw(8) << "姓名" << setw(8) << "宿舍號" << endl; ? ? stu.length = stu.length + n; ? ? for (i; i <= stu.length; i++) ? ? ? ? cin >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid; ? ? InsertSort(stu); } //查詢學(xué)生信息 void Findstudent(SqList &stu) { ? ? string a, b, c; ? ? string name, id, dormid; ? ? cout << "1.學(xué)號查詢 ?2.姓名查詢 ?3.宿舍號查詢" << endl; ? ? cout << "請輸入你的查詢選擇(1~3)" << endl; ? ? int i; ? ? int n; ? ? cin >> n; ? ? if (n < 1 && n>3) ? ? { ? ? ? ? cout << "您輸入有誤,請重新輸入:" << endl; ? ? ? ? Findstudent(stu); ? ? } ? ? if (1 == n) ? ? { ? ? ? ? cout << "請輸入學(xué)生學(xué)號:" << endl; ? ? ? ? cin >> id; ? ? ? ? ifstream infile("student.txt", ios::in);//定義輸入文件流對象,以輸入方式打開磁盤文件"student.txt" ? ? ? ? infile >> stu.length; ? ? ? ? for (i = 1; i <= stu.length; i++) { ? ? ? ? ? ? infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid; ? ? ? ? } ? ? ? ? infile.close(); ? ? ? ? for (i = 1; i <= stu.length; i++) ? ? ? ? { ? ? ? ? ? ? infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid; ? ? ? ? ? ? if (stu.r[i].id == id) ? ? ? ? ? ? ? ? cout << stu.r[i].id << setw(8) << stu.r[i].name << setw(8) << stu.r[i].dormid << endl; ? ? ? ? } ? ? ? ? infile.close();//關(guān)閉磁盤文件 ? ? } ? ? if (2 == n) ? ? { ? ? ? ? cout << "請輸入學(xué)生姓名:" << endl; ? ? ? ? cin >> name; ? ? ? ? ifstream infile("student.txt", ios::in);//定義輸入文件流對象,以輸入方式打開磁盤文件"student.txt" ? ? ? ? infile >> stu.length; ? ? ? ? for (i = 1; i <= stu.length; i++) { ? ? ? ? ? ? infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid; ? ? ? ? } ? ? ? ? infile.close(); ? ? ? ? for (i = 1; i <= stu.length; i++) ? ? ? ? { ? ? ? ? ? ? infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid; ? ? ? ? ? ? if (stu.r[i].name == name) ? ? ? ? ? ? ? ? cout << stu.r[i].id << setw(8) << stu.r[i].name << setw(8) << stu.r[i].dormid << endl; ? ? ? ? } ? ? ? ? infile.close();//關(guān)閉磁盤文件 ? ? } ? ? if (3 == n) ? ? { ? ? ? ? cout << "請輸入學(xué)生宿舍號:" << endl; ? ? ? ? cin >> dormid; ? ? ? ? ifstream infile("student.txt", ios::in);//定義輸入文件流對象,以輸入方式打開磁盤文件"student.txt" ? ? ? ? infile >> stu.length; ? ? ? ? for (i = 1; i <= stu.length; i++) { ? ? ? ? ? ? infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid; ? ? ? ? } ? ? ? ? infile.close(); ? ? ? ? for (i = 1; i <= stu.length; i++) ? ? ? ? { ? ? ? ? ? ? infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid; ? ? ? ? ? ? if (stu.r[i].dormid == dormid) ? ? ? ? ? ? ? ? cout << stu.r[i].id << setw(8) << stu.r[i].name << setw(8) << stu.r[i].dormid << endl; ? ? ? ? } ? ? } } //修改學(xué)生信息 void Renewstudent(SqList &stu) { ? ? int n; ? ? string id, name, dormid; ? ? cout << "1.姓名 ?2.宿舍號" << endl; ? ? cout << "請輸入您的選擇(1~2):" << endl; ? ? cin >> n; ? ? cout << "請輸入需要修改學(xué)生的學(xué)號" << endl; ? ? cin >> id; ? ? if (n != 1 && n != 2) ? ? { ? ? ? ? cout << "輸入有誤,請重新輸入:" << endl; ? ? ? ? Renewstudent(stu); ? ? } ? ? if (1 == n) ? ? { ? ? ? ? cout << "請輸入修改姓名" << endl; ? ? ? ? cin >> name; ? ? ? ? int i = 0; ? ? ? ? ifstream infile("student.txt", ios::in); ? ? ? ? infile >> stu.length; ? ? ? ? for (i = 1; i <= stu.length; i++) { ? ? ? ? ? ? infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid; ? ? ? ? } ? ? ? ? infile.close(); ? ? ? ? for (i = 1; i <= stu.length; i++)//先找到再修改 ? ? ? ? { ? ? ? ? ? ? if (stu.r[i].id == id) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? stu.r[i].name = name; ? ? ? ? ? ? ? ? InsertSort(stu); ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? if (2 == n) ? ? { ? ? ? ? int i; ? ? ? ? cout << "請輸入修改宿舍號" << endl; ? ? ? ? cin >> dormid; ? ? ? ? ifstream infile("student.txt", ios::in); ? ? ? ? infile >> stu.length; ? ? ? ? for (i = 1; i <= stu.length; i++) { ? ? ? ? ? ? infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid; ? ? ? ? } ? ? ? ? infile.close(); ? ? ? ? for (i = 1; i <= stu.length; i++)//先找到再修改 ? ? ? ? { ? ? ? ? ? ? if (stu.r[i].id == id) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? stu.r[i].dormid = dormid; ? ? ? ? ? ? ? ? InsertSort(stu); ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? } ? ? ? ? } ? ? } } //顯示宿舍信息 void Showstudent(SqList &stu) { ? ? string a, b, c; ? ? int i; ? ? cout << "學(xué)生的信息如下:" << endl; ? ? cout << "**********************************" << endl; ? ? ifstream infile("student.txt", ios::in); ? ? if (!infile) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//如果文件打開失敗 ? ? ? ? cout << "文件打開失敗" << endl; ? ? ? ? exit(1); ? ? } ? ? /*infile >> a >> b >> c;//從磁盤文件讀入 ? ? cout << a << setw(8) << b << setw(8) << c << endl;//在顯示器上顯示*/ ? ? infile >> stu.length; ? ? for (i = 1; i <= stu.length; i++) { ? ? ? ? infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid; ? ? ? ? cout << stu.r[i].id << setw(8) << stu.r[i].name << setw(8) << stu.r[i].dormid << endl; ? ? } ? ? infile.close(); } //刪除宿舍信息 void Deletstudent(SqList &stu) { ? ? int ?i, j; ? ? string a, b, c, id; ? ? cout << "請輸入刪除學(xué)生學(xué)號" << endl; ? ? cin >> id; ? ? ifstream infile("student.txt", ios::in);//定義輸入文件流對象,以輸入方式打開磁盤文件"student.txt" ? ? infile >> stu.length; ? ? for (i = 1; i <= stu.length; i++) { ? ? ? ? infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid; ? ? } ? ? infile.close(); ? ? for (i = 1; i <= stu.length; i++)//先找到再刪除 ? ? { ? ? ? ? if (stu.r[i].id == id) ? ? ? ? { ? ? ? ? ? ? for (j = i; j<stu.length; j++) ? ? ? ? ? ? ? ? stu.r[j] = stu.r[j + 1]; ? ? ? ? ? ? stu.length--; ? ? ? ? ? ? InsertSort(stu); ? ? ? ? ? ? return; ? ? ? ? } ? ? } } //主函數(shù) int main() { ? ? SqList stu; ? ? int n; ? ? for (;;) ? ? { ? ? ? ? cout << "**************************宿舍管理查詢軟件**************************" << endl; ? ? ? ? cout << "1. 創(chuàng)建學(xué)生信息" << endl; ? ? ? ? ? ? ? ? ? ? ? ?//InitList ? ? ? ? cout << "2. 增加學(xué)生信息" << endl; ? ? ? ? ? ? ? ? ? ? ? ?//Addstudent ? ? ? ? ? ? cout << "3. 查詢學(xué)生信息" << endl; ? ? ? ? ? ? ? ? ? ? ? ?//Findstudent ? ? ? ? cout << "4. 顯示學(xué)生信息" << endl; ? ? ? ? ? ? ? ? ? ? ? ?//Showstudent ? ? ? ? cout << "5. 修改學(xué)生信息" << endl; ? ? ? ? ? ? ? ? ? ? ? ?//Renewstudent ? ? ? ? cout << "6. 刪除學(xué)生信息" << endl; ? ? ? ? ? ? ? ? ? ? ? ?//Deletstudent ? ? ? ? cout << "0. 退出系統(tǒng)" << endl; ? ? ? ? cout << "*******************************************************************" << endl; ? ? ? ? cout << "請輸入你需要的操作(0~6):" << endl; ? ? ? ? cin >> n; ? ? ? ? switch (n) ? ? ? ? { ? ? ? ? case 1: ? ? ? ? ? ? cout << "輸入學(xué)生人數(shù)" << endl; ? ? ? ? ? ? cin >> stu.length; ? ? ? ? ? ? InitList(stu); ? ? ? ? ? ? cout << "###########################################" << endl; ? ? ? ? ? ? break; ? ? ? ? case 2: ? ? ? ? ? ? Addstudent(stu); ? ? ? ? ? ? cout << "###########################################" << endl; ? ? ? ? ? ? break; ? ? ? ? case 3: ? ? ? ? ? ? Findstudent(stu); ? ? ? ? ? ? cout << "###########################################" << endl; ? ? ? ? ? ? break; ? ? ? ? case 4: ? ? ? ? ? ? Showstudent(stu); ? ? ? ? ? ? cout << "###########################################" << endl; ? ? ? ? ? ? break; ? ? ? ? case 5: ? ? ? ? ? ? Renewstudent(stu); ? ? ? ? ? ? cout << "###########################################" << endl; ? ? ? ? ? ? break; ? ? ? ? case 6: ? ? ? ? ? ? Deletstudent(stu); ? ? ? ? ? ? cout << "###########################################" << endl; ? ? ? ? ? ? break; ? ? ? ? case 0: ? ? ? ? ? ? cout << "您已退出系統(tǒng)!" << endl; ? ? ? ? ? ? return 0; ? ? ? ? } ? ? } ? ? system("pause"); ? ? return 0; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
C語言動態(tài)數(shù)組的使用實(shí)現(xiàn)代碼
這篇文章主要介紹了C語言動態(tài)數(shù)組的使用實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-01-01windows下安裝QT及visual studio 2017搭建開發(fā)環(huán)境
這篇文章主要介紹了windows下安裝QT及visual studio 2017搭建開發(fā)環(huán)境,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03