C++实现宿舍管理查询系统
作者:DialogD
这篇文章主要为大家详细介绍了C++实现宿舍管理查询系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了C++实现宿舍管理查询系统的具体代码,供大家参考,具体内容如下
C++使用IO流关联.txt文件
各模块之间的调用关系如下:
函数的调用关系反映了演示程序的层次结构:
代码如下:
#include<iostream> #include<fstream> #include<iomanip> #include<cstdlib> #include<string> using namespace std; #define MAXSIZE 100 //顺序表的最大长度 typedef struct { string name; //姓名 string id; //学号 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 << "学号" << 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 << "学生信息数:" << stu.length << endl; outfile.close(); cout << stu.length; } //创建学生信息(只能创建一次,不然会被刷新) void InitList(SqList &stu) { int i; cout << "学号" << 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); } //增加学生信息 void Addstudent(SqList &stu) { int n; int i = stu.length + 1; cout << "输入增加学生人数" << endl; cin >> n; cout << "学号" << 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); } //查询学生信息 void Findstudent(SqList &stu) { string a, b, c; string name, id, dormid; cout << "1.学号查询 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 << "请输入学生学号:" << 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();//关闭磁盘文件 } if (2 == n) { cout << "请输入学生姓名:" << 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();//关闭磁盘文件 } if (3 == n) { cout << "请输入学生宿舍号:" << 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; } } } //修改学生信息 void Renewstudent(SqList &stu) { int n; string id, name, dormid; cout << "1.姓名 2.宿舍号" << endl; cout << "请输入您的选择(1~2):" << endl; cin >> n; cout << "请输入需要修改学生的学号" << 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 << "学生的信息如下:" << 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 << "请输入删除学生学号" << 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; } } } //主函数 int main() { SqList stu; int n; for (;;) { cout << "**************************宿舍管理查询软件**************************" << endl; cout << "1. 创建学生信息" << endl; //InitList cout << "2. 增加学生信息" << endl; //Addstudent cout << "3. 查询学生信息" << endl; //Findstudent cout << "4. 显示学生信息" << endl; //Showstudent cout << "5. 修改学生信息" << endl; //Renewstudent cout << "6. 删除学生信息" << endl; //Deletstudent cout << "0. 退出系统" << endl; cout << "*******************************************************************" << endl; cout << "请输入你需要的操作(0~6):" << endl; cin >> n; switch (n) { case 1: cout << "输入学生人数" << 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 << "您已退出系统!" << endl; return 0; } } system("pause"); return 0; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。