Program reading in week 9-student information management system and student information management system
Read the program "simple C ++ student information management system" to find out where constructor, youyuan function, operator overload, and static Number member syntax appear, and carefully understand its usage, relevant methods and techniques can be flexibly applied in future designs.
# Include <iostream> # include <cstring> using namespace std; # define MAX 100 class CDate // defines the date class {private: unsigned short int year; // year unsigned short int month; // month unsigned short int day; // day public: CDate (int y = 0, int m = 0, int d = 0 ); bool operator <(CDate d); friend istream & operator> (istream & in, CDate & d); friend ostream & operator <(ostream & out, CDate & d ); friend bool CheckValid (CDate d); frien D bool LeapYear (int year); void SetDate (int y, int m, int d) ;}; CDate: CDate (int y, int m, int d ): year (y), month (m), day (d) {}// set the date void CDate: SetDate (int y, int m, int d) {year = y; month = m; day = d ;}// overload input operator >>istream & operator >>( istream & in, CDate & d) {char success, ch2; cout <"enter date (input format: YYYY-MM-DD):"; while (1) {cin> d. year> week> d. month> ch2> d. day; if (region = '-' & ch2 = '-') if (CheckValid (d) break; cerr <"Incorrect time format or value! Enter \ n ";}return cin ;}// reload the output operator <ostream & operator <(ostream & out, CDate & d) {out <d. year <"year" <d. month <"month" <d. day <"day"; return out;} // judge date d1 <d2bool CDate: operator <(CDate d) {if (year <d. year) return true; if (year> d. year) return false; if (month <d. month) return true; if (month> d. month) return false; if (day <d. day) return true; return false;} // check whether it is a leol LeapYear (int year) {if (year % 4 = 0 & Year % 100) | year % 400 = 0) return true; return false;} // check date validity bool CheckValid (CDate d) {int n; if (d. month <1 | d. month> 12) return false; if (d. day <1) return false; n = 31; switch (d. month) {case 2: if (LeapYear (d. year) n = 29; else n = 28; break; case 4: case 6: case 9: case 11: n = 30; break;} if (d. day> n) return false; return true;} class CStudent {private: char * name; // name bool sex; // gender CDate d Ate; // Date of birth, class object as data member public: static int num; // Number of Students CStudent (); void InputData (); friend void Sort (); // sort friend void FindName (); // query friend void Statistic () by name; // obtain friend void Display () by gender (); // display all information} stu [MAX]; int CStudent: num = 0; CStudent: CStudent () {}// enter the void CStudent: InputData () {int p; char s [41]; cout <"Enter student information (NO. "<num <"): \ n "; cout <" name: "; cin> s; name = new char [strlen (s) + 1]; s Trcpy (name, s); cout <"gender (1-male, 0-female):"; cin> p; if (p) sex = true; else sex = false; cin> date; cout <endl ;}// Sort void Sort () {int I, j, p, num; char * tn; bool ts; CDate td; num = CStudent: num; for (I = 1; I <num; I ++) {p = I; for (j = I + 1; j <= num; j ++) if (stu [j]. date <stu [p]. date) p = j; // locate the subscript if (p = I) continue of the oldest object in the currently unordered element; // stu [I] and stu [p] tn = stu [I] are exchanged below. name; stu [I]. name = stu [p]. name; stu [p]. name = tn; ts = stu [I]. sex; Stu [I]. sex = stu [p]. sex; stu [p]. sex = ts; td = stu [I]. date; stu [I]. date = stu [p]. date; stu [p]. date = td ;}// query void FindName () {char name [41]; int I, num; cout <"Enter name :"; cin> name; num = CStudent: num; for (I = 1; I <= num; I ++) if (strcmp (stu [I]. name, name) = 0) break; if (I> num) {cout <"No such person found! "<Endl; return;} // if the student information is found, cout is displayed. <" name: "<stu [I]. name <endl; cout <"Gender:"; if (stu [I]. sex) cout <"male" <endl; else cout <"female" <endl; cout <"birthday:" <stu [I]. date <endl; cout <endl;} // void Statistic () {int I, num, s1, s0; num = CStudent: num; s1 = 0; s0 = 0; for (I = 1; I <= num; I ++) if (stu [I]. sex = 1) s1 ++; else s0 ++; cout <"Male:" <s1 <endl; cout <"female: "<s0 <endl; cout <endl;} // Display all information void Display () {Int I, num; num = CStudent: num; for (I = 1; I <= num; I ++) {cout <stu [I]. name <"\ t"; if (stu [I]. sex) cout <"male"; else cout <"female"; cout <"\ t" <stu [I]. date <endl ;}cout <endl ;}int main () {char * menu [] = {"", "input information", "sort ", "query by name", "Gender Statistics", "show all information", "exit"}; int I, p; bool end; end = false; while (! End) {for (I = 1; I <7; I ++) cout <I <"" <menu [I] <endl; cin> p; switch (p) {case 1: // input information CStudent: num ++; stu [CStudent: num]. inputData (); break; case 2: // Sort (); break; case 3: // query FindName (); break; case 4: // count by Gender Statistic (); break; case 5: // Display all information Display (); break; case 6: // Exit end = true; break ;}} return 0 ;}
Running result: