pta实验报告二认真读题(补上了“-”的重载)
#include <bits/stdc++.h>
using namespace std;
template<class T>
T maxn(T x[], int len) {
int i = 1;
T max = x[0];
for (i; i < len; i++) {
bool n = max < x[i];
bool m = max - x[i];
if (n || m)
max = x[i];
}
return max;
}
class Date {
private:
int year = 0, month = 0, day = 0;
public:
Date() {};
void dateset(int a, int b, int c) {
year = a;
month = b;
day = c;
}
bool operator<(Date a) {
if (year * 365 + month * 30 + day < a.year * 365 + a.month * 30 + a. day)
return 1;
else
return 0;
}
bool operator-(Date b) {
if (*this < b)
return 1;
else
return 0;
}
int y() {
return year;
}
int m() {
return month;
}
int d() {
return day;
}
};
class Time {
private:
int hh = 0, mm = 0, ss = 0;
public:
Time() {};
void timeset(int a, int b, int c) {
hh = a;
mm = b;
ss = c;
}
int h() {
return hh;
};
int m() {
return mm;
};
int s() {
return ss;
}
bool operator-(Time a) {
int n = hh * 3600 + mm * 60 + ss - (a.hh * 3600 + a.mm * 60 + a.ss);
if (n < 0)
return 1;
else
return 0;
}
bool operator <(Time b) {
if (*this - b)
return 1;
else
return 0;
}
};
int main() {
int flag;
int intArray[100];
double douArray[100];
Time timeArray[100];
Date dateArray[100];
while (1) {
cin >> flag;
if (flag == -1)
break;
else {
switch (flag) {
case 1: {
int count = 0;
for (int i = 0; i < 100; i++) {
cin >> intArray[i];
if (intArray[i] == 0)
break;
else
count++;
}
cout << maxn(intArray, count) << endl;
break;
};
case 2: {
int count = 0;
for (int i = 0; i < 100; i++) {
cin >> douArray[i];
if (douArray[i] == 0)
break;
else
count++;
}
cout << maxn(douArray, count) << endl;
break;
};
case 3: {
int h, m, s, count = 0;
while (cin >> h) {
if (h == 0)
break;
else {
cin >> m >> s;
timeArray[count].timeset(h, m, s);
count++;
}
}
cout << maxn(timeArray, count).h() << " " << maxn(timeArray, count).m() << " " << maxn(timeArray, count).s() << endl;
break;
};
case 4: {
int y, m, d;
int count = 0;
while (cin >> y) {
if (y == 0)
break;
else {
cin >> m >> d;
dateArray[count].dateset(y, m, d);
count++;
}
}
cout << maxn(dateArray, count).y() << " " << maxn(dateArray, count).m() << " " << maxn(dateArray, count).d() << endl;
break;
}
}
}
}
return 0;
}
总结: