江门网站免费制作,重庆信息网官网,wordpress调用分类产品,wordpress动漫本文通过STL类库的list数据结构来完成学生管理系统#xff0c;采用链表内排序#xff0c;通过list类库自带sort函数进行排序。
注意#xff1a;此时排序规则函数定义为类静态成员函数#xff0c;等价于全局函数函数指针定义为 BY_FUNC ps[] { CStudent::byNumb,CStudent:…本文通过STL类库的list数据结构来完成学生管理系统采用链表内排序通过list类库自带sort函数进行排序。
注意此时排序规则函数定义为类静态成员函数等价于全局函数函数指针定义为 BY_FUNC ps[] { CStudent::byNumb,CStudent::byName,CStudent::byMath };
C中定义CStudent类
文件名Student.h
#pragma once
#include list
typedef struct SUser
{int nNumb;char sName[20];float fMath;
}DATA;//using namespace std; //头文件中最好不要usingtypedef bool (*BY_FUNC)(DATA q, DATA m);class CStudent
{std::listDATA m_list;int Menu();int Input();bool Delete();void Modify();void Print();//void Sort(BY_FUNC pFunc);int SortMenu();void Load();void Save();//void PrintPS(POSITION* ps);bool Check(int nNumb);int FindMenu();//void FindByNumb();static bool byNumb(DATA q, DATA m) // 静态成员函数{return q.nNumb m.nNumb;}static bool byName(DATA q, DATA m){return strcmp(q.sName, m.sName) 0;}static bool byMath(DATA q, DATA m){return q.fMath m.fMath;}
public:CStudent();~CStudent();void Start();
};类成员函数实现
文件名Student.cpp
#define _CRT_SECURE_NO_WARNINGS#include Student.h
#include iostream
#include conio.h
using namespace std;bool CStudent::Check(int nNumb)
{listDATA::iterator it m_list.begin();while (it!m_list.end()){if (it-nNumb nNumb)return true;it;}return false;
}int CStudent::FindMenu()
{return 0;
}CStudent::CStudent()
{
}CStudent::~CStudent()
{
}void CStudent::Start()
{Load();while (Menu());
}int CStudent::SortMenu()
{system(cls);puts(1.按学号排序);puts(2.按姓名排序);puts(3.按成绩排序);puts(4.不排序);puts(0.返回主菜单);int i 0;cin i;BY_FUNC ps[] { CStudent::byNumb,CStudent::byName,CStudent::byMath };switch (i){case 1:case 2:case 3:m_list.sort(ps[i - 1]);case 4:Print();default:return i;}return i;
}void CStudent::Load()
{FILE* pf fopen(stud.lv, r);if (!pf){puts(加载文件时失败);system(pause);return;}DATA t;while (fread(t, 1, sizeof(DATA), pf) sizeof(DATA))m_list.push_back(t);fclose(pf);
}void CStudent::Save()
{FILE* pf fopen(stud.lv, w);if (!pf){puts(保存文件时失败);system(pause);return;}listDATA::iterator it m_list.begin();while (it!m_list.end()){fwrite((*it), 1, sizeof(DATA), pf);it;}fclose(pf);
}int CStudent::Menu()
{system(cls);//clear screenputs(\n\t\t1、浏览所有信息);puts(\t\t2、添加信息);puts(\t\t3、删除信息);puts(\t\t4、修改信息);puts(\t\t5、查找信息);puts(\t\t0、退出);printf(\t\t请选择);int i 0;cin i;switch (i){case 1:while (SortMenu());break;case 2:while (Input());break;case 3:while (Delete());break;case 4:Modify();break;case 5:while (FindMenu());}return i;
}int CStudent::Input()
{cout 请输入学号;DATA d;while (true){cin d.nNumb;if (Check(d.nNumb))cout 学号已存在请重新输入;elsebreak;}cout 请输入姓名和数学成绩空格间隔;cin d.sName d.fMath;m_list.push_back(d);Save();Print();cout 是否继续添加[y/n];rewind(stdin);char c getchar();return c y || c Y;
}bool CStudent::Delete()
{int nNumb;Print();cout 请输入要删除的学号;cin nNumb;listDATA::iterator it m_list.begin();while (it!m_list.end()){if (it-nNumb nNumb){m_list.erase(it);Print();cout 删除成功 endl;system(pause);Save();return false;}it;}cout 你输入的学号不存在是否继续删除[y/n];char c _getch();putchar(c);puts();return y c || Y c;
}void CStudent::Modify()
{
}void CStudent::Print()
{listDATA::iterator it m_list.begin();cout 学号\t姓名\t成绩 endl;while (it!m_list.end()){cout it-nNumb \t it-sName \t it-fMath endl;it;}system(pause);
}主函数
文件名main.cpp
#include Student.h
void main()
{CStudent st;st.Start();
}