栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

实验内容

头文件

#pragma once
#include
using namespace std;

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

template
struct linkNode {
	T data;
	linkNode* link;
	linkNode(linkNode* ptr= NULL) {
		link = ptr;
		
	}
	linkNode(const T& item,linkNode *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 List {
public:
	List() {
		first = new linkNode();
	}
	List(const T &x) {
		first = new linkNode(x);
	}
	linkNode* Search(string x); //搜索含数据x的元素 
	linkNode* 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* first;  //第一个结点 
};




template    //将链表置为空表 
void List::makeEmpty() {
	linkNode* q;
	
	while (first->link != NULL) {
		
		q = first->link;
		first->link = q->link;
		delete  q;
	}
}

template
linkNode* List::Search(string x)  //搜索含数据x的元素 
{
	linkNode* current = first->link;
	
	while (current != NULL)
	{
		if (current->data.number == x)
		{
			break;
		}
		else
		{
			current = current->link;
		}
	}
	return current;
}

template
linkNode *List::Locate(int i)  //定位第i个元素的地址 
{
	//if (i < 0) return NULL;
	linkNode* current = first;
	
	int k = 0;
	while (current != NULL && k < i)
	{
		current = current->link;
		k++;
	}
	
	return current;
}

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

	linkNode* current = Locate(i);
	
	linkNode *newNode = new linkNode(x);//?????????????
	//linkNode* newNode;
	//newNode->data = x;
	newNode->link = current->link;
	current->link = newNode;;

	return 1;
}

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

template
void List::output()
{
	//makeEmpty();
	linkNode* current = first->link;
	
	
	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;
	}
	
}
template
int List::Length()const
{
	linkNode* current = first->link;
	int count = 0;
	while (current != NULL)
	{
		current = current->link;
		count++;
	}
	return count;


}

template
void List::intput(int sum) {
	linkNode* last = first;
	linkNode* newNode;
	T val;
	makeEmpty();
	
	//linkNode* newNode = new linkNode(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(val);
		if (newNode == NULL) {
			cerr << " 存储分配错误" << endl;
		}
		last->link = newNode;
		last = newNode;
		

	}
	
	
}


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
#include
using namespace std;
#include "linkNode.h"


int main()
{
	Student e, s, x;
	Listlist(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* temp;
			temp = list.Search(e.number);
			//cout << temp;
			if (temp != 0)
			{
				cout << "学号:" << temp->data.number<data.name<data.birthDay<data.sex<data.PC< 

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/312431.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号