C++简单面向对象学校管理系统




关键词

类与对象
vector容器
文件输入输出流
排序
运算符重载
人生苦短,我用STL
尽量利用C++的特点,不必重复造轮子
删库跑路


设计

功能:

  • 登录

  • 批量添加学生、老师信息

  • 查看学生、老师学习

  • 学生信息修改,排序,删除

  • 保存,读取文件

  • 删库跑路


一、头文件

1.menu.h

//
// Created by Asus on 2021/1/7.
//
#ifndef MAIN_CPP_MENU_H
#define MAIN_CPP_MENU_H

#include<string>
#include<vector>
#include<fstream>
#include "Student.h"
#include "Teacher.h"
#define FILE "file.txt"
using namespace std;
class Menu {
public:
    vector<Student> stu;
    vector<Teacher> tea;

    static void showDirection();

    void addStu();

    void addTea();

    void showStu();

    void showTea();

    void delStu();

    void modStu();

    void clean();

    void sortStu();

    bool isEmpty;

    void save();

    Menu();

    void init();

	friend ostream& operator<<(ostream&, vector<Student>&stud);//运算符重载


};

#endif //MAIN_CPP_MENU_H

2.Login.h

#ifndef MAIN_CPP_LOGIN_H
#define MAIN_CPP_LOGIN_H

#include<iostream>

using namespace std;

class Login {
private:
    int passwd = 1234;
    string account = "root";
public:
    void login();


};

#endif //MAIN_CPP_LOGIN_H


3.People.h

#ifndef MAIN_CPP_PEOPLE_H
#define MAIN_CPP_PEOPLE_H

#include<iostream>
using namespace std;
class people{
public:
    int id;
    string name;

    virtual void showInfo()=0;//纯虚函数

};
#endif //MAIN_CPP_PEOPLE_H


4.Student.h

#ifndef MAIN_CPP_STUDENT_H
#define MAIN_CPP_STUDENT_H
#include<iostream>

#include "people.h"

using namespace std;


class Student : public people {
private:

public:
    int score;

    void showInfo() override;

    Student(int x, string y, int z);

    ~Student();

};


#endif //MAIN_CPP_STUDENT_H

5.Teacher.h

#ifndef MAIN_CPP_TEACHER_H
#define MAIN_CPP_TEACHER_H

#include "people.h"

class Teacher: public people{

public:
    void showInfo() override;//虚函数重写
    Teacher(int x,string y);

};
#endif //MAIN_CPP_TEACHER_H


二、源文件

1.main.cpp


/**
* Copyright (c) 2021,
* All rights reserved.
* 文件名称:student.cpp
* 作者:Horace
* 完成日期:2021年1月11日
* 版本号:8.05
*
* 问题描述:学生信息管理系统
 * 需要完成的功能至少有:学生成绩的输入和输出、
 * 老师信息的输入和输出、老师能够输入、查询并修改学生的成绩、
 *
*/

#include<iostream>
#include <unistd.h>
#include "head/Login.h"
#include "head/Menu.h"
using namespace std;

int main() {
    //登录
    Login log;
    log.login();

    int direc;//创建指令

    Menu menu;//创建对象

    while (true) {
        Menu::showDirection();//直接通过类本身来访问静态成员
        cin >> direc;
        switch (direc) {
            case 0://退出系统
                //保存信息
                menu.save();
                exit(0);
            case 1://增加学生信息
                menu.addStu();
                break;
            case 2://增加老师信息
                menu.addTea();
                break;
            case 3://显示学生信息
                menu.showStu();
                break;
            case 4://显示老师信息
                menu.showTea();
                break;
            case 5://排序
                menu.sortStu();
                break;
            case 6://删除学生信息
                menu.delStu();
                break;
            case 7://修改学生信息
                menu.modStu();
                break;
            case 8://删库跑路
                menu.clean();
            default:
                cout << "请输入正确指令";

        }
        system("pause");
        system("cls");

    }


}




2. Student.cpp

#include <utility>

#include "head/Student.h"


void Student::showInfo() {
    cout << "学生id:" << id << "\t name:" << name << "\t score:" << score << endl;


}

Student::~Student() = default;

Student::Student(int x, string y, int z) {
    id=x;
    name=std::move(y);//多使用右值引用,可以通过 std::move 将参数转化为右值引用
    score=z;

}


3. Teacher.cpp

//
// Created by Asus on 2021/1/7.
//
#include "head/Teacher.h"
#include<iostream>
using namespace std;


void Teacher::showInfo() {
    cout << "老师id: "<<id<<"\t name: "<<name<<endl;

}

Teacher::Teacher(int x, string y) {
    id=x;
    name=std::move(y);//多使用右值引用,可以通过 std::move 将参数转化为右值引用

}


4. Login.cpp

#include <unistd.h>
#include <windows.h>
#include "../head/Login.h"

