版本更新12.28修复了全部已知bug

1.修复了回显缺失的bug
2.修复了变量导致结果不正确的bug
3.修复了数据冲突的bug
4.修复了文件读写未关闭的bug 

题目:

设计一个学生学籍管理系统:

  1. 学生信息包括:姓名、学号、性别和英语、数学、程序设计、体育成绩。
  1. 数据录入支持键盘输入和文件导入;同时支持导入+输入,如自动列出“姓名、学号、性别”,而成绩部分由键盘输入。录入结果存入数据文件student.dat。
  1. 支持按学生姓名和学号查询,支持指定班级的两位序号范围查询,查询结果回显到屏幕上。
  1. l对所有学生,按照班级计算各科平均成绩。
  1. l支持按单科成绩排序和总分排序,排序结果写入文件并回显,文件名自拟。

需求分析:

由电脑程序执行的管理系统广泛用于个人信息,物品信息的储存上,而学生信息管理系统对于每个学校来说都是现代化管理不可或缺的一部分。

学生信息管理系统其开发主要包括后台数据库的建立和维护以及前端应用程序的开发两个方面,对于前者要求建立起数据库一致性和完整性、安全性好的数据库。而对于后者则要求应用程序功能完备,易使用的特点。

学生信息管理系统要实现的目标是为学校提供学生管理解决方案,具体目标如下:

  1. 提高学生信息管理效率,节约管理成本,增强学生管理的安全性。
  2. 满足学校学生管理的人员、老师和学生的不同层次和不同方面的需要。
  3. 为学校将来的信息化建设提供必要的支持。总之,通过该系统的建设来提高学校的学生信息管理效率,使得学校的发展能够适应当前的教育信息化建设的中体发展趋势。

究其要点,学生信息管理系统正在朝着更快速,更便捷,更安全的发展,并且将成为现在及未来很长一段时间学校对学生信息管理的重要工具。

设计思想和方案设计:

  1. 建立简洁明了的菜单,每个菜单都用函数包装好,便于代码的后序拼接和修改。
  2. 建立符合要求的学生类,学生重要的信息(姓名,学号等)应该作为私有成员被添加,并且提供函数便于外界访问或修改相关内容。
  3. 分数应该使用double类型方便求平均分,学号姓名等用string更方便。
  4. 一些常用的功能比如类的复制,带参构造,输出,访问,修改函数应该提前声明好方便后续运用。
  5. 重复多次的步骤尽量给其写一个通用函数,减少代码量和阅读压力。
  6. 所有的函数应该按顺序封装到一个头文件里,防止主函数和函数顺序混账不清的情况。
  7. 为了在读取文件到程序中时获得更大的储存空间,应该在主函数外声明大容量的数组。
  8. 进行文件读写时,应该确定以哪种方式,比如:添加应该以追加(ios::app),排序回显应该用清除再写(ios::trunc)。
  9. 在进行信息添加时应该注意雷同的情况,即:一个学号只能够对应一个人,而可以有多个同名的人。因此对于相同学号,应该给予更新,对于原先不存在的应该给予添加。
  10. 每实现一个功能应该立刻对其进行测试,否则全写完测试会导致错误难以判断甚至出现连环错误的情况,并且要检查当前实现的功能和之前实现的功能是否存在冲突,发现问题立刻修改,而不是搁置。
  11. 增加一些注释方便阅读,也方便长时间后再看进行回忆。

核心代码:


