fork download
  1. #include <iostream>
  2. #include <stdexcept>
  3.  
  4. using namespace std;
  5.  
  6. class CThoiGian
  7. {
  8. private:
  9. int hours, minutes, seconds;
  10. public:
  11. CThoiGian(int h = 0, int m = 0, int s = 0) : hours(h), minutes(m), seconds(s) {}
  12. bool operator>=(const CThoiGian& time)
  13. {
  14. if (hours > time.hours) return true;
  15. if (hours == time.hours && minutes > time.minutes) return true;
  16. if (hours == time.hours && minutes == time.minutes && seconds >= time.seconds) return true;
  17. return false;
  18. }
  19. bool operator!=(const CThoiGian& time)
  20. {
  21. return hours != time.hours || minutes != time.minutes || seconds != time.seconds;
  22. }
  23. CThoiGian operator+(const CThoiGian& time)
  24. {
  25. CThoiGian res;
  26. res.seconds = seconds + time.seconds;
  27. res.minutes = minutes + time.minutes + res.seconds / 60;
  28. res.seconds %= 60;
  29. res.hours = hours + time.hours + res.minutes / 60;
  30. res.minutes %= 60;
  31. return res;
  32. }
  33. CThoiGian operator-(CThoiGian time)
  34. {
  35. CThoiGian res;
  36. res.seconds = seconds - time.seconds;
  37. if (res.seconds < 0)
  38. {
  39. res.seconds += 60;
  40. time.minutes++;
  41. }
  42. res.minutes = minutes - time.minutes;
  43. if (res.minutes < 0)
  44. {
  45. res.minutes += 60;
  46. time.hours++;
  47. }
  48. res.hours = hours - time.hours;
  49. return res;
  50. }
  51. CThoiGian& operator++()
  52. {
  53. seconds++;
  54. if (seconds == 60)
  55. {
  56. seconds = 0;
  57. minutes++;
  58. if (minutes == 60)
  59. {
  60. minutes = 0;
  61. hours++;
  62. if (hours == 24)
  63. {
  64. hours = 0;
  65. }
  66. }
  67. }
  68. return *this;
  69. }
  70. CThoiGian& operator--()
  71. {
  72. seconds--;
  73. if (seconds == -1)
  74. {
  75. seconds = 59;
  76. minutes--;
  77. if (minutes == -1)
  78. {
  79. minutes = 59;
  80. hours--;
  81. if (hours == -1)
  82. {
  83. hours = 23;
  84. }
  85. }
  86. }
  87. return *this;
  88. }
  89. friend istream& operator>>(istream& is, CThoiGian& time)
  90. {
  91. is >> time.hours >> time.minutes >> time.seconds;
  92. return is;
  93. }
  94. friend ostream& operator<<(ostream& os, const CThoiGian& time)
  95. {
  96. os << time.hours << " " << time.minutes << " " << time.seconds;
  97. return os;
  98. }
  99. };
  100.  
  101. class CNgay {
  102. private:
  103. int day, month, year;
  104.  
  105. bool isLeapYear(int year) const {
  106. return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
  107. }
  108.  
  109. int daysInMonth(int month, int year) const {
  110. switch (month) {
  111. case 4: case 6: case 9: case 11: return 30;
  112. case 2: return isLeapYear(year) ? 29 : 28;
  113. default: return 31;
  114. }
  115. }
  116.  
  117. public:
  118. CNgay(int d = 1, int m = 1, int y = 2000) : day(d), month(m), year(y) {}
  119.  
  120. // Operator <
  121. bool operator<(const CNgay& other) const {
  122. if (year < other.year) return true;
  123. if (year == other.year && month < other.month) return true;
  124. if (year == other.year && month == other.month && day < other.day) return true;
  125. return false;
  126. }
  127.  
  128. // Operator ==
  129. bool operator==(const CNgay& other) const {
  130. return day == other.day && month == other.month && year == other.year;
  131. }
  132.  
  133. // Postfix increment operator ++
  134. CNgay operator++(int) {
  135. CNgay temp = *this;
  136. ++day;
  137. if (day > daysInMonth(month, year)) {
  138. day = 1;
  139. ++month;
  140. if (month > 12) {
  141. month = 1;
  142. ++year;
  143. }
  144. }
  145. return temp;
  146. }
  147.  
  148. // Postfix decrement operator --
  149. CNgay operator--(int) {
  150. CNgay temp = *this;
  151. --day;
  152. if (day < 1) {
  153. --month;
  154. if (month < 1) {
  155. month = 12;
  156. --year;
  157. }
  158. day = daysInMonth(month, year);
  159. }
  160. return temp;
  161. }
  162.  
  163. // Operator >>
  164. friend istream& operator>>(istream& is, CNgay& date) {
  165. is >> date.day >> date.month >> date.year;
  166. return is;
  167. }
  168.  
  169. // Operator <<
  170. friend ostream& operator<<(ostream& os, const CNgay& date) {
  171. os << date.day << "/" << date.month << "/" << date.year;
  172. return os;
  173. }
  174. };
  175.  
  176. class CDonThuc {
  177. private:
  178. double coefficient;
  179. int exponent;
  180.  
  181. public:
  182. CDonThuc(double c = 0, int e = 0) : coefficient(c), exponent(e) {}
  183.  
  184. // Comparison operators
  185. bool operator>(const CDonThuc& other) const {
  186. return exponent > other.exponent;
  187. }
  188.  
  189. bool operator<(const CDonThuc& other) const {
  190. return exponent < other.exponent;
  191. }
  192.  
  193. bool operator==(const CDonThuc& other) const {
  194. return exponent == other.exponent && coefficient == other.coefficient;
  195. }
  196.  
  197. bool operator>=(const CDonThuc& other) const {
  198. return *this > other || *this == other;
  199. }
  200.  
  201. bool operator<=(const CDonThuc& other) const {
  202. return *this < other || *this == other;
  203. }
  204.  
  205. bool operator!=(const CDonThuc& other) const {
  206. return !(*this == other);
  207. }
  208.  
  209. // Arithmetic operators
  210. CDonThuc operator+(const CDonThuc& other) const {
  211. if (exponent == other.exponent) {
  212. return CDonThuc(coefficient + other.coefficient, exponent);
  213. }
  214. throw invalid_argument("Exponents must be the same for addition.");
  215. }
  216.  
  217. CDonThuc operator-(const CDonThuc& other) const {
  218. if (exponent == other.exponent) {
  219. return CDonThuc(coefficient - other.coefficient, exponent);
  220. }
  221. throw invalid_argument("Exponents must be the same for subtraction.");
  222. }
  223.  
  224. CDonThuc operator*(const CDonThuc& other) const {
  225. return CDonThuc(coefficient * other.coefficient, exponent + other.exponent);
  226. }
  227.  
  228. CDonThuc operator/(const CDonThuc& other) const {
  229. if (other.coefficient == 0) {
  230. throw invalid_argument("Division by zero.");
  231. }
  232. return CDonThuc(coefficient / other.coefficient, exponent - other.exponent);
  233. }
  234.  
  235. // Unary operators
  236. CDonThuc operator!() const {
  237. if (exponent == 0) {
  238. return CDonThuc(0, 0);
  239. }
  240. return CDonThuc(coefficient * exponent, exponent - 1);
  241. }
  242.  
  243. CDonThuc operator~() const {
  244. return CDonThuc(coefficient / (exponent + 1), exponent + 1);
  245. }
  246.  
  247. // Input and output operators
  248. friend istream& operator>>(istream& is, CDonThuc& dt) {
  249. is >> dt.coefficient >> dt.exponent;
  250. return is;
  251. }
  252.  
  253. friend ostream& operator<<(ostream& os, const CDonThuc& dt) {
  254. os << dt.coefficient << "x^" << dt.exponent;
  255. return os;
  256. }
  257. };
  258.  
  259. int main() {
  260. CThoiGian t1(1, 30, 45);
  261. CThoiGian t2(0, 45, 30);
  262.  
  263. CThoiGian t3 = t1 + t2;
  264. cout << "t1 + t2 = " << t3 << endl; // 2:16:15
  265.  
  266. CThoiGian t4 = t1 - t2;
  267. cout << "t1 - t2 = " << t4 << endl; // 0:45:15
  268.  
  269. ++t1;
  270. cout << "++t1 = " << t1 << endl; // 1:30:46
  271.  
  272. --t2;
  273. cout << "--t2 = " << t2 << endl; // 0:45:29
  274.  
  275. cout << "t1 >= t2: " << (t1 >= t2) << endl; // 1
  276. cout << "t1 != t2: " << (t1 != t2) << endl; // 1
  277.  
  278. CNgay d1(28, 2, 2020);
  279. CNgay d2(1, 3, 2020);
  280.  
  281. cout << "d1: " << d1 << endl; // 28/2/2020
  282. cout << "d2: " << d2 << endl; // 1/3/2020
  283.  
  284. cout << "d1 < d2: " << (d1 < d2) << endl; // 1
  285. cout << "d1 == d2: " << (d1 == d2) << endl; // 0
  286.  
  287. d1++;
  288. cout << "d1++: " << d1 << endl; // 29/2/2020
  289.  
  290. d2--;
  291. cout << "d2--: " << d2 << endl; // 29/2/2020
  292.  
  293. CDonThuc dt1(3, 2);
  294. CDonThuc dt2(2, 2);
  295.  
  296. cout << "dt1: " << dt1 << endl; // 3x^2
  297. cout << "dt2: " << dt2 << endl; // 2x^2
  298.  
  299. cout << "dt1 + dt2: " << (dt1 + dt2) << endl; // 5x^2
  300. cout << "dt1 - dt2: " << (dt1 - dt2) << endl; // 1x^2
  301. cout << "dt1 * dt2: " << (dt1 * dt2) << endl; // 6x^4
  302. cout << "dt1 / dt2: " << (dt1 / dt2) << endl; // 1.5x^0
  303.  
  304. cout << "!dt1: " << !dt1 << endl; // 6x^1
  305. cout << "~dt1: " << ~dt1 << endl; // 1x^3
  306.  
  307. cout << "dt1 > dt2: " << (dt1 > dt2) << endl; // 0
  308. cout << "dt1 < dt2: " << (dt1 < dt2) << endl; // 0
  309. cout << "dt1 == dt2: " << (dt1 == dt2) << endl; // 0
  310. cout << "dt1 >= dt2: " << (dt1 >= dt2) << endl; // 1
  311. cout << "dt1 <= dt2: " << (dt1 <= dt2) << endl; // 1
  312. cout << "dt1 != dt2: " << (dt1 != dt2) << endl; // 1
  313.  
  314. return 0;
  315. }
Success #stdin #stdout 0.01s 5272KB
stdin
Standard input is empty
stdout
t1 + t2 = 2 16 15
t1 - t2 = 0 45 15
++t1 = 1 30 46
--t2 = 0 45 29
t1 >= t2: 1
t1 != t2: 1
d1: 28/2/2020
d2: 1/3/2020
d1 < d2: 1
d1 == d2: 0
d1++: 29/2/2020
d2--: 29/2/2020
dt1: 3x^2
dt2: 2x^2
dt1 + dt2: 5x^2
dt1 - dt2: 1x^2
dt1 * dt2: 6x^4
dt1 / dt2: 1.5x^0
!dt1: 6x^1
~dt1: 1x^3
dt1 > dt2: 0
dt1 < dt2: 0
dt1 == dt2: 0
dt1 >= dt2: 0
dt1 <= dt2: 0
dt1 != dt2: 1