学校C++结课课程设计(学生管理系统)

        题目:实现一个简单的信息管理系统,如成绩管理系统,学籍信息管理系统,超市管理系统,银行管理系统等,题目可以自定义。

        要求:

        1.功能完善,实现基本的业务功能(信息的增删查改,以及数据分析等),需要连接文本文档,不需要连接数据库。

        2.使用面向对象的程序设计思想实现,类中必须包含必要的数据成员,构造函数以及成员函数。

        3.使用到继承,派生,多态,文本文件操作。

一、需求分析

        用一个登录界面来区分管理员和学生登录,让学生可以查询自己的个人信息和修改自己的密码,而便于管理员管理学生的信息,并且可以看到学院的优秀率和平均绩点,将学生和管理员的权限分开。数据的规范有要求的输出到文本中。

二、功能设计   

        登录界面,学生登录界面,管理员登录界面。

        登录界面:登录功能,忘记密码功能。判断登录是否为学生。

        学生登录界面:学生自己的个人信息和修改密码功能。

        管理员登录界面:显示学生信息,添加学生信息,修改学生信息,查询学生信息,删除学生信息,学生信息汇总。

        部分功能设计:验证码登录确认,验证码为长度5的四个字母一个数字(显示问题将数字不为1和0)。判断身份证号为18位,前17位纯数字,最后一位为x(X)或者数字。判断学号要求纯数字13位。判断绩点在0到5之间。每次添加删除数据后,数据以相同学院及绩点高低排序后显示。学院平均绩点,学院人数,优秀率。数据文件的读取,分析,写出数据。

三、对象(类)设计

        

四、编码与测试

        1.初始的管理员用户名为admin 密码为123456(代码中的主函数创建的管理员类对象)

        2.原始数据.txt是指最初数据的文本文档

        3.Test.txt是程序运行时读取数据的文本文档(与原始数据一致)

        4.LastDataValue .txt是退出程序后保存学生数据的文本文档

        5.analyzeData.txt是数据分析的结果的文本文档

        6.代码.txt是程序代码的文本文档

        7.可以将读取目标文件(Test.txt)和写出最后数据的文档(LastDataValue.txt)指向同一文件,就可以满足每次的增删查改可以保存到下一次运行

        8.所用的文本文件编码形式都为:ANSI(以免出现乱码情况)此代码我是用codeblocks20.03版本编写,若要用其他编译器需要自己修改代码

        ps:有什么意见或者问题可以留言或者私聊我,我会想办法帮你解决。

        以下是运行文件(需要自己创建相应的文本文档):

         

代码演示:

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <time.h>
#include <fstream>
using namespace std;
//用户类
class User{
public:
    User(){
    }
    User(string name,string num ,string passWord, string idCard){
       this->name = name;
       this->num = num;
       this->passWord = passWord;
       this->idCard = idCard;
    }
    string getName(){
        return name;
    }
    void setName(string name){
        this->name = name;
    }
    string getNum(){
        return num;
    }
    void setNum(string num){
        this->num = num;
    }
    string getPassWord(){
        return passWord;
    }
    void setPassWord(string passWord){
        this->passWord = passWord;
    }
    string getIdCard(){
        return idCard;
    }
    void setIdCard(string idCard){
        this->idCard = idCard;
    }
    virtual void display()=0;
protected:
    string name;
    string num;
    string passWord;
    string idCard;
};
//学生类
class Student:public User{
public:
    Student(){
    }
    Student(string name,string num,string passWord,string idCard,string college,string major,double point):User(name,num,passWord,idCard){
        this->college = college;
        this->major = major;
        this->point = point;
    }
    string getCollege(){
        return college;
    }
    void setCollege(string college){
        this->college = college;
    }
    string getMajor(){
        return major;
    }
    void setMajor(string major){
        this->major = major;
    }
    double getPoint(){
        return point;
    }
    void setPoint(double point){
        this->point = point;
    }
    void display();
private:
    string college;
    string major;//专业
    double point;
};
void Student::display(){
    cout<<"|"<<name<<"\t"<<num<<"\t"<<college<<"\t"<<major<<"  "<<idCard<<"\t";
    cout.setf(ios::fixed);
    cout.precision(2);
    cout<<point<<"|"<<endl;
}
//管理员类
class Admin:public User{
public:
    Admin(){
    }
    Admin(string name,string num,string passWord,string idCard,string phoneNum):User(name,num,passWord,idCard){
        this->phoneNum = phoneNum;
    }
    string getPhoneNum(){
        return phoneNum;
    }
    void setPhoneNum(string phoneNum){
        this->phoneNum = phoneNum;
    }
    void display(){}
    void display(vector<Student> students){
        for(unsigned i = 0; i < students.size(); i++){
            students[i].display();
        }
    }
private:
    string phoneNum;
};
vector<Student> swapStudent(vector<Student> student,int a,int b){//交换vector<Student>中对象的位置
    Student temp = student[a];
    student[a] = student[b];
    student[b] = temp;
    return student;
}
vector<Student> sortVector(vector<Student> sortStudent){//选择排序
    int size = (int)sortStudent.size();
    if(size < 1){
        return sortStudent;
    }
    for(int i = 0; i < size - 1; i++){
        int minIndex = i;
        for(int j = i + 1; j < size; j++){
            minIndex = sortStudent[minIndex].getPoint() < sortStudent[j].getPoint() ? j : minIndex;
        }
        sortStudent = swapStudent(sortStudent,i,minIndex);
    }
    return sortStudent;
}
vector<Student> sortStudents(vector<Student> student){
    if(student.empty()){
        return student;
    }
    //学院排序
    int col = 1;//指向当前学院的最后一个对象的后一个位置
    int index = 1;//遍历标识
    //col - 1始终指向当前学院最后一个对象的位置
    //col 始终指向当前学院最后一个对象的后一个位置 作为交换边界
    //index 来寻找相等学院 若相等 交换index和col 并且都后走一步 否则index自身++
    while(col != (int)student.size()){
        if(student[col - 1].getCollege() == student[index].getCollege()){
            student = swapStudent(student,col,index);
            col++;
            index++;
        }else{
            index++;
        }
        if(index >= (int)student.size() && col != (int)student.size()){//防止越界
            col++;
            index = col;
        }
    }
    //在学院排序好之后绩点排序
    vector<Student> sortStudent;
    sortStudent.push_back(student[0]);
    int lastCollege = 0;//最后相同学院的下标值
    int indexs = 1;
    while(indexs != (int)student.size()){
        if(student[indexs - 1].getCollege() != student[indexs].getCollege()){//在学院不相等的情况下 清空sortStudet
            sortStudent.clear();
            lastCollege = indexs;
        }

        sortStudent.push_back(student[indexs]);
        sortStudent = sortVector(sortStudent);
        for(int i = 0, j = lastCollege; i < (int)sortStudent.size(); i++, j++){//排序返回到Student容器
            student[j] = sortStudent[i];
        }
        indexs++;
    }
    return student;
}

