c++实现学生信息管理类

1.头文件

//Student.h
#pragma once
#include "List.h"
class CStudent
{
	CList m_list;
public:
	CStudent();
	~CStudent();
	void Start();
private:
	int Menu();
	int Input();
	void Delete();
	void Print();
	void Load();
	void Save();
	void SortIn();
	int PrintMenu();
	void SortOut(int nIndex);
	void PrintS(POSITION* ps);
	void Modify();
	void Find();
};


2.源文件

#include "Student.h"
#include <iostream>
#include <stdlib.h>
using namespace std;

CStudent::CStudent()
{
}

CStudent::~CStudent()
{
}


void CStudent::Start()
{
	Load();
	while (Menu())
		;
}

int CStudent::Menu()
{
	puts("");
	puts("1. 浏览所有信息");
	puts("2. 添加信息");
	puts("3. 删除信息");
	puts("4. 修改信息");
	puts("5. 查找信息");
	puts("0. 退出");
	printf("请选择:");
	int i = 0;
	scanf_s("%d", &i);
	switch (i)
	{
	case 1:
		while (PrintMenu())
			;
		break;
	case 2:
		while (Input())
			;
		break;
	case 3:
	{
		Delete();
		break;
	}
	case 4:
	{
		Modify();
		break;
	}
	case 5:
	{
		Find();
		break;
	}
	}
	return i;	
}
int CStudent::PrintMenu()
{
	puts("1. 按学号排序");
	puts("2. 按姓名排序");
	puts("3. 按成绩排序");
	puts("4. 不排序打印");
	puts("0. 返回主菜单");
	printf("请选择:");
	int i = 0;
	scanf_s("%d", &i);
	switch (i)
	{
	case 1:
	case 2:
	case 3:
		SortOut(i);
		break;
	case 4:
		Print();
		break;
	}
	return i;
}

int CStudent::Input()
{
	cout << "请输入学号:";
	DATA data;
	cin >> data.nNumb;
	cout << "请输入姓名:";
	cin >> data.sName;
	cout << "请输入成绩:";
	cin >> data.fScore;
	m_list.AddTail(data);
	Save();
	Print();
	cout << "是否继续添加?(y/n):";
	fflush(stdin);
	char c;
	scanf_s("%c", &c);
	while(c=='\n')
		scanf_s("%c", &c);
	return (c == 'y'||c=='Y') ? 1 : 0;
}

void CStudent::Delete()
{
	int nNumb;
	cout << "请输入要删除的学号:";
	cin >> nNumb;
	POSITION pos = m_list.GetHeadPosition();
	while (pos)
	{
		if (m_list.GetAt(pos).nNumb == nNumb)
		{
			m_list.RemoveAt(pos);
			Save();
			break;
		}
		m_list.GetNext(pos);
	}
	cout << "为找到指定学号" << endl;
	Print();
}

void CStudent::Print()
{
	cout << "学号\t姓名\t成绩" << endl;
	POSITION pos = m_list.GetHeadPosition();
	DATA data;
	while (pos)
	{
		data = m_list.GetAt(pos);
		cout << data.nNumb << "\t" <<data.sName<<"\t"<<data.fScore<<endl;
		m_list.GetNext(pos);
	}
}

void CStudent::Load()
{
	FILE* fp = fopen("data.info", "rb");
	if (!fp)
		return;
	DATA data;
	while (fread(&data,1,sizeof(DATA),fp)>0)
		m_list.AddTail(data);
	fclose(fp);
}


void CStudent::Save()
{
	FILE* fp = fopen("data.info", "wb");
	if (!fp)
		return;
	POSITION pos = m_list.GetHeadPosition();
	DATA data;
	while (pos)
	{
		data = m_list.GetAt(pos);
		fwrite(&data, 1, sizeof(DATA), fp);
		m_list.GetNext(pos);
	}
	fclose(fp);
}

//表内选择排序
void CStudent::SortIn()
{
	POSITION pos = m_list.GetHeadPosition();
	while (pos)
	{
		POSITION q = pos;
		m_list.GetNext(q);
		POSITION pMin = pos;
		while (q)
		{
			if (m_list.GetAt(q).nNumb < m_list.GetAt(pMin).nNumb)
				pMin = q;
			m_list.GetNext(q);
		}
		if (pMin != pos)
		{
			DATA data = m_list.GetAt(pMin);
			m_list.SetAt(pMin, m_list.GetAt(pos));
			m_list.SetAt(pos, data);
		}
		m_list.GetNext(pos);
	}
	Print();
}

bool ByNumb(DATA &data, DATA &minData)
{
	return data.nNumb < minData.nNumb;
}
bool ByName(DATA &data, DATA &minData)
{
	return strcmp(minData.sName, data.sName) > 0;
}
bool ByScore(DATA &data, DATA &minData)
{
	return data.fScore > minData.fScore;
}
typedef bool(*PFUNC)(DATA&, DATA&);
//表外指针数组排序
void CStudent::SortOut(int nIndex)
{
	int n = m_list.GetCount();
	POSITION *ps = new POSITION[n+1];
	POSITION pos = m_list.GetHeadPosition();
	PFUNC pFuc[] = {ByNumb,ByName,ByScore};
	int i = 0;
	while (pos)
	{
		ps[i++] = pos;
		m_list.GetNext(pos);
	}
	int j = 0;
	while (j < n-1)
	{
		int k = j+1;
		int min = j;
		while (k<n)
		{
			if ((pFuc[nIndex-1])(m_list.GetAt(ps[k]),m_list.GetAt(ps[min])))
				min = k;
			++k;
		}
		if (j != min)
		{
			POSITION tmp = ps[min];
			ps[min] = ps[j];
			ps[j] = tmp;
		}
		++j;
	}
	ps[n] = NULL;
	PrintS(ps);
	delete[]ps;
}

void CStudent::PrintS(POSITION* ps)
{
	cout << "学号\t姓名\t成绩" << endl;
	int i = 0;
	DATA data;
	while (ps[i])
	{
		data = m_list.GetAt(ps[i++]);
		cout << data.nNumb << "\t" << data.sName << "\t" << data.fScore << endl;
	}
}

void CStudent::Modify()
{
	Print();
	cout << "请输入要修改的学号:";
	int nNumb = 0;
	cin >> nNumb;
	POSITION pos = m_list.GetHeadPosition();
	while (pos)
	{
		if (m_list.GetAt(pos).nNumb == nNumb)
			break;
		m_list.GetNext(pos);
	}
	if (!pos)
	{
		cout << "未找到此学号!" << endl;
		return;
	}
	DATA data = m_list.GetAt(pos);
	cout << "请输入修改后的成绩:";
	cin >> data.fScore;
	m_list.SetAt(pos, data);
	Save();
	cout << "修改成功!" << endl;
}

void CStudent::Find()
{
	cout << "请输入要查找的学号:";
	int nNumb = 0;
	cin >> nNumb;
	POSITION pos = m_list.GetHeadPosition();
	while (pos)
	{
		DATA data = m_list.GetAt(pos);
		if (data.nNumb == nNumb)
		{
			cout << "学号\t姓名\t成绩\n";
			cout << data.nNumb << "\t" << data.sName << "\t" << data.fScore << endl;
			return;
		}
		m_list.GetNext(pos);
	}
	cout << "未找到此学号的信息" << endl;
}

发布了21 篇原创文章 · 获赞 20 · 访问量 2979

猜你喜欢

转载自blog.csdn.net/weixin_42844163/article/details/104122966