3. [50%] Write a program DayOfWeek.cpp that inputs a date (e.g., March 17, 2017) and outputs the day of the week that corresponds to that date. The following algorithm is from
https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_we ek. The implementation will require several functions. bool isLeapYear(int year); This function should return true if year is a leap year and false if it is not. Here is pseudocode to determine a leap year: leap_year ((year divisible by 400) or (year divisible by 4 and year not divisible by 100)) int getCenturyValue(int year); This function should take the first two digits of the year (i.e., the century), divide by 4, and save the remainder. Subtract the remainder from 3 and return this value multiplied by 2. For example, the year 2017 becomes: (20/4) 5 with a remainder of 0. 3−0=3. Return 3 * 2 = 6. int getYearValue(int year); This function computes a value based on the years since the beginning of the century. First, extract the last two digits of the year. For example, 17 is extracted for 2017. Next, factor in leap years. Divide the value from the previous step by 4 and discard the remainder. Add the two results together and return this value. For example, from 2017 we extract 17. Then (17/4)= 4 with a remainder of 1. Return 4 + 17 = 21. int getMonthValue(int month, int year); This function should return a value based on the table below and will require invoking the isLeapYear function.
Month Return Value
January 0 (6 if year is leap year)
February 3 (2 if year is a leap year)
March 3
April 6
May 1
June 4
July 6
August 2
September 5
October 0
November 3
December 5
Finally, to compute the day of the week, compute the sum of the date’s day plus the values returned by getMonthValue, getYearValue, and getCenturyValue. Divide the sum by 7 and compute the remainder. A remainder of 0 corresponds to Sunday, 1 corresponds to Monday, etc., up to 6 which corresponds to Saturday. For example, the date March 17, 2017 should be computed as (day of month) + (getMonthValue) + (getYearValue) + (getCenturyValue) = 17 + 3 + 21 + 6 = 47. 47/7 = 6 with a remainder of 5. The fifth day of the week corresponds to Friday. Your program should allow the user to enter any date and output the corresponding day of the week in English. This program should include a void function named getInput that prompts the user for the date and returns the month, day, and year
using pass-by-reference parameters. The user input is three integers, month, day, year, separated by comma.
This question is adapted from one problem in Chapter 5 of textbook “Problem Solving With C++”. \
可以唔洗睇前邊 問題係點樣Input 可以間住啲comma 而又係同一行
應該要出到既答案:
3, 17, 2017
Friday
4, 1, 2017
Saturday
我寫既:
#include <iostream>
using namespace std;
bool isLeapYear(int& year)
{
bool leap;
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
leap = true;
else
leap = false;
return leap;
}
int getCenturyValue(int& year)
{
int calculation1, f2digit;
f2digit = year/100;
calculation1 = (3 - f2digit % 4) * 2;
return calculation1;
}
int getYearValue(int& year)
{
int calculation2, l2digit;
l2digit = year % 100;
calculation2 = l2digit / 4 + l2digit;
return calculation2;
}
int getMonthValue(int& month, int& year)
{
int leap=0 ;
switch (month)
{
case 1:
if (isLeapYear(year) ==1)
leap =6 ;
else leap =0; break;
case 2:
if (isLeapYear(year) ==1)
leap = 2;
else leap =3; break;
case 3:
leap = 3; break;
case 4:
leap = 6; break;
case 5:
leap = 1; break;
case 6:
leap = 4; break;
case 7:
leap = 6; break;
case 8:
leap = 2; break;
case 9:
leap = 5; break;
case 10:
leap = 0; break;
case 11:
leap = 3; break;
case 12:
leap = 5;
}
return leap;
}
void getInput (int& month, int& day, int& year)
{
cout << month <<", "<< day <<", " << year <<", ";
}
int main()
{
int month, day=0, year;
cin >> month;
cout << ", ";
cin >> day;
cout << ", ";
cin >> year;
switch ((day + getCenturyValue(year) + getYearValue(year) + getMonthValue(month,year))%7)
{
case 1:
cout << "Monday"; break;
case 2:
cout << "Tuesday"; break;
case 3:
cout << "Wednesday"; break;
case 4:
cout << "Thursday"; break;
case 5:
cout << "Friday"; break;
case 6:
cout << "Saturday"; break;
case 7:
cout << "Sunday"; break;
}
}
但就會出左咁
3,
17,
2017
Friday
點先可以放反同一行
[
本帖最後由 aa764258 於 2017-3-17 11:41 AM 編輯 ]