#include <iostream>
#include <stdexcept>
using namespace std;
class CThoiGian
{
private:
int hours, minutes, seconds;
public:
CThoiGian(int h = 0, int m = 0, int s = 0) : hours(h), minutes(m), seconds(s) {}
bool operator>=(const CThoiGian& time)
{
if (hours > time.hours) return true;
if (hours == time.hours && minutes > time.minutes) return true;
if (hours == time.hours && minutes == time.minutes && seconds >= time.seconds) return true;
return false;
}
bool operator!=(const CThoiGian& time)
{
return hours != time.hours || minutes != time.minutes || seconds != time.seconds;
}
CThoiGian operator+(const CThoiGian& time)
{
CThoiGian res;
res.seconds = seconds + time.seconds;
res.minutes = minutes + time.minutes + res.seconds / 60;
res.seconds %= 60;
res.hours = hours + time.hours + res.minutes / 60;
res.minutes %= 60;
return res;
}
CThoiGian operator-(CThoiGian time)
{
CThoiGian res;
res.seconds = seconds - time.seconds;
if (res.seconds < 0)
{
res.seconds += 60;
time.minutes++;
}
res.minutes = minutes - time.minutes;
if (res.minutes < 0)
{
res.minutes += 60;
time.hours++;
}
res.hours = hours - time.hours;
return res;
}
CThoiGian& operator++()
{
seconds++;
if (seconds == 60)
{
seconds = 0;
minutes++;
if (minutes == 60)
{
minutes = 0;
hours++;
if (hours == 24)
{
hours = 0;
}
}
}
return *this;
}
CThoiGian& operator--()
{
seconds--;
if (seconds == -1)
{
seconds = 59;
minutes--;
if (minutes == -1)
{
minutes = 59;
hours--;
if (hours == -1)
{
hours = 23;
}
}
}
return *this;
}
friend istream& operator>>(istream& is, CThoiGian& time)
{
is >> time.hours >> time.minutes >> time.seconds;
return is;
}
friend ostream& operator<<(ostream& os, const CThoiGian& time)
{
os << time.hours << " " << time.minutes << " " << time.seconds;
return os;
}
};
class CNgay {
private:
int day, month, year;
bool isLeapYear(int year) const {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int daysInMonth(int month, int year) const {
switch (month) {
case 4: case 6: case 9: case 11: return 30;
case 2: return isLeapYear(year) ? 29 : 28;
default: return 31;
}
}
public:
CNgay(int d = 1, int m = 1, int y = 2000) : day(d), month(m), year(y) {}
// Operator <
bool operator<(const CNgay& other) const {
if (year < other.year) return true;
if (year == other.year && month < other.month) return true;
if (year == other.year && month == other.month && day < other.day) return true;
return false;
}
// Operator ==
bool operator==(const CNgay& other) const {
return day == other.day && month == other.month && year == other.year;
}
// Postfix increment operator ++
CNgay operator++(int) {
CNgay temp = *this;
++day;
if (day > daysInMonth(month, year)) {
day = 1;
++month;
if (month > 12) {
month = 1;
++year;
}
}
return temp;
}
// Postfix decrement operator --
CNgay operator--(int) {
CNgay temp = *this;
--day;
if (day < 1) {
--month;
if (month < 1) {
month = 12;
--year;
}
day = daysInMonth(month, year);
}
return temp;
}
// Operator >>
friend istream& operator>>(istream& is, CNgay& date) {
is >> date.day >> date.month >> date.year;
return is;
}
// Operator <<
friend ostream& operator<<(ostream& os, const CNgay& date) {
os << date.day << "/" << date.month << "/" << date.year;
return os;
}
};
class CDonThuc {
private:
double coefficient;
int exponent;
public:
CDonThuc(double c = 0, int e = 0) : coefficient(c), exponent(e) {}
// Comparison operators
bool operator>(const CDonThuc& other) const {
return exponent > other.exponent;
}
bool operator<(const CDonThuc& other) const {
return exponent < other.exponent;
}
bool operator==(const CDonThuc& other) const {
return exponent == other.exponent && coefficient == other.coefficient;
}
bool operator>=(const CDonThuc& other) const {
return *this > other || *this == other;
}
bool operator<=(const CDonThuc& other) const {
return *this < other || *this == other;
}
bool operator!=(const CDonThuc& other) const {
return !(*this == other);
}
// Arithmetic operators
CDonThuc operator+(const CDonThuc& other) const {
if (exponent == other.exponent) {
return CDonThuc(coefficient + other.coefficient, exponent);
}
throw invalid_argument("Exponents must be the same for addition.");
}
CDonThuc operator-(const CDonThuc& other) const {
if (exponent == other.exponent) {
return CDonThuc(coefficient - other.coefficient, exponent);
}
throw invalid_argument("Exponents must be the same for subtraction.");
}
CDonThuc operator*(const CDonThuc& other) const {
return CDonThuc(coefficient * other.coefficient, exponent + other.exponent);
}
CDonThuc operator/(const CDonThuc& other) const {
if (other.coefficient == 0) {
throw invalid_argument("Division by zero.");
}
return CDonThuc(coefficient / other.coefficient, exponent - other.exponent);
}
// Unary operators
CDonThuc operator!() const {
if (exponent == 0) {
return CDonThuc(0, 0);
}
return CDonThuc(coefficient * exponent, exponent - 1);
}
CDonThuc operator~() const {
return CDonThuc(coefficient / (exponent + 1), exponent + 1);
}
// Input and output operators
friend istream& operator>>(istream& is, CDonThuc& dt) {
is >> dt.coefficient >> dt.exponent;
return is;
}
friend ostream& operator<<(ostream& os, const CDonThuc& dt) {
os << dt.coefficient << "x^" << dt.exponent;
return os;
}
};
int main() {
CThoiGian t1(1, 30, 45);
CThoiGian t2(0, 45, 30);
CThoiGian t3 = t1 + t2;
cout << "t1 + t2 = " << t3 << endl; // 2:16:15
CThoiGian t4 = t1 - t2;
cout << "t1 - t2 = " << t4 << endl; // 0:45:15
++t1;
cout << "++t1 = " << t1 << endl; // 1:30:46
--t2;
cout << "--t2 = " << t2 << endl; // 0:45:29
cout << "t1 >= t2: " << (t1 >= t2) << endl; // 1
cout << "t1 != t2: " << (t1 != t2) << endl; // 1
CNgay d1(28, 2, 2020);
CNgay d2(1, 3, 2020);
cout << "d1: " << d1 << endl; // 28/2/2020
cout << "d2: " << d2 << endl; // 1/3/2020
cout << "d1 < d2: " << (d1 < d2) << endl; // 1
cout << "d1 == d2: " << (d1 == d2) << endl; // 0
d1++;
cout << "d1++: " << d1 << endl; // 29/2/2020
d2--;
cout << "d2--: " << d2 << endl; // 29/2/2020
CDonThuc dt1(3, 2);
CDonThuc dt2(2, 2);
cout << "dt1: " << dt1 << endl; // 3x^2
cout << "dt2: " << dt2 << endl; // 2x^2
cout << "dt1 + dt2: " << (dt1 + dt2) << endl; // 5x^2
cout << "dt1 - dt2: " << (dt1 - dt2) << endl; // 1x^2
cout << "dt1 * dt2: " << (dt1 * dt2) << endl; // 6x^4
cout << "dt1 / dt2: " << (dt1 / dt2) << endl; // 1.5x^0
cout << "!dt1: " << !dt1 << endl; // 6x^1
cout << "~dt1: " << ~dt1 << endl; // 1x^3
cout << "dt1 > dt2: " << (dt1 > dt2) << endl; // 0
cout << "dt1 < dt2: " << (dt1 < dt2) << endl; // 0
cout << "dt1 == dt2: " << (dt1 == dt2) << endl; // 0
cout << "dt1 >= dt2: " << (dt1 >= dt2) << endl; // 1
cout << "dt1 <= dt2: " << (dt1 <= dt2) << endl; // 1
cout << "dt1 != dt2: " << (dt1 != dt2) << endl; // 1
return 0;
}