void printNowStudent(vector<Student> students, int now){//学生界面-=-当前学生信息
    cout<<"|***************************学生信息如下************************************|"<<endl;
    cout<<"|姓名\t学号\t\t所属学院\t专业班级  身份证\t\t绩点|"<<endl;
    students[now].display();
}
void judgeReturn(){//退出设置
    string c;
    cout<<"输入0返回:";
    cin>>c;
    while(c != "0"){
        cout<<"输入0返回:";
        cin>>c;
    }
    cout<<"**********************************"<<endl;
}
bool verification(){//随机验证码
    //生成1数字4字母的随机验证码
    char ch1[52] = {};//字母字符数组
    char ch2[8] = {};//数字字符数组//除去1和0
    for(int i = 2; i < 10; i++){
        ch2[i - 2] = (char)(48 + i);
    }
    for(int i = 0; i < 26; i++){
        ch1[i] = (char)(65 + i);
    }
    for(int i = 26; i < 52; i++){
        ch1[i] = (char)(97 + i - 26);
    }
    //生产纯字母的验证码
    srand((unsigned int)time(NULL));
    while(true){
        string str = "";
        for(int i = 0; i < 5; i++){
            str += ch1[rand()%52];
        }
        string temp;
        int j = rand()%5;//随机替换位置
        char num = ch2[rand()%8];//随机替换的数字
        string str1 = "";
        str1 += num;
        str.replace(j, str1.length(),str1);
        cout<<"验证码(区分大小写):"<<str<<endl;
        cout<<"请输入验证码:";
        cin>>temp;
        if(temp == str){
            return true;
        }else{
            cout<<"验证码错误!"<<endl;
        }
    }
}
//判断学生学号是否存在
bool repeat(vector<Student> students, string num){
    for(unsigned i = 0; i < students.size(); i++){
        if(students[i].getNum() == num){
            return false;
        }
    }
    return true;
}
int repeatNum(vector<Student> students, string num){//查找当前学生位置
    for(unsigned i = 0; i < students.size(); i++){
        if(students[i].getNum() == num){
                return i;
        }
    }
    return -1;
}
int repeatVtNum(vector <Student> students, string num){//查找当前学生容器中的位置
    int vt = 0;
    for(unsigned i = 0; i < students.size(); i++){
        if(num == students[i].getNum()){
            return vt;
        }
        vt++;
    }
    return -1;
}
vector<Student> studentRevamp(vector<Student> students,int now){//学生修改自己的密码
    while(true){
        string newPassWord1;
        string newPassWord2;
        cout<<"请输入新密码:";
        cin>>newPassWord1;
        cout<<"请再次输入密码:";
        cin>>newPassWord2;
        if(newPassWord1 == newPassWord2){
            students[now].setPassWord(newPassWord1);
            cout<<"修改成功!"<<endl;
            break;
        }else{
            cout<<"两次输入密码不一致!请重试!"<<endl;
        }
    }
    return students;
}
vector<Student> studentMenu(vector<Student> students,int nowStudent){//学生菜单界面
    cout<<"|*******************************您的信息如下:************************************|"<<endl;
    cout<<"|姓名\t学号\t\t所属学院\t专业班级  身份证\t\t绩点|"<<endl;
    students[nowStudent].display();
    string x;
    while(true){
        cout<<"是否修改密码(Y/N):";
        cin>>x;
        if(x == "Y"){
            students = studentRevamp(students,nowStudent);
            break;
        }else if(x=="N"){
            break;
        }else{
            cout<<"输入错误!"<<endl;
        }
    }
    return students;
}
void print(vector<Student> students){//管理员菜单-=-显示学生信息
    if(students.empty()){
        cout<<"暂无学生信息!"<<endl;
    }else{
        cout<<"|---------------------------显示学生信息------------------------------------|"<<endl;
        cout<<"|***************************学生信息如下************************************|"<<endl;
        cout<<"|姓名\t学号\t\t所属学院\t专业班级  身份证\t\t绩点|"<<endl;
        //cout<<students.size()<<endl;
        for(unsigned i = 0; i < students.size(); i++){
            students[i].display();
        }
        cout<<"|---------------------------------------------------------------------------|"<<endl;
    }
    judgeReturn();
}
//判断学号是否符合要求13位
bool limitNum(string num){
    if((int)num.length() != 13){
        return false;
    }
    char arr[10] = {};
    for(int i = 0; i < 10; i++){
        arr[i] = (char)(48 + i);
    }
    int k = 0;
    for(int i = 0;i < 13; i++){
        for(int j = 0; j < 10; j++){
            if(num.at(i) == arr[j])
                k++;
        }
    }
    if(k != 13){
        return false;
    }else{
        return true;
    }
}
//确定是18位密码 首位不为0 中间只能是数字 末尾可以是数字也可以是xX
bool judgeId(string idCard){
    char arr[10] = {};
    for(int i = 0; i < 10; i++){
        arr[i] = (char)(48 + i);
    }
    int k = 0;
    for(int i = 0;i < (int)idCard.length(); i++){
        for(int j = 0; j < 10; j++){
            if(idCard.at(i) == arr[j])
                k++;
        }
    }
    if(idCard.at(17) == 'x' || idCard.at(17) == 'X'){
        k++;
    }
    if(k == 18){
        return true;
    }else{
        return false;
    }
}
string limitId(){//身份证确认
    while(true){
        string str;
        cout<<"请输入身份证号:";
        cin>>str;
        if(str.length() == 18 && str.at(0) != '0' && judgeId(str)){
            return str;
        }else{
            cout<<"身份证号不符合要求!"<<endl;
        }
    }
}
vector<Student> add(vector<Student> students){//管理员菜单-=-添加学生信息
    cout<<"|**********添加学生信息**********|"<<endl;
    string num;
    cout<<"请输入添加学生学号:";
    cin>>num;
    while(!limitNum(num)){
        cout<<"学号格式错误!请重新输入:";
        cin>>num;
    }
    if(repeat(students,num)){
        string name;
        cout<<"请输入姓名:";
        cin>>name;
        string college;
        cout<<"请输入所属院系:";
        cin>>college;
        string major;
        cout<<"请输入所属专业班级:";
        cin>>major;
        string idCard;
        idCard = limitId();
        //默认密码身份证后八位
        string passWord = "";
        for(int i = 10; i < (int)idCard.length(); i++){//无符号类型有可能溢出
            passWord += idCard.at(i);
        }
        cout<<"默认密码为:"<<passWord<<endl;
        double point;
        cout<<"请输入绩点:";
        cin>>point;
        while(point < 0 && point > 5){
                cout<<"输入绩点错误!请重新输入:";
                cin>>point;
        }
        Student s(name,num,passWord,idCard,college,major,point);
        students.insert(students.end(),s);
        cout<<"添加成功!"<<endl;
    }else{
        cout<<"学号此学号已存在!"<<endl;
    }
    judgeReturn();
    return students;
}
vector<Student> revampStudent(vector<Student> students){//管理员菜单-=-修改学生信息
    cout<<"|**********修改学生信息**********|"<<endl;
    if(students.empty()){
        cout<<"暂无学生信息!"<<endl;
        judgeReturn();
        return students;
    }
    cout<<"请输入修改学生学号:";
    string num;
    cin>>num;
    int now = repeatNum(students,num);
    if(now == -1){
        cout<<"此学号不存在!"<<endl;
    }else{
        printNowStudent(students,now);
        cout<<"选择修改内容(标签):";
        string x;
        cin>>x;
        while(x != "姓名" && x != "学号" && x != "所属学院" && x != "专业班级" && x != "身份证" && x != "绩点" && x != "重置密码"){
            cout<<"输入错误!请重新输入:";
            cin>>x;
        }
        if(x == "姓名"){
            string name;
            cout<<"请输入修改后姓名:";
            cin>>name;
            students[now].setName(name);
        }else if(x == "学号"){
            string num;
            cout<<"请输入修改后学号:";
            cin>>num;
            while(!repeat(students,num) || !limitNum(num)){
                cout<<"学号重复或者学号格式错误!请重新输入:";
                cin>>num;
            }
            students[now].setNum(num);
        }else if(x == "所属学院"){//修改学院要修改专业班级
            string college;
            cout<<"请输入修改后所属学院:";
            cin>>college;
            students[now].setCollege(college);
            string major;
            cout<<"请输入修改后所属专业班级:";
            cin>>major;
            students[now].setMajor(major);
        }else if(x == "专业班级"){
            string major;
            cout<<"请输入修改后专业班级:";
            cin>>major;
            students[now].setMajor(major);
        }else if(x == "身份证"){
            string idCard;
            idCard = limitId();
            students[now].setIdCard(idCard);
        }else if(x == "绩点"){
            double point;
            cout<<"请输入修改后绩点:";
            cin>>point;
            while(point < 0 && point > 5){
                cout<<"输入绩点错误!请重新输入:";
                cin>>point;
            }
            students[now].setPoint(point);
        }else{
            string passWord = "";
            for(int i = 10; i < 18; i++){
                passWord += students[now].getIdCard().at(i);
            }
            cout<<"重置密码为:"<<passWord<<endl;
            students[now].setPassWord(passWord);
        }
        cout<<"修改成功!"<<endl;
    }
    judgeReturn();
    return students;
}
void queryStudent(vector<Student> students){//管理员菜单-=-查询学生信息
    cout<<"|**********查询学生信息**********|"<<endl;
    if(students.empty()){
        cout<<"暂无学生信息!"<<endl;
        judgeReturn();
        return;
    }
    string num;
    cout<<"请输入查询学生学号:";
    cin>>num;
    int now = repeatNum(students,num);
    if(now != -1){
        printNowStudent(students,now);
    }else{
        cout<<"此学号不存在!"<<endl;
    }
    judgeReturn();
}
vector<Student> deleteStudent(vector<Student> students){//管理员菜单-=-删除学生信息
    cout<<"|**********删除学生信息**********|"<<endl;
    if(students.empty()){
        cout<<"暂无学生信息!"<<endl;
        judgeReturn();
        return students;
    }
    string num;
    cout<<"请输入删除学生的学号:";
    cin>>num;
    int now = repeatNum(students, num);
    int vt = repeatVtNum(students, num);//在容器中的位置
    if(vt != -1){
        printNowStudent(students, now);
        string x;
        cout<<"确认是否删除(Y/N):";
        cin>>x;
        while(x != "Y" && x != "N"){
            cout<<"输入错误!请重新输入:";
            cin>>x;
        }
        if(x == "Y"){
            students.erase(students.begin() + vt);
            cout<<"删除成功!"<<endl;
        }else{
            cout<<"取消删除!"<<endl;
        }
        }else{
            cout<<"学号不存在!"<<endl;
        }
    judgeReturn();
    return students;
}
void foutAnalyseData(vector<Student> student){//数据分析的写入
    if(student.empty()){
        return;
    }
    vector<string> sumCollege;//学院
    vector<int> collegeNum;//学院人数
    vector<double> avgPoint;//绩点累加
    vector<int> qualifiedNum;//优秀人数
    int numIndex = 0;//指向第一个出现的学院 学院唯一
    //添加第一个数据对象
    sumCollege.push_back(student[numIndex].getCollege());
    collegeNum.push_back(1);
    avgPoint.push_back(student[numIndex].getPoint());
    qualifiedNum.push_back(0);
    if(student[numIndex].getPoint() >= 3.0){
        qualifiedNum[numIndex]++;
    }
    int index = 1;
    while(index < (int)student.size()){//开始遍历
        if(sumCollege[numIndex] == student[index].getCollege()){
            collegeNum[numIndex]++;//相同学院 人数+1
            avgPoint[numIndex] += student[index].getPoint();//绩点累加
        }else{
            sumCollege.push_back(student[index].getCollege());//不同学院 添加学院
            numIndex++;//学院数+1
            collegeNum.push_back(1);//将该学院人数设置为1
            avgPoint.push_back(student[index].getPoint());//添加绩点
            qualifiedNum.push_back(0);//将优秀人数设置为0
        }
        if(student[index].getPoint() >= 3.0){//判断是否优秀
            qualifiedNum[numIndex]++;
        }
        index++;
    }
    ofstream outf("analyzeData.txt");
    for(int i = 0; i < (int)sumCollege.size(); i++){
        outf<<sumCollege[i]<<" "<<collegeNum[i]<<"人 "<<"平均绩点:";
        outf.setf(ios::fixed);
        outf.precision(2);
        outf<<avgPoint[i]/collegeNum[i]<<" 优秀率:"<<((double)qualifiedNum[i]/collegeNum[i])*100<<"%"<<endl;
    }
    outf.close();
}
//各学院绩点平均值
void analyse(vector<Student> student){//管理员信息-=-学生数据分析
    cout<<"|**********学生数据分析**********|"<<endl;
    if(student.empty()){
        cout<<"暂无学生信息!"<<endl;
        judgeReturn();
        return;
    }
    vector<string> sumCollege;//学院
    vector<int> collegeNum;//学院人数
    vector<double> avgPoint;//绩点累加
    vector<int> qualifiedNum;//优秀人数
    int numIndex = 0;//指向第一个出现的学院 学院唯一
    //添加第一个数据对象
    sumCollege.push_back(student[numIndex].getCollege());
    collegeNum.push_back(1);
    avgPoint.push_back(student[numIndex].getPoint());
    qualifiedNum.push_back(0);
    if(student[numIndex].getPoint() >= 3.0){
        qualifiedNum[numIndex]++;
    }
    int index = 1;
    while(index < (int)student.size()){//开始遍历
        if(sumCollege[numIndex] == student[index].getCollege()){
            collegeNum[numIndex]++;//相同学院 人数+1
            avgPoint[numIndex] += student[index].getPoint();//绩点累加
        }else{
            sumCollege.push_back(student[index].getCollege());//不同学院 添加学院
            numIndex++;//学院数+1
            collegeNum.push_back(1);//将该学院人数设置为1
            avgPoint.push_back(student[index].getPoint());//添加绩点
            qualifiedNum.push_back(0);//将优秀人数设置为0
        }
        if(student[index].getPoint() >= 3.0){//判断是否优秀
            qualifiedNum[numIndex]++;
        }
        index++;
        }
    for(int i = 0; i < (int)sumCollege.size(); i++){
        cout<<sumCollege[i]<<" "<<collegeNum[i]<<"人 "<<"平均绩点:";
        cout.setf(ios::fixed);
        cout.precision(2);
        cout<<avgPoint[i]/collegeNum[i]<<" 优秀率:"<<((double)qualifiedNum[i]/collegeNum[i])*100<<"%"<<endl;
    }
    ofstream outf("analyzeData.txt");
    for(int i = 0; i < (int)sumCollege.size(); i++){
        outf<<sumCollege[i]<<" "<<collegeNum[i]<<"人 "<<"平均绩点:";
        outf.setf(ios::fixed);
        outf.precision(2);
        outf<<avgPoint[i]/collegeNum[i]<<" 优秀率:"<<((double)qualifiedNum[i]/collegeNum[i])*100<<"%"<<endl;
    }
    outf.close();
    judgeReturn();
}