/*总控制函数*/void long_main() {ifstream ifs;while (true) {switch (Mainmenu()){case 1: {switch (findstumenu()) {case 1: {bool p = false;        /*是否查到*/string num;cout << "请输入学生学号:";cin >> num;ifs.open("student.dat", ios::in);string buf;           //文件读取while (getline(ifs, buf)){stu* stu1 = new stu();stringtostudent(buf, stu1);if (stu1->getnum() == num) {stu1->studisplay();p = true;}}if (!p)cout << "无此学生信息" << endl;ifs.close();break;}case 2: {string name;int len = 0;cout << "请输入学生姓名:";cin >> name;ifs.open("student.dat", ios::in);string buf;         //文件读取while (getline(ifs, buf)){stu* stu1 = new stu();stringtostudent(buf, stu1);if (stu1->getname() == name) {st[len].stucpy(*stu1);len++;}}if (!len)cout << "无此学生信息" << endl;else {for (int i = 0; i < len; i++) {        /*输出排序*/for (int j = 0; j < len - 1; j++) {if (st[j].getnum() > st[j + 1].getnum()) {stu s;s.stucpy(st[j]);st[j].stucpy(st[j + 1]);st[j + 1].stucpy(s);}}}cout << "查询到" << len << "条信息:" << endl;for (int i = 0; i < len; i++) {cout << i + 1 << ": " << st[i].getnum() << " " << st[i].getname() << endl;        /*不确定具体信息时只展示姓名和学号*/}cout << "选择查询学生序号:";int k;     /*选择具体信息编号*/do {cin >> k;if (k > len || k < 1)cout << "无效的指令,请重新输入" << endl;else break;} while (true);st[k - 1].studisplay();}ifs.close();break;}case 3: {string clas;int len = 0;cout << "请输入班号:";cin >> clas;ifs.open("student.dat", ios::in);string buf;          //文件读取while (getline(ifs, buf)){stu* stu1 = new stu();stringtostudent(buf, stu1);if (strsplit(stu1->getnum(), 0, 7) == clas) {st[len].stucpy(*stu1);len++;}}if (!len)cout << "无此班级" << endl;else {cout << "查询到" << len << "条信息:" << endl;for (int i = 0; i < len; i++) {cout << i + 1 << ": " << st[i].getnum() << " " << st[i].getname() << endl;      /*不确定具体信息时只展示姓名和学号*/}cout << "选择查询学生序号:";int k;do {cin >> k;if (k > len || k < 1)cout << "无效的指令,请重新输入" << endl;else break;} while (true);st[k - 1].studisplay();}ifs.close();break;}case 4:break;}break;case 2: {switch (addstumenu()){case 1: {cout << "输入学号(十位),姓名,性别(m/w),英语成绩,数学成绩,程序设计成绩,体育成绩" << endl;stu stu1;string nu, na, se;cin >> nu >> na >> se >> stu1.English >> stu1.Math >> stu1.Program_design >> stu1.Sports;        /*手动输入(空格为间隔)*/bool p = false;       /*相同学号是否已存在*/int loc = 0;ifs.open("student.dat", ios::in);string buf;            //文件读取while (getline(ifs, buf)){stu* stu2 = new stu();stringtostudent(buf, stu2);if (stu2->getnum() == nu) {p = true;break;}loc++;}ifs.close();if (!p) {       /*不存在添加*/int len = 0;ifs.open("student.dat", ios::in);string buf;            //文件读取while (getline(ifs, buf)){stu* stu2 = new stu();stringtostudent(buf, stu2);st[len].stucpy(*stu2);len++;}ofstream ofs;ofs.open("student.dat", ios::out);for (int i = 0; i < len; i++) {ofs << st[i].getnum() << " " << st[i].getname() << " "<<st[i].getsex()<<" " << st[i].English << " " << st[i].Math << " " << st[i].Program_design << " " << st[i].Sports << " " << "1" << endl;}ofs << nu << " " << na << " " << se << " " << stu1.English << " " << stu1.Math << " " << stu1.Program_design << " " << stu1.Sports << " " << "1" << endl;ofs.close();num++;}else {      /*存在则更新*/int len = 0;ifs.open("student.dat", ios::in);string buf;            //文件读取while (getline(ifs, buf)){stu* stu2 = new stu();stringtostudent(buf, stu2);st[len].stucpy(*stu2);len++;}ifs.close();st[loc].stucpy(stu1);st[loc].changename(na);st[loc].changenum(nu);st[loc].changesex(se);ofstream ofs;ofs.open("student.dat", ios::out);for (int i = 0; i < len; i++) {ofs << st[i].getnum() << " " << st[i].getname() << " "<<st[i].getsex()<<" " << st[i].English << " " << st[i].Math << " " << st[i].Program_design << " " << st[i].Sports << " " << "1" << endl;}ofs.close();}break;}case 2: {cout << "输入文件路径" << endl;string ss;cin >> ss;int len = 0;int len1 = 0;stu st1[maxsize];ifs.open("student.dat", ios::in);string buf;          //文件读取while (getline(ifs, buf)){stu* stu2 = new stu();stringtostudent(buf, stu2);st[len].stucpy(*stu2);len++;}ifs.close();ifs.open(ss, ios::in);if (!ifs.is_open()) {        /*打不开证明没有这个文件,或者权限不够*/cout << "文件路径无效!" << endl;break;}while (getline(ifs, buf)){stu* stu2 = new stu();stringtostudent(buf, stu2);st1[len1].stucpy(*stu2);len1++;}ifs.close();ofstream ofs;ofs.open("student.dat", ios::app);for (int i = 0; i < len1; i++) {bool t = true;     /*是否已存在*/for (int j = 0; j < len; j++) {if (st1[i].getnum() == st[j].getnum())t = false;}if (t)ofs << st1[i].getnum() << " " << st1[i].getname() << " " << st1[i].getsex() << " " << st1[i].English << " " << st1[i].Math << " " << st1[i].Program_design << " " << st1[i].Sports << " " << "1" << endl;       /*未存在添加,已存在不添加*/}ofs.close();check();break;}case 3:break;}break;}break;case 3: {int len = 0;ifs.open("student.dat", ios::in);string buf;          //文件读取while (getline(ifs, buf)){bool b = true; /*未找到班级*/stu* stu1 = new stu();stringtostudent(buf, stu1);for (int i = 0; i < len; i++) {if (st[i].getnum() == strsplit(stu1->getnum(), 0, 7)) {        /*班级名和学生学号前八位相等*/b = false;        st[i].English += stu1->English;        /*此段代码中st数组作为班级数组*/st[i].Math += stu1->Math;st[i].Program_design += stu1->Program_design;st[i].Sports += stu1->Sports;st[i].score++;       /*记录人数*/}}if (b) {      /*没有班级则新建班级*/st[len].changenum(strsplit(stu1->getnum(), 0, 7));st[len].Math = st[len].Program_design = st[len].Sports = st[len].English =st[len].score=0;st[len].English += stu1->English;st[len].Math += stu1->Math;st[len].Program_design += stu1->Program_design;st[len].Sports += stu1->Sports;st[len].score++;       /*记录人数*/len++;}}for (int i = 0; i < len; i++) {     /*输出排序*/for (int j = 0; j < len - 1; j++) {if (st[j].getnum() > st[j + 1].getnum()) {stu s;s.stucpy(st[j]);st[j].stucpy(st[j + 1]);st[j + 1].stucpy(s);}}}for (int i = 0; i < len; i++) {cout << "班号:" << st[i].getnum() << "  " << "英语平均分:" << st[i].English / st[i].score << "  数学平均分:" << st[i].Math / st[i].score << "  程序设计平均分" << st[i].Program_design / st[i].score << "  体育平均分" << st[i].Sports / st[i].score << endl;}ifs.close();}break;case 4: {ifstream ifs;int len = 0;ifs.open("student.dat", ios::in);string buf;          //文件读取while (getline(ifs, buf)){stu* stu2 = new stu();stringtostudent(buf, stu2);st[len].stucpy(*stu2);len++;}ifs.close();switch (scoresortmenu()) {case 1: {for (int i = 0; i < len; i++) {for (int j = 0; j < len - 1; j++) {      /*成绩排序*/if (st[j].total < st[j + 1].total) {stu stu1;stu1.stucpy(st[j]);st[j].stucpy(st[j + 1]);st[j+1].stucpy(stu1);}}}ofstream ofs;ofs.open("Total_stu.dat", ios::trunc);     /*重写文件*/for (int i = 0; i < len; i++) {ofs << st[i].getnum() << " " << st[i].getname() << " ";if (st[i].getsex() == "m")ofs << "男";else ofs << "女";ofs<<" "<<st[i].total << endl;cout << st[i].getnum() << " " << st[i].getname() << " ";if (st[i].getsex() == "m")cout << "男";else cout << "女";cout << " " << st[i].total<< endl;}ofs.close();break;}case 2: {for (int i = 0; i < len; i++) {for (int j = 0; j < len - 1; j++) {if (st[j].English < st[j + 1].English) {stu stu1;stu1.stucpy(st[j]);st[j].stucpy(st[j + 1]);st[j + 1].stucpy(stu1);}}}ofstream ofs;ofs.open("English_stu.dat", ios::trunc);for (int i = 0; i < len; i++) {ofs << st[i].getnum() << " " << st[i].getname() << " ";if (st[i].getsex() == "m")ofs << "男";else ofs << "女";ofs << " " << st[i].English  << endl;ofs.close();cout << st[i].getnum() << " " << st[i].getname() << " ";if (st[i].getsex() == "m")cout << "男";else cout << "女";cout << " " << st[i].English << endl;}break;}case 3: {for (int i = 0; i < len; i++) {for (int j = 0; j < len - 1; j++) {if (st[j].Math < st[j + 1].Math) {stu stu1;stu1.stucpy(st[j]);st[j].stucpy(st[j + 1]);st[j + 1].stucpy(stu1);}}}ofstream ofs;ofs.open("Math_stu.dat", ios::trunc);for (int i = 0; i < len; i++) {ofs << st[i].getnum() << " " << st[i].getname() << " ";if (st[i].getsex() == "m")ofs << "男";else ofs << "女";ofs << " " << st[i].Math<< endl;cout << st[i].getnum() << " " << st[i].getname() << " ";if (st[i].getsex() == "m")cout << "男";else cout << "女";cout << " " << st[i].Math<< endl;}ofs.close();break;}case 4: {for (int i = 0; i < len; i++) {for (int j = 0; j < len - 1; j++) {if (st[j].Program_design < st[j + 1].Program_design) {stu stu1;stu1.stucpy(st[j]);st[j].stucpy(st[j + 1]);st[j + 1].stucpy(stu1);}}}ofstream ofs;ofs.open("Program_design_stu.dat", ios::trunc);for (int i = 0; i < len; i++) {ofs << st[i].getnum() << " " << st[i].getname() << " ";if (st[i].getsex() == "m")ofs << "男";else ofs << "女";ofs << " " << st[i].Program_design  << endl;cout << st[i].getnum() << " " << st[i].getname() << " ";if (st[i].getsex() == "m")cout << "男";else cout << "女";cout << " " << st[i].Program_design << endl;}ofs.close();break;}case 5: {for (int i = 0; i < len; i++) {for (int j = 0; j < len - 1; j++) {if (st[j].Sports < st[j + 1].Sports) {stu stu1;stu1.stucpy(st[j]);st[j].stucpy(st[j + 1]);st[j + 1].stucpy(stu1);}}}ofstream ofs;ofs.open("Sports_stu.dat", ios::trunc);for (int i = 0; i < len; i++) {ofs << st[i].getnum() << " " << st[i].getname() << " ";if (st[i].getsex() == "m")ofs << "男";else ofs << "女";ofs << " "<< st[i].Sports << endl;cout << st[i].getnum() << " " << st[i].getname() << " ";if (st[i].getsex() == "m")cout << "男";else cout<< "女";cout << " "<< st[i].Sports << endl;}ofs.close();break;}case 6:break;}}break;default:return;}}}
}

测试用例:

本代码涉及大量文件操作:

运行要素:

  1. 在源代码所在文件夹中应该创建一个student.dat文件
  2. 在进行以文件形式添加人员信息时应该指明添加信息所在的路径

运行截图:

查询操作:

添加操作(stuadd.dat为同文件夹内的文件):

平均成绩(按学号中的班级计算):

排名回显:

总结:

  1. 在此次实验中,我相对于以前对“面对对象”开发和“如何使用好封装类”有了更好的认识。并且对于C++相关文件的操作有了更加透彻的理解,方便日后进一步的使用。
  2. 对结构化,标准化的程序设计有了更深的理解,较以前更能够将自己的一段代码标准化和兼容化,不仅方便对代码的解读,也在代码的写作上更加省时省力。
  3. 程序的不足之处在于功能很少,有更多完善空间,面对一些问题还是没有足够的解决方法,比如:转专业,分流等导致学号和班级不符,一些操作没有纠错处理等。
  4. 在程序设计过程中,发现了自己在文件操作上有很多不足,经常错误的使用读取或者写入方法导致实验数据丢失,功亏一篑的情况。
  5. 在此次实验后,我更加意识到我在C++方面的很多不足,尤其是知识掌握程度,面对未来的学习,我应该不断查缺补漏,并学习更多新知识提升自己的程序设计能力和与程序设计相关的逻辑思维能力。

全部代码:

stu.cpp:

#include<iostream>
#include<algorithm>
#include<string>
#include<fstream>
#define spaces 7
#define maxsize 1000
using namespace std;
static int num = 0;class stu {
private:        /*学生信息属于私密信息*/string name;  /*姓名*/string num;       /*学号*/string sex;       /*性别*/
public:double English;      /*各科成绩*/double Math;double Program_design;double Sports;double total;       /*总成绩*/int score;       /*判断此条信息是否完善*/stu() {       /*默认构造*/name = " "; num = " "; sex = " "; English = 0; Math = 0; Program_design = 0; Sports = 0; score = 0;settotal();}stu(string na, string nu, string se, double en, double ma, double c, double s) {       /*全参数构造*/name = na; num = nu;  English = en; Math = ma; Program_design = c; Sports = s; score = 1;if (se == "男" || se == "m")sex = 'm';else sex = 'w';settotal();}stu(string na, string nu, string se) {       /*不含成绩的构造*/name = na, num = nu;English = 0; Math = 0; Program_design = 0; Sports = 0; score = 0;if (se == "男" || se == "m")sex = 'm';else sex = 'w';settotal();}void stucpy(stu st) {      /*值复制函数*/name = st.getname();num = st.getnum();sex = st.getsex();English = st.English;Math = st.Math;Program_design = st.Program_design;Sports = st.Sports;score = st.score;settotal();}void studisplay() {     /*输出函数*/cout << num << " " << name << " " << English << " " << Math << " " << Program_design << " " << Sports << endl;}void settotal() {      /*计和函数*/total = English + Math + Program_design + Sports;}string getname() {        /*私有成员访问*/return name;}string getnum() {return num;}string getsex() {return sex;}void changename(string na) {       /*私有成员修改*/name = na;}void changenum(string nu) {num = nu;}void changesex(string se) {sex = se;}
};
stu st[maxsize];
#include"stu_deal.h"      /*函数文件*/
int main() {check();        /*查阅人数*/long_main();return 0;
}

stu_deal.h:

#pragma once
void check() {      /*查阅函数,统计文件中学生个数*/num = 0;ifstream ifs;ifs.open("student.dat", ios::in);if (!ifs.is_open()){cout << "Erro Read" << endl;}string buf;            //文件读取while (getline(ifs, buf)){num++;}ifs.close();
}void findspace(string s, int* a) { /*找字符串中空格方便进行字符串分割*/int len = 0;for (int i = 0; i < s.length(); i++) {if (s[i] == ' ') {a[len] = i;len++;}}
}string strsplit(string s, int begin, int end) {        /*取begin到end的所有字符*/string ss;for (int i = begin; i <= end; i++) {ss.push_back(s[i]);}return ss;
}double stringtodouble(string s) {      /*从文件中提取分数为string类型,将其转化为double类型(有精度损失)*/double ss = 0, sss = 0;int loc = s.length();for (int i = 0; i < s.length(); i++) {if (s[i] == '.')loc = i;}for (int i = 0; i < loc; i++) {       /*整数位*/ss *= 10;ss += (int)s[i] - 48;}for (int i = s.length() - 1; i >= loc; i--) {     /*小数位*/sss /= 10;sss += (int)s[i] - 48;}return sss + ss;
}void stringtostudent(string s, stu* student) {     /*从文件提取的信息为string类型,将其转化为student(class)类型*/string na, nu, se, en, ma, c, sp;int* a;a = new int[spaces + 1];findspace(s, a);nu = strsplit(s, 0, a[0] - 1);       /*从长字符串中提取各项信息*/na = strsplit(s, a[0] + 1, a[1] - 1);se = strsplit(s, a[1] + 1, a[2] - 1);en = strsplit(s, a[2] + 1, a[3] - 1);ma = strsplit(s, a[3] + 1, a[4] - 1);c = strsplit(s, a[4] + 1, a[5] - 1);sp = strsplit(s, a[5] + 1, a[6] - 1);student->changename(na);student->changenum(nu);student->changesex(se);student->English = stringtodouble(en);student->Math = stringtodouble(ma);student->Program_design = stringtodouble(c);student->Sports = stringtodouble(sp);student->score = 1;
}int Mainmenu() {       /*主菜单*/cout << endl << "档案中共有学生" << num << "人" << endl;cout << "请选择操作:" << endl;cout << "1:查询学生" << endl;cout << "2:添加学生" << endl;cout << "3:平均成绩" << endl;cout << "4:分数排名" << endl;cout << "5:退出" << endl;int chose;do {cin >> chose;if (chose < 1 || chose>5)cout << "无效指令,重新输入!" << endl;       /*指令异常处理*/else break;} while (true);return chose;
}int findstumenu() {        /*查询信息菜单*/cout << endl << "选择查询方式:" << endl;cout << "1.学号查询" << endl;cout << "2.姓名查询" << endl;cout << "3.班级序号查询" << endl;cout << "4.后退" << endl;int chose;do {cin >> chose;if (chose < 1 || chose>4)cout << "无效指令,重新输入!" << endl;else break;} while (true);return chose;
}int addstumenu() {     /*添加信息菜单*/cout << endl << "选择添加方式:" << endl;cout << "1.手动输入" << endl;cout << "2.文件导入" << endl;cout << "3.后退" << endl;int chose;do {cin >> chose;if (chose < 1 || chose>3)cout << "无效指令,重新输入!" << endl;else break;} while (true);return chose;
}int scoresortmenu() {      /*分数排名菜单*/cout << endl<<"选择排序所参照的成绩" << endl;cout << "1.总分" << endl;cout << "2.英语" << endl;cout << "3.数学" << endl;cout << "4.程序设计" << endl;cout << "5.体育" << endl;cout << "6.后退" << endl;int chose;do {cin >> chose;if (chose < 1 || chose>5)cout << "无效指令,重新输入!" << endl;else break;} while (true);return chose;
}void long_main() {ifstream ifs;while (true) {switch (Mainmenu()){case 1: {switch (findstumenu()) {case 1: {bool p = false;        /*是否查到*/string num;cout << "请输入学生学号:";cin >> num;ifs.open("student.dat", ios::in);string buf;           //文件读取while (getline(ifs, buf)){stu* stu1 = new stu();stringtostudent(buf, stu1);if (stu1->getnum() == num) {stu1->studisplay();p = true;}}if (!p)cout << "无此学生信息" << endl;ifs.close();break;}case 2: {string name;int len = 0;cout << "请输入学生姓名:";cin >> name;ifs.open("student.dat", ios::in);string buf;         //文件读取while (getline(ifs, buf)){stu* stu1 = new stu();stringtostudent(buf, stu1);if (stu1->getname() == name) {st[len].stucpy(*stu1);len++;}}if (!len)cout << "无此学生信息" << endl;else {for (int i = 0; i < len; i++) {        /*输出排序*/for (int j = 0; j < len - 1; j++) {if (st[j].getnum() > st[j + 1].getnum()) {stu s;s.stucpy(st[j]);st[j].stucpy(st[j + 1]);st[j + 1].stucpy(s);}}}cout << "查询到" << len << "条信息:" << endl;for (int i = 0; i < len; i++) {cout << i + 1 << ": " << st[i].getnum() << " " << st[i].getname() << endl;        /*不确定具体信息时只展示姓名和学号*/}cout << "选择查询学生序号:";int k;     /*选择具体信息编号*/do {cin >> k;if (k > len || k < 1)cout << "无效的指令,请重新输入" << endl;else break;} while (true);st[k - 1].studisplay();}ifs.close();break;}case 3: {string clas;int len = 0;cout << "请输入班号:";cin >> clas;ifs.open("student.dat", ios::in);string buf;          //文件读取while (getline(ifs, buf)){stu* stu1 = new stu();stringtostudent(buf, stu1);if (strsplit(stu1->getnum(), 0, 7) == clas) {st[len].stucpy(*stu1);len++;}}if (!len)cout << "无此班级" << endl;else {cout << "查询到" << len << "条信息:" << endl;for (int i = 0; i < len; i++) {cout << i + 1 << ": " << st[i].getnum() << " " << st[i].getname() << endl;      /*不确定具体信息时只展示姓名和学号*/}cout << "选择查询学生序号:";int k;do {cin >> k;if (k > len || k < 1)cout << "无效的指令,请重新输入" << endl;else break;} while (true);st[k - 1].studisplay();}ifs.close();break;}case 4:break;}break;case 2: {switch (addstumenu()){case 1: {cout << "输入学号(十位),姓名,性别(m/w),英语成绩,数学成绩,程序设计成绩,体育成绩" << endl;stu stu1;string nu, na, se;cin >> nu >> na >> se >> stu1.English >> stu1.Math >> stu1.Program_design >> stu1.Sports;        /*手动输入(空格为间隔)*/bool p = false;       /*相同学号是否已存在*/int loc = 0;ifs.open("student.dat", ios::in);string buf;            //文件读取while (getline(ifs, buf)){stu* stu2 = new stu();stringtostudent(buf, stu2);if (stu2->getnum() == nu) {p = true;break;}loc++;}ifs.close();if (!p) {       /*不存在添加*/int len = 0;ifs.open("student.dat", ios::in);string buf;            //文件读取while (getline(ifs, buf)){stu* stu2 = new stu();stringtostudent(buf, stu2);st[len].stucpy(*stu2);len++;}ofstream ofs;ofs.open("student.dat", ios::out);for (int i = 0; i < len; i++) {ofs << st[i].getnum() << " " << st[i].getname() << " "<<st[i].getsex()<<" " << st[i].English << " " << st[i].Math << " " << st[i].Program_design << " " << st[i].Sports << " " << "1" << endl;}ofs << nu << " " << na << " " << se << " " << stu1.English << " " << stu1.Math << " " << stu1.Program_design << " " << stu1.Sports << " " << "1" << endl;ofs.close();num++;}else {      /*存在则更新*/int len = 0;ifs.open("student.dat", ios::in);string buf;            //文件读取while (getline(ifs, buf)){stu* stu2 = new stu();stringtostudent(buf, stu2);st[len].stucpy(*stu2);len++;}ifs.close();st[loc].stucpy(stu1);st[loc].changename(na);st[loc].changenum(nu);st[loc].changesex(se);ofstream ofs;ofs.open("student.dat", ios::out);for (int i = 0; i < len; i++) {ofs << st[i].getnum() << " " << st[i].getname() << " "<<st[i].getsex()<<" " << st[i].English << " " << st[i].Math << " " << st[i].Program_design << " " << st[i].Sports << " " << "1" << endl;}ofs.close();}break;}case 2: {cout << "输入文件路径" << endl;string ss;cin >> ss;int len = 0;int len1 = 0;stu st1[maxsize];ifs.open("student.dat", ios::in);string buf;          //文件读取while (getline(ifs, buf)){stu* stu2 = new stu();stringtostudent(buf, stu2);st[len].stucpy(*stu2);len++;}ifs.close();ifs.open(ss, ios::in);if (!ifs.is_open()) {        /*打不开证明没有这个文件,或者权限不够*/cout << "文件路径无效!" << endl;break;}while (getline(ifs, buf)){stu* stu2 = new stu();stringtostudent(buf, stu2);st1[len1].stucpy(*stu2);len1++;}ifs.close();ofstream ofs;ofs.open("student.dat", ios::app);for (int i = 0; i < len1; i++) {bool t = true;     /*是否已存在*/for (int j = 0; j < len; j++) {if (st1[i].getnum() == st[j].getnum())t = false;}if (t)ofs << st1[i].getnum() << " " << st1[i].getname() << " " << st1[i].getsex() << " " << st1[i].English << " " << st1[i].Math << " " << st1[i].Program_design << " " << st1[i].Sports << " " << "1" << endl;       /*未存在添加,已存在不添加*/}ofs.close();check();break;}case 3:break;}break;}break;case 3: {int len = 0;ifs.open("student.dat", ios::in);string buf;          //文件读取while (getline(ifs, buf)){bool b = true; /*未找到班级*/stu* stu1 = new stu();stringtostudent(buf, stu1);for (int i = 0; i < len; i++) {if (st[i].getnum() == strsplit(stu1->getnum(), 0, 7)) {        /*班级名和学生学号前八位相等*/b = false;        st[i].English += stu1->English;        /*此段代码中st数组作为班级数组*/st[i].Math += stu1->Math;st[i].Program_design += stu1->Program_design;st[i].Sports += stu1->Sports;st[i].score++;       /*记录人数*/}}if (b) {      /*没有班级则新建班级*/st[len].changenum(strsplit(stu1->getnum(), 0, 7));st[len].Math = st[len].Program_design = st[len].Sports = st[len].English =st[len].score=0;st[len].English += stu1->English;st[len].Math += stu1->Math;st[len].Program_design += stu1->Program_design;st[len].Sports += stu1->Sports;st[len].score++;       /*记录人数*/len++;}}for (int i = 0; i < len; i++) {     /*输出排序*/for (int j = 0; j < len - 1; j++) {if (st[j].getnum() > st[j + 1].getnum()) {stu s;s.stucpy(st[j]);st[j].stucpy(st[j + 1]);st[j + 1].stucpy(s);}}}for (int i = 0; i < len; i++) {cout << "班号:" << st[i].getnum() << "  " << "英语平均分:" << st[i].English / st[i].score << "  数学平均分:" << st[i].Math / st[i].score << "  程序设计平均分" << st[i].Program_design / st[i].score << "  体育平均分" << st[i].Sports / st[i].score << endl;}ifs.close();}break;case 4: {ifstream ifs;int len = 0;ifs.open("student.dat", ios::in);string buf;          //文件读取while (getline(ifs, buf)){stu* stu2 = new stu();stringtostudent(buf, stu2);st[len].stucpy(*stu2);len++;}ifs.close();switch (scoresortmenu()) {case 1: {for (int i = 0; i < len; i++) {for (int j = 0; j < len - 1; j++) {      /*成绩排序*/if (st[j].total < st[j + 1].total) {stu stu1;stu1.stucpy(st[j]);st[j].stucpy(st[j + 1]);st[j+1].stucpy(stu1);}}}ofstream ofs;ofs.open("Total_stu.dat", ios::trunc);     /*重写文件*/for (int i = 0; i < len; i++) {ofs << st[i].getnum() << " " << st[i].getname() << " ";if (st[i].getsex() == "m")ofs << "男";else ofs << "女";ofs<<" "<<st[i].total << endl;cout << st[i].getnum() << " " << st[i].getname() << " ";if (st[i].getsex() == "m")cout << "男";else cout << "女";cout << " " << st[i].total<< endl;}ofs.close();break;}case 2: {for (int i = 0; i < len; i++) {for (int j = 0; j < len - 1; j++) {if (st[j].English < st[j + 1].English) {stu stu1;stu1.stucpy(st[j]);st[j].stucpy(st[j + 1]);st[j + 1].stucpy(stu1);}}}ofstream ofs;ofs.open("English_stu.dat", ios::trunc);for (int i = 0; i < len; i++) {ofs << st[i].getnum() << " " << st[i].getname() << " ";if (st[i].getsex() == "m")ofs << "男";else ofs << "女";ofs << " " << st[i].English  << endl;ofs.close();cout << st[i].getnum() << " " << st[i].getname() << " ";if (st[i].getsex() == "m")cout << "男";else cout << "女";cout << " " << st[i].English << endl;}break;}case 3: {for (int i = 0; i < len; i++) {for (int j = 0; j < len - 1; j++) {if (st[j].Math < st[j + 1].Math) {stu stu1;stu1.stucpy(st[j]);st[j].stucpy(st[j + 1]);st[j + 1].stucpy(stu1);}}}ofstream ofs;ofs.open("Math_stu.dat", ios::trunc);for (int i = 0; i < len; i++) {ofs << st[i].getnum() << " " << st[i].getname() << " ";if (st[i].getsex() == "m")ofs << "男";else ofs << "女";ofs << " " << st[i].Math<< endl;cout << st[i].getnum() << " " << st[i].getname() << " ";if (st[i].getsex() == "m")cout << "男";else cout << "女";cout << " " << st[i].Math<< endl;}ofs.close();break;}case 4: {for (int i = 0; i < len; i++) {for (int j = 0; j < len - 1; j++) {if (st[j].Program_design < st[j + 1].Program_design) {stu stu1;stu1.stucpy(st[j]);st[j].stucpy(st[j + 1]);st[j + 1].stucpy(stu1);}}}ofstream ofs;ofs.open("Program_design_stu.dat", ios::trunc);for (int i = 0; i < len; i++) {ofs << st[i].getnum() << " " << st[i].getname() << " ";if (st[i].getsex() == "m")ofs << "男";else ofs << "女";ofs << " " << st[i].Program_design  << endl;cout << st[i].getnum() << " " << st[i].getname() << " ";if (st[i].getsex() == "m")cout << "男";else cout << "女";cout << " " << st[i].Program_design << endl;}ofs.close();break;}case 5: {for (int i = 0; i < len; i++) {for (int j = 0; j < len - 1; j++) {if (st[j].Sports < st[j + 1].Sports) {stu stu1;stu1.stucpy(st[j]);st[j].stucpy(st[j + 1]);st[j + 1].stucpy(stu1);}}}ofstream ofs;ofs.open("Sports_stu.dat", ios::trunc);for (int i = 0; i < len; i++) {ofs << st[i].getnum() << " " << st[i].getname() << " ";if (st[i].getsex() == "m")ofs << "男";else ofs << "女";ofs << " "<< st[i].Sports << endl;cout << st[i].getnum() << " " << st[i].getname() << " ";if (st[i].getsex() == "m")cout << "男";else cout<< "女";cout << " "<< st[i].Sports << endl;}ofs.close();break;}case 6:break;}}break;default:return;}}}
}

C++:设计一个学生学籍管理系统,设计相关信息,并执行一些计算和文件操作相关推荐

  1. 设计一个学生学籍管理系统

    本人也是一个代码小白,这个代码本人知道很多修改和完善的地方,还望看到的大佬能留下斧正点!感谢感谢! [题目] 学生信息包括:姓名.学号.性别和英语.数学.程序设计.体育成绩. 从键盘输入数据,建立数据 ...

  2. C++期末课程设计:设计一个学生学籍管理系统

    学生信息包括:姓名.学号.性别和英语.数学.程序设计.体育成绩.   从键盘输入数据,建立数据文件student.dat.   实现对学生或学号查询,显示信息.   对所有学生,按照班级计算平均 ...

  3. 第1题:设计一个学生学籍管理系统

     学生信息包括:姓名.学号.性别和英语.数学.程序设计.体育成绩.  从键盘输入数据,建立数据文件student.dat.  实现对学生或学号查询,显示信息.  对所有学生,按照班级计算平均成 ...

  4. c语言设计一个学生成绩管理系统,用C语言设计的学生成绩管理系统1.doc

    实 训 报 告 实训名称: "学生成绩管理系统"设计 时间: 2009年 06月15日至 2009年06月 19日 学生实训任务书 一.题目 "学生成绩管理系统" ...

  5. C语言课程设计之学生学籍管理系统

    做了好长时间的,里面有点小问题,希望大佬找到并帮忙改改,注意输入密码三次错误会自动注销用户,密码123456 代码: #include<stdio.h> #include<stdli ...

  6. 用PHP写一个学生学籍管理系统

    为了搭建一个完整的学生学籍管理系统,需要分模块进行设计和实现.这里我们按照以下模块来讲解: 数据库设计 用户登录和权限控制 学生信息管理 班级信息管理 课程信息管理 成绩信息管理 1. 数据库设计 我 ...

  7. 如何基于SSM设计实现一个学生学籍管理系统

    本系统是一个基于ssm+layui的学籍管理系统: 本系统比较简单,适用于新手,上手简单易操作,主要是帮助理解java web (ssm框架)项目的整体运行流程,附带着也熟悉一下这种项目的搭建: 后面 ...

  8. 基于java的学生学籍管理系统(含源文件)

    欢迎添加微信互相交流学习哦! 项目源码:https://gitee.com/oklongmm/biye 目录 内容摘要    - 2 - 引言    - 4 - 学生学籍管理系统开发的意义和目的   ...

  9. 用mysql设计学籍管理系统_学生学籍管理系统(SQL数据库系统设计)(完整版).pdf...

    . 数据库课程设计报告 < 学生学籍管理系统 > 专业 班级 小组成员 指导老师 开始时间 完成时间 word 专业资料 . 目录 数据库课程设计报告 1 1. 问题描述 3 1.1 背景 ...

最新文章

  1. Eclipse 中maven插件坏死解决办法
  2. 我可以在Markdown中使用“ target =“ _ blank””创建链接吗?
  3. python导入excel文件-python使用xlrd模块读写Excel文件的方法
  4. 你还以为,除了你自己就没有人懂你女朋友吗?
  5. xay loves count 枚举-复杂度-顺序无关-选择
  6. Jsoncpp Compiler、Programming
  7. redius mysql_采用Linux系统的Freeradius+MySQL实现RADIUS认证服务器
  8. lintcode:买卖股票的最佳时机 III
  9. delphi Hi 和 High
  10. JAVA中解决Filter过滤掉css,js,图片文件等问题
  11. 联通实时计算平台演进与实践
  12. 如何使用google进行搜索
  13. openproj centos安装及其输入中文变方块乱码解决
  14. Hilbert 变换与瞬时频率
  15. sql server 2000收缩数据库【极简操作】
  16. Ubuntu使用lightdm避坑
  17. 2021年最受数据分析师欢迎的副业排行榜TOP1
  18. H5/C3基础(1)
  19. 详解Java基础数据类型
  20. 信息增益与信息增益率详解

热门文章

  1. asp.net中小银行客户信息管理系统
  2. easyswoole学习记录
  3. NOI Online 2022 入门组
  4. 阿里程序员自述:入职才两个月,我决定离职
  5. 微信小程序学习笔记(二)UI设计
  6. c语言 字符串 timu,这个C语言题目究竟谁该背锅?竟然是它。。。
  7. 制作自己的Vscode主题插件
  8. HTML5图文混排:把握移动端字体设计的七大准则
  9. codevs 1036
  10. Go-Excelize API源码阅读(三十一)——ProtectSheet(sheet string, settings *SheetProtectionOptions)