數據的最大值問題(重載+函數模板)

兩個類以下設計:類time有三個數據成員,hh,mm,ss,分別表明時,分和秒,並有若干構造函數和一個重載-(減號)的成員函數。類date有三個數據成員,year,month,day分別表明年月日,並有若干構造函數和一個重載>(<)(大於號或者小於號)的成員函數。ios

要求設計一個函數模板template <\class T>\ double maxn(T x[], int len) 對int,float,time和date或者其餘類型的數據,返回最大值。函數

主函數有以下數據成員:spa

int intArray[100];設計

double douArray[100];time timeArray[100];code

date dateArray[100];blog

其中,hh = 3600 ss, mm = 60 ss, year = 365 day, month = 30 day,對於time和date類型,數據在轉換成ss或者day後進行運算。ci

輸入格式:it

每行爲一個操做,每行的第一個數字爲元素類型,1爲整型元素,2爲浮點型元素,3爲time類型,4爲date類型,若爲整型元素,接着輸入整型數據,以0結束。若爲浮點型元素,接着輸入浮點型數據,以0結束。若爲time型元素, 輸入time型數據(hh1 mm1 ss1 hh2 mm2 ss2),以0結束。若爲date型數據,輸入date型數據(year1 month1 day1 year2 month2 day2),以0結束。輸入-1時表示全體輸入結束。io

 1 #include <iostream>
 2 using namespace std;  3 
 4 class time  5 {  6 private:  7     int hh, mm, ss;  8 public:  9  time()  10  {  11         hh = 0;  12         mm = 0;  13         ss = 0;  14     }; //構造函數
 15     time(int h, int m, int s): hh(h), mm(m), ss(s) {};  //構造函數
 16     void settime(int h, int m, int s)  17  {  18         hh = h, mm = m, ss = s;  19  };  20     friend time operator - (time A, time B);  21     friend bool operator > (time A, time B);  22     friend ostream& operator << (ostream &output, time &A);  23 };  24 
 25 time operator - (time A, time B)    //在該程序中無實際用途,忽略
 26 {  27     return time(A.hh - B.hh, A.mm - B.mm, A.ss - B.ss); //實際狀況須要考慮負數狀況
 28 }  29 
 30 bool operator > (time A, time B)    //重載大於號,用於比較
 31 {  32     return A.hh * 3600 + A.mm * 60 + A.ss > B.hh * 3600 + B.mm * 60 + B.ss;  33 }  34 
 35 ostream& operator << (ostream &output, time &A)     //重載輸出流函數,方便輸出
 36 {  37     output << A.hh << " " << A.mm << " " << A.ss;  38     return output;  39 }  40 
 41 class date  42 {  43 private:  44     int year, month, day;  45 public:  46  date() {};  47     date(int y, int m, int d): year(y), month(m), day(d) {};  48     void setdate(int y, int m, int d)  49  {  50         year = y, month = m, day = d;  51  };  52     friend bool operator > (date A, date B);  53     friend ostream& operator << (ostream &output, date &A);  54 };  55 
 56 bool operator > (date A, date B)        //重載大於號
 57 {  58     return A.year * 365 + A.month * 30 + A.day > B.year * 365 + B.month * 30 + B.day;  59 }  60 
 61 ostream& operator << (ostream &output, date &A)        //重載輸出流,方便輸出
 62 {  63     output << A.year << " " << A.month << " " << A.day;  64     return output;  65 }  66 
 67 template<class T>
 68 T maxn(T x[], int len)        //模板函數
 69 {  70     T temp = x[0];  71 
 72     for(int i(1); i < len; i++)        //冒泡比較
 73         if(x[i] > temp)  74             temp = x[i];  75 
 76     return temp;  77 }  78 
 79 int main()  80 {  81     int intArray[100], flag;  82     double doubleArray[100];  83     time timeArray[100];  84     date dateArray[100];  85     cin >> flag;  86 
 87     while(flag != -1)  88  {  89         switch(flag)  90  {  91             case 1:  92  {  93                     int Max, a, i(0);  94 
 95                     while(cin >> a, a)  96                         intArray[i++] = a;  97 
 98                     Max = maxn(intArray, i);  99                     cout << Max << endl; 100                     break; 101  } 102 
103             case 2: 104  { 105                     double Max, a; 106                     int i(0); 107 
108                     while(cin >> a, a) 109                         doubleArray[i++] = a; 110 
111                     Max = maxn(doubleArray, i); 112                     cout << Max << endl; 113                     break; 114  } 115 
116             case 3: 117  { 118  time Max; 119                     int a, b, c, i(0); 120 
121                     while(cin >> a, a) 122  { 123                         cin >> b >> c; 124                         timeArray[i++].settime(a, b, c); 125  } 126 
127                     Max = maxn(timeArray, i); 128                     cout << Max << endl; 129                     break; 130  } 131 
132             case 4: 133  { 134  date Max; 135                     int a, b, c, i(0); 136 
137                     while(cin >> a, a) 138  { 139                         cin >> b >> c; 140                         dateArray[i++].setdate(a, b, c); 141  } 142 
143                     Max = maxn(dateArray, i); 144                     cout << Max << endl; 145                     break; 146  } 147  } 148 
149         cin >> flag; 150  } 151 
152     return 0; 153 }
相關文章
相關標籤/搜索