c/c++实现简单学校人员管理系统
动手前列的框架:
成品是这样子的:
剩下的操作有兴趣的话复制粘贴去试试:
另一篇C++课设传送门
代码如下:
//Main.cpp
#include"US.h"
int People::sizes = 0;
int main()
{
US s;
s.start();
s.out();
return 0;
}
以上的Main函数中的US类是用来管理人员以及实现操作的(即开头的框架图的右边部分)。
US类:
//US.h
#pragma once
#include"Student.h"
#include"Staff.h"
class US :virtual public Student ,virtual public Staff {
public:
void start();//总开关
void outScreen();//显示菜单
void out();//退出
void Oper();//操作
void writeFile();//保存在文件中
void readFile();//从文件中读取
void deleFile();//删除保存的数据
protected:
People* P = NULL, * head = P,*temp=NULL;
};
//US.cpp
#include"US.h"
void US::start() {
this->outScreen();
this->Oper();
}
void US::outScreen() {
cout << "---------------------------------------------------------\n";
cout << "\n\n";
cout << " 学校人员管理系统\n";
cout << " 实现功能:\n";
cout << " 1.增 2.删 3.查 4.改 5.按编号排序\n";
cout << " 6.查看所有信息 7.保存在 人员.txt 中 8.从文件中读取\n";
cout << " 9.删除 人员.txt中的数据\n";
cout << "\n\n";
cout << "---------------------------------------------------------\n";
}
void US::Oper() {
cout << "输入数字做相应操作,输入0则退出\n";
int t;
int u;
People* p = NULL;
Student* p1 = NULL;
Staff* p2 = NULL;
while (true) {
cin >> t;
if (t == 0) out();
else {
switch (t){
case 1:
cout << "新增学生输入1,新增教职工输入2:\n";
cin >> u;
if (u == 1) {
p1 = new Student;
p1->addMessage();
if (P == NULL) P = p1;
else P->next = p1;
}
else {
p2 = new Staff;
p2->addMessage();
if (P == NULL) P = p2;
else P->next = p2;
}
if (head == NULL) {
head = P;
temp = head;
}
else {
temp->next = P;
temp = temp->next;
}
P = P->next;
break;
case 2:
cout << "输入删除第几个成员:"; cin >> u;
head=head->delMessage(u);
break;
case 3:
cout << "输入查看第几个成员:"; cin >> u;
p = (head->searchPeople(u));
if(p!=NULL) p->showMess();
break;
case 4:
head->changePeople();
break;
case 5:
head->sortPeople();
break;
case 6:
p = head;
while (p != NULL) {
p->showMess();
p = p->next;
}
break;
case 7:
writeFile();
break;
case 8:
readFile();
break;
case 9:
deleFile();
break;
default:
break;
}
}
system("pause");
system("cls");
outScreen();
cout << "输入数字做相应操作,输入0则退出\n";
}
}
void US::out() {
exit(0);
}
void US::writeFile() {
ofstream ofs;
People* p = head;
ofs.open("人员.txt", ios::out | ios::app);
while (p != NULL) {
p->writeF(ofs);
p = p->next;
}
ofs.close();
}
void US::readFile() {
ifstream ifs("人员.txt", ios::in);
char s[80];
while (!ifs.eof()) {
ifs.getline(s, 80);
cout << s;
cout << '\n';
}
ifs.close();
}
void US::deleFile() {
ofstream ofs("人员.txt", ios::trunc);
ofs.close();
}
以上是框架图中右边的部分,以下则是左边的部分:
People类:
//People.h
#pragma once
#include<iostream>
#include<fstream>
using namespace std;
class People {
public:
People() { num = 0; sizes = 0; sex = ' '; name = " "; };
virtual void setBasic();//设置信息
virtual void showMess();//显示信息
virtual void addMessage();//增加人员信息
virtual void outPeople();//普通输出人员信息
virtual void outOne();//输出一个人员信息
virtual void changePeople();//修改人员信息
virtual void writeF(ofstream& ofs);//写入文件
People* delMessage(int nums);//删除人员信息
People* searchPeople(int num);//查找人员信息
People* sortPeople();根据某样数据来排序
static int sizes;//人数
People* next = NULL;
protected:
int num;//编号
char sex;//性别
string name;//姓名
};
//People.cpp
#include"People.h"
People* People::sortPeople() {//根据编号排序,后面回看发现这部分链表的排序有误
People* p = this, * t = this, * head = this;
for (int i = 0; i < sizes; i++) {
p = t = this;
while (p->next != NULL) {
if (p->num > p->next->num) {
p = p->next;
t->next = p->next;
p->next = t;
head = p;
}
else {
p = p->next;
t = t->next;
}
}
}
return head;
}
bool People::judge(People* l, People* r) {
if (l->num > r->num) return true;
else return false;
}
void People::addMessage() {
if (this == NULL) {
this->next = new People;
People* p = this->next;
p->setBasic();
}
else {
this->setBasic();
}
}
People* People::delMessage(int nums) {
People* head=this;
People* p = this, * t = this;
int i;
for (i = 1; i < nums; i++)
p = p->next;
if (i == 1) { delete p; return NULL; }
else {
t = p;
p = p->next;
t->next = p->next;
delete p;
return head;
}
}
People* People::searchPeople(int num) {
People* p = this;
if (p != NULL) {
for (int i = 1; i < num; i++)
p = p->next;
return p;
}
else {
cout << "查无此人!\n"; return NULL;
}
}
void People::outOne() {
cout << num << ' ' << sex << ' ' << name << '\n';
}
void People::outPeople() {
People* p = this;
while (p != NULL) {
cout << num << ' ' << sex << ' ' << name << '\n';
p = p->next;
}
}
void People::changePeople() {
People* t = this;
cout << "输入要修改第几个成员信息:";
int u1; cin >> u1;
for (int i = 1; i <= u1; i++)
t = t->next;
if (t == NULL) { cout << "查无此人\n"; }
else {
int u;
cout << "输入操作符: \n";
cout << "1.改姓名,2.改性别,3.改编号\n";
cin >> u;
string nName; char x; int nums;
switch (u) {
case 1://改姓名
cout << "输入新名字:"; cin >> nName;
t->name = nName;
break;
case 2://改性别
cout << "输入新性别:"; cin >> x;
t->sex = x;
break;
case 3://改编号
cout << "输入新编号:"; cin >> nums;
t->num = nums;
break;
default:
break;
}
}
}
void People::setBasic() {
cout << "输入新成员的编号、性别(m/w)、名字:\n";
cin >> num >> sex >> name;
sizes++;
}
void People::showMess() {
if(this!=NULL)
cout << "编号:" << num << '\t';
cout << "性别:" << sex << '\t';
cout << "名字:" << name << '\n';
}
void People::writeF(ofstream& ofs) {
People* p = this;
if (p == NULL) { cout << "没有数据! 请重新操作\n"; return; }
ofs << this->num<<' ' << this->sex<<' ' << this->name << '\n';
}
上面的People是基类,接下来的Staff和Student是派生类:
Staff类:
//Staff.h
#pragma once
#include"People.h"
class Staff : protected People {
public:
Staff() { money = 0; workTime = 0; }
void setBasic();
void showMess();
void addMessage();
Staff* delMessage(int nums);
//Staff* searchPeople();
Staff* sortPeople(int value);//根据value排序
void changePeople();
void writeF(ofstream& ofs);
Staff* next = NULL;
protected:
int money;
int workTime;
string type;
};
#include"Staff.h"
void Staff::setBasic() {
cout << "输入新成员的编号、性别、名字:\n";
cin >> num >> sex >> name;
cout << "输入员工的工资、工作时间:\n";
cin >> this->money >> this->workTime;
cout << "输入职位(教师、职工):\n";
cin >> type;
sizes++;
}
void Staff::showMess() {
cout << "编号:" << num << '\t';
cout << "性别:" << sex << '\t';
cout << "名字:" << name << '\t';
cout << "职位:" << type << '\t';
cout << "工资:" << money << '\t';
cout << "工作时长:" << workTime << '\n';
}
void Staff::addMessage() {
if (this == NULL) {
this->next = new Staff;
Staff* p = this->next;
p->setBasic();
}
else {
this->setBasic();
}
}
Staff* Staff::delMessage(int nums) {
Staff* head=this;
Staff* p = this, * t = this;
int i;
for (i = 1; i < nums; i++)
p = p->next;
if (i == 1) { delete p; return NULL; }
else {
t = p;
p = p->next;
t->next = p->next;
delete p;
return head;
}
}
Staff* Staff::sortPeople(int value) {
Staff* p = this, * t = this, * head = this;
if (value == 1) {
for (int i = 0; i < sizes; i++) {
p = t = this;
while (p->next != NULL) {
if (p->money > p->next->money) {
p = p->next;
t->next = p->next;
p->next = t;
head = p;
}
else {
p = p->next;
t = t->next;
}
}
}
}
else {
for (int i = 0; i < sizes; i++) {
p = t = this;
while (p->next != NULL) {
if (p->workTime > p->next->workTime) {
p = p->next;
t->next = p->next;
p->next = t;
head = p;
}
else {
p = p->next;
t = t->next;
}
}
}
}
return head;
}
void Staff::changePeople() {
Staff* t = this;
cout << "输入要修改第几个成员信息:";
int u1; cin >> u1;
for (int i = 1; i <= u1; i++)
t = t->next;
int u2;
cout << "输入操作符,选择改变某个数据:\n";
cout << " 1.改姓名,2.改性别(m/w),3.改编号,4.改工资,5.改工作时间\n";
cin >> u2;
string nName; char x; int nums;
switch (u2) {
case 1://改姓名
cout << "输入新名字:"; cin >> nName;
t->name = nName;
break;
case 2://改性别
cout << "输入新性别:"; cin >> x;
t->sex = x;
break;
case 3://改编号
cout << "输入新编号:"; cin >> nums;
t->num = nums;
break;
case 4://改工资
cout << "输入新工资:"; cin >> nums;
t->money = nums;
break;
case 5://改工作时间
cout<< "输入新工作时间:"; cin >> nums;
t->workTime = nums;
break;
default:
break;
}
}
void Staff::writeF(ofstream& ofs) {
Staff* p = this;
if (p == NULL) { cout << "没有数据! 请重新操作\n"; return; }
ofs << this->num <<' '<< this->sex<<' ' << this->name<<' ' <<this->type<<' '<< this->money<<' ' << this->workTime<<'\n';
}
Student类:
//Student.h
#pragma once
#include"People.h"
class Student : protected People {
public:
Student() { averGrade = 0; eduCa = " "; }
void addMessage();
void setBasic();
void showMess();
Student* delMessage(int nums);
//People* searchPeople();
Student* sortPeople(int value);
void changePeople();
void writeF(ofstream& ofs);
Student* next = NULL;
protected:
int averGrade;
string eduCa;
};
//Student.cpp
#include"Student.h"
void Student::addMessage() {
if (this == NULL) {
this->next = new Student;
Student* p = this->next;
p->setBasic();
}
else {
this->setBasic();
}
}
Student* Student::delMessage(int nums) {
Student* head=this;
Student* p = this, * t = this;
int i;
for (i = 1; i < nums; i++)
p = p->next;
if (i == 1) { delete p; return NULL; }
else {
t = p;
p = p->next;
t->next = p->next;
delete p;
return head;
}
}
void Student::setBasic() {
cout << "输入新成员的编号、性别(m/w)、名字:\n";
cin >> num >> sex >> name;
cout << "输入平均绩点:\n";
cin >> this->averGrade;
cout << "输入学历(本科生、研究生、博士生):\n";
cin >> eduCa;
sizes++;
}
void Student::showMess() {
cout << "编号:" << num << '\t';
cout << "性别:" << sex << '\t';
cout << "名字:" << name << '\t';
cout << "学历:" << eduCa << '\t';
cout << "平均绩点" << averGrade << '\n';
}
Student* Student::sortPeople(int value) {
Student* p = this, * t = this, * head = this;
if (value == 1) {
for (int i = 0; i < sizes; i++) {
p = t = this;
while (p->next != NULL) {
if (p->averGrade > p->next->averGrade) {
p = p->next;
t->next = p->next;
p->next = t;
head = p;
}
else {
p = p->next;
t = t->next;
}
}
}
}
return head;
}
void Student::changePeople() {
Student* t = this;
cout << "输入要修改第几个成员信息:";
int u1; cin >> u1;
for (int i = 1; i < u1; i++)
t = t->next;
int u2;
cout << "输入操作符,选择改变某个数据:\n";
cout << " 1.改姓名,2.改性别,3.改编号,4.改工资,5.改工作时间\n";
cin >> u2;
string nName; char x; int nums;
switch (u2) {
case 1://改姓名
cout << "输入新名字:"; cin >> nName;
t->name = nName;
break;
case 2://改性别
cout << "输入新性别(m/w):"; cin >> x;
t->sex = x;
break;
case 3://改编号
cout << "输入新编号:"; cin >> nums;
t->num = nums;
break;
case 4://改平均绩点
cout << "输入新平均绩点:"; cin >> nums;
t->averGrade = nums;
break;
default:
break;
}
}
void Student::writeF(ofstream& ofs) {
Student* p = this;
if (p == NULL) { cout << "没有数据! 请重新操作。\n"; return; }
ofs << this->num<<' ' << this->sex<<' ' << this->name<<' ' << this->averGrade<<' ' << this->eduCa << '\n';
}
本来还想用上更多的数据结构方面的知识,比如用二叉树来排序,但是因为不熟悉,毕竟刚学完离散数学还没有具体实现过那些数据结构,,
而又临近deadLine。。。
只好删除一些操作,以后再找机会实现那些了。
最新博文:
2020.12.23
大二上学期临近期末总结
2021.1.14
大二数据结构课程设计-随机数流中取中位数