-
Notifications
You must be signed in to change notification settings - Fork 1
/
Tugas Petruk.cpp
131 lines (100 loc) · 2.55 KB
/
Tugas Petruk.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>
#include <ctime>
using namespace std;
template<typename T> class AgeCalculator {
public:
// Constructor
AgeCalculator(T newDay, T newMonth, T newYear) : day(newDay), month(newMonth), year(newYear) {}
// Setter
void setDay(T newDay);
void setMonth(T newMonth);
void setYear(T newYear);
// Getter
T getDay();
T getMonth();
T getYear();
// Member function for calculating age based on day of birth
void printAge();
// Member function for calculating the number of days in a given month in a given year
int numberOfDays(int month, int year);
// Member function for leap year checking
bool isLeapYear(int year);
private:
T day;
T month;
T year;
};
template<typename T>
void AgeCalculator<T>::setDay(T newDay)
{
this->day = newDay;
}
template<typename T>
void AgeCalculator<T>::setMonth(T newMonth)
{
this->month = newMonth;
}
template<typename T>
void AgeCalculator<T>::setYear(T newYear)
{
this->year = newYear;
}
template<typename T>
T AgeCalculator<T>::getDay()
{
return this->day;
}
template<typename T>
T AgeCalculator<T>::getMonth()
{
return this->month;
}
template<typename T>
T AgeCalculator<T>::getYear()
{
return this->year;
}
template<typename T>
void AgeCalculator<T>::printAge()
{
time_t now = time(0);
tm *ltm = localtime(&now);
int currentYear = 1900 + ltm->tm_year;
int currentMonth = 1 + ltm->tm_mon;
int currentDay = ltm->tm_mday;
int years = currentYear - this->year;
int months = currentMonth - this->month;
int days = currentDay - this->day;
if (days < 0) {
--months;
days += 30;
}
if (months < 0) {
--years;
months += 12;
}
cout << "Umur anda adalah " << years << " tahun " << months << " bulan " << days << " hari" << endl;
}
template<typename T>
int AgeCalculator<T>::numberOfDays(int month, int year)
{
int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (isLeapYear(year) && month == 2) return days[month-1]+1;
return days[month-1];
}
template<typename T>
bool AgeCalculator<T>::isLeapYear(int year)
{
int query = year ? year : this->year;
return query % 4 == 0 && (query % 100 != 0 || query % 400 == 0);
}
int main()
{
int day, month, year;
cout << "Masukkan tanggal, bulan, dan tahun lahir anda: ";
cin >> day >> month >> year;
AgeCalculator<int> person(day, month, year);
person.printAge();
cout << person.isLeapYear(2004) << endl;
cout << person.numberOfDays(9, 2004) << endl;
}