void Login::login() {
    while (true) {
        string m_account;
        int m_passwd;
        cout << "============请登录=========="<<endl;
        cout << "| 帐号:  ";
        cin>>m_account;
        cout << "| 密码:  ";
        cin>>m_passwd;
        if(m_account==account &&m_passwd==passwd){
            cout << "登录成功";
            Sleep(500);
            system("cls");
            break;
        }
        cout << "\n请重新输入";
    }


}



5. Menu.cpp

//
// Created by Asus on 2021/1/7.
//

#include "head/Menu.h"
#include<iostream>
#include<algorithm>
#include <unistd.h>
#include <windows.h>
#include <chrono>
using namespace std;
ostream& operator<<(ostream&, vector<Student>& stud){//运算符重载
    for (auto i:stud) {
        i.showInfo();
    }

}

bool NameUp(const Student &a, const Student &b) {
    return a.name < b.name;
}

bool IdUp(const Student &a, const Student &b) {
    return a.id < b.id;
}

bool ScoreDown(const Student &a, const Student &b) {
    return a.score > b.score;
}

bool ScoreUp(const Student &a, const Student &b) {
    return a.score < b.score;
}



void Menu::showDirection() {

    cout << "      欢迎使用学校管理系统 v8.0      " << endl;
    cout << "=====================================" << endl;
    cout << "******---|0.退出并保存系统|---*******\n";
    cout << "==============  添加  ===============\n";
    cout << "******---| 1.增加学生信息 |---*******\n";
    cout << "******---| 2.增加老师信息 |---*******\n";
    cout << "==============  显示  ===============\n";
    cout << "******---| 3.显示学生信息 |---*******\n";
    cout << "******---| 4.显示老师信息 |---*******\n";
    cout << "******---| 5.学生信息排序 |---*******\n";
    cout << "==============  修改  ===============\n";
    cout << "******---| 6.删除学生信息 |---*******\n";
    cout << "******---| 7.修改学生信息 |---*******\n";
    cout << "******---| 8.删!库!跑!路! |---*******\n";
    cout << "*请输入指令*\n";

}


void Menu::addStu() {
    int num;
    cout << "请输入增加学生的数量" << endl;
    cin >> num;

    for (int i = 0; i < num; i++) {
        int id;
        string name;
        int score;
        cout << "请输入学生id" << endl;
        cin >> id;
        cout << "请输入学生姓名" << endl;
        cin >> name;
        cout << "请输入学生成绩" << endl;
        cin >> score;
        Student temp(id, name, score);
        stu.push_back(temp);
    }
    isEmpty = false;
    system("cls");
    showDirection();
    showStu();
    cout << "添加成功!" << endl;
}

void Menu::addTea() {
    int num;
    cout << "请输入增加老师的数量" << endl;
    cin >> num;

    for (int i = 0; i < num; i++) {
        int id;
        string name;

        cout << "请输入老师id" << endl;
        cin >> id;
        cout << "请输入老师姓名" << endl;
        cin >> name;
        Teacher temp(id, name);
        tea.push_back(temp);
    }
    isEmpty = false;
    system("cls");
    showDirection();
    showTea();
    cout << "添加成功!" << endl;
}

void Menu::showStu() {
    cout<<stu;//运算符已重载
}

void Menu::showTea() {

    for (auto i:tea) {
        i.showInfo();
    }
}

void Menu::delStu() {
    showStu();
    cout << "输入删除学生的id" << endl;
    int delId;
    cin >> delId;

    for (int i = 0; i <= stu.size(); i++) {
        if (delId == stu[i].id)
            stu.erase(stu.begin() + i);
    }
    system("cls");
    showDirection();
    showStu();
    cout << "删除成功!" << endl;
}

void Menu::modStu() {
    showStu();
    cout << "输入要修改的学生的id" << endl;
    int Id;
    cin >> Id;

    for (auto &i:stu) {
        if (Id == i.id) {
            cout << "请输入新的成绩" << endl;
            cin >> i.score;
            break;
        }

    }
    system("cls");
    showDirection();
    showStu();
    cout << "修改成功!" << endl;


}

void Menu::sortStu() {
    showStu();
    cout << " 请输入排序方式" << endl;
    cout << "1.成绩降序    2.成绩升序   3.姓名升序    4.id升序" << endl;
    int x;
    cin >> x;
    if (x == 1) {
        sort(stu.begin(), stu.end(), ScoreDown);
    }

    if (x == 2) {
        sort(stu.begin(), stu.end(), ScoreUp);
    }
    if (x == 3) {
        sort(stu.begin(), stu.end(), NameUp);
    }
    if (x == 4) {
        sort(stu.begin(), stu.end(), IdUp);
    }
    system("cls");
    showDirection();
    showStu();
}

