-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
136 lines (122 loc) · 3.01 KB
/
Source.cpp
File metadata and controls
136 lines (122 loc) · 3.01 KB
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
132
133
134
135
136
#include <iostream>
#include <iomanip>
const int MIN_MONTH = 1;
const int MAX_MONTH = 12;
const int MIN_DAY = 1;
const int MIN_YEAR = 1980;
const int MAX_YEAR = 2107;
const int INVALID = -1;
const int WIDTH = 4;
enum MONTH {
JANUARY = 1, FEBRUARY, MARCH, APRIL, MAY,
JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER,
DECEMBER
};
bool isValidYear(int year);
bool isLeapYear(int year);
bool isValidMonth(int month);
int getDaysInMonth(int month, int year);
bool isValidDay(int day, int month, int year);
void purgeInvalidInput(std::string error_mess);
int main() {
int month, day, year;
bool is_valid_year, is_valid_month, is_valid_day;
std::cout << "Enter a month, day, and year between 1-1-1980 and 12-31-2107: ";
std::cin >> month >> day >> year;
// Check if the input operation failed and handle accordingly
if (!std::cin) {
purgeInvalidInput("\nError: Invalid Input");
}
else {
is_valid_year = isValidYear(year);
is_valid_month = isValidMonth(month);
is_valid_day = isValidDay(day, month, year);
if (is_valid_year && is_valid_month && is_valid_day) {
// Bits 0-4: Day, Bits 5-8: Month, Bits 9-15: Year
short int fatDate = ((year - MIN_YEAR) << 9) | (month << 5) | day;
std::cout << "The hexadecimal FAT date is: " << std::hex << std::uppercase << std::setfill('0') << std::setw(WIDTH) << fatDate << std::endl;
}
else {
if (!is_valid_month) {
std::cout << "\nError: Invalid Month";
}
if (!is_valid_day) {
std::cout << "\nError: Invalid Day";
}
if (!is_valid_year) {
std::cout << "\nError: Invalid Year";
}
std::cout << std::endl;
}
}
std::cout << "\nTerminating Program...\n";
return 0;
}
bool isValidYear(int year) {
return (year >= MIN_YEAR && year <= MAX_YEAR);
}
bool isLeapYear(int year) {
// A year is a leap year if it is divisible by 4, but years divisible by 100 are not leap years unless they are also divisible by 400
return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0);
}
bool isValidMonth(int month) {
return (month >= MIN_MONTH && month <= MAX_MONTH);
}
int getDaysInMonth(int month, int year) {
int days = 0;
switch (month) {
case JANUARY:
days = 31;
break;
case FEBRUARY:
if (isLeapYear(year)) {
days = 29;
}
else {
days = 28;
}
break;
case MARCH:
days = 31;
break;
case APRIL:
days = 30;
break;
case MAY:
days = 31;
break;
case JUNE:
days = 30;
break;
case JULY:
days = 31;
break;
case AUGUST:
days = 31;
break;
case SEPTEMBER:
days = 30;
break;
case OCTOBER:
days = 31;
break;
case NOVEMBER:
days = 30;
break;
case DECEMBER:
days = 31;
break;
default:
days = INVALID;
}
return days;
}
bool isValidDay(int day, int month, int year) {
int num_days_in_given_month = getDaysInMonth(month, year);
return (day >= MIN_DAY && day <= num_days_in_given_month);
}
void purgeInvalidInput(std::string error_mess) {
std::cout << error_mess << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}