#include
#include
using namespace std;
class Date{
public:
Date(int year, int month, int day);
Date(string s);
int Year, Month, Day;
int getYear(), getMonth(), getDay();
void show();
};
Date::Date(int year, int month, int day) {
Year = year;
Month = month;
Day = day;
}
Date::Date(string s) {
int slash1,slash2,count =0; // 1번째 슬래시 위치, 2번째 슬래시 위치, 슬래시 카운터
for (int i = 0; i < s.length(); i++){ // 0부터 문자열 길이까지 반복
if (s[i] == '/' && count == 0){ // '/'를 발견하고, count 가 0인 경우
slash1 = i; // 1번째 슬래시 위치
count++; // count + 1
}
else if (s[i] == '/' && count == 1){ // '/'를 발견하고, count 가 1인 경우
slash2 = i; // 2번째 슬래시 위치
count++; // count + 1
}
}
Year = stoi(s.substr(0, slash1)); // 0~1번째 슬래시까지 잘라서 숫자화
Month = stoi(s.substr(slash1 + 1, slash2 - slash1 - 1)); // 1번째 슬래시 +1 ~ 2번째 슬래시 -1 까지 잘라서 숫자화
Day = stoi(s.substr(slash2 + 1, s.length())); // 2번째 슬래시 +1 ~ 끝까지 숫자화
}
int Date::getYear() { // getYear()를 통해서 Year변수를 리턴
return Year;
}
int Date::getMonth() { // getMonth()를 통해서 Month변수를 리턴
return Month;
}
int Date::getDay() { // getDay()를 통해서 Day변수를 리턴
return Day;
}
void Date::show() {
cout << Year << "년" << Month << "월" << Day << "일" << endl;
}
int main() {
Date birth(2014, 3, 20);
Date independenceDay("1945/8/15");
independenceDay.show();
cout << birth.getYear() << "," << birth.getMonth() << "," << birth.getDay() << endl;
}