/*Student.h*/
#pragma once #include<string.h> using namespace std; #include<string> class Student { public: //ID age name math english sport 專業 是子類都具有的 所以就在基類中聲明 int ID; int age; string major = "1"; char name[20]; float math, english, sport; Student *next; //鏈表指針 //一下get函數 因為子類的專業不同 所以實現的方式不同 聲明虛函數 在子類中實現 virtual float get_shi_bian_fun(); virtual float get_fan_han_fen_xi() ; virtual float get_wei_fen_jihe(); virtual float get_zonghe_english(); virtual float get_spoken_english(); virtual float get_zu_cheng_yuanli(); virtual float get_ti_xi_jie_gou(); virtual float get_hui_bian(); virtual string get_major(); //因為每個子類遍歷的東西也不同 所以遍歷函數也要為虛函數 在子類中以不同的方式來實現 virtual void traverse(Student *t); virtual float get_main_average();//取平均數也一樣,返回平均成績 virtual float get_average_score(); virtual void change(); //每個成員的科目不一樣,需要修改的東西也不一樣 Student(); virtual ~Student(); };
/*Student.cpp
/*Math_Student.h*/ #pragma once #include "Student.h" #include <string> class Math_Student : public Student { public: //特有的科目成員 都聲明為float float shi_bian_fun; float fan_han_fen_xi; float wei_fen_jihe; string major = "Math_Student";//專業直接初始化 Math_Student(); ~Math_Student(); //get函數 方便在manage類里面拿到數據信息 float get_shi_bian_fun(); float get_fan_han_fen_xi() ; float get_wei_fen_jihe() ; float get_main_average(); float get_average_score(); string get_major(); //遍歷信息 void traverse(Student *t); void change(); //修改信息 };
/*Math_Student.cpp*/ #include "Math_Student.h" #include<iostream> #include"string.h" using namespace std; Math_Student::Math_Student() { cout << "請輸入學生信息:" << endl; cout << "age:" << endl; cin >> age; getchar(); cout << "name:"; cin >> name; getchar(); cout << "ID:" << endl; cin >> ID; getchar(); cout << "sport_score:" << endl; cin >>sport; getchar(); cout << "math_score:" << endl; cin >> math; getchar(); cout << "english_score" << endl; cin >> english; getchar(); cout << "shi_bian_fun_score" << endl; cin >>shi_bian_fun; getchar(); cout << "fan_han_fen_xi_score" << endl; cin >>fan_han_fen_xi; getchar(); cout << "wei_fen_jihe_score" << endl; cin >>wei_fen_jihe; getchar(); cout << "錄入成功!" << endl; } Math_Student::~Math_Student() { } void Math_Student::change() { cout << " 實變函數:" << endl; cin >> shi_bian_fun; getchar(); cout << " 泛函分析:" << endl; cin >> fan_han_fen_xi; getchar(); cout << " 微分幾何:" << endl; cin >> wei_fen_jihe; getchar(); } void Math_Student::traverse(Student *t) { //公共的信息修改 在 manage 里面 這里來到類里面來修改特有的信息 cout << "學生信息為:" << endl << " 學號:" << t->ID << " 姓名:" <<name << " 年齡:" <<age << " 專業:" <<major << endl <<"公共課成績:"<<endl << " 數學:" <<math << " 英語:" <<english << " 體育:" << sport << endl << "專業課成績:"<<endl <<" 實變函數:"<<t->get_shi_bian_fun()<<" 泛函分析:"<<t->get_fan_han_fen_xi()<<" 微分幾何:"<<t->get_wei_fen_jihe()<<endl <<endl; } float Math_Student::get_main_average() { return ((math + english + sport) / 3.0); } float Math_Student::get_average_score() { float av = 0; av += math; av += sport; av += english, av += shi_bian_fun, av += fan_han_fen_xi, av += wei_fen_jihe; return (av / 6.0); } float Math_Student:: get_shi_bian_fun() { return shi_bian_fun; } float Math_Student ::get_fan_han_fen_xi() { return fan_han_fen_xi; } float Math_Student::get_wei_fen_jihe() { return wei_fen_jihe; } string Math_Student::get_major() { return major; } ;
/*English_Student.h*/ #pragma once #include "Student.h" #include <string> class English_Student : public Student { public: //特有成員變量 float zonghe_english; float spoken_english; string major = "English_Student"; English_Student(); ~English_Student(); float get_zonghe_english(); float get_spoken_english(); string get_major(); float get_main_average(); void change(); void traverse(Student *t); float get_average_score(); };
/*English_Student.cpp*/ #include "English_Student.h" #include<iostream> #include"string.h" using namespace std; English_Student::English_Student() { cout << "age:" << endl; cin >> age; getchar(); cout << "name:"; cin >> name; getchar(); cout << "ID:" << endl; cin >> ID; getchar(); cout << "sport_score:" << endl; cin >> sport; getchar(); cout << "math_score:" << endl; cin >> math; getchar(); cout << "english_score" << endl; cin >> english; cout << "zonghe_english_score" << endl; cin >> zonghe_english; cout << "spoken_english_score" << endl; cin >> spoken_english; getchar(); cout << "錄入成功!" << endl; } English_Student::~English_Student() { } void English_Student::change() { //公共的信息修改 在 manage 里面 這里來到類里面來修改特有的信息 cout << " 綜合英語:" << endl; cin >> zonghe_english; cout << " 口語英語:" << endl; cin >> spoken_english; getchar(); } void English_Student:: traverse(Student *t) { //因為實在 類里面 可以直接訪問到數據 //輸出的信息 分了 幾行 便於好看 cout << "學生信息為:" << endl << " 學號:" << ID << " 姓名:" << name << " 年齡:" << age << " 專業:" << major << endl <<"公共課成績:"<<endl << " 數學:" << math << " 英語:" << english << " 體育:" << sport << endl << "專業課成績:" <<endl <<" 綜合英語:"<< t->get_zonghe_english() << " 口語英語:" << t->get_spoken_english() <<endl <<endl; } float English_Student::get_average_score() { float av = 0.0; //來求和 在類里面 可以訪問到數據 av += math; av += sport; av += english; av += zonghe_english; av += spoken_english; return (av / 5.0) ; //返回平均分 /5.0 為float } float English_Student::get_main_average() { //直接返回 公共課平均分 為float return ((math + english + sport) / 3.0); } float English_Student::get_zonghe_english() { return zonghe_english; //返回綜合英語的分 } float English_Student::get_spoken_english() { return spoken_english;//返回口語英語的分 } string English_Student::get_major() { return major; //返回專業 為string } ;
/*IT_Student.h*/ #pragma once #include "Student.h" #include <string> class IT_Student : public Student { public: //特有科目成員變量 float zu_cheng_yuanli; float ti_xi_jie_gou; float hui_bian; string major = "IT_Student"; //一系列get函數 返回float float get_zu_cheng_yuanli(); float get_ti_xi_jie_gou(); float get_hui_bian(); float get_average_score(); float get_main_average(); string get_major(); IT_Student(); ~IT_Student(); void traverse(Student *t); void change(); };
/*IT_Student.cpp*/ #include "IT_Student.h" #include<iostream> #include"string.h" using namespace std; IT_Student::IT_Student() { cout << "age:" << endl; cin >> age; getchar(); cout << "name:"; cin >> name; getchar(); cout << "ID:" << endl; cin >> ID; getchar(); cout << "sport_score:" << endl; cin >> sport; getchar(); cout << "math_score:" << endl; cin >> math; getchar(); cout << "english_score" << endl; cin >> english; cout << "zu_cheng_yuanli_score" << endl; cin >> zu_cheng_yuanli; cout << "ti_xi_jie_gou_score" << endl; cin >> ti_xi_jie_gou; cout << "hui_bian_score" << endl; cin >> hui_bian; getchar(); cout << "錄入成功!" << endl; } IT_Student::~IT_Student() { } void IT_Student::change() { //公共的信息修改 在 manage 里面 這里來到類里面來修改特有的信息 cout << " 組成原理:" << endl; cin >> zu_cheng_yuanli; cout << " 體系結構:" << endl; cin >> ti_xi_jie_gou; cout << " 匯編語言:" << endl; cin >> hui_bian; getchar(); } float IT_Student::get_main_average() { return ((math + english + sport) / 3.0); } void IT_Student::traverse(Student *t) { //因為實在 類里面 可以直接訪問到數據 //輸出的信息 分了 幾行 便於好看 cout << "學生信息為:" << endl << " 學號:" << ID << " 姓名:" << name << " 年齡:" << age << " 專業:" << major << endl <<"公共課成績:"<<endl << " 數學:" <<math << " 英語:" << english << " 體育" << sport << endl << "專業課成績: "<<endl <<" 組成原理:"<< t->get_zu_cheng_yuanli() << " 體系結構:" << t->get_ti_xi_jie_gou() <<" 匯編語言:"<<t->get_hui_bian()<< endl <<endl; } float IT_Student::get_average_score() { float av = 0; av += math; av += sport; av += english; av += zu_cheng_yuanli; av += ti_xi_jie_gou; av += hui_bian; return (av / 6.0); } float IT_Student::get_zu_cheng_yuanli() { return zu_cheng_yuanli; }
/*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; }
float IT_Student::get_ti_xi_jie_gou() { return ti_xi_jie_gou; } float IT_Student::get_hui_bian() { return hui_bian; } string IT_Student::get_major() { return major; } ;
*/
#include "Student.h" #include<iostream> Student::Student() { } Student::~Student() { } //這些函數都是假的 真正實現在子類里面 float Student::get_average_score() { return 0; } float Student::get_main_average() { return 0; } void Student::traverse(Student *t) { cout << "我在Student 里面" << endl; } float Student:: get_shi_bian_fun() { return 0; } float Student::get_fan_han_fen_xi() { return 0; } float Student::get_wei_fen_jihe() { return 0; } float Student::get_zonghe_english() { return 0; } float Student::get_spoken_english() { return 0; } float Student::get_zu_cheng_yuanli() { return 0; } float Student::get_ti_xi_jie_gou() { return 0; } float Student::get_hui_bian() { return 0; } string Student::get_major() { return 0; } void Student::change() { } ;