この記事では、STLクラスライブラリのリストデータ構造を使用して、リンクリストでの並べ替えを使用し、リストクラスライブラリの並べ替え機能を使用して、学生管理システムを完成させます。
- 注:現時点では、照合関数はクラス静的メンバー関数として定義されています。これはグローバル関数と同等です。
- 関数ポインタは次のように定義されます
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; //头文件中最好不要using
typedef 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(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)
{
list<DATA>::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;
}
list<DATA>::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 screen
puts("\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 << "学号已存在,请重新输入:";
else
break;
}
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");
}
メイン機能
ファイル名:main.cpp
#include "Student.h"
void main()
{
CStudent st;
st.Start();
}