C++实现寝室卫生管理系统

本文实例为大家分享了C++实现寝室卫生管理系统的具体代码,供大家参考,具体内容如下
介绍: 当初写程序的时候很多知识都不足,我算是一个半路出家的。当时在另一课本上看到goto语句,感觉挺有趣的,就用上了,但goto语句,不适合很长的代码,由易造成逻辑混乱,也可以换成do-while语句。
我设计了输出表格框架,再循环输出数据,为使数据对齐,使用setw(),setw(int n)是c+ +中在输出操作中使用的字段宽度设置,设置输出的域宽,n表示字段宽度。此函数只对紧接着的输出有效,紧接着的输出结束后又变回默认的域宽。当后面紧跟着的输出字段长度小于n的时候,在该字段前面用空格补齐; 当输出字段长度大于n时,全部整体输出。头文件为:iomanip;
输入密码时,如非回车键,皆输出“*”,通过getch()函数实现,此函数是一个不回显函数,当用户按下某个字符时,函数自动读取,无需按回车键,所在头文件:conio.h,int __cdecl getch(); 声明,等,还用了一些算法,在此先不做详细介绍。
一、基本功能 1.(管理员,需要密码)实现成绩录入(地面卫生、床铺、物品摆放及其他)成绩求和和保存、修改及删除
2.(学生查询)实现显示所有寝室成员(按寝室号排序和按成绩排序)、显示自己寝室成绩-输入号(按寝室号排序和按成绩排序)、本周文明寝室、本周差评寝室
二、扩展功能 1.管理员也可对学生寝室信息包括寝室号,寝室成员进行基本管理,包括增,删,查,改等功能
2.对系统使用成员实现所有寝室成绩的查询,显示功能(可按寝室号排序或成绩排序),学生也可查看其他寝室相关信息
三、功能结构 本系统的总体功能结构是:首先学生可以查看寝室卫生检查结果信息,管理员具有唯一的帐号以及密码,登录后可以对基本数据进行增删改查等操作。
总体分为五大模块:寝室基本信息录入模块,寝室基本信息修改模块,寝室基本信息查询模块,寝室基本信息显示模块,寝室基本信息保存模块。总体功能结构图如图所示:
C++实现寝室卫生管理系统
文章图片

代码如下:

