C++实现教师管理系统
时间:2023-03-12 11:24:49|栏目:C代码|点击: 次
本文实例为大家分享了C++实现教师管理系统的具体代码,供大家参考,具体内容如下
链表模板定义:就是用这种数据类型来进行存储
#include<iostream> #ifndef NODELIST_S #define NODELIST_S template<typename T> class Node { public: T s_Data; Node<T>* s_Next; Node(const T&data) :s_Data(data), s_Next(nullptr){} }; template<typename T> class NodeList { private: Node<T>* s_Head; int s_Number; public: NodeList() : s_Head(nullptr), s_Number(0){} ~NodeList() { Clear(); } Node<T>*GetHead() { return s_Head; } int GetNum() { return s_Number; } bool Clear() { Node<T>*p = s_Head; for (int i = 0; i < s_Number; i++) { if (!p)return false; Node<T>*q = p; p = p->s_Next; delete q; } s_Head = nullptr; s_Number = 0; return true; } bool IsEmpty() { return (s_Number == 0); } bool AddAt(const T&, int i);//添加元素到第i个位置,计数从0开始 bool RemoveAt(int i);//删除第i个元素 }; template<typename T> bool NodeList<T>::AddAt(const T& data, int i) { Node<T>* OneNode = new Node<T>(data); Node<T>* p; if (OneNode) { if (s_Head) p = s_Head; else { s_Head = OneNode; s_Number++; return true; } if (!i) { OneNode->s_Next = s_Head; s_Head = OneNode; s_Number++; return true; } for (int j = 1; j < i; j++) { p = p->s_Next; } //此时p指向要插入的位置的前一个节点 OneNode->s_Next = p->s_Next; p->s_Next = OneNode; s_Number++; return true; } return false; } template<typename T> bool NodeList<T>::RemoveAt(int i) { Node<T>*p; if (s_Head) p = s_Head; else return false; if (!i) { s_Head = p->s_Next; delete p; s_Number--; return true; } for (int j = 1; j < i; j++) { p = p->s_Next; } //此时p指向要删除的位置的前一个节点 Node<T>*q = p->s_Next; p->s_Next = p->s_Next->s_Next; delete q; s_Number--; return true; } #endif // !NODELIST_S
教师类的声明:对教师的一些信息的抽象,包含的数据及行为
#include<iostream> #include<string> #ifndef TEACHER_S #define TEACHER_S using namespace std; class Teacher { private: const int s_ID; const string s_Name; const string s_Sex; int s_Age; string s_Major; string s_Number; float s_Money; int s_Pingfen; public: Teacher(int Id, string Name, string Sex, int Age, string Major,string Number,float Money,int Pingfen) : s_ID(Id), s_Name(Name), s_Sex(Sex), s_Age(Age), s_Major(Major),s_Number(Number),s_Money(Money),s_Pingfen(Pingfen){} int GetID() { return s_ID; } int GetAge(){ return s_Age; } string GetMajor(){ return s_Major; } int GetPingfen(){ return s_Pingfen; } void AddAge() { s_Age++; } void ChangeMajor(string m) { s_Major = m; } void ChangeNumber(string n) { s_Number = n; } void ChangeMoney(float m) { s_Money = m; } void ChangePingfen(int p){ s_Pingfen = p; } friend ostream& operator<<(ostream& out, const Teacher& one) { out << one.s_ID << " " << one.s_Name << " " << one.s_Sex << " " << one.s_Age << " " << one.s_Major << " " << one.s_Number << " " << one.s_Money << " " << one.s_Pingfen << endl; return out; } }; #endif // !TEACHER_S
管理类:对教师这个管理系统进行管理
#pragma once #include<iostream> #include"List.h" #include"Teacher.h" #include<fstream> #ifndef MANAGE_S #define MANAGE_S class Management { private: NodeList<Teacher> s_TeacherList; public: Management(); ~Management(); void ShowAll(); void AddTeacher(); void DeletTeacher(); void SearchById(); void ChangeTeacherXinxi(); void ShowByPaixu(); void ReadFile(); void WriteFile(); void AddAllAge(); void ClearAllDate(); }; #endif // !MANAGE_S
管理类的具体实现:
#include "Management.h" Management::Management() { } Management::~Management() { } void Management::ShowAll() { cout << "从左到右依次为教职工号、姓名、性别、年龄、教学科目、电话号码、月薪、评分" << endl; Node<Teacher>*p = s_TeacherList.GetHead(); while (p) { cout << p->s_Data << endl; p = p->s_Next; } } void Management::AddTeacher() { cout << "请输入教师的教职工号、姓名、性别、年龄:" << endl; int id, age; string name, sex; cin >> id >> name >> sex >> age; Node<Teacher>*p = s_TeacherList.GetHead(); while (p) { if (p->s_Data.GetID() == id) { cout << "已有该老师的信息!添加失败" << endl; return; } p = p->s_Next; } cout << "请依次输入老师的教学科目、电话号码、月薪和评分:" << endl; string km, hm; float yx; int pf; cin >> km >> hm >> yx>>pf; Teacher one(id, name, sex, age, km, hm, yx,pf); if (s_TeacherList.AddAt(one, s_TeacherList.GetNum())) cout << "教师信息添加成功!" << endl; else cout << "信息添加失败!" << endl; } void Management::DeletTeacher() { cout << "输入您想删除的教师的教职工号:"; int id, i(0); cin >> id; Node<Teacher>*p = s_TeacherList.GetHead(); while (p) { if (p->s_Data.GetID() == id) { if (s_TeacherList.RemoveAt(i)) cout << "删除成功!" << endl; else cout << "删除失败!" << endl; return; } else { i++; p = p->s_Next; } } cout << "系统中没有该老师信息!" << endl; return; } void Management::SearchById() { cout << "输入您想查询的教师的教职工号:"; int id; cin >> id; Node<Teacher>*p = s_TeacherList.GetHead(); while (p) { if (p->s_Data.GetID() == id) { cout << p->s_Data << endl; return; } else p = p->s_Next; } cout << "系统中没有该老师信息!" << endl; return; } void Management::ChangeTeacherXinxi() { cout << "输入您想更改的老师的教职工号:"; int id; cin >> id; Node<Teacher>*p = s_TeacherList.GetHead(); while (p) { if (p->s_Data.GetID() == id) { cout << p->s_Data << endl; break; } else p = p->s_Next; } if (!p) { cout << "系统中没有该老师的信息!" << endl; return; } cout << "您要修改哪一样信息?(1.教学科目 2.电话号码 3.月薪 4.评分 0.返回)" << endl; int mark,pf; string k, n; float m; cin >> mark; if (!mark)return; cout << "输入修改信息:"; if (mark == 1) cin >> k; else if (mark == 2) cin >> n; else if (mark == 3) cin >> m; else if (mark == 4) cin >> pf; switch (mark) { case 1: p->s_Data.ChangeMajor(k); break; case 2:p->s_Data.ChangeNumber(n); break; case 3:p->s_Data.ChangeMoney(m); break; case 4:p->s_Data.ChangePingfen(pf); break; default:break; } cout << "修改成功!" << endl; cout << p->s_Data << endl; return; } //这里采用最简单的桶排序 void Management::ShowByPaixu() { cout << "请问您想根据哪一项对教师信息进行排序?" << endl << "(1.年龄 2.评分 0.返回)" << endl; int c; cin >> c; NodeList<Teacher> pucket[100];//100个桶 Node<Teacher>*p = s_TeacherList.GetHead(); switch (c) { case 1: { while (p) { pucket[p->s_Data.GetAge() - 1].AddAt(p->s_Data, pucket[p->s_Data.GetAge() - 1].GetNum()); p = p->s_Next; } break; } case 2: { while (p) { pucket[p->s_Data.GetPingfen() - 1].AddAt(p->s_Data, pucket[p->s_Data.GetPingfen() - 1].GetNum()); p = p->s_Next; } break; } case 0:break; default:break; } for (int i = 99; i >= 0; i--) { p = pucket[i].GetHead(); while (p) { cout << p->s_Data << endl; p = p->s_Next; } } } void Management::ReadFile() { ifstream in("data.txt"); if (in.fail()) { cout << "Data error!" << endl; return; } do{ int id, age, pingfen; string name, sex, major, number; float money; in >> id >> name >> sex >> age >> major>> number >> money>>pingfen; Teacher one(id, name, sex, age, major, number, money,pingfen); s_TeacherList.AddAt(one, s_TeacherList.GetNum()); } while (!in.eof()); cout << "Read file successful!" << endl; in.close(); } void Management::WriteFile() { ofstream out("outdata.txt"); if (!out) { cerr << " file error!" << endl; return; } Node<Teacher>*p = s_TeacherList.GetHead(); while (p) { out << p->s_Data; cout << p->s_Data; p = p->s_Next; } cout << "Write file successful!" << endl; out.close(); } void Management::AddAllAge() { Node<Teacher>*p = s_TeacherList.GetHead(); while (p) { p->s_Data.AddAge(); p = p->s_Next; } cout << "教师年龄增加完成!" << endl; return; } void Management::ClearAllDate() { if (s_TeacherList.IsEmpty()) { cout << "数据已经为空!" << endl; return; } else if (s_TeacherList.Clear()) cout << "删除数据成功!" << endl; else { cout << "删除数据失败,程序退出" << endl; exit(0); } return; }
主函数:菜单选项
#include<iostream> #include<fstream> #include <string> #include"Management.h" using namespace std; int main() { Management manager; while (1) { cout << "**********************************" << endl; cout << "****欢迎您使用教师信息系统********" << endl; cout << "* *" << endl; cout << "* |输入数字执行您想使用的功能| *" << endl; cout << "* 1.读取文件 *" << endl; cout << "* 2.写入文件 *" << endl; cout << "* 3.显示教师信息 *" << endl; cout << "* 4.添加教师信息 *" << endl; cout << "* 5.删除教师信息 *" << endl; cout << "* 6.更改教师信息 *" << endl; cout << "* 7.查询教师信息 *" << endl; cout << "* 8.按年龄或评分排序 *" << endl; cout << "* 9.增加所有老师年龄 *" << endl; cout << "* 10.清空所有数据 *" << endl; cout << "* 0.退出 *" << endl; cout << "**********************************" << endl; int input; cin >> input; switch (input) { case 1: {manager.ReadFile(); manager.ShowAll(); break; } case 2: {manager.WriteFile(); break; } case 3: {manager.ShowAll(); break; } case 4: {manager.AddTeacher(); break; } case 5: {manager.DeletTeacher(); break; } case 6: {manager.ChangeTeacherXinxi(); break; } case 7: {manager.SearchById(); break; } case 8: {manager.ShowByPaixu(); break; } case 9: {manager.AddAllAge(); break; } case 10: {manager.ClearAllDate(); break; } case 0: exit(0); default:return 0; } } return 0; }