Traversal of CList class library in MFC
#include <iostream>
#include "List.h"
using namespace std;
void main()
{
CList list;
list.AddHead(33);
list.AddHead(88);
list.AddHead(99);
POSITION pos = list.GetHeadPosition();
cout << "正向:" << endl;
while (pos)
cout << list.GetNext(pos) << endl;
cout << "反向:" << endl;
pos = list.GetTailPosition();
while (pos)
cout << list.GetPrev(pos) << endl;
cout << "共有:" << list.GetCount() << "条数据" << endl;
}
Define the CStudent class in C++
file name: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();
public:
CStudent();
~CStudent();
void Start();
};
Class member function implementation
file name: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();
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)
{
POSITION pos = m_list.GetHeadPosition(), m, q;
while (pos)
{
m = q = pos;
m_list.GetNext(q);
while (q)
{
//if (m_list.GetAt(q).nNumb < m_list.GetAt(m).nNumb)
if(pFunc(m_list.GetAt(q),m_list.GetAt(m)))
m = q;
m_list.GetNext(q);
}
if (m != pos)
{
DATA t = m_list.GetAt(pos);
m_list.SetAt(pos, m_list.GetAt(m));
m_list.SetAt(m, t);
}
m_list.GetNext(pos);
}
Print();
}
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 function
file name:main.cpp
#include <iostream>
#include "Student.h"
int main()
{
CStudent st;
st.Start();
return 0;
}