Skip to content

Instantly share code, notes, and snippets.

@agiletalk
Created March 30, 2011 03:16
Show Gist options
  • Save agiletalk/893800 to your computer and use it in GitHub Desktop.
Save agiletalk/893800 to your computer and use it in GitHub Desktop.
오늘 날짜로부터 D-day까지 남은 날짜 구하기
#include <iostream>
using namespace std;
class DayofYear
{
private:
int d_mon;
int d_day;
int t_mon;
int t_day;
char today[12];
public:
void SetDday(int m, int d);
void SetToday(int m, int d);
char* GetMonth(int m);
char* GetToday();
void ShowToday();
int GetDays(int m);
int GetPastDay(int m, int d);
int GetLeftDay();
void ShowLeftDay();
};
void DayofYear::SetDday(int m, int d)
{
d_mon = m;
d_day = d;
}
void DayofYear::SetToday(int m, int d)
{
t_mon = m;
t_day = d;
}
// m월의 영문 표기
char* DayofYear::GetMonth(int m)
{
char* month[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
return month[m-1];
}
char* DayofYear::GetToday()
{
sprintf(today, "%s %d", GetMonth(t_mon), t_day);
return today;
}
void DayofYear::ShowToday()
{
//cout << GetMonth(t_mon) << " " << t_day << endl;
cout << GetToday() << endl;
}
// m월의 날짜 수
int DayofYear::GetDays(int m)
{
int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
return days[m-1];
}
// 1월 1일부터 m월 d일까지 날짜 합
int DayofYear::GetPastDay(int m, int d)
{
int i, past = 0;
// 1월부터 바로 전달까지 날짜 합
for(i = 1; i < m; i++)
{
past = past + GetDays(i);
}
// 이번달 날짜 합
past = past + d;
return past;
}
int DayofYear::GetLeftDay()
{
// 1월 1일부터 D-day까지의 날짜 합 - 1월 1일부터 오늘까지의 날짜 합
return GetPastDay(d_mon, d_day) - GetPastDay(t_mon, t_day);
}
void DayofYear::ShowLeftDay()
{
int left = GetLeftDay();
// D-day가 오늘 날짜보다 앞설 경우
if(left < 0)
{
cout << "D-day가 오늘 날짜보다 앞설 수 없음" << endl;
return;
}
cout << GetMonth(d_mon) << " " << d_day << " (" << left << " days left)" << endl;
}
int main()
{
int mon, day;
DayofYear dy;
dy.SetToday(3, 30);
cout << "입력: ";
cin >> mon >> day;
dy.SetDday(mon, day);
dy.ShowToday();
dy.ShowLeftDay();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment