问题描述
实现学生健康情况管理的几个操作功能(新建、插入、删除、从文本读取、写入文件和查询、屏幕输出等功能)。健康表中学生的信息有学号、姓名、出生日期、性别、身体状况等。
实验内容
头文件
#pragma once #includeusing 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; List 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 * temp; temp = list.Search(e.number); //cout << temp; if (temp != 0) { cout << "学号:" << temp->data.number< data.name< data.birthDay< data.sex< data.PC< 还有很多需要修改的地方,等过几天再回来修改