void Menu::save() {
    ofstream ofs;
    ofs.open(FILE, ios::out);//写入文件

    for (const auto &i:stu)
        ofs << "学生 " << i.id << " " << i.name << " " << i.score << endl;
    for (const auto &i:tea)
        ofs << "老师 " << i.id << " " << i.name << " " << 0 << endl;

    ofs.close();//关闭文件


}

Menu::Menu() {
    ifstream ifs;
    //读文件
    ifs.open(FILE, ios::in);
    //假如文件不存在
    if (!ifs.is_open()) {
        isEmpty = true;
        ifs.close();
        return;
    }
    //假设文件存在为空
    char ch;
    ifs >> ch;
    if (ifs.eof()) {
        ifs.close();
        return;

    }

   //文件存在
    init();

}


void Menu::init() {
    ifstream ifs;
    ifs.open(FILE, ios::in);

    int id;
    int score;
    string name;
    string dId;

    while (ifs >> dId && ifs >> id && ifs >> name && ifs >> score) {
        if (dId == "学生") {
            stu.emplace_back(id, name, score);
        } else {
            tea.emplace_back(id, name);
        }
    }
    ifs.close();


}


void Menu::clean() {
    system("cls");
    cout << "根据《中华人民共和国刑法》第二百八十六条第一款、第二款:" << endl;
    cout << "对计算机信息系统中存储的数据和应用程序进行删除,造成计算机信息系统不能正常运行,后果特别严重,其行为已构成破坏计算机信息系统罪,依法应予惩处。" << endl;
    cout << "是否继续删库?  1.确定  2.取消" << endl;
    int x;
    cin >> x;
    if (x == 1) {
        unsigned seed = chrono::high_resolution_clock::now().time_since_epoch().count();
        mt19937 rand_generator(seed);
        mt19937 rand_generator1(seed);
        uniform_int_distribution<int> dist(50, 700);
        uniform_int_distribution<int> dist1(4, 15);
        stu.clear();
        tea.clear();
        for (int i = 0; i < 3; i++) {
            cout << "删除成功 rm -rf */ sys/module/drm/section/__kcrctab:operator permitted" << endl;
            cout << "删除成功 rm -rf */ sys/module/drm/section/.text.unlikely':operator permitted" << endl;
            cout << "删除成功 rm -rf */ sys/module/drm/section/. rodata':operator permitted" << endl;
            cout << "正在删除 sys/module/drm/section/. ref.data'";
            for (int j = 0; j < dist1(rand_generator1); j++) {
                cout << "■";
                Sleep(dist(rand_generator));
            }
            cout << "\n删除成功 rm -rf */ sys/module/drm/section/. ref.data'::operator permitted" << endl;
            Sleep(dist(rand_generator));
            cout
                    << "删除成功 rm -rf */ /usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/dean/bin'::operator permitted"
                    << endl;
            Sleep(dist(rand_generator));
            cout
                    << "删除成功 rm -rf */ /usr/lib64/qt-3.3/bin:/usr/usr/local/sbin:/usr/sbin:/sbin:/home/dean/bin'::operator permitted"
                    << endl;
            cout
                    << "删除成功 rm -rf */ /usr/lib64/qt-/etc/alternatives/java -> /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.242.b08-0.el7_7.x86_64/jre/bin/'::operator permitted"
                    << endl;
            cout << "删除成功 rm -rf */ sys/module/drm/section/__ex_table':operator permitted" << endl;
            cout << "删除成功 rm -rf */ sys/module/drm/section/.smp_locks:operator permitted" << endl;
            cout << "删除成功 rm -rf */ sys/module/drm/parameters/vblankoffdelay.:operator permitted" << endl;
            Sleep(dist(rand_generator));
            cout << "删除成功 rm -rf */ sys/block/drm/parameters/timestamp_precision_usec':operator permitted" << endl;
            cout << "删除成功 rm -rf */ sys/block/drm/section/__kcrctab:operator permitted" << endl;
            cout << "删除成功 rm -rf */ sys/block/drm-0/section/__kcrctab:operator permitted" << endl;
            cout << "删除成功 rm -rf */ sys/block/vda':operator permitted" << endl;
        }
        system("cls");
        cout << "删库成功,赶紧跑路!" << endl;
    } else {
        cout << "取消成功,继续打工!";
    }
}



三.效果图

1.登录界面

在这里插入图片描述

2.添加

添加学生
在这里插入图片描述
在这里插入图片描述
添加老师
在这里插入图片描述
在这里插入图片描述

追加学生
在这里插入图片描述

在这里插入图片描述

3.修改

在这里插入图片描述
在这里插入图片描述

4.查看

成绩降序
在这里插入图片描述
在这里插入图片描述
姓名升序
在这里插入图片描述
在这里插入图片描述
id升序
在这里插入图片描述
在这里插入图片描述

6.保存

在这里插入图片描述
在这里插入图片描述

7.读取txt

在这里插入图片描述

8.删库跑路

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 5
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值