要求:
实现增删查改,两种方式查询,登录功能
设计:
学生端:可以查询个人成绩
管理员端:对学籍信息增删查改,查看所有信息,单人信息,学籍排序,统计绩点
三个文件,学生信息,用户信息,数据保存文件
main.cpp:
#include <iostream>
#include <string.h>
#include <vector>
#include <algorithm>//包含大量算法函数的声明,这些函数用于操作和变换容器中的元素
//例如:sort(),find()
#include "Stu.h"
#include "User.h"
using namespace std;bool ReadStudent(Stu& s)
{string file_path1("stus.txt");fstream openfile(file_path1.c_str(), fstream::in | fstream::out);
//打开文件流,可以同时读取和写入文件vector<string> OneInfo;
//字符串向量临时存储从文件中读取的每行学生信息string txtline, item;;while (getline(openfile, txtline))
//逐行读取数据并存储在textline中{OneInfo.clear();istringstream items(txtline);
//将每行数据分割成多个部分while (items >> item)
//从items读取一个数据并将其赋值给变量item,读取成功则继续OneInfo.push_back(item);
//将每一个读取到的item添加到oneinfo的末尾s.AddStudent(OneInfo);//添加到stu对象s中}cout << "学生文件读取成功!" << endl;return true;
}bool ReadUser(User &u)
{string file_path2("users.txt");fstream openfile(file_path2.c_str(),fstream::in|fstream::out);vector<string> OneInfo2;string txtline, item;;while(getline(openfile,txtline)){OneInfo2.clear();istringstream items(txtline); while(items>>item)OneInfo2.push_back(item);u.AddUser(OneInfo2);//添加到user对象u中}cout<<"用户文件读取成功!"<<endl;return true;
}int Menulog(User u,Stu s)
//通过该函数返回值判断身份
{string name,password,key;int flag1=0,flag2=0;system("cls");printf("******************************************************\n");printf("*******************学籍管理系统***********************\n");printf("*********************登录界面*************************\n");printf("账号:");cin>>name;printf("密码:");cin>>password;printf("******************************************************\n");system("pause");flag1=u.SearchName(name);flag2=u.SearchPassword(password,name);if(flag1==1 && flag2==1)//用户存在且账号密码正确{cout<<"账号密码验证通过,登陆成功!"<<endl;key=u.SearchKey(name);if(key=="无"){cout<<"您的身份为:学生"<<endl;cout<<"欢迎使用学籍管理系统!"<<endl;system("pause");return 1;}else{cout<<"您的身份为:管理员"<<endl;cout<<"欢迎使用学籍管理系统!"<<endl;system("pause");return 2;}}else{cout<<"账号或密码错误,登陆失败!"<<endl;exit(0);}
}void Menu2(Stu s)
{int n;int choice=0;do{system("cls");printf("\n");printf("******************************************************\n");printf("*******************学籍管理系统***********************\n");printf("** 1.学籍查询 2.退出系统 **\n");printf("******************************************************\n");puts("\n");printf("请选择功能序号:");cin>>n;if(n!=1&&n!=2){system("cls");cout<<"选择错误! 请重新选择!"<<endl;system("pause");choice=1;}else{break;}}while(choice==1);switch(n){case 1:{cout << "请选择查询方式:1.姓名 2.学号" << endl;int sel;cin >> sel;if (sel == 1){s.SearchName();}else if (sel == 2){s.SearchId();}else{cout << "选择错误!" << endl;}system("pause");break;}case 2:{cout<<"已退出学籍管理系统!"<<endl;exit(0);break;}}Menu2(s);
}void Menu(Stu s)
{int n;int choice=0;vector<string>addinfo;do{system("cls");printf("\n");printf("******************************************************\n");printf("*******************学籍管理系统***********************\n");printf("** 1.学籍添加 2.学籍删除 **\n");printf("** 3.学籍修改 4.学籍查询 **\n");printf("** 5.学籍排序 6.统计绩点 **\n");printf("** 7.学籍显示 8.保存并退出 **\n");printf("******************************************************\n");puts("\n");printf("请选择功能序号:");cin>>n;if(n<1||n>8){system("cls");cout<<"选择错误! 请重新选择!"<<endl;system("pause");choice=1;}else{break;}}while(choice==1);{switch(n){case 1:{cout<<"即将进行学籍数据添加,请分别输入以下内容:"<<endl;cout<<"学号 姓名 性别 年龄 身份证号 政治面貌 家庭住址 院系 班级 绩点 奖惩情况"<<endl;string info;for(int i=0;i<11;i++){cin>>info;
//循环输入11个字符串addinfo.push_back(info);
//存储在addinfo这个vector中}s.AddStudent(addinfo);cout<<"添加学籍信息成功!"<<endl;system("pause");break;}case 2:{cout<<"请选择删除方式:1.单条删除 2.整个删除"<<endl;int sel;cin>>sel;if(sel==1){s.DeleteOneStudent();}else if(sel==2){s.DeleteAllStudent();}else{cout<<"选择错误!"<<endl;}system("pause");break;}case 3:{cout<<"即将进行学籍数据修改,请按提示操作"<<endl;s.ChangeOneStudent();system("pause");break;}case 4:{cout << "请选择查找方式:1.姓名 2.学号" << endl;int sel;cin >> sel;if (sel == 1){s.SearchName();}else if (sel == 2){s.SearchId();}else{cout << "选择错误!" << endl;}system("pause");break;}case 5:{cout<<"学籍信息按学号从小到大排序:"<<endl;s.SortStudent();system("pause");break;}case 6:{s.StatisticsInfos();system("pause");break;}case 7:{cout<<"学籍信息如下:"<<endl;s.ShowAllInfo();system("pause");break;}case 8:{cout<<"已成功保存至本地!"<<endl;cout<<"已成功退出学籍管理系统,欢迎下次使用!"<<endl;s.WriteToFile("stussave.txt") ;//system("pause");exit(0);break;}}}Menu(s);
}int main()
{Stu s;User u;int userkind;if (ReadStudent(s)){if (ReadUser(u))system("pause");}else{return 0;}userkind = Menulog(u, s);if (userkind == 2)//管理员菜单{Menu(s);}else if (userkind == 1)//学生菜单{Menu2(s);}return 0;
}
stu.h:
#include <iostream>
#include <string.h>
#include <algorithm>
#include <functional>//函数对象相关操作
#include <sstream>//字符串流
#include <fstream>//文件流
#include <vector>//动态数组
using namespace std;class Stu
{
private:string snoa;string sname;string ssex;string sage;string snob;string sstatus;string slocal;string sdept;string sclass;string sgpa;string sevent;vector<Stu *> student;
//定义一个student向量存储Stu类型对象的地址
//例如增加一个元素:
//Stu* s=new Stu();//创建一个新Stu对象获取其地址
//student.push_back(s);//将这个地址添加到student向量中vector<Stu> student2;
//存储对象本身,向量长度不能变化
//以上两个一个存储地址向量长度,一个存储对象本身
public:Stu(){};//构造函数~Stu(){};//析构函数Stu(string noa, string name,string sex,string age,string nob,string status,string local,string dept,string sclasss,string gpa,string events);//带参数的构造函数void AddStudent(vector<string>st); //增加学生学籍信息void ShowAllInfo(); //打印全部学生学籍信息void ShowInfo(); //打印一个学生学籍信息bool operator()(Stu &other); //重载函数void SearchName(); //按姓名查找某学生学籍信息void SearchId(); //按学号查找某学生学籍信息void DeleteAllStudent(); //删除所有学生学籍信息void DeleteOneStudent(); //删除某个学生学籍信息void ChangeOneStudent(); //修改某个学生学籍信息void SortStudent(); //给学籍信息排序bool operator<(Stu &stu); //重载学生学号比较大小void StatisticsInfos(); //统计绩点void WriteToFile(string filename) ;//保存至本地};
user.h
#include <iostream>
#include <string>
using namespace std;class User
{
private:string sname;string spass;string skey;vector<User *> user;
//存储对象地址vector<User> user2;
public:User(){};~User(){};User(string name,string pass,string key);void AddUser(vector<string>use); //增加用户信息void ShowInfo();void ShowAllInfo();int SearchName(string name);int SearchPassword(string password,string name);string SearchKey(string name);
};
user.cpp
#include <string.h>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <vector>
#include "User.h"
#include <iostream>
using namespace std;User::User(string name,string pass,string key)//初始化
{sname = name;spass = pass;skey = key;
};void User::AddUser(vector<string>use)
{User use1(use[0],use[1],use[2]);//创建一个新的User对象user2.push_back(use1);//添加到user2向量中
}void User::ShowInfo()
{cout<<sname<<" "<<spass<<" "<<skey<<endl;}void User::ShowAllInfo()
{ vector<User>::iterator it;//声明一个迭代器cout<<"用户名 "<<"密码 "<<"秘钥 "<<endl;for(it=user2.begin();it!=user2.end();it++)//遍历所有元素{cout<<(*it).sname<<" "<<(*it).sname<<" "<<(*it).spass<<" "<<(*it).skey<<endl;}
}int User::SearchName(string name)
{int flag1=0;for(vector<User>::iterator it=user2.begin();it!=user2.end();it++){if((*it).sname==name){flag1=1;return 1;}}if(flag1==1){return 1;}else{return 0;}
}int User::SearchPassword(string password,string name)
{int flag1=0;for(vector<User>::iterator it=user2.begin();it!=user2.end();it++){if((*it).sname==name){if((*it).spass==password){flag1=1;}}}if(flag1==1){return 1;}else{return 0;}
}string User::SearchKey(string name)
{string key="无";//如果没有密匙则保留无for(vector<User>::iterator it=user2.begin();it!=user2.end();it++){if((*it).sname==name){key=(*it).skey;}}return key;}
stu.cpp
#include <string.h>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <vector>
#include "Stu.h"
#include <iostream>using namespace std;Stu::Stu(string noa, string name, string sex, string age, string nob, string status, string local, string dept, string sclasss, string gpa, string events)//初始化
{snoa = noa;sname = name;ssex = sex;sage = age;snob = nob;sstatus = status;slocal = local;sdept = dept;sclass = sclasss;sgpa = gpa;sevent = events;
}void Stu::AddStudent(vector<string>st)
{Stu stu1(st[0], st[1], st[2], st[3], st[4], st[5], st[6], st[7], st[8], st[9], st[10]);student2.push_back(stu1);//创建的stu对象添加到student2向量中
}//容器增加void Stu::ShowInfo()
{cout << snoa << " " << sname << " " << ssex << " " << sage << " " << snob << " " << sstatus << " " << slocal << " " << sdept << " " << sclass << " " << sgpa << " " << sevent << endl;}//一个学生学籍信息显示void Stu::ShowAllInfo()
{vector<Stu>::iterator it;cout << "学号 " << "姓名 " << "性别 " << "年龄 " << "身份证号 " << "政治面貌 " << "家庭住址 " << "院系 " << "班级 " << "绩点 " << "奖惩情况 " << endl;for (it = student2.begin(); it != student2.end(); it++){cout << (*it).snoa << " " << (*it).sname << " " << (*it).ssex << " " << (*it).sage << " " << (*it).snob << " " << (*it).sstatus << " " << (*it).slocal << " " << (*it).sdept << " " << (*it).sclass << " " << (*it).sgpa << " " << (*it).sevent << endl;}
}bool Stu::operator()(Stu& other) //重载(),使类的对象可以像函数一样调用
//例:stu1(stu2),两者相同则返回true
{bool flag;if (this->snoa == other.snoa){flag = true;}else{flag = false;}return flag;
}void Stu::SearchName()
{string name;int flag1 = 0;cout << "请输入学生姓名:" << endl;cin >> name;for (vector<Stu>::iterator it = student2.begin(); it != student2.end(); it++)
//迭代器本质上是一个指针,指向容器中的元素{if ((*it).sname == name){cout << "查询成功!" << endl;(*it).ShowInfo();flag1 = 1;}}if (flag1 == 0)cout << "该学生学籍不存在!" << endl;}
void Stu::SearchId()
{string id;int flag1 = 0;cout << "请输入学生学号:" << endl;cin >> id;for (vector<Stu>::iterator it = student2.begin(); it != student2.end(); it++){if ((*it).snoa == id){cout << "查询成功!" << endl;(*it).ShowInfo();flag1 = 1;}}if (flag1 == 0)cout << "该学生学籍不存在!" << endl;
}void Stu::DeleteAllStudent() //删除所有学生学籍信息
{student2.clear();
}void Stu::DeleteOneStudent() //删除一个学生学籍信息
{string id;int flag1 = 0;cout << "请输入学生学号:" << endl;cin >> id;for (vector<Stu>::iterator it = student2.begin(); it != student2.end(); it++){if ((*it).snoa == id){cout << "此条学籍数据删除成功!" << endl;it = student2.erase(it);//返回一个指向删除元素之后元素的迭代器,需要同步flag1 = 1;}else{++it;}}if (flag1 == 0)cout << "该学生学籍不存在!" << endl;
}
void Stu::ChangeOneStudent() //修改一个学生学籍信息
{string id;int flag1 = 0;cout << "请输入学生学号:" << endl;cin >> id;for (vector<Stu>::iterator it = student2.begin(); it != student2.end(); it++){if ((*it).snoa == id){cout << "此条学籍数据定位成功!" << endl;(*it).ShowInfo();cout << "请输入要修改的内容:1.学号 2.政治面貌 3.院系 4.班级 5.绩点 6.奖惩情况" << endl;int sel;cin >> sel;if (sel == 1){cout << "请输入修改后的学号:";string newinfo;cin >> newinfo;(*it).snoa = newinfo;cout << "修改成功!" << endl;(*it).ShowInfo();}else if (sel == 2){cout << "请输入修改后的政治面貌:";string newinfo;cin >> newinfo;(*it).sstatus = newinfo;cout << "修改成功!" << endl;(*it).ShowInfo();}else if (sel == 3){cout << "请输入修改后的院系:";string newinfo;cin >> newinfo;(*it).sdept = newinfo;cout << "修改成功!" << endl;(*it).ShowInfo();}else if (sel == 4){cout << "请输入修改后的班级:";string newinfo;cin >> newinfo;(*it).sclass = newinfo;cout << "修改成功!" << endl;(*it).ShowInfo();}else if (sel == 5){cout << "请输入修改后的绩点:";string newinfo;cin >> newinfo;(*it).sgpa = newinfo;cout << "修改成功!" << endl;(*it).ShowInfo();}else if (sel == 6){cout << "请输入修改后的奖惩情况:";string newinfo;cin >> newinfo;(*it).sevent = newinfo;cout << "修改成功!" << endl;(*it).ShowInfo();}else{cout << "选择错误,修改失败!" << endl;}flag1 = 1;}}if (flag1 == 0)cout << "该学生学籍不存在!" << endl;
}bool Stu::operator<(Stu& stu) //重载学生学号比较大小
{return snoa < stu.snoa;
}void Stu::SortStudent() //给学生学籍信息排序
{sort(student2.begin(), student2.end()); //sort算法,默认从小到大排,默认调用operator<()重载函数for (vector<Stu>::iterator it = student2.begin(); it != student2.end(); it++){(*it).ShowInfo();}cout << endl;
}void Stu::StatisticsInfos() //统计绩点
{float max = 0, min = 100, avg = 0, sum = 0, count = 0;for (vector<Stu>::iterator it = student2.begin(); it != student2.end(); it++){count++;if (atof((*it).sgpa.c_str()) > max)
//c_str():string转化为cstirng
//atof():cstring转化为浮点数{max = atof((*it).sgpa.c_str());}if (atof((*it).sgpa.c_str()) < min){min = atof((*it).sgpa.c_str());}sum = sum + atof((*it).sgpa.c_str());}avg = sum / count;cout << "学籍信息中学生的最高绩点为:" << max << endl;cout << "学籍信息中学生的最低绩点为:" << min << endl;cout << "学籍信息中学生的平均绩点为:" << avg << endl;}void Stu::WriteToFile(string filename)
{string file_path(filename);fstream outfile(file_path.c_str(), fstream::out);//输出for (vector<Stu>::iterator it = student2.begin(); it != student2.end(); it++){outfile << (*it).snoa << " " << (*it).sname << " " << (*it).ssex << " " << (*it).sage << " " << (*it).snob << " " << (*it).sstatus << " " << (*it).slocal << " " << (*it).sdept << " " << (*it).sclass << " " << (*it).sgpa << " " << (*it).sevent << endl;//按行写入}return;
}
效果图:
保存并退出: