经过一个单元的学习,学生信息管理系统迎来了优化!
#include <iostream> #include <string> using namespace std;class student2 ;class subject { int score[3 ]; const int SMath, SEng, SCpp; public : subject (); subject (int math, int eng, int cpp); void Display () const ; void Input () ; void Insert () ; void Delete () ; friend class student2 ; };
#include "subject.h" #include <iostream> #include <string> using namespace std;class student2 { string name; string ID; double GPA; static int count; public : student2 (); student2 (string na, string id); string GetName () const ; void ReckonGPA (const subject& sub) ; double GetGPA () const ; void Display (const subject& sub) const ; void Display () const ; void Input () ; void Insert () ; void Delete () ; static int GetCount () ; friend void OutputStu (const student2* array) ; };
#include "subject.h" subject::subject ():SMath (0 ), SEng (0 ), SCpp (0 ) { for (int i=0 ; i<3 ; i++) score[i] = -1 ; } subject::subject (int math, int eng, int cpp):SMath (0 ), SEng (0 ), SCpp (0 ) { score[0 ]=math; score[1 ]=eng; score[2 ]=cpp; } void subject::Display () const { cout<<"Math " <<" SEng " <<"SCpp " <<endl; cout<<SMath<<" " <<SEng<<" " <<SCpp<<endl; for (int i=0 ; i<3 ; i++) cout<<" " <<score[i]; cout<<endl; } void subject::Input () { cout<<"输入成绩:" <<endl; cout << "输入 数学:" ; cin >> score[0 ]; cout << "输入 英语:" ; cin >> score[1 ]; cout << "输入 C++:" ; cin >> score[2 ]; } void subject::Insert () { if (score[0 ]<0 ) Input (); } void subject::Delete () { for (int i=0 ; i<3 ; i++) score[i]=-1 ; }
#include "student2.h" void student2::Delete () { GPA=-1 ; count--; } int student2::count = 0 ;int student2::GetCount () { return count; } student2::student2 () { name = "NULL" ; ID = "NULL" ; GPA = -1 ; } student2::student2 (string na, string id) { name = na; ID = id; GPA = -1 ; count++; } string student2::GetName () const { return name; } void student2::ReckonGPA (const subject& sub) { GPA = ((sub.score[0 ] - 60 ) / 40.0 * sub.SMath * sub.SMath + (sub.score[0 ]-60 )/40.0 *sub.SEng*sub.SEng + (sub.score[0 ]-60 )/40.0 *sub.SCpp*sub.SCpp); } double student2::GetGPA () const { return GPA; } void student2::Display (const subject& sub) const { cout << "Name: " << name << endl; cout << "ID: " << ID << endl; cout<<" Math " <<" SEng " <<" SCpp " <<endl; for (int i=0 ;i<3 ;i++) cout<<sub.score[i]<<" " ; cout<<endl; cout << "GPA: " << GPA << endl; } void student2::Display () const { cout << "Name: " << name << endl; cout << "ID: " << ID << endl; } void student2::Input () { cout << "Enter name: " ; cin >> name; cout << "Enter ID: " ; cin >> ID; count++; } void student2::Insert () { if (GPA<0 ) Input (); } void OutputStu (const student2* array) { cout<<"学生总人数=" <<student2::GetCount ()<<endl; for (int i = 0 ;i < student2::GetCount ();i++) { if (array[i].GetGPA () != -1 ) { cout << "name: " <<array[i].name<<", ID: " <<array[i].ID<<", GPA: " <<array[i].GPA<<endl; } } }
#include "student2.h" const int N = 10 ;void menu () ;int InputStu (student2* array) ;void InputSel (subject* Select, int i) ;int SearchStu (const student2* array, string na) ;int InsertStu (student2* array) ;bool DeleteStu (student2* array,subject* Select, string na) ;int main () { student2 array[N]; subject Select[N]; int choice, i; string name; char ch = 'Y' ; do { menu (); cin >> choice; switch (choice) { case 1 : while (ch == 'Y' || ch == 'y' ) { i = InputStu (array); InputSel (Select, i); array[i].ReckonGPA (Select[i]); cout << "Do you want to add more students? (Y/N): " ; cin >> ch; } break ; case 2 : cout << "Enter the name of the student to search:\n " ; cin >> name; int i; i = SearchStu (array, name); if (i == N) cout << "Student not found." << endl; else { array[i].Display (Select[i]); } break ; case 3 : OutputStu (array); break ; case 4 : i = InsertStu (array); if (i) { cout << "成功插入一条记录\n" ; cout << "录入成绩吗?(Y/N):" ; cin >> ch; if (ch == 'Y' || ch == 'y' ) { Select[i].Input (); array[i].ReckonGPA (Select[i]); } } else cout << "已达到最大容量,无法插入。\n" ; break ; case 5 : cout << "Enter the name of the student to delete:\n " ; cin >> name; if (DeleteStu (array, Select, name)) cout << "删除成功。\n" ; else cout << "删除失败。\n" ; break ; default : cout << "Invalid choice.\n" ; break ; } }while (choice); return 0 ; } void menu () { cout << "*********1.录入信息*********\n" ; cout << "*********2.查询信息*********\n" ; cout << "*********3.浏览信息*********\n" ; cout << "*********4.插入信息*********\n" ; cout << "*********5.删除信息*********\n" ; cout << "*********0.退 出*********\n" ; } int InputStu (student2* array) { int i = 0 ; if (student2::GetCount () ==N) { cout << "已达到最大容量,无法插入。\n" ; } else { while (array[i].GetGPA () > 0 ) i++; array[i].Input (); cout << "录入成功。\n" ; } return i; } void InputSel (subject* Select, int i) { Select[i].Input (); } int SearchStu (const student2* array, string na) { int i,j=N; for (i = 0 ; i < N; i++) { if (array[i].GetGPA () != -1 ) { if (array[i].GetName () == na) { j = i; break ; } } } return j; } int InsertStu (student2* array) { int i = 0 ; if (student2::GetCount () == N) { cout << "已达到最大容量,无法插入。\n" ; return 0 ; } while (array[i].GetGPA () > 0 ) i++; array[i].Input (); return i; } bool DeleteStu (student2* array, subject* Select, string na) { if (student2::GetCount () == 0 ) { cout << "学生信息为空,无法删除。\n" ; return false ; } int i = SearchStu (array, na); if (i == N) { cout << "学生不存在。\n" ; return false ; } array[i].Delete (); Select[i].Delete (); return true ; }
*********1.录入信息********* *********2.查询信息********* *********3.浏览信息********* *********4.插入信息********* *********5.删除信息********* *********0.退 出*********