/*main.cpp*/
**#include "manage_student.h"
#include "Student.h"
#include<iostream>
#include "Math_Student.h"
#include "IT_Student.h"
#include "English_Student.h"
using namespace std;
/*void traverse(Student *head) {
int index = 1; // 用於計數第幾個學生
Student *temp = head;
while (temp != NULL) { //同樣一直到后面沒有地址結束
cout << temp->math;
temp = temp->next;
}
}*/
void menu(Student *head,manage_student Chead) {
int m = 0;
int ID = 0;
int conter = 0;
while (1) {
cout << " ***************請選擇您需要的操作:****************" << endl;
cout << " * 1. 增加學生信息 *" << endl;
cout << " * 2. 刪除學生信息 *" << endl;
cout << " * 3. 修改學生信息 *" << endl;
cout << " * 4. 按學號ID查詢 *" << endl;
cout << " * 5. 查詢某位學生的平均分 *" << endl;
cout << " * 6. 某個學生的三科平均成績 *" << endl;
cout << " * 7. 按公共課平均分從高到低排序 *" << endl;
cout << " * 8. 遍歷學生信息 *" << endl;
cout << " * 9. 結束功能並把信息寫入文件中 *" << endl;
cout << " * 0. 退出程序 *" << endl;
cout << " ***************************************************" << endl;
rewind(stdin);
cin >> m;
if (m >=0 && m < 10) {
switch (m) {
case 1: Chead.append_student(head); break;
case 2: {cout << "要刪除學生信息的學號:";
cin >> ID;
head = Chead.delete_student(head, ID, Chead.getlength(head)); }break;
case 3: {cout << "需要修改學生信息的同學ID:";
cin >> ID;
Chead.change_message(head, ID); }break;
case 4: {cout << "按學號查詢 請輸入需要查詢的同學的ID為:";
cin >> ID;
Chead.search(head, ID); }break;
case 5: {
cout << "請輸入您需要查詢學生的平均成績的ID:";
cin >> ID;
Chead.get_average_score(head,Chead.getlength(head),ID );
}break;
case 6: {
cout << "請輸入您需要查詢學生的平均成績的ID:";
cin >> ID;
Student *t = head;
while (t != NULL) {
if (t->ID == ID) {
cout << "該同學的公共課平均成績為:" << t->get_main_average() << endl;
}t = t->next;
}
break;
}
case 7: {
Chead.rank_average_score(head, Chead.getlength(head));
cout << "排序成功!"; break;
}
case 8: {
Student *t = head;
while (t != NULL) {
t->traverse(t);
t=t->next;
}break;
}
case 9: {
Chead.openfile(head, Chead.getlength(head));
cout << "寫入文件成功!"; break;
}
case 0: {
Chead.release(head);//結束之前free掉鏈表
return; }
}
}
else cout << "輸入有錯誤 請輸入0-9的整數"; continue;
}
/*manage_student.h*/
#pragma once
#include<iostream>
#include "Student.h"
class manage_student
{
public:
//存入文件 傳入頭指針和長度
void openfile(Student *head, int len);
//成績排序 傳入頭指針和長度
void rank_average_score(Student *head,int len);
//修改信息 指定ID找到學生信息 修改 傳入頭 和 ID
void change_message(Student *head, int ID);
//增加信息 傳入頭就可以
void append_student(Student *head);
//刪除學生信息 傳入頭 需要刪除學生信息的ID 鏈表長度
Student * delete_student(Student *head,int ID,int len);
//創建鏈表
Student * create();
//搜索查詢學生信息 根據ID搜索
void search(Student *head, int ID);
//得到鏈表的長度 傳入頭 返回鏈表長度
int getlength(Student *head);
//得到平均分 顯示平均分
void get_average_score(Student *head, int len,int ID);
void release(Student *head);
manage_student();
~manage_student();
};
/*manage_student.cpp*/
#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 << " 基本信息:" << " 學號" << t->ID << " 姓名:" << t->name << " 年齡:" << t->age << " 專業:" << t->get_major() << endl
<< " 公共課成績:" << " 數學:" << t->math << " 英語:" << t->english << " 體育:" << t->sport << endl
<< " 專業課成績:" << " 實變函數" << t->get_shi_bian_fun() << " 泛函分析:" << t->get_fan_han_fen_xi() << " 微分幾何" << t->get_wei_fen_jihe() << endl;
t = t->next; continue;
}
if (t->get_major() == "English_Student") {
ofile << "錄入的各學生的信息如下: " << endl;
ofile << " 基本信息:" << " 學號" << t->ID << " 姓名:" << t->name << " 年齡:" << t->age << " 專業:" << t->get_major() << endl
<< " 公共課成績:" << " 數學:" << t->math << " 英語:" << t->english << " 體育:" << t->sport << endl
<< " 專業課成績:" << " 綜合英語:" << t->get_zonghe_english() << " 口語英語:" << t->get_spoken_english() << endl;
t = t->next; continue;
}
if (t->get_major() == "IT_Student") {
ofile << "錄入的各學生的信息如下: " << endl;
ofile << " 基本信息:" << " 學號" << t->ID << " 姓名:" << t->name << " 年齡:" << t->age << " 專業:" << t->get_major() << endl
<< " 公共課成績:" << " 數學:" << t->math << " 英語:" << t->english << " 體育:" << t->sport << endl
<< " 專業課成績:" << " 組成原理:" << t->get_zu_cheng_yuanli() << " 體系結構:" << t->get_ti_xi_jie_gou() << " 匯編語言:" << t->get_hui_bian() << endl;
t = t->next; continue;
}
}
ofile.close();
return ;
}
void manage_student::get_average_score(Student *head, int len,int ID) {
Student *t = head; //用t遍歷
for (int i = 0; i < len; ++i) {
if (t->ID == ID) {
//用基類的指針調用子類的函數 返回平均分 輸出
cout << "該學生的平均成績為:" << t->get_average_score() << endl; return;
}
t = t->next;
}
cout << "請確認您輸入的ID是否有誤" << endl;
}
;
}
void main() {
cout << "歡迎進入學生管理系統!請輸入需要添加的學生數: "<<endl;
Student temp; //用來初始化指針
Student *Head=&temp;
Student *t=&temp;
int len;
manage_student Chead;
Head=Chead.create();//把頭地址傳回來給Head 指向的是第一個類stu
menu(Head, Chead); //調用菜單函數
system("pause");
return;
}