Read the program "simple C + + Student Information Management system", to find out the occurrence of the structure function, friend function, operator overloading, static number member syntax phenomenon appears, carefully understand its usage, in the future design can be flexible application of the methods and techniques.
#include <iostream> #include <cstring>using namespace std; #define MAX 100class CDate//define date class {Private:unsi gned 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); friend 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 date void cdate::setdate (int y,int m,int d) {year=y; Month=m; Day=d;} Overloaded input operator >>istream &operator>> (IStream &in,cdate &d) {char ch1,ch2; cout<< "Please enter the date (input format: YYYY-MM-DD):"; while (1) {cin>>d.year>>ch1>>d.month>>ch2>>d.day; if (ch1== '-' && ch2== '-') if (Checkvalid (d)) break; cerr<< "time format or value is incorrect! Please re-enter \ n "; } return CIN; Overloaded output operator <<ostream &operator<< (ostream &out,cdate &d) {out<<d.year<< "year" < <d.month<< "Month" <<d.day<< "Day"; return out;} Judgment 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 the leap year is bool Leapyear (int year) {if ((year%4==0 && year%100) | | year%400==0) return true; return false;} Check date legitimacy 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; Sex CDate date; 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 (); Search by name friend void Statistic (); By gender statistics friend Void Display (); Show All information} Stu[max];int cstudent::num=0; Cstudent::cstudent () {}//input information void Cstudent::inputdata () {int p; Char s[41]; cout<< "Please enter student information (NO.") <<num<< "): \ n"; cout<< "Name:"; cin>>s; Name=new Char[strlen (s) +1]; strcpy (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;//find subscript for the youngest object in the currently unsorted element if (p==i) continue; Below Exchange Stu[i] and stu[p] tn=stu[i].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; }}//Search by name void FindName () {char name[41]; int i,num; cout<< "Please enter your 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 this person!" <<endl<<endl; Return }//If found, show student information cout<< "Name:" <<stu[i].name<<endl; cout<< "Gender:"; if (stu[i].sex) cout<< "male" <<endl; else cout<< "Women" <<endl; cout<< "Birthday:" <<stu[i].date<<endl; Cout<<endl;} Statistics by sex 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<< "Number of boys:" <<s1<<endl; cout<< "Number of girls:" <<s0<<endl; Cout<<endl;} Show 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", "Statistics by Gender", "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://sorting sort (); Break Case 3://Search by name FindName (); Break Case 4: Statistic ()//by gender statistics; Break Case 5://Show All information display (); Break Case 6://exit end=true; Break }} return 0;}
Operation Result:
Nineth Week program Reading-Student Information management system