#include "manage_student.h"
#include "Student.h"
#include<iostream>
#include "Math_Student.h"
#include "IT_Student.h"
#include "English_Student.h"
#include<string.h>
using namespace std;
#include<fstream>
#include<iomanip>
manage_student::manage_student()
{
}
manage_student::~manage_student()
{
}
Student * manage_student:: create() {
int num = 0; //来计数 输入的第几个学生
int temp; //用来输入选择初始化的学生的专业
int len = 0; //表示链表的长度
cin >> len; //也就是初始输入需要添加的学生数目
Student stu;//用来初始化指针
Student *pre = &stu;
Student *c = &stu;
Student *Head = &stu; //最后要返回的头指针
while (num < len) {
cout << "************您需要添加此学生的专业是? 1:数学系 2:英语系 3:计算机系" << endl;
cin >> temp;
if (temp == 1) {
c = new Math_Student; //一定要new一个**********不然会覆盖之前的
if (num == 0) { // 千万不能 Math_Student c 这个错误调了几天!!!!!!
Head = c; // 头指针的地址
Head->next = NULL; //第一个的尾给空
pre = Head; //pre指向 前一个 也就是头
}
if (num) {
pre->next = c; //前一个的尾接到下一个的地址
pre = pre->next; //pre 指向next指针
pre->next = NULL; //next指针指向的 弄为空
}
++num;
continue;
}
else if (temp ==2 ) {
c = new English_Student;
if (num == 0) {
Head = c; // 头指针的地址
Head->next = NULL; //第一个的尾给空
pre = Head; //pre指向 前一个 也就是头
}
if (num) {
pre->next = c; //前一个的尾接到下一个的地址
pre = pre->next; //pre 指向next指针
pre->next = NULL; //next指针指向的 弄为空
}
++num;
continue;
}
else if (temp == 3) {
c = new IT_Student;
if (num == 0) {
Head = c; // 头指针的地址
Head->next = NULL; //第一个的尾给空
pre = Head; //pre指向 前一个 也就是头
}
if (num) {
pre->next = c; //前一个的尾接到下一个的地址
pre = pre->next; //pre 指向next指针
pre->next = NULL; //next指针指向的 弄为空
}
++num;
continue;
}
else {
cout << "输入有误!请重新输入" << endl; continue;
}
}
return Head; //把头指针返回到main里 便于取用
}
int manage_student::getlength(Student * head)
{
int num = 1; //=0 或者 =1 取决于while的判断条件
Student *t=head;
while (t->next != 0) { //当head指向后面没有了 它就是NULL 结束
++num;
t = t->next; //如果head 不是NULL ++num后要把head指针指向最后
}
return num; //返回int长度
}
void manage_student::search(Student *head,int ID) {
Student *t = head; //用*t来遍历
while (t != NULL) { //只要t不是空 就进入
if (t->ID == ID) { //如果匹配到 ID
t->traverse(t); //用基类的指针调用子类的遍历方法
}t = t->next;
}
}
void manage_student::release(Student *head) {
Student *n; //需要一个指针存着下一个地址
while (head != NULL) {
n = head->next; //把n指向下一块要释放的地址
free(head);
head = n; //然后再把head从前一个地址移到下一个地址
}
}
void manage_student::append_student(Student *head) {
Student *c = head; //都用基类的指针来操作
Student *t = head;
int temp;
while (t->next != NULL) {
t = t->next; //把t移动到最后一块链表
}
cout << "************您需要添加此学生的专业是? 1:数学系 2:英语系 3:计算机系" << endl;
cin >> temp;
if (temp == 1) {
c = new Math_Student; //一定要new一个
t->next = c; //前一个的尾接到下一个的地址
t = t->next; //pre 指向next指针
t->next = NULL; //next指针指向的 弄为空
}
else if (temp == 2) {
c = new English_Student; //一定要new一个
t->next = c; //前一个的尾接到下一个的地址
t = t->next; //pre 指向next指针
t->next = NULL; //next指针指向的 弄为空
}
else if (temp == 3) {
c = new IT_Student; //一定要new一个
t->next = c; //前一个的尾接到下一个的地址
t = t->next; //pre 指向next指针
t->next = NULL; //next指针指向的 弄为空
}
else {
cout << "输入有误!请重新输入" << endl;
}
}
Student * manage_student::delete_student(Student *head, int ID,int len)
{
//用*t来遍历
Student *t = head;
Student *temp;//临时指针
for (int i = 0; i < (len - 1); ++i) {
if (i == 0) { //如果就是第一块链表 需要特殊处理 因为头Head会变
if (head->ID == ID) { head = head->next; delete t; return head; } //一定要返回一个新的头Head
if ((t->next)->ID == ID) { //如果是第二块链表匹配
temp = t->next; //把第一块跟第三块连接起来
t->next = (t->next)->next;
delete temp; return head; //delete调 第二块 返回头Head
}
}
if (i != 0) {
if ((t->next)->ID == ID) { //如果遍历的不是第一块了,操作都一样
temp = t->next; //前一块的next
t->next = (t->next)->next; //前后连接
delete temp; return head; //删掉中间
}t = t->next;
}
}cout << "删除成功!" << endl; return head; //返回头指针
}
void manage_student::change_message(Student *head, int ID) {
Student *t = head;
while (t!= NULL) {
if (t->ID == ID) {//ID匹配 进入
// 匹配属于数学系
if (t->get_major() == "Math_Student") {
//在此处修改公共的信息
cout << "*******请输入要修改的学生信息:" << endl;
cout << " ID:" << endl;
cin >> t->ID;
cout << " 姓名:" << endl;
cin >> t->name;
cout << " 年龄:" << endl;
cin >> t->age;
cout << " 数学:" << endl;
cin >> t->math;
cout << " 英语:" << endl;
cin >> t->english;
cout << " 体育:" << endl;
cin >> t->sport;
t->change(); //然后再去子类里面调用change修改特有的成员变量
}
if (t->get_major() == "English_Student") {
cout << "*******请输入要修改的学生信息:" << endl;
cout << " ID:" << endl;
cin >> t->ID;
cout << " 姓名:" << endl;
cin >> t->name;
cout << " 年龄:" << endl;
cin >> t->age;
cout << " 数学:" << endl;
cin >> t->math;
cout << " 英语:" << endl;
cin >> t->english;
cout << " 体育:" << endl;
cin >> t->sport;
t->change();
}
if (t->get_major() == "IT_Student") {
cout << "*******请输入要修改的学生信息:" << endl;
cout << " ID:" << endl;
cin >> t->ID;
cout << " 姓名:" << endl;
cin >> t->name;
cout << " 年龄:" << endl;
cin >> t->age;
cout << " 数学:" << endl;
cin >> t->math;
cout << " 英语:" << endl;
cin >> t->english;
cout << " 体育:" << endl;
cin >> t->sport;
t->change();
}
cout << "信息修改完成!"<<endl; return;
}
t = t->next;
}
cout << "对不起!查无此人,请重新确认学号是否输入正确,谢谢!"<<endl;
}
void manage_student::rank_average_score(Student *head,int len) {
Student *t = head; //用*t来 遍历
Student *pre = head;//把前面的存着 来跟后面的比较
t = t->next; //t往后移动一个
int ID;
char name[20];
float math, english, sport;
for (int i = 0; i < (len - 1); ++i) {
while (t != NULL) {
//如果后面的平均分比前面的平均分高 就进入
if ( t->get_average_score() >pre->get_average_score() ){
strcpy_s(name, t->name); strcpy_s(t->name, pre->name); strcpy_s(pre->name, name);
ID = t->ID; t->ID = pre->ID; pre->ID = ID;
math = t->math; t->math = pre->math; pre->math = math;
sport = t->sport; t->sport = pre->sport; pre->sport = sport;
english = t->english; t->english = pre->english; pre->english = english;
}
t = t->next;
//t每次循环完了之后 就把pre后移一个 t接在pre的后面
}pre = pre->next; t = pre->next;
}
}
void manage_student::openfile(Student *head, int len) {
//同样用t遍历
Student *t = head;
ofstream ofile; //定义输出文件
ofile.open("D:\\学生信息"); //作为输出文件打开
//for循环把链表 里面的信息写入文件
for (int i = 0; i < len; ++i) {
//判断系别 因为每个专业的成员不一样
if (t->get_major() == "Math_Student") {
ofile << "录入的各学生的信息如下: " << endl;
ofile << " 基本信息:" << " 学号
【本人课设代码】C++学生成绩管理系统课程设计【多态 虚函数】
5星 · 超过95%的资源 需积分: 46 38 浏览量
2020-02-19
11:41:24
上传
评论 6
收藏 11KB ZIP 举报
本人课设代码上传
- 粉丝: 2
- 资源: 12
最新资源
- mongodb数据库基本操作.pdf
- C#,布尔可满足性问题(Boolean Satisfiability Problem)算法与源代码
- C#,回文分割问题(Palindrome Partitioning Problem)算法与源代码
- C#,煎饼排序问题(Pancake Sorting Problem)算法与源代码
- C#,排列组合的堆生成法(Heap’s Algorithm for generating permutations)算法与源代码
- C#,老鼠迷宫问题的回溯法求解(Rat in a Maze)算法与源代码
- 6693eeb8d683458a07938615fba9e68f.apk
- C#,数值计算,解微分方程的龙格-库塔二阶方法与源代码
- C#,数值计算,用割线法(Secant Method)求方程根的算法与源代码
- C#,子集和问题(Subset Sum Problem)的算法与源代码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论10