新建一个win32控制台应用程序(CList是MFC的类库,STL是标准类库,所有c++都有)
CStudent.h的内容
#include<afxtempl.h>
typedef struct Student
{
char name[20];
int number;
}student;
class CStudent
{
public:
CStudent();
void Start();
~CStudent();
private:
void ShowInit();
CList<student> m_list;
// 添加数据
bool Add();
// 浏览
void Printf();
// 删除
void Delete();
// 修改
int Modify();
// 排序
int Sort();
// 查询
int Find();
// 保存
int Save();
// 加载文件
int Load();
// 表外排序
int SortLink();
};
CStudent.c的内容
#include "stdafx.h"
#include "CStudent.h"
#include<iostream>
using namespace std;
CStudent::CStudent()
{
}
void CStudent::ShowInit()
{
cout << "\t学生管理系统\n" << endl;
cout << "\t1、添加\n" << endl;
cout << "\t2、浏览\n" << endl;
cout << "\t3、保存\n" << endl;
cout << "\t4、排序\n" << endl;
cout << "\t0、退出\n" << endl;
cout << "================================================\n" << endl;
cout << "请输入命令:";
}
void CStudent::Start()
{
int flag = 1;
int cmd;
Load();
ShowInit();
while (flag)
{
cin >> cmd;
cout << endl;
switch (cmd)
{
case 0:
flag = 0;
break;
case 1:
Add();
break;
case 2:
Printf();
break;
case 3:
Save();
ShowInit();
break;
case 4:
Sort();
ShowInit();
break;
case 5:
SortLink();
ShowInit();
break;
default:
break;
}
}
}
CStudent::~CStudent()
{
m_list.RemoveAll(); //释放所有的节点
}
// 添加数据
bool CStudent::Add()
{
// TODO: 在此处添加实现代码.
/*输入数据*/
student stu;
int flag = 1;
while (flag)
{
cout << "请输入名字:";
cin >> stu.name;
cout << endl;
cout << "请输入学号:";
cin >> stu.number;
cout << endl;
/*建立链表*/
m_list.AddTail(stu);
char cmd[2];
/*清理屏幕录入缓冲区*/
cout << "是否继续添加?(Y/N):";
cin >> cmd;
cout << endl;
if (0 == strcmp(cmd, "N") || 0 == strcmp(cmd,"n"))
flag = 0;
}
ShowInit();
return 0;
}
// 浏览
void CStudent::Printf()
{
// TODO: 在此处添加实现代码.
student stu;
POSITION pos = m_list.GetHeadPosition();
cout << "名字\t" << "学号\n" << endl;
while (pos)
{
stu = m_list.GetAt(pos);
cout << stu.name << "\t" << stu.number <<"\n"<< endl;
m_list.GetNext(pos);
}
ShowInit();
}
// 删除
void CStudent::Delete()
{
// TODO: 在此处添加实现代码.
student stu;
int flag = 1;
char name[20];
while (flag)
{
cout << "请输入要删除的名字:";
cin >> name;
cout << endl;
POSITION pos = m_list.GetHeadPosition();
cout << "名字\t" << "学号" << endl;
while (pos)
{
stu = m_list.GetAt(pos);
if(0 == strcmp(stu.name,name))
{
m_list.RemoveAt(pos);
break;
}
m_list.GetNext(pos);
}
char cmd[2];
cout << "是否继续删除?(Y/N):";
cin >> cmd;
cout << endl;
if (0 == strcmp(cmd, "N") || 0 == strcmp(cmd, "n"))
flag = 0;
}
ShowInit();
}
// 修改
int CStudent::Modify()
{
// TODO: 在此处添加实现代码.
return 0;
}
// 表内排序
int CStudent::Sort()
{
// TODO: 在此处添加实现代码.
POSITION p = m_list.GetHeadPosition(),m,q;
while (p)
{
m = q = p;
m_list.GetNext(q);
while (q)
{
if (m_list.GetAt(m).number > m_list.GetAt(q).number)
m = q;
m_list.GetNext(q);
}
if (m != p)
{
student stu = m_list.GetAt(p); //将p内的数据保存在stu中
m_list.SetAt(p,m_list.GetAt(m)); //将p修改为m内的数据
m_list.SetAt(m,stu); //将m内的数据改为stu的数据
}
m_list.GetNext(p);
}
return 0;
}
// 查询
int CStudent::Find()
{
// TODO: 在此处添加实现代码.
return 0;
}
// 保存
int CStudent::Save()
{
// TODO: 在此处添加实现代码.
student stu;
FILE *file;
fopen_s(&file,"./student.zjl","w");
if (!file)
return 0;
POSITION p = m_list.GetHeadPosition(); //获取头结点地址
while (p)
{
stu = m_list.GetAt(p); //获取对应结点数据,并赋值给stu
fwrite(&stu,1,sizeof(stu),file); //将stu中的数据写入文件中
m_list.GetNext(p); //获取下一个节点地址
}
fclose(file);
return 0;
}
// 加载文件
int CStudent::Load()
{
// TODO: 在此处添加实现代码.
student stu;
FILE *file;
fopen_s(&file,"./student.zjl","r");
if (!file)
return 0;
while (fread_s(&stu, sizeof(stu), 1, sizeof(stu), file) == sizeof(stu))
m_list.AddTail(stu); //向结点尾部插入数据
fclose(file);
return 0;
}
// 表外排序
int CStudent::SortLink()
{
// TODO: 在此处添加实现代码.
int n = m_list.GetCount(); //获取有多少个数据
POSITION *ps = new POSITION[n+1]; //创建POSITION内存
POSITION pos = m_list.GetHeadPosition();
/*将m_list表的数据地址复制给ps指针数组*/
int i = 0;
while (pos)
{
ps[i] = pos;
m_list.GetNext(pos);
i++;
}
/*按名字比较ps数组内内容*/
int m, j, q = 0;
while (q < n)
{
m = q + 1;
j = q;
while (m < n)
{
if (strcmp(m_list.GetAt(ps[m]).name,m_list.GetAt(ps[j]).name) > 0)
j = m;
m++;
}
if (j != q)
{
POSITION temp = ps[j];
ps[j] = ps[q];
ps[q] = temp;
}
q++;
}
/*显示*/
i = 0;
cout << "姓名\t" << "学号\n" << endl;
while (i < n)
{
student stu;
stu = m_list.GetAt(ps[i]);
cout << stu.name << "\t" << stu.number <<"\n" << endl;
i++;
}
delete []ps;
return 0;
}