년월일 입력받아 요일 출력

jka0725의 이미지

년, 월, 일을 입력받아서 무슨 요일인지 출력하는 과제를 해결해야합니다

아래소스를 이용해서 해야하는데요 고수님들 도와주시면 감사하겠습니다.

#include
#include
using namespace std;

void printMonth(int year, int month);
void printMonthTitle(int year, int month);
void printMonthName(int month);
void printMonthBody(int year, int month);
int getStartDay(int year, int month);
int getTotalNumberOfDays(int year, int month);
int getNumberOfDaysInMonth(int year, int month);
bool isLeapYear(int year);

int main()
{

printf("Enter full year (e.g., 2001):");
int year;
cin >> year ;

printf("Enter month in number between 1 and 12:");
int month;
cin >> month ;

printMonth(year,month);

return 0;
}

void printMonth(int year, int month)
{
printMonthTitle(year,month);
printMonthBody(year,month);
}

void printMonthTitle(int year, int month)
{
printMonthName(month);
cout << " " << year << endl;
cout << "-------------------------" << endl;
cout << " Sun Mon Tue Wed Thu Fri Sat" << endl;
}

void printMonthName(int month)
{
switch (month)
{
case 1 :
cout << "January";
break;
case 2 :
cout << "February";
break;
case 3 :
cout << "March";
break;
case 4 :
cout << "April";
break;
case 5 :
cout << "May";
break;
case 6 :
cout << "June";
break;
case 7 :
cout << "July";
break;
case 8 :
cout << "August";
break;
case 9 :
cout << "September";
break;
case 10 :
cout << "October";
break;
case 11 :
cout << "November";
break;
case 12 :
cout << setw(16) << "December";
break;
}
}

void printMonthBody(int year, int month)
{
int startDay = getStartDay(year, month);

int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);

int i = 0;
for (i = 0; i < startDay; i++)
cout << " ";


for (i =1; i<= numberOfDaysInMonth; i++)
{
cout << setw(4) << i;

if ((i + startDay) % 7 == 0)
cout << endl;
}
}

int getStartDay(int year,int month)
{
int startDay1800 = 3;
int totalNumberOfDays = getTotalNumberOfDays(year, month);

return (totalNumberOfDays + startDay1800) % 7;
}

int getTotalNumberOfDays(int year,int month)
{
int total = 0 ;

for (int i=1800; i < year; i++)
if (isLeapYear(i))
total = total + 366;
else
total = total + 365;
for (int i=1; i < month;i++)
total = total + getNumberOfDaysInMonth(year,i);

return total;
}

int getNumberOfDaysInMonth(int year,int month)
{
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
return 31;

if (month == 4 || month == 6 || month == 9 ||month == 11)
return 30;

if (month ==2 ) return isLeapYear(year) ? 29 : 28;

return 0;
}

bool isLeapYear(int year)
{
return year % 400 == 0 || (year % 4 == 0 && year %100 != 0);
}

익명 사용자의 이미지

#include
#include
using namespace std;

void printMonth(int year, int month);
void printMonthTitle(int year, int month);
void printMonthName(int month);
void printMonthBody(int year, int month);
int getStartDay(int year, int month);
int getTotalNumberOfDays(int year, int month);
int getNumberOfDaysInMonth(int year, int month);
bool isLeapYear(int year);

int main()
{

printf("Enter full year (e.g., 2001):");
int year;
cin >> year ;

printf("Enter month in number between 1 and 12:");
int month;
cin >> month ;

printMonth(year,month);

return 0;
}

void printMonth(int year, int month)
{
printMonthTitle(year,month);
printMonthBody(year,month);
}

void printMonthTitle(int year, int month)
{
printMonthName(month);
cout << " " << year << endl;
cout << "-------------------------" << endl;
cout << " Sun Mon Tue Wed Thu Fri Sat" << endl;
}

void printMonthName(int month)
{
switch (month)
{
case 1 :
cout << "January";
break;
case 2 :
cout << "February";
break;
case 3 :
cout << "March";
break;
case 4 :
cout << "April";
break;
case 5 :
cout << "May";
break;
case 6 :
cout << "June";
break;
case 7 :
cout << "July";
break;
case 8 :
cout << "August";
break;
case 9 :
cout << "September";
break;
case 10 :
cout << "October";
break;
case 11 :
cout << "November";
break;
case 12 :
cout << setw(16) << "December";
break;
}
}

void printMonthBody(int year, int month)
{
int startDay = getStartDay(year, month);

int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);

int i = 0;
for (i = 0; i < startDay; i++)
cout << " ";

for (i =1; i<= numberOfDaysInMonth; i++)
{
cout << setw(4) << i;

if ((i + startDay) % 7 == 0)
cout << endl;
}
}

int getStartDay(int year,int month)
{
int startDay1800 = 3;
int totalNumberOfDays = getTotalNumberOfDays(year, month);

return (totalNumberOfDays + startDay1800) % 7;
}

int getTotalNumberOfDays(int year,int month)
{
int total = 0 ;

for (int i=1800; i < year; i++)
if (isLeapYear(i))
total = total + 366;
else
total = total + 365;
for (int i=1; i < month;i++)
total = total + getNumberOfDaysInMonth(year,i);

return total;
}

int getNumberOfDaysInMonth(int year,int month)
{
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
return 31;

if (month == 4 || month == 6 || month == 9 ||month == 11)
return 30;

if (month ==2 ) return isLeapYear(year) ? 29 : 28;

return 0;
}

bool isLeapYear(int year)
{
return year % 400 == 0 || (year % 4 == 0 && year %100 != 0);
}

jka0725의 이미지

어떤것이 추가된건지 잘모르겠습니다 ㅠㅠ

익명 사용자의 이미지

diff 해보시면 보일거예요.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.