Typing diary

명품 C++ programming 3강 2번문제 본문

C, C++

명품 C++ programming 3강 2번문제

Jcon 2022. 8. 16. 15:13
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
#include <iostream>
#include <string>
 
using namespace std;
 
class Date
{
    int Year, Month, Day;
public:
    Date(int year, int month, int day) { Year = year; Month = month; Day = day; }
    Date(string date);
    void show();
    int getYear() { return Year; }
    int getMonth() { return Month; }
    int getDay() { return Day; }
};
 
int main(void)
{
    Date birth(2014320);
    Date intdependenceDay("1945/8/15");
    intdependenceDay.show();
    cout << birth.getYear() << ',' << birth.getMonth() << ',' << birth.getDay() << endl;
}
 
Date::Date(string date)
{
    int count[2= { 0 };
    string a;
    int c = 0;
 
    for (int i = 0; i < date.length(); i++)
    {
 
        if (date.at(i) == '/')
        {
            count[c] = i;
            c++;
        }
    }
 
    Year = stoi(date.substr(0, count[0]));
    Month = stoi(date.substr(count[0+ 1, count[1]));
    Day = stoi(date.substr(count[1+ 1, date.length()));
}
 
void Date::show()
{
    cout << Year << "년" << Month << "월" << Day << "일" << endl;
}
 
cs

문자열 다루는 연습이 필요할듯 .. 

'C, C++' 카테고리의 다른 글

string 문자열  (0) 2022.08.16
명품 c++ programming 4강 Open Challenge  (0) 2022.08.16
명품 c++ programming 3강 3,4,5번  (0) 2022.08.16
c++ 객체 배열  (0) 2022.08.16
C 메모리 동적할당  (0) 2022.08.16