vector<Student> adminMenu(vector<Student> students){//管理员菜单
    students = sortStudents(students);
    cout<<"|***********管理员菜单***********|"<<endl;
    cout<<"|*--------1.显示学生信息--------*|"<<endl;
    cout<<"|*--------2.添加学生信息--------*|"<<endl;
    cout<<"|*--------3.修改学生信息--------*|"<<endl;
    cout<<"|*--------4.查询学生信息--------*|"<<endl;
    cout<<"|*--------5.删除学生信息--------*|"<<endl;
    cout<<"|*--------6.学生数据汇总--------*|"<<endl;
    cout<<"|*------------7.退出------------*|"<<endl;
    string x;
    cout<<" 请选择操作:";
    cin>>x;
    while(x != "1" && x != "2" && x != "3" && x != "4" && x != "5" && x != "6" && x != "7"){
        cout<<"输入错误!请重新输入:";
        cin>>x;
    }
    if(x == "1"){
        print(students);//显示学生信息
        students = adminMenu(students);
    }else if(x == "2"){
        students = add(students);//添加学生信息
        students = adminMenu(students);
    }else if(x == "3"){
        students = revampStudent(students);//修改学生信息
        students = adminMenu(students);
    }else if(x == "4"){
        queryStudent(students);//查询学生信息
        students = adminMenu(students);
    }else if(x == "5"){
        students = deleteStudent(students);//删除学生信息
        students = adminMenu(students);
    }else if(x == "6"){
        analyse(students);//数据汇总
        students = adminMenu(students);
    }else{
        foutAnalyseData(students);//将数据写入到目标文本文件
        cout<<"退出成功!"<<endl;
    }
    return students;
}
vector<Student> adminPassWord(vector<Student> students,vector<Admin> admins,int nowAdmin){//管理员密码验证
    string passWord;
    cout<<"请输入密码:";
    cin>>passWord;
    verification();
    if(admins[nowAdmin].getPassWord() == passWord){
        cout<<"登录成功!"<<endl;
        students = adminMenu(students);
    }else{
        cout<<"用户名或密码错误!登陆失败!"<<endl;
    }
    return students;
}
vector<Student> studentPassWord(vector<Student> students,string num){//学生密码验证
    int nowStudent = repeatNum(students,num);
    string passWord;
    cout<<"请输入密码:";
    cin>>passWord;
    verification();
    if(nowStudent != -1 && students[nowStudent].getPassWord() == passWord){
        cout<<"登录成功!"<<endl;
        students = studentMenu(students,nowStudent);
    }else{
        cout<<"用户名或密码错误!登录失败!"<<endl;
    }
    return students;
}
vector<Student> loginMenu(vector<Student> students,vector<Admin> admins){//用户登录界面
    cout<<"|***********用户登录*************|"<<endl;
    string str;
    cout<<"请输入用户名:";
    cin>>str;
    //先判断是否为管理员
    int loginChooes = -1;
    for(unsigned i = 0; i < admins.size(); i++){
        if(str == admins[i].getNum()){
            loginChooes = i;
            break;
        }
    }
    if(loginChooes != -1){
        students = adminPassWord(students,admins,loginChooes);
    }else{
        students = studentPassWord(students,str);
        judgeReturn();
    }
    return students;
}
vector<Student> forgetPassWord(vector<Student> students){//忘记密码
    cout<<"|***********忘记密码*************|"<<endl;
    cout<<"请输入学号:";
    string num;
    cin>>num;
    cout<<"请输入身份证号:";
    string idCard;
    cin>>idCard;
    int nowNum = repeatNum(students,num);
    if(nowNum != -1 && idCard == students[nowNum].getIdCard()){
        students = studentRevamp(students,nowNum);
    }else{
        cout<<"学号或身份证号错误!"<<endl;
    }
    judgeReturn();
    return students;
}
void outFile(vector<Student> students){//数据写出
    ofstream fout("LastDateValue.txt");
    //ofstream fout("Test.txt");
    if(!fout){
        cout<<"打开文件失败!"<<endl;
        abort();
    }
    for(int i = 0; i < (int)students.size(); i++){
        fout<<students[i].getName()<<" "<<students[i].getNum()<<" "<<students[i].getPassWord()<<" "<<students[i].getIdCard()<<" "<<students[i].getCollege()<<" "<<students[i].getMajor()<<" ";
        fout.setf(ios::fixed);
        fout.precision(2);
        fout<<students[i].getPoint()<<endl;
    }
    fout.close();
}
vector<Student> menu(vector<Student> students,vector<Admin> admins){//菜单
    string x;
    cout<<"|================================|"<<endl;
    cout<<"|=----欢迎到学生信息管理系统----=|"<<endl;
    cout<<"|=-----------1.登录-------------=|"<<endl;
    cout<<"|=---------2.忘记密码-----------=|"<<endl;
    cout<<"|=-----------3.退出-------------=|"<<endl;
    cout<<"|================================|"<<endl;
    cout<<"   请选择操作:";
    cin>>x;
    while(x != "1" && x != "2" && x != "3"){
        cout<<"操作错误!请选择操作:";
        cin>>x;
    }
    if(x == "1"){
        students = loginMenu(students,admins);
        students = menu(students,admins);
    }else if(x == "2"){
        students = forgetPassWord(students);
        students = menu(students,admins);
    }else{
        cout<<"   退出成功!"<<endl;
    }
    return students;
}
vector<Student> fileData(vector<Student> students){//写入数据
    ifstream fin("Test.txt");
    if(!fin){
        cout<<"文件打开失败!"<<endl;
        abort();
    }
    vector<string> FileData;
    while(!fin.eof()){
        string str;
        fin>>str;
        FileData.push_back(str);
    }
    if(!FileData.empty()){
        int indexF = 0;
        while(indexF + 6 < (int)FileData.size()){
            string name = FileData[indexF++];
            string num = FileData[indexF++];
            string passWord = FileData[indexF++];
            string idCard = FileData[indexF++];
            string college = FileData[indexF++];
            string major = FileData[indexF++];
            double point = atof(FileData[indexF++].c_str());//将string类型转化成double类型
            Student s(name,num,passWord,idCard,college,major,point);
            students.push_back(s);
        }
    }
    fin.close();
    return students;
}