#include#include#include#include#include#define _CRT_SECURE_NO_WARNINGSusing namespace std; #define M 50//表长typedef int shu; //寝室信息结构typedef struct {shu ground; //地面卫生shu bed; //床铺shu article; //物品摆放shu other; //其他shu grade; //总成绩char member[50]; //寝室成员char number[10]; //寝室号int Total; }Student, St; Student S[M]; //1、定义清屏函数void Clear(){system("pause"); system("cls"); }//2、定义显示函数表头void showhead(){cout << "\n********************欢迎使用宿舍卫生管理系统********************\n " << endl; cout << "\n显示方式:\n " << endl; cout << "\t①按成绩显示\n" << endl; cout << "\t②按寝室显示\n" << endl; cout << "\t③返回上级\n\n" << endl; cout << "\t请选择:"; }//3、登录函数void load(){int n1; int i; loop:cout << "\n********************欢迎使用宿舍卫生管理系统********************\n\n" << endl; cout << "\t\t帐号:"; cin >> n1; cout << "\t\t密码:"; char a[40], b[14] = { "20190228" }; for (i = 0; ; i++){int __cdecl getch(); a[i] = getch(); if (a[i] != '\r')cout << "*"; else {a[i] = '\0'; if (strcmp(a, b) == 0 && n1 == 20190228){cout << "\n登录成功。。。。\n" << endl; Clear(); }else {cout << "\n用户名或密码错误。。。\n" << endl; Clear(); goto loop; }break; }}}//4、定义查询函数表头void searchheader(){cout << "\n********************欢迎使用宿舍卫生管理系统********************\n\n " << endl; cout << "\t①按成绩查询\n" << endl; cout << "\t②按寝室查询\n" << endl; cout << "\t③返回上级\n\n" << endl; cout << "请选择:"; }//5、定义录入函数void Add(Student* S){char ch[10]; char ch1[2] = { "0" }; do{cout << "\n********************欢迎使用宿舍卫生管理系统********************\n\n" << endl; cout << "\t****************学生信息录入****************\n"; cout << "请输入第" << S[0].Total + 1 << "个寝室的信息\n"; cout << "寝 室 号:"; cin >> S[S[0].Total + 1].number; cout << " 成员 :"; cin >> S[S[0].Total + 1].member; cout << "地面卫生:"; cin >> S[S[0].Total + 1].ground; cout << " 床铺 :"; cin >> S[S[0].Total + 1].bed; cout << "物品摆放:"; cin >> S[S[0].Total + 1].article; cout << " 其他 :"; cin >> S[S[0].Total + 1].other; cout << "总 成 绩:"; S[S[0].Total + 1].grade = S[S[0].Total + 1].ground + S[S[0].Total + 1].bed + S[S[0].Total + 1].article + S[S[0].Total + 1].other; cout<< S[S[0].Total + 1].grade<> ch; system("cls"); //清屏函数} while (strcmp(ch, ch1) != 0); }//6、定义显示函数 void Display_All(Student* S){ cout << "\n" << endl; int i; cout << "" << endl; cout << "———————————————————————————————————————————————— " << endl; cout << " |学 生 宿 舍 信 息|" << endl; cout << " |————————————————————————————————————————————————|" << endl; cout << " | 寝室号 | 总成绩 |寝室成员|地面卫生| 床铺 |物品摆放| 其他 |" << endl; if (S[0].Total > 0){for (i = 1; i <= S[0].Total; i++){cout << " |————|————|—————————————————————|————|————|————|————|" << endl; cout << " |" << setw(8) << S[i].number << "|" << setw(8) << S[i].grade << "|"; cout << setw(42) << S[i].member << "|" << setw(8) << S[i].ground << "|"; cout << setw(8) << S[i].bed << "|" << setw(8) << S[i].article << "|"; cout << setw(8) << S[i].other << "|" <= pivotkey) --high; S[low] = S[high]; //将比枢轴记录小的记录移到低端while (low < high && S[low].grade <= pivotkey) ++low; S[high] = S[low]; //比枢轴记录大的记录移到高端}S[low] = i; //枢轴记录到位 return low; //返回枢轴位置 }void QSort(Student* S, int low, int high){//对顺序表中的子表进行快速排序int pivotloc; if (low < high) //长度大于1{pivotloc = Partition(S, low, high); //将子表一分为二,pivotloc是枢轴位置QSort(S, low, pivotloc - 1); //对左子表递归排序QSort(S, pivotloc + 1, high); //对右子表递归排序}}void Sort_grade(Student* S){//对顺序表L进行快速排序QSort(S, 1, S[0].Total); }//8、排序函数按照寝室号从小到大排序(堆排序) void HeapAdjust(Student* S, int s, int m){Student rc; int j; rc = S[s]; for (j = 2 * s; j <= m; j *= 2)//沿number较大的孩子结点向下筛选{if (j < m&& strcmp(S[j].number, S[j + 1].number)<0) ++j; //j为number较大的记录下标if (strcmp(rc.number , S[j].number)>=0) break; //rc应插入在位置s上S[s] = S[j]; s = j; }S[s] = rc; }void CreatHeap(Student* S){//把无序序列S[1...n]建成大根堆int n, i; n = S[0].Total; for (i = n / 2; i > 0; --i)//反复调用HeapAdjustHeapAdjust(S, i, n); }void Sort_number(Student* S){//对顺序表S进行堆排序int i; Student x; CreatHeap(S); for (i = S[0].Total; i > 1; --i){x = S[1]; //将堆顶记录和当前未经排序子序列S[1...i]中最后一个记录互换S[1] = S[i]; S[i] = x; HeapAdjust(S, 1, i - 1); //将S[1..i-1]重新调整为大根堆}}//9、查询函数以成绩为关键字进行查询(折半查找)void searh_grade(Student* S){int high, low, mid; int j=0; shu grade; low = 1; high = S[0].Total; cout << "请输入你要查找学生的成绩:"; cin >> grade; Sort_grade(S); //声明排序函数按照成绩从小到大排序(快排)cout << "\n\n" << endl; cout << "———————————————————————————————————————————————— " << endl; cout << " |所 查 找 寝 室 信 息|" << endl; cout << " |————————————————————————————————————————————————|" << endl; cout << " | 寝室号 | 总成绩 |寝室成员|地面卫生| 床铺 |物品摆放| 其他 |" << endl; if (grade== S[1].grade){cout << " |————|————|—————————————————————|————|————|————|————|" << endl; cout << " |" << setw(8) << S[1].number << "|" << setw(8) << S[1].grade << "|"; cout << setw(42) << S[1].member << "|" << setw(8) << S[1].ground << "|"; cout << setw(8) << S[1].bed << "|" << setw(8) << S[1].article << "|"; cout << setw(8) << S[1].other <<"|"< S[mid].grade)low = mid + 1; elsehigh = mid - 1; }}if (j != 1){cout << " |————————————————————————————————————————————————|" << endl; cout << " |查 无 此 寝 室 信 息|" << endl; }cout << "———————————————————————————————————————————————— " << endl; }//10、查询函数以寝室号为关键字进行查询(折半查找)void searh_number(Student* S){int high, low, mid; char number[20]; int j=0; low = 1; high = S[0].Total; cout << "请输入你要查找学生的寝室号:"; cin >> number; Sort_number(S); //声明排序函数按照寝室号从小到大排序(堆排序)cout << "'\n\n" << endl; cout << "———————————————————————————————————————————————— " << endl; cout << " |所 查 找 的 寝 室 信 息|" << endl; cout << " |————————————————————————————————————————————————|" << endl; cout << " | 寝室号 | 总成绩 |寝室成员|地面卫生| 床铺 |物品摆放| 其他 |" << endl; while (low <= high){mid = (low + high) / 2; if (strcmp(number, S[mid].number) == 0){int i = mid; j = 1; cout << " |————|————|—————————————————————|————|————|————|————|" << endl; cout << " |" << setw(8) << S[i].number << "|" << setw(8) << S[i].grade << "|"; cout << setw(42) << S[i].member << "|" << setw(8) << S[i].ground << "|"; cout << setw(8) << S[i].bed << "|" << setw(8) << S[i].article << "|"; cout << setw(8) << S[i].other << "|" << endl; for (i = mid + 1; i <= high; i++){if (strcmp(number, S[i].number) == 0){cout << " |————|————|—————————————————————|————|————|————|————|" << endl; cout << " |" << setw(8) << S[i].number << "|" << setw(8) << S[i].grade << "|"; cout << setw(42) << S[i].member << "|" << setw(8) << S[i].ground << "|"; cout << setw(8) << S[i].bed << "|" << setw(8) << S[i].article << "|"; cout << setw(8) << S[i].other << "|" << endl; }}for (i = mid - 1; i >= low; i--){if (strcmp(number, S[i].number) == 0){cout << " |————|————|—————————————————————|————|————|————|————|" << endl; cout << " |" << setw(8) << S[i].number << "|" << setw(8) << S[i].grade << "|"; cout << setw(42) << S[i].member << "|" << setw(8) << S[i].ground << "|"; cout << setw(8) << S[i].bed << "|" << setw(8) << S[i].article << "|"; cout << setw(8) << S[i].other << "|" << endl; }}break; }elseif (strcmp(number, S[mid].number) > 0)low = mid + 1; elsehigh = mid - 1; }if (j != 1){cout << " |————————————————————————————————————————————————|" << endl; cout << " |查 无 此 寝 室 信 息|" << endl; }cout << "———————————————————————————————————————————————— " << endl; }//11、判断是否是文明寝室void y_n_number(Student* S){int high, low, mid; char number[20]; int j=0; low = 1; high = S[0].Total; cout << "请输入你要查找学生的寝室号:"; cin >> number; Sort_number(S); //声明排序函数按照寝室号号从小到大排序(堆排序)while (low <= high){mid = (low + high) / 2; if (strcmp(number, S[mid].number) == 0){int i = mid; j = 1; if (S[i].grade >= 80)cout << "\n\n本周文明寝室\n\n"; else cout << "\n\n本周差评寝室\n\n"; for (i = mid + 1; i <= high; i++){if (strcmp(number, S[i].number) == 0){if (S[i].grade >= 80)cout << "\n\n本周文明寝室\n\n"; else cout << "\n\n本周差评寝室\n\n"; }}for (i = mid - 1; i >= low; i--){if (strcmp(number, S[i].number) == 0){if (S[i].grade >= 80)cout << "\n\n本周文明寝室\n\n"; else cout << "\n\n本周差评寝室\n\n"; }}break; }elseif (strcmp(number, S[mid].number) > 0)low = mid + 1; elsehigh = mid - 1; }if (j != 1){cout << "———————————————————————————————————————————————— " << endl; cout << " |查 无 此 寝 室 信 息|" << endl; cout << "———————————————————————————————————————————————— " << endl; }}//12、定义修改学生信息表头函数void Exchangehead(){cout << "\n********************欢迎使用宿舍卫生管理系统********************\n\n " << endl; cout << "********************修改菜单********************\n " << endl; cout << "\t①单个删除信息\n" << endl; cout << "\t②删除全部信息\n" << endl; cout << "\t③修改寝室信息\n" << endl; cout << "\t④返回上级菜单\n\n" << endl; cout << "请选择:"; }//13、写文件函数void Keep(Student* S){ofstream output("学生宿舍信息.txt"); //初始化输出流对象if (output.is_open()){int i, j; j= S[0].Total; output << j<> j; S[0].Total = j; for (i = 1; i <= S[0].Total; i++){input >> S[i].number; input >> S[i].grade; input >> S[i].member; input >> S[i].ground; input >> S[i].bed; input >> S[i].article; input>> S[i].other; }}input.close(); }//主函数开始void main(){Student S[M]; S[0].Total = 0; Input(S); loop1:cout << "\n********************欢迎使用宿舍卫生管理系统********************\n" << endl; cout << "\n请选择以何身份登录系统:\n\n\n"; cout << "\t1、管理员\n\n"; cout << "\t2、学生\n\n"; cout << "\t3、退出系统\n\n"; cout << "\t请选择:"; int x; cin >> x; switch (x){case 1: {system("cls"); //清屏函数; load(); //调用登录函数int choicem; do {cout << "\n********************欢迎使用宿舍卫生管理系统********************\n" << endl; cout << "\t ********************主菜单********************\n " << endl; cout << "\t①录入学生信息\n" << endl; cout << "\t②显示学生信息.\n" << endl; cout << "\t③查询学生信息\n" << endl; cout << "\t④修改学生信息\n" << endl; cout << "\t⑤存储学生信息\n" << endl; cout << "\t⑥返回上级\n" << endl; cout << "\t⑦退出系统\n\n" << endl; cout << "\t请选择:"; cin >> choicem; switch (choicem){case 1: // 录入信息.{system("cls"); //清屏函数Add(S); // 调用录入信息函数system("cls"); //清屏函数Keep(S); break; }case 2: // 显示学生信息{system("cls"); //清屏函数int choice2; loop2:showhead(); cin >> choice2; if (choice2 == 1){Sort_grade(S); //调用排序函数按照成绩从小到大排序(快排) system("cls"); //清屏函数cout << "按成绩大小显示信息." << endl; Display_All(S); // 调用显示学生信息函数Keep(S); Clear(); goto loop2; system("cls"); //清屏函数}else if (choice2 == 2){Sort_number(S); //排序函数按照寝室号从小到大排序(堆排序)system("cls"); //清屏函数cout << "按寝室号大小显示信息." << endl; Display_All(S); // 调用显示学生信息函数Keep(S); Clear(); goto loop2; system("cls"); //清屏函数}else if (choice2 == 3){system("cls"); //清屏函数break; }else {cout << "\n\t输入错误,请重新输入。。。。 " << endl; Clear(); goto loop2; }}case 3: // 查询学生信息、{system("cls"); //清屏函数int choice3; loop3:searchheader(); //调用查询表头函数cin >> choice3; if (choice3 == 1)//按成绩查询{system("cls"); //清屏函数searh_grade(S); //查询函数以成绩为关键字进行查询(折半查找)Clear(); goto loop3; }else if (choice3 == 2)//按寝室号查询{system("cls"); //清屏函数searh_number(S); //调用查询函数以寝室号为关键字进行查询(折半查找)Clear(); goto loop3; }else if (choice3 == 3)//返回上级菜单system("cls"); //清屏函数else // 输入错误{cout << "\n\n\t选择错误,请重新输入。。。 \n" << endl; Clear(); goto loop3; }break; }case 4: // 修改学生信息{system("cls"); //清屏函数loop4: Exchangehead(); //调用修改学生信息表头函数int choice1; cin >> choice1; if (choice1 == 1)//单个删除{int i, j; int flag = 0; // 用来判断表中是否存在所要册除的学生的信息.char number[20]; system("cls"); //清屏函数cout << "请输入你要删除寝室的寝室号:"; cin >> number; for (i = 1; i <= S[0].Total; i++)if (strcmp(S[i].number, number) == 0)flag = i; if (!flag) {cout << "\n \n\t你所要删除的信息在表中不存在! \n" << endl; Clear(); goto loop4; }else {for (i = flag; i <= S->Total; i++){j = i + 1; S[i] = S[j]; }S[0].Total--; cout << "\n\t删除成功!\n" << endl; Clear(); Keep(S); goto loop4; }}else if (choice1 == 2) // 全部删除{system("cls"); //清屏函数S[0].Total = 0; cout << "\n\n\n册除成功。。。。。\n\n" << endl; Clear(); Keep(S); goto loop4; }else if (choice1 == 3)//修改信息{int i; int flag = 0; // 用来判断表中是否存在所要修改的学生的信息.char number[20]; system("cls"); //清屏函数cout << "请输入你要修改的寝室的寝室号:"; cin >> number; for (i = 1; i <= S[0].Total; i++)if (strcmp(S[i].number, number) == 0)flag = i; if (!flag){cout << "\n\t你所要修改的学生在表中不存在! \n" << endl; Clear(); goto loop4; }else{cout << "\n\n" << endl; cout << "———————————————————————————————————————————————— " << endl; cout << " |修 改 前 的 寝 室 信 息|" << endl; cout << " |————————————————————————————————————————————————|" << endl; cout << " | 寝室号 | 总成绩 |寝室成员|地面卫生| 床铺 |物品摆放| 其他 |" << endl; cout << " |————|————|—————————————————————|————|————|————|————|" << endl; cout << " |" << setw(8) << S[flag].number << "|" << setw(8) << S[flag].grade << "|"; cout << setw(42) << S[flag].member << "|" << setw(8) << S[flag].ground << "|"; cout << setw(8) << S[flag].bed << "|" << setw(8) << S[flag].article << "|"; cout << setw(8) << S[flag].other << "|" << endl; cout << "———————————————————————————————————————————————— " << endl; cout << "********************学生信息修改********************\n"; cout << " 寝室号 :"; cin >> S[flag].number; cout << " 成员 :"; cin >> S[flag].member; cout << "地面卫生:"; cin >> S[flag].ground; cout << " 床铺 :"; cin >> S[flag].bed; cout << "物品摆放:"; cin >> S[flag].article; cout << " 其他 :"; cin >> S[flag].other; cout << " 总成绩 :"; S[flag].grade = S[flag].ground + S[flag].bed + S[flag].article + S[flag].other; cout << S[flag].grade; cout << "\n"; cout << "———————————————————————————————————————————————— " << endl; cout << " |修 改 后 的 寝 室 信 息|" << endl; cout << " |————————————————————————————————————————————————|" << endl; cout << " | 寝室号 | 总成绩 |寝室成员|地面卫生| 床铺 |物品摆放| 其他 |" << endl; cout << " |————|————|—————————————————————|————|————|————|————|" << endl; cout << " |" << setw(8) << S[flag].number << "|" << setw(8) << S[flag].grade << "|"; cout << setw(42) << S[flag].member << "|" << setw(8) << S[flag].ground << "|"; cout << setw(8) << S[flag].bed << "|" << setw(8) << S[flag].article << "|"; cout << setw(8) << S[flag].other << "|" << endl; cout << "———————————————————————————————————————————————— " << endl; Clear(); Keep(S); goto loop4; }}elseif (choice1 == 4)//返回上级菜单{system("cls"); //清屏函数break; }else//输入错误{cout << "\n\t选择错误,请重新选择。。。\n" << endl; Clear(); goto loop4; }}case 5: // 存储学生信息、{system("cls"); //清屏函数Keep(S); cout << "\n********************欢迎使用宿舍卫生管理系统********************\n " << endl; cout << "\n\t存储成功。。。。。\n" << endl; Clear(); break; }case 6: //返回上级{system("cls"); //返回上级goto loop1; }case 7:break; // 退出管理系统default: {cout << "\n\n\t输入错误,请重新选择。。。\n\n" << endl; Clear(); break; }}} while (choicem != 7); break; }case 2: {system("cls"); //清屏函数; int choice; do {cout << "\n********************欢迎使用宿舍卫生管理系统********************\n " << endl; cout << "\n显示方式:\n " << endl; cout << "\t①按寝室查询\n" << endl; cout << "\t②按寝室显示\n" << endl; cout << "\t③按成绩显示\n" << endl; cout << "\t④本周寝室评价\n" << endl; cout << "\t⑤返回上级\n" << endl; cout << "\t⑥退出系统\n\n" << endl; cout << "请选择:"; cin >> choice; if (choice == 1)//按成绩查询{system("cls"); //清屏函数searh_number(S); // 查询函数以寝室号为关键字进行查询(折半查找)Clear(); }else if (choice == 2)//按寝室号显示{system("cls"); //清屏函数Sort_number(S); //排序函数按照寝室号从小到大排序(堆排序)Display_All(S); //显示函数Clear(); }else if (choice == 3)//按成绩显示{Sort_grade(S); //调用排序函数按照成绩从小到大排序(快排) system("cls"); //清屏函数Display_All(S); // 调用显示学生信息函数Clear(); }else if (choice == 4){system("cls"); //清屏函数y_n_number(S); //是否是文明寝室okClear(); }else if (choice == 5)//返回上级{system("cls"); //清屏函数goto loop1; }else if(choice == 6){break; }else {cout << "\n\t输入错误,请重新输入。。。。 \n\n" << endl; Clear(); }} while (choice != 5); break; }case 3:break; default: {cout << "\n\n\t输入错误,请重新输入。。。\n\n"<
【C++实现寝室卫生管理系统】以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

    推荐阅读