- 注意:此时排序规则函数定义为全局函数
C++中定义CStudent类
文件名:Student.h
#pragma once
#include <afxtempl.h>
typedef struct SUser
{
int nNumb;
char sName[20];
float fMath;
}DATA;
typedef bool(*BY_FUNC)(DATA& q,DATA& m);
class CStudent
{
CList<DATA> m_list;
int Menu();
int Input();
void Delete();
void Modify();
void Print();
void Sort(BY_FUNC pFunc);
int SortMenu();
void Load();
void Save();
void PrintPS(POSITION* ps); // 与上篇博客不同之处
public:
CStudent();
~CStudent();
void Start();
};
类成员函数实现
文件名:Student.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "Student.h"
using namespace std;
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:
Delete();
case 4:
Modify();
}
return i;
}
int CStudent::Input()
{
cout << "请输入学号:";
DATA d;
cin >> d.nNumb;
cout << "请输入姓名和数学成绩(空格间隔):";
cin >> d.sName >> d.fMath;
m_list.AddTail(d);
Print();
Save();
cout << "是否继续添加?";
rewind(stdin);
char c = getchar();
return c == 'y' || c == 'Y';
return 0;
}
void CStudent::Delete()
{
int nNumb;
Print();
cout << "\n请输入要删除的学号:";
cin >> nNumb;
POSITION pos = m_list.GetHeadPosition();
while (pos)
{
if (m_list.GetAt(pos).nNumb == nNumb)
{
m_list.RemoveAt(pos);
Print();
cout << "删除成功!" << endl;
system("pause");
Save();
return false;
}
m_list.GetNext(pos);
}
cout << "你输入的学号不存在,是否继续删除?[y/n]" ;
char c = _getch();
putchar(c);
puts("");
return 'y' == c || 'Y' == c;
}
void CStudent::Modify()
{
}
void CStudent::Print()
{
POSITION pos = m_list.GetHeadPosition();
cout << "学号\t姓名\t成绩" << endl;
while (pos)
{
DATA d = m_list.GetAt(pos);
cout << d.nNumb << "\t" << d.sName << "\t" << d.fMath << endl;
m_list.GetNext(pos);
}
system("pause");
}
bool byNumb(DATA& q,DATA& m)
{
return q.nNumb < m.nNumb;
}
bool byName(DATA& q, DATA& m)
{
return strcmp(q.sName, m.sName) < 0;
}
bool byMath(DATA& q, DATA& m)
{
return q.fMath > m.fMath;
}
void CStudent::Sort(BY_FUNC pFunc)
{
int n = m_list.GetCount();
POSITION* ps = new POSITION[n + 1];
POSITION pos = m_list.GetHeadPosition();
int i = 0, m = 0, j = 0;
while (ps[i++] = pos)
m_list.GetNext(pos);
pos = m_list.GetHeadPosition();
i = 0;
while (i < n - 1)
{
m = i;
j = i + 1;
while (j < n)
{
if (pFunc(m_list.GetAt(ps[j]), m_list.GetAt(ps[m]))) //ps[j]的类型是POSITION
m = j;
++j;
}
if (m != i)
{
POSITION t = ps[i];
ps[i] = ps[m];
ps[m] = t;
}
++i;
}
PrintPS(ps);
delete[] ps;
}
void CStudent::PrintPS(POSITION* ps)
{
int i = 0;
cout << "学号\t姓名\t成绩" << endl;
while (ps[i])
{
DATA d = m_list.GetAt(ps[i]);
cout << d.nNumb << "\t" << d.sName << "\t" << d.fMath << endl;
++i;
}
system("pause");
}
int CStudent::SortMenu()
{
system("cls");
puts("1.按学号排序");
puts("2.按姓名排序");
puts("3.按成绩排序");
puts("4.不排序");
puts("0.返回主菜单");
int i = 0;
cin >> i;
BY_FUNC ps[] = {
byNumb,byName,byMath };
switch (i)
{
case 1:
case 2:
case 3:
Sort(ps[i-1]);
break;
case 4:
Print();
default:
return i;
}
return i;
return 0;
}
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.AddTail(t);
fclose(pf);
}
void CStudent::Save()
{
FILE* pf = fopen("stud.lv", "w");
if (!pf)
{
puts("保存文件时失败!");
system("pause");
return;
}
POSITION pos = m_list.GetHeadPosition();
while (pos)
{
fwrite(pos, 1, sizeof(DATA), pf);
m_list.GetNext(pos);
}
}
CStudent::CStudent()
{
}
CStudent::~CStudent()
{
}
void CStudent::Start()
{
Load();
while (Menu())
;
}
主函数
文件名:main.cpp
#include <iostream>
#include "Student.h"
int main()
{
CStudent st;
st.Start();
return 0;
}