int main()
{
    vector<Student> students;//学生
    students = fileData(students);//打开目标文本文件读取数据
    vector<Admin> admins;//管理员
    Admin a("admin","admin","123456","420115200203170056","18827664695");//为了方便学号设置为amdin
    admins.push_back(a);
    students = sortStudents(students);//将数据按照学院顺序和绩点高低来排列
    students = menu(students,admins);//进入菜单
    outFile(students);//写出数据
    foutAnalyseData(students);//将分析数据写入到目标文本文件
    return 0;
}
#include "iostream" #include "fstream" #include "iomanip" #include "string" using namespace std; bool equal(char *a,char *b,int n)//判断两个字符串是否一样 { int temp=0; for(int i=0;i<n;i++) if(a[i]==b[i]) temp++; if(temp==n) return 1; else return 0; } int getline(int n)//求文件中信息的行数(既文件中学生的数量,课程的数量) n为5表示为求A.txt中的行数,n为7表示求B.txt中的行数 { int count=0; char temp[20]; char temp1[20]; for(int i=0;i<20;i++) { temp1[i]=0; } ifstream f1; if(n==5) { f1.open("A.txt"); } if(n==7) { f1.open("B.txt"); } while(f1.get()) { if(!equal(temp,temp1,20)) { for(int i=0;i>temp; count++; } else break; } f1.close(); return count/n; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// struct binfo//学生基本信息 { char num[8],name[8],sex[8],flatnum[4],telenum[11];//学号,姓名,宿舍号,电话号码 }; struct sinfo//学生课程信息 { char num[8],jectnum[8],jectname[20];//学号,课程编号,课程名称 double xuefen,usualscore,labscore,testscore,comscore,turexuefen;//学分,平时成绩,实验成绩,卷面成绩,综合成绩,实得学分 double subxufen,subscore; }; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class studentb { friend ostream &operator;<>(istream &is;,studentb &st;); public: studentb(); void dele();//删除信息 void displayb1(ostream &os;);//查询基本信息 void displayb(ostream &os;); void displaypart(); void inputb(istream &is;); char* getnum(); char* getname(); private: binfo b;//基本信息 }; void studentb::displaypart() { cout<<"学号:"<<b.num<<'\t'<<"姓名:"<<b.name<<endl; } studentb::studentb() { } void studentb::displayb(ostream &os;) { os<<b.num<<'\t'<<b.name<<'\t'<<b.sex<<'\t'<<b.flatnum<<'\t'<<b.telenum<<endl; } void studentb::displayb1(ostream &os;) { os<<"学号:"<<b.num<<" "<<"姓名:"<<b.name<<" "<<"性别:"<<b.sex<<" "<<"宿舍号:"<<b.flatnum<<" "<<"电话号码:"<<b.telenum<>b.num>>b.name>>b.sex>>b.flatnum>>b.telenum; } ///////////////////////////////////////////////////////////////////////////////////////// class students { public: students(){}; void inputs(istream &is;); void displays(); void displays1(); void displays(ostream &os;); void inputsubxuefen(double x); void inputsubscore(double x); char *getnum(); double getxuefen(); double getcomscore(); void cal(); private: sinfo s; }; void students::inputsubscore(double x) { s.subscore=x; } void students::inputsubxuefen(double x) { s.subxufen=x; } double students::getcomscore() { return s.comscore; } double students::getxuefen() { return s.turexuefen; } void students::cal() { if(s.labscore==-1) s.comscore=s.usualscore*0.3+s.testscore*0.7; else s.comscore=s.usualscore*0.15+s.labscore*0.15+s.testscore*0.7; if(s.comscore<60) s.turexuefen=0; else s.turexuefen=s.xuefen; } char* students::getnum() { return s.num; } void students::inputs(istream &is;) { if(is==cin) cout<<"请输入学号,课程编号,课程名称,学分,平时成绩,实验成绩,卷面成绩:"<>s.num>>s.jectnum>>s.jectname>>s.xuefen>>s.usualscore>>s.labscore>>s.testscore; cal(); } void students::displays1() { cout<<"学号:"<<s.num<<" "<<"课程编号:"<<s.jectnum<<'\t'<<"课程名称:"<<s.jectname<<'\t'<<"综合成绩:"<<s.comscore<<'\t'<<"实得学分:"<<s.turexuefen<<endl; } void students::displays() { cout<<"课程编号:"<<s.jectnum<<'\t'<<"课程名称:"<<s.jectname<<'\t'<<"综合成绩:"<<s.comscore<<'\t'<<"实得学分:"<<s.turexuefen<<endl; } void students::displays(ostream &os;) { os<<s.num<<'\t'<<s.jectnum<<'\t'<<s.jectname<<'\t'<<s.xuefen<<'\t'<<s.usualscore<<'\t'<<s.labscore<<'\t'<<s.testscore<<endl; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class allstudentb { public: allstudentb(int n) { studentnum=n; sptr=new studentb [studentnum]; ifstream f("A.txt"); for(int i=0;i<studentnum;i++) sptr[i].inputb(f); f.close(); } ~allstudentb(){delete [] sptr;} void searchb(char *ch);//查询基本信息 void searchpart(char *ch); void deleb(char *ch);//删除信息 void sort();//信息排序 private: studentb *sptr; int studentnum; }; void allstudentb::deleb(char *ch) { int temp=strlen(ch); ofstream f("A.txt"); for(int i=0;i<studentnum;i++) { if(!equal(ch,sptr[i].getnum(),temp)&&!equal(ch,sptr[i].getname(),temp)) sptr[i].displayb(f); } f.close(); } void allstudentb::searchb(char *ch) { int temp=strlen(ch); for(int i=0;i<studentnum;i++) if(equal(ch,sptr[i].getnum(),temp)||equal(ch,sptr[i].getname(),temp)) sptr[i].displayb1(cout); } void allstudentb::searchpart(char *ch) { int temp=strlen(ch); for(int i=0;i<studentnum;i++) if(equal(ch,sptr[i].getnum(),temp)||equal(ch,sptr[i].getname(),temp)) sptr[i].displaypart(); } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class allstudents { public: allstudents(int n){ num=n; ifstream f("B.txt"); sptr=new students [n]; for(int i=0;i<n;i++) sptr[i].inputs(f); f.close(); } ~allstudents(){delete [] sptr;} void searchs(char *ch); void inputs(); void displays(); void deles(char *ch); void sortxuefen(int n); void sortcomscore(int n); void print();//在屏幕上显示信息 private: students *sptr; int num; }; void allstudents::print() { for(int i=0;i<num;i++) sptr[i].displays1(); } void allstudents::sortxuefen(int n) { students temp; int i,j; if(n==0) { for(i=1;i<num;i++) for(j=0;j<num-i;j++) if(sptr[j].getxuefen()<sptr[j+1].getxuefen())//降序 { temp=sptr[j]; sptr[j]=sptr[j+1]; sptr[j+1]=temp; } } else { for(i=1;i<num;i++) for(j=0;jsptr[j+1].getxuefen())//升序 { temp=sptr[j]; sptr[j]=sptr[j+1]; sptr[j+1]=temp; } } displays(); } void allstudents::sortcomscore(int n) { int i,j; students temp; if(n==0) { for(i=1;i<num;i++) for(j=0;j<num-i;j++) if(sptr[j].getcomscore()<sptr[j+1].getcomscore())//降序 { temp=sptr[j]; sptr[j]=sptr[j+1]; sptr[j+1]=temp; } } else { for(i=1;i<num;i++) for(j=0;jsptr[j+1].getcomscore())//升序 { temp=sptr[j]; sptr[j]=sptr[j+1]; sptr[j+1]=temp; } } displays(); } void allstudents::deles(char *ch) { int temp=strlen(ch); ofstream f("B.txt"); for(int i=0;i<num;i++) { if(!equal(ch,sptr[i].getnum(),temp)) sptr[i].displays(f); } f.close(); } void allstudents::searchs(char *ch) { int count=0; double subxuefen=0; double subscore=0; int temp=strlen(ch); for(int i=0;i<num;i++) { if(equal(ch,sptr[i].getnum(),temp)) { sptr[i].displays(); count++; subxuefen=subxuefen+sptr[i].getxuefen(); subscore=subscore+sptr[i].getcomscore(); } sptr[i].inputsubscore(subscore); sptr[i].inputsubxuefen(subxuefen); } cout<<"共修:"<<count<<"科 实得总学分为:"<<subxuefen<<endl; } void allstudents::inputs() { students *p=new students [num]; for(int i=0;i<num;i++) p[i]=sptr[i]; delete []sptr; num++; sptr =new students [num]; for(int i=0;i<num-1;i++) sptr[i]=p[i]; sptr[num-1].inputs(cin); displays(); } void allstudents::displays() { ofstream f("B.txt"); for(int i=0;i<num;i++) sptr[i].displays(f); f.close(); } int main() { int m,n; m=getline(5);//学生数 n=getline(7); allstudentb stb(m); allstudents sts(n); int *score=new int[m]; int *xuefen=new int[m]; int x; char ch[8]; while(1) { cout<<"\n\n\n" <<"\t\t※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n" <<"\t\t※ ※\n" <<"\t\t※ 学生管理系统 ※\n" <<"\t\t※ ※\n" <<"\t\t※ 操作: ※\n" <<"\t\t※ 1--查询学生基本信息 ※\n" <<"\t\t※ 2--查询学生课程信息 ※\n" <<"\t\t※ 3--录入学生课程信息 ※\n" <<"\t\t※ 4--对学生信息进行排序 ※\n" <<"\t\t※ 5--删除学生信息 ※\n" <<"\t\t※ 6--退出 ※\n" <<"\t\t※ ※\n" <<"\t\t※ ※\n" <<"\t\t※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n"<<endl; cout<>x; switch(x) { case 1: { if(m!=0) { cout<>ch; cout<<endl; cout<<endl; stb.searchb(ch); } else { cout<<"无学生基本信息可查!请录入学生信息!!"<<endl; } cout<<endl; cout<<endl; }break; case 2: { cout<>ch; stb.searchpart(ch);//查询学号为ch的学生的基本信息 sts.searchs(ch);//查询学号为ch的学生的所有课程信息 }break; case 3: { int temp1,count=0; cout<>temp1; while(count<temp1) { sts.inputs(); count++; } }break; case 4: { cout<<endl; cout<<endl; cout<<"1--按学分排序"<<endl; cout<<"2--按综合成绩排序"<<endl; cout<>temp1; if(temp1==1) { cout<<endl; cout<<endl; cout<<"输入0按降序排列"<<endl; cout<<"输入1安升序排列"<<endl; cout<>temp2; sts.sortxuefen(temp2); sts.print(); } if(temp1==2) { cout<<endl; cout<<endl; cout<<"输入0按降序排列"<<endl; cout<<"输入1安升序排列"<<endl; cout<>temp2; sts.sortcomscore(temp2); sts.print(); } }break; case 5: { char num[8]; cout<>num; stb.deleb(num); sts.deles(num); cout<<"学生信息已删除!"<<endl; }break; case 6:return 0; default:break; } } }
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值