C++-----利用链式存储结构实现学生健康管理系统

C+±----利用链式存储结构实现学生健康管理系统

问题描述
实现学生健康情况管理的几个操作功能(新建、插入、删除、从文本读取、写入文件和查询、屏幕输出等功能)。健康表中学生的信息有学号、姓名、出生日期、性别、身体状况等。

实验内容
在这里插入图片描述
头文件

#pragma once
#include<iostream>
using namespace std;

struct Student {
	string number;
	string name;
	string birthDay;
	string sex;
	string PC;
};

template<class T>
struct LinkNode {
	T data;
	LinkNode<T>* link;
	LinkNode(LinkNode<T>* ptr= NULL) {
		link = ptr;
		
	}
	LinkNode(const T& item,LinkNode<T> *ptr=NULL) {
		//data.number = item.number;
		//data.name = item.name;
		//data.birthDay = item.birthDay;
		//data.sex = item.sex;
		//data.PC = item.PC;
		data = item;
		link = ptr;

	}
	//初始化结点??????? 
};

template<class T>
class List {
public:
	List() {
		first = new LinkNode<T>();
	}
	List(const T &x) {
		first = new LinkNode<T>(x);
	}
	LinkNode<T>* Search(string x); //搜索含数据x的元素 
	LinkNode<T>* Locate(int i); //搜索第i个元素的地址 
	bool Insert(int i, T &x);  //在第i个地址插入值为x的元素 
	bool Remove(int i);   //删除第i个元素,返回值为x的值 
	int Length()const;         //求链表的长度 
	void output();
	void intput(int sum);
	void makeEmpty();
protected:
	LinkNode<T>* first;  //第一个结点 
};




template<class T>    //将链表置为空表 
void List<T>::makeEmpty() {
	LinkNode<T>* q;
	/*if (first->link == NULL) {
		cout << "空指针" << endl;*/
	while (first->link != NULL) {
		
		q = first->link;
		first->link = q->link;
		delete  q;
	}
}

template<class T>
LinkNode<T>* List<T>::Search(string x)  //搜索含数据x的元素 
{
	LinkNode<T>* current = first->link;
	/*if (current == NULL) {
		cout << "有错误" << endl;
	}*/
	while (current != NULL)
	{
		if (current->data.number == x)
		{
			break;
		}
		else
		{
			current = current->link;
		}
	}
	return current;
}

template<class T>
LinkNode<T> *List<T>::Locate(int i)  //定位第i个元素的地址 
{
	//if (i < 0) return NULL;
	LinkNode<T>* current = first;
	/*if (current == NULL) {
		cout << "有错误" << endl;
	}*/
	int k = 0;
	while (current != NULL && k < i)
	{
		current = current->link;
		k++;
	}
	/*if (current == NULL) {
		cout << "错误" << endl;
	}*/
	return current;
}

template<class T>
bool List<T>::Insert(int i, T &x)
{
	if (i<1 ) return 0;

	LinkNode<T>* current = Locate(i);
	/*if (current == NULL)
	{

		cout << "错误在次数" << endl;
	}*/
	LinkNode<T> *newNode = new LinkNode<T>(x);//?????????????
	//LinkNode<T>* newNode;
	//newNode->data = x;
	newNode->link = current->link;
	current->link = newNode;;

	return 1;
}

template<class T>
bool List<T>::Remove(int i)
{
	LinkNode<T>* current = Locate(i - 1);
	if (current == NULL)
	{
		return 0;
	}
	LinkNode<T>* del = current->link;
	current->link = del->link;
	return 1;
}

template<class T>
void List<T>::output()
{
	//makeEmpty();
	LinkNode<T>* current = first->link;
	/*if (current == NULL) {
		cout << "错误" << endl;
	}*/
	
	while (current != NULL)
	{
		
		cout << "学号:" << current->data.number << endl;
		cout << "姓名:" << current->data.name << endl;
		cout << "出生日期:" << current->data.birthDay << endl;
		cout << "性别:" << current->data.sex << endl;
		cout << "身体状况:" << current->data.PC << endl;
		//current = current->link;
		cout << "**********************" << endl;
		current = current->link;
	}
	/*for(int i=0;i<sum;i++)
	{
		cout<<"学号:"<<current;
	}*/
}
template<class T>
int List<T>::Length()const
{
	LinkNode<T>* current = first->link;
	int count = 0;
	while (current != NULL)
	{
		current = current->link;
		count++;
	}
	return count;


}
/*template<class T>
void List<T>::intput(int sum)
{
	List();
	LinkNode<T>* current = first->link;
	cout << "请输入学生的相关信息" << endl;
	for (int i = 0; i < sum; i++)
	{
		cout << "学号:";
		cin >> current->data.number;
		cout << "姓名:";
		cin >> current->data.name;
		cout << "出生日期:";
		cin >> current->data.birthDay;
		cout << "性别:";
		cin >> current->data.sex;
		cout << "身体状况:";
		cin >> current->data.PC;
		current = current->link;
		cout << "---------------------" << endl;
	}

}*/
template<class T>
void List<T>::intput(int sum) {
	LinkNode<T>* last = first;
	LinkNode<T>* newNode;
	T val;
	makeEmpty();
	/*cout << "学号:";
	cin >> val.number;
	cout << "姓名:";
	cin >> val.name;
	cout << "出生日期:";
	cin >> val.birthDay;
	cout << "性别:";
	cin >> val.sex;
	cout << "身体状况:";
	cin >> val.PC;
	cout << "*****************" << endl;*/
	//LinkNode<T>* newNode = new LinkNode<T>(val);
	for (int i = 1; i<=sum; i++) {
		cout << "学号:";
		cin >> val.number;
		cout << "姓名:";
		cin >> val.name;
		cout << "出生日期:";
		cin >> val.birthDay;
		cout << "性别:";
		cin >> val.sex;
		cout << "身体状况:";
		cin >> val.PC;
		cout << "*****************" << endl;
		newNode = new LinkNode<T>(val);
		if (newNode == NULL) {
			cerr << " 存储分配错误" << endl;
		}
		last->link = newNode;
		last = newNode;
		/*cout << "学号:";
		cin >> val.number;
		cout << "姓名:";
		cin >> val.name;
		cout << "出生日期:";
		cin >> val.birthDay;
		cout << "性别:";
		cin >> val.sex;
		cout << "身体状况:";
		cin >> val.PC;
		cout << "********************" << endl;*/

	}
	
	
}


void menu()
{
	cout << "     欢迎进入学生健康管理系统    " << endl;
	cout << "1-----新建学生健康表--------------" << endl;
	cout << "2-----向学生健康表插入学生信息----" << endl;
	cout << "3-----在健康表删除学生信息--------" << endl;
	cout << "4-----从文件中读取健康表信息--------" << endl;
	cout << "5-----向文件写入学生健康表信息--------" << endl;
	cout << "6-----在健康表中查询学生信息(按照学生学号来进行查找)--------" << endl;
	cout << "7-----在屏幕中输出全部学生信息--------" << endl;
	cout << "8-----退出--------" << endl;
}

主函数

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


int main()
{
	Student e, s, x;
	List<Student>list(x);
	int select, sum, num;
	while (1)
	{
		menu();
		//list.makeEmpty();
		cout << "请输入您的选择" << endl;
		cin >> select;
		switch (select)
		{
		case 1:
			cout << "请输入您要添加的学生的数量:" << endl;
			cin >> sum;
			list.intput(sum);
			break;
		case 2:
			cout << "请输入您要插入的位置";
			cin >> num;
			cout << "请输入学号:";
			cin >> s.number;
			cout << "请输入姓名:";
			cin >> s.name;
			cout << "请输入出生日期:";
			cin >> s.birthDay;
			cout << "请输入性别:";
			cin >> s.sex;
			cout << "请输入身体状况:";
			cin >> s.PC;
			//list.Locate(num);
			if (list.Insert(num, s)) {
				cout << "插入成功" << endl;
			}
			else
			{
				cout << "操作失败" << endl;
			}
			break;
		case 3:
			cout << "请输入您想要删除的学生的位置" << endl;
			cin >> num;
			if (list.Remove(num))
			{
				cout << "操作成功" << endl;
			}
			else
			{
				cout << "操作失败" << endl;
			}
			//L.length--; 
			break;
			//case 4:
			//case 5:
		case 6:
			cout << "请输入你要查询的学生的学号" << endl;
			cin >> e.number;
			LinkNode<Student>* temp;
			temp = list.Search(e.number);
			//cout << temp;
			if (temp != 0)
			{
				cout << "学号:" << temp->data.number<<endl;
				cout << "姓名:" << temp->data.name<<endl;
				cout << "出生日期:" << temp->data.birthDay<<endl;
				cout << "性别:" << temp->data.sex<<endl;
				cout << "身体状况:" << temp->data.PC<<endl;
			}
			else
			{
				cout << "未查询到你想要查询的学生的信息" << endl;
			}
			break;
		case 7:
			list.output();
			break;
		case 8:
			exit(1);
			break;

		}
	}
	return 0;

}

还有很多需要修改的地方,等过几天再回来修改

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值