Inputs a date (e.g. July 4, 2008) and outputs the day of the week-judge the day of the week based on the input date

Source: Internet
Author: User

Inputs a date (e.g. July 4, 2008) and outputs the day of the week-judge the day of the week based on the input date:

//inputs a date (e.g. July 4, 2008) and outputs the day of the week#include<iostream>#include<string>using namespace std;bool leapyear;void getInput(string& a,int& month,int& day,int& year);bool isLeapYear(int year);int getCenturyValue(int year);int getYearValue(int year);int getMonthValue(string a,int month,int year);int main(){    int month,day,year,week;    int centuryvalue,yearvalue,monthvalue;    string a,b;        getInput(a,month,day,year);    leapyear = isLeapYear(year);    cout<<"isLeapYear "<<leapyear<<endl;    centuryvalue = getCenturyValue(year);    cout<<"centuryvalue "<<centuryvalue<<endl;    yearvalue = getYearValue(year);    cout<<"yearvalue "<<yearvalue<<endl;    monthvalue = getMonthValue(a,month,year);    cout<<"monthvalue "<<monthvalue<<endl;        week = (day + monthvalue + yearvalue + centuryvalue) % 7;    cout<<"week "<<week<<endl;    switch(week)    {        case 1:            b = "Monday";            break;        case 2:            b = "Tuesday";            break;        case 3:            b = "Wednesday";            break;        case 4:            b = "Thursday";            break;        case 5:            b = "Firday";            break;        case 6:            b = "Saturday";            break;        case 0:            b = "Sunday";            break;        default:            cout<<"ERROR!";    }    cout<<"The day of week is "<<b<<endl;        return 0;}void getInput(string& a,int& month,int& day,int& year){    int number;    cout<<"Enter the date:"<<endl;    cout<<"1:month is english,"<<endl;    cout<<"2:month is numbers."<<endl;    cin>>number;    switch(number)    {        case 1:            cout<<"Enter the date(july 4 2008):\n";            cin>>a>>day>>year;            break;        case 2:            cout<<"Enter the date(7 4 2008):\n";            cin>>month>>day>>year;            break;        default:            cout<<"Error!";    }}bool isLeapYear(int year){    if(0 == year % 400 || (year % 4 ==0 && 0 != year % 100))        return 1;    return 0;}int getCenturyValue(int year){    int tem;    tem =(3 - (year / 100) % 4)*2;    return tem;}int getYearValue(int year){    int tem;    tem = (year % 100) / 4 + year % 100;    return tem;}int getMonthValue(string a,int month,int year){    if(a == "october" || month == 10 || (a == "january" && (!leapyear)) || (month == 1 && (!leapyear)))        return 0;    else if(a == "may" || month == 5)        return 1;    else if(a == "august" || month == 8 || (a == "february" && (leapyear)) || (month == 2 && (leapyear)))        return 2;    else if(a == "march" || month == 3 || a == "november" || month == 11 || (a == "february" && (!leapyear)) || (month == 2 && (!leapyear)))        return 3;    else if(a == "june" || month == 6 )        return 4;    else if(a == "september" || month == 9 || a == "december" || month == 12)        return 5;    else if(a == "april" || month == 4 || a == "july" || month == 7 || (a == "january" && (leapyear)))        return 6;    return -1;}

Result:

Enter the date:1:month is english,2:month is numbers.2Enter the date(7 4 2008):9 1 2014isLeapYear 0centuryvalue 6yearvalue 17monthvalue 5week 1The day of week is MondayEnter the date:1:month is english,2:month is numbers.2Enter the date(7 4 2008):7 4 2008isLeapYear 1centuryvalue 6yearvalue 10monthvalue 6week 5The day of week is FirdayEnter the date:1:month is english,2:month is numbers.1Enter the date(july 4 2008):july 4 2008isLeapYear 1centuryvalue 6yearvalue 10monthvalue 6week 5The day of week is Firday


Inputs a date (e.g. July 4, 2008) and outputs the day of the week-judge the day of the week based on the input date

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.