类与对象
vector容器
文件输入输出流
排序
运算符重载
人生苦短,我用STL
尽量利用C++的特点,不必重复造轮子
删库跑路
登录
批量添加学生、老师信息
查看学生、老师学习
学生信息修改,排序,删除
保存,读取文件
删库跑路
//
// Created by Asus on 2021/1/7.
//
#ifndef MAIN_CPP_MENU_H
#define MAIN_CPP_MENU_H
#include
#include
#include
#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
#ifndef MAIN_CPP_LOGIN_H
#define MAIN_CPP_LOGIN_H
#include
using namespace std;
class Login {
private:
int passwd = 1234;
string account = "root";
public:
void login();
};
#endif //MAIN_CPP_LOGIN_H
#ifndef MAIN_CPP_PEOPLE_H
#define MAIN_CPP_PEOPLE_H
#include
using namespace std;
class people{
public:
int id;
string name;
virtual void showInfo()=0;//纯虚函数
};
#endif //MAIN_CPP_PEOPLE_H
#ifndef MAIN_CPP_STUDENT_H
#define MAIN_CPP_STUDENT_H
#include
#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
#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
/**
* Copyright (c) 2021,
* All rights reserved.
* 文件名称:student.cpp
* 作者:Horace
* 完成日期:2021年1月11日
* 版本号:8.05
*
* 问题描述:学生信息管理系统
* 需要完成的功能至少有:学生成绩的输入和输出、
* 老师信息的输入和输出、老师能够输入、查询并修改学生的成绩、
*
*/
#include
#include
#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");
}
}
#include
#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;
}
//
// Created by Asus on 2021/1/7.
//
#include "head/Teacher.h"
#include
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 将参数转化为右值引用
}
#include
#include
#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请重新输入";
}
}
//
// Created by Asus on 2021/1/7.
//
#include "head/Menu.h"
#include
#include
#include
#include
#include
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 << "取消成功,继续打工!";
}
}