本文通过STL类库的list数据结构来完成学生管理系统,采用链表外
排序,通过函数指针实现。
- 注意:此时排序规则函数定义为类静态成员函数,等价于全局函数
- 函数指针定义为
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::list<DATA> 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(std::list<DATA>::iterator* ps);bool Check(int nNumb);int FindMenu();void FindbyNumb();void FindbyName();void FindbyMath();void PrintFind(std::list<DATA>& cl);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;CStudent::CStudent()
{
}
CStudent::~CStudent()
{
}bool CStudent::Check(int nNumb)
{list<DATA>::iterator it = m_list.begin();while (it!=m_list.end()){if (it->nNumb == nNumb)return true;++it;}return false;
}typedef void(*FIND_FUNC)();
int CStudent::FindMenu()
{system("cls");puts("1、按学号查找:");puts("2、按姓名查找:");puts("3、按数学分数段查找:");puts("0、返回主菜单:");//FIND_FUNC ps[] = { FindbyNumb,FindbyName };int i;cin >> i;switch (i){case 1:FindbyNumb();break;case 2:FindbyName();break;case 3:FindbyMath();break;}return i;
}void CStudent::FindbyNumb()
{cout << "请输入要查找的学号:";int nNumb;cin >> nNumb;list<DATA>::iterator it = m_list.begin();list<DATA> cl;while (it != m_list.end()){if (it->nNumb == nNumb){cl.push_back(*it);break;}++it;}if (cl.size())PrintFind(cl);elsecout << "没有找到符合的学号" << endl;system("pause");
}void CStudent::FindbyName()
{cout << "请输入要查找的姓名:";char sName[20];cin >> sName;list<DATA>::iterator it = m_list.begin();list<DATA> cl;while (it != m_list.end()){//strchr strpbrk strstrif (strstr(it->sName,sName)) //模糊查找cl.push_back(*it);++it;}if (cl.size())PrintFind(cl);elsecout << "没有找到符合条件的姓名" << endl;system("pause");
}void CStudent::FindbyMath()
{cout << "请输入要查找的分数段(2个数):";float nMin, nMax;cin >> nMin >> nMax;if (nMin > nMax)swap(nMin, nMax);list<DATA>::iterator it = m_list.begin();list<DATA> cl;while (it != m_list.end()){//strchr strpbrk strstrif (it->fMath>nMin && it->fMath<=nMax) //模糊查找cl.push_back(*it);++it;}if (cl.size())PrintFind(cl);elsecout << "没有找到符合条件分数" << endl;system("pause");}void CStudent::PrintFind(std::list<DATA>& cl)
{cout << "学号\t姓名\t成绩" << endl;list<DATA>::iterator it = cl.begin();while (it != cl.end()){cout << it->nNumb << "\t" << it->sName << "\t" << it->fMath << endl;++it;}
}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]);Sort(ps[i-1]);break;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;}list<DATA>::iterator it = m_list.begin();while (it!=m_list.end()){fwrite(&(*it), 1, sizeof(DATA), pf);++it;}fclose(pf);
}void CStudent::PrintPS(list<DATA>::iterator* ps)
{int i = 0;cout << "学号\t姓名\t成绩\t" << endl;while (ps[i] != m_list.end()){cout << ps[i]->nNumb << "\t" << ps[i]->sName << "\t" << ps[i]->fMath << endl;++i;}system("pause");
}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;list<DATA>::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()
{list<DATA>::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");
}void CStudent::Sort(BY_FUNC pFunc)
{int n = m_list.size(), i = 0;list<DATA>::iterator* ps = new list<DATA>::iterator[n + 1];list<DATA>::iterator it = m_list.begin();//方法一//while (i<n+1)// ps[i++] = it++;//方法二while ((ps[i] = it) != m_list.end())++i,++it;i = 0;while (i < n - 1){int j = i + 1;int m = i;//*ps[m] *ps[j]是什么类型? DATAwhile (j < n) //ps[m] ps[j]是什么类型? list<DATA>::iterator{if (pFunc(*ps[j], *ps[m]))m = j;++j;}if (m != i){list<DATA>::iterator t = ps[m];ps[m] = ps[i];ps[i] = t;}++i;}PrintPS(ps);delete []ps;
}
主函数
文件名:main.cpp
#include "Student.h"
void main()
{CStudent st;st.Start();
}