|
发表于 2020-4-16 13:01:02
|
显示全部楼层
本楼为最佳答案
 之前的回复有一些不正确,思考了一下。直接回复完整代码,该代码经过测试,且附带输入输出信息。
相关信息:windows10 64位 vs2015 解决方案平台x86
Student.h代码
- #pragma once
- #include<iostream>
- #include<string>
- #include<cstdlib>
- using namespace std;
- class Student
- {
- private:
- friend class StudentMessage;
- Student* next; //指向下一个节点的指针
- Student* pre; //指向上一个节点的指针
-
- //string Class, Name, Sex; 建议分开定义,代码清晰
- string Class;
- string Name;
- string Sex;
- long long Number;
- double Score[3];
- public:
- //构造和析构函数统一在类外定义
- Student();
- Student(string _name, double* _score, long long _number, string _class, string _sex);
- ~Student() {}
- };
- class StudentMessage
- {
- private:
- friend class Student;
- //Student* num[1000]{};//不需要额外的空间保存节点用来删除,
- //而且此处申请的空间大小为4*1000kb。楼主可多定义几个StudentMessage对象测试一下会怎么样。
- Student* first; //头指针
- //Student* ex; //不需要
- int nums; //学生数
- public:
- //构造和析构函数统一在类外定义
- StudentMessage();
- //以下注释代码,如果first和ex发生改变,内存泄漏。
- //而且在该成程序中,只需要定义头指针就好。双向链表不需要ex(指向链表倒数第二个节点)
- /*{
- first = new Student;//???
- ex = new Student;//???
- nums = 0;
- }
- */
- ~StudentMessage();
- //{
- //delete first;
- //delete ex;
- //}
- void Insert();
- long Table();
- void Find();
- void Find_Num();
- void Find_Name();
- //void Change();
- void Display();
- void Delete();
- void Delete_Name();
- void Delete_Num();
- };
复制代码
Student.cpp代码
- #include"Student.h"
- Student::Student()
- {
- Name = Class = Sex = "null";
- Score[0] = Score[1] = Score[2] = 0;
- Number = 0;
- next = NULL;
- pre = NULL;
- }
- Student::Student(string _name, double* _score, long long _number, string _class, string _sex)
- {
- Name = _name;
- Score[0] = _score[0];
- Score[1] = _score[1];
- Score[2] = _score[2];
- Number = _number;
- Class = _class;
- Sex = _sex;
- next = NULL;
- pre = NULL;
- }
- StudentMessage::StudentMessage()
- : first(NULL)
- , nums(0)
- {
- }
- StudentMessage::~StudentMessage()
- {
- }
- long StudentMessage::Table()
- {
- long ch;
- cout << "************* 学生信息管理系统 ****************" << endl;
- cout << endl;
- cout << "************* 1、显示所有学生信息 ****************" << endl;
- cout << "************* 2、录入学生信息 ****************" << endl;
- cout << "************* 3、查询学生信息 ****************" << endl;
- cout << "************* 4、修改学生信息 ****************" << endl;
- cout << "************* 5、删除学生信息 ****************" << endl;
- cout << "************* 6、系统初始化 ****************" << endl;
- cout << "************* 0、退出系统 ****************" << endl;
- cout << endl;
- cout << "“选项(0—6)”" << endl;
- cout << "请输入:";
- cin >> ch;
- return ch;
- }
- void StudentMessage::Insert()
- {
- long long num;
- string name, sex, cla;
- double score[3];
- cout << "请输入学生姓名:";
- cin >> name;
- cout << endl;
- cout << "请输入学生性别:";
- cin >> sex;
- cout << endl;
- cout << "请输入学生学号:";
- cin >> num;
- cout << endl;
- cout << "请输入学生班级:";
- cin >> cla;
- cout << endl;
- cout << "请输入第一门成绩:";
- cin >> score[0];
- cout << endl;
- cout << "请输入第二门成绩:";
- cin >> score[1];
- cout << endl;
- cout << "请输入第三门成绩:";
- cin >> score[2];
- cout << endl;
- Student* newstu = new Student(name, score, num, cla, sex);
- Student* pNode = this->first;
- if (first == NULL)
- {
- first = newstu;
- nums++;
- }
- else
- {
- while (pNode)
- {
- if (pNode->next)
- {
- pNode = pNode->next;
- }
- else
- {
- break;
- }
- }
- pNode->next = newstu;
- newstu->pre = pNode;
- nums++;
- }
- }
- void StudentMessage::Display()
- {
- if (nums == 0)
- {
- cout << "当前系统中无学生信息!!" << endl;
- }
- else
- {
- Student * p = first;//此处修改,原代码为Student * p = first->next;。修改原因first指向第一个节点
- while (p != NULL)
- {
- cout << "姓名:" << p->Name << " " <<
- "学号:" << p->Number << " " <<
- "班级:" << p->Class << " " <<
- "性别:" << p->Sex << " " <<
- "第一门成绩:" << p->Score[0] << " " <<
- "第二门成绩:" << p->Score[1] << " " <<
- "第三门成绩:" << p->Score[2] << endl;
- p = p->next;
- }
- }
- }
- void StudentMessage::Find()
- {
- //按1、姓名 2、学号 查找;
- cout << "【1、姓名查找】" << endl;
- cout << "【2、学号查找】" << endl;
- cout << "【0、返回】" << endl;
- cout << "请选择:" << endl;
- long long op;
- cin >> op;
- switch (op)
- {
- case 1:Find_Name(); break;
- case 2:Find_Num(); break;
- default:
- cout << "请正确输入:";
- cin >> op;
- }
- cout << "是否继续查找?" << endl;
- cout << "【1、继续查找】" << endl;
- cout << "【0、返回菜单】" << endl;
- int op1;
- cin >> op1;
- switch (op1)
- {
- case 1:
- Find();
- break;
- default:
- break;
- }
- }
- void StudentMessage::Find_Name()
- {
- Student* p = first;//此处修改,原代码为Student * p = first->next;。修改原因first指向第一个节点
- string name;
- cout << "请输入姓名:";
- cin >> name;
- int nor = 0;
- while (p != NULL)
- {
- if (p->Name == name)
- {
- nor++;
- cout << "姓名:" << p->Name << " " <<
- "学号:" << p->Number << " " <<
- "班级:" << p->Class << " " <<
- "性别:" << p->Sex << " " <<
- "第一门成绩:" << p->Score[0] << " " <<
- "第二门成绩:" << p->Score[1] << " " <<
- "第三门成绩:" << p->Score[2] << endl;
- }
- p = p->next;
- }
- if (nor == 0)
- {
- cout << "无此同学..." << endl;
- cout << "是否继续?";
- cout << "【1、是】" << endl;
- cout << "【0、否】" << endl;
- long long op;
- cin >> op;
- while (op)
- {
- switch (op)
- {
- case 1:
- Find_Name();
- break;
- default:
- cout << "请正确输入:";
- cin >> op;
- }
- }
- }
- }
- void StudentMessage::Find_Num()
- {
- Student* p = first;//此处修改,原代码为Student * p = first->next;。修改原因first指向第一个节点
- long long number;
- cout << "请输入学号:";
- cin >> number;
- int nor = 0;
- while (p != NULL)
- {
- if (p->Number == number)
- {
- nor++;
- cout << "姓名:" << p->Name << " " <<
- "学号:" << p->Number << " " <<
- "班级:" << p->Class << " " <<
- "性别:" << p->Sex << " " <<
- "第一门成绩:" << p->Score[0] << " " <<
- "第二门成绩:" << p->Score[1] << " " <<
- "第三门成绩:" << p->Score[2] << endl;
- }
- p = p->next;
- }
- if (nor == 0)
- {
- cout << "无此同学..." << endl;
- cout << "是否继续?";
- cout << "【1、是】" << endl;
- cout << "【0、否】" << endl;
- long long op;
- cin >> op;
- while (op)
- {
- switch (op)
- {
- case 1:
- Find_Num();
- break;
- default:
- cout << "请正确输入:";
- cin >> op;
- }
- }
- }
- }
- void StudentMessage::Delete()
- {
- //按1、姓名 2、学号 删除;
- cout << "【1、删除姓名】" << endl;
- cout << "【2、删除学号】" << endl;
- int op;
- cin >> op;
- switch (op)
- {
- case 1:
- Delete_Name();
- break;
- case 2:
- Delete_Num();
- break;
- default:
- cout << "请正确输入:";
- cin >> op;
- }
- }
- void StudentMessage::Delete_Name()
- {
- Student* pNode = first;//此处修改,原代码为Student * p = first->next;。修改原因first指向第一个节点
- string name;
- cout << "请输入姓名:";
- cin >> name;
- while (pNode != NULL)
- {
- if (pNode->Name == name)
- {
- cout << "姓名:" << pNode->Name << " "
- << "学号:" << pNode->Number << " "
- << "班级:" << pNode->Class << " "
- << "性别:" << pNode->Sex << " "
- << "第一门成绩:" << pNode->Score[0] << " "
- << "第二门成绩:" << pNode->Score[1] << " "
- << "第三门成绩:" << pNode->Score[2] << endl;
- break;
- }
- pNode = pNode->next;
- }
- if (pNode == NULL)
- {
- cout << "无此同学..." << endl;
- cout << "是否继续?";
- cout << "【1、是】" << endl;
- cout << "【0、否】" << endl;
- long long op;
- cin >> op;
- while (op)
- {
- switch (op)
- {
- case 1:
- Find_Name();
- break;
- default:
- cout << "请正确输入:";
- cin >> op;
- }
- }
- }
- else
- {
- int op;
- cout << "是否删除?" << endl;
- cout << "【1、是】" << endl;
- cout << "【0、否】" << endl;
- cin >> op;
- if (op == 1)
- {
- if (pNode->pre != NULL)
- {
- pNode->pre->next = pNode->next;
- }
- if (pNode->next != NULL)
- {
- pNode->next->pre = pNode->pre;
- }
- if (pNode == first)
- {
- first = pNode->next;
- }
- delete pNode;
- --nums;
- if (nums == 0)
- {
- first = NULL;
- }
- }
- }
- }
- void StudentMessage::Delete_Num()
- {
- Student* pNode = first;//此处修改,原代码为Student * p = first->next;。修改原因first指向第一个节点
- long long number;
- cout << "请输入学号:";
- cin >> number;
- while (pNode != NULL)
- {
- if (pNode->Number == number)
- {
- cout << "姓名:" << pNode->Name << " "
- << "学号:" << pNode->Number << " "
- << "班级:" << pNode->Class << " "
- << "性别:" << pNode->Sex << " "
- << "第一门成绩:" << pNode->Score[0] << " "
- << "第二门成绩:" << pNode->Score[1] << " "
- << "第三门成绩:" << pNode->Score[2] << endl;
- break;
- }
- pNode = pNode->next;
- }
- if (pNode == NULL)
- {
- cout << "无此同学..." << endl;
- cout << "是否继续?";
- cout << "【1、是】" << endl;
- cout << "【0、否】" << endl;
- long long op;
- cin >> op;
- while (op)
- {
- switch (op)
- {
- case 1:
- Find_Name();
- break;
- default:
- cout << "请正确输入:";
- cin >> op;
- }
- }
- }
- else
- {
- int op;
- cout << "是否删除?" << endl;
- cout << "【1、是】" << endl;
- cout << "【0、否】" << endl;
- cin >> op;
- if (op == 1)
- {
- if (pNode->pre != NULL)
- {
- pNode->pre->next = pNode->next;
- }
- if (pNode->next != NULL)
- {
- pNode->next->pre = pNode->pre;
- }
- if (pNode == first)
- {
- first = pNode->next;
- }
- delete pNode;
- --nums;
- if (nums == 0)
- {
- first = NULL;
- }
- }
- }
- }
复制代码
main.cpp
- #include <iostream>
- #include "Student.h"
- int main()
- {
- StudentMessage studentmessage;
-
- long ch = studentmessage.Table();
- do
- {
- switch (ch)
- {
- case 1:
- studentmessage.Display();
- break;
- case 2:
- studentmessage.Insert();
- break;
- case 3:
- studentmessage.Find();
- break;
- case 4:
- //studentmessage.Change();
- break;
- case 5:
- studentmessage.Delete();
- break;
- case 6:
- //do 系统初始化
- break;
- default:
- break;
- }
- ch = studentmessage.Table();
- } while (ch);
- return 0;
- }
复制代码
测试输入输出信息:
|
评分
-
查看全部评分
|