-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA_Beautiful_Matrix.cpp
63 lines (55 loc) · 1.79 KB
/
A_Beautiful_Matrix.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
#include <iostream>
#include <stdexcept>
#include <string>
using namespace std;
class Date {
private:
int day;
int month;
int year;
std::string displayFormat;
public:
string replacePlaceholder(std::string str, const std::string& placeholder, const std::string& replacement) {
size_t startPos = str.find(placeholder);
if (startPos != std::string::npos) {
str.replace(startPos, placeholder.length(), replacement);
}
return str;
}
Date(int day, int month, int year) {
if (!isValidDate(day, month, year)) {
throw std::invalid_argument("Invalid date");
}
this->day = day;
this->month = month;
this->year = year;
this->displayFormat = "dd/mm/yyyy";
}
void setDisplayFormat(const std::string& format) {
if (!isValidFormat(format)) {
throw std::invalid_argument("Invalid date format provided.");
}
this->displayFormat = format;
}
friend std::ostream& operator<<(std::ostream& os, const Date& date) {
std::string formattedDate = date.displayFormat;
formattedDate = replacePlaceholder(formattedDate, "dd", std::to_string(date.day));
formattedDate = replacePlaceholder(formattedDate, "mm", std::to_string(date.month));
formattedDate = replacePlaceholder(formattedDate, "yyyy", std::to_string(date.year));
os << formattedDate;
return os;
}
private:
bool isValidDate(int day, int month, int year) {
return true;
}
bool isValidFormat(const std::string& format) {
return (format.find("dd") != std::string::npos &&
format.find("mm") != std::string::npos &&
format.find("yyyy") != std::string::npos);
}
};
int main()
{
return 0;
}