为什么不读我的日子c ++

时间:2015-05-06 20:57:24

标签: c++

我是c ++的新手,我在准备之前获取我的代码时遇到了一些麻烦,并且在特定日期的某一天出现了。当我说出我的代码时,它说 您的日期是:2015年2月15日 给定日期的日期是 有关如何解决此问题的任何建议吗?

#include <iostream>
#include <string>
using namespace std;

class Date
{
    private:
        int day;
        int month;
        int year;
        bool isLeapYear (int);
        int getMonthValue (int, int);
        int getCenturyValue (int);
        int getYearValue (int);
        string calculateWeekday();

    public:
        Date ();
        Date (int, int, int);
        void setDay (int);
        void setYear (int);
        void setMonth (int);
        int getDay ();
        int getMonth ();
        int getYear ();
        void displayWeekday ();
};





#include <iostream>
#include <string>
using namespace std;

Date::Date()
{
}


Date::Date (int day, int month, int year)

{

    setDay(day);
    setMonth(month);
    setYear(year);
}

void Date::setDay (int d)
{
    day = d;
}

void Date::setMonth (int m)
{
    month = m;
}

void Date::setYear (int y)
{
    year = y;
}

int Date::getDay()
{
    return day;
}

int Date::getMonth()
{
    return month;
}

int Date::getYear()
{
    return year;
}

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

int Date::getMonthValue (int month, int year)
{
    int monthValue;
    switch(month)
    {
        case 1:
            if(isLeapYear(year))
                monthValue = 6;
            else
                monthValue = 0;
            break;

        case 2:
            if(isLeapYear(year))
                monthValue = 2;
            else
                monthValue = 3;
            break;

        case 3: 
            monthValue = 3;
            break;
        case 11:
            monthValue = 3;
            break;
        case 4:
            monthValue = 6;
            break;
        case 5:
            monthValue = 1;
            break;
        case 6:
            monthValue = 4;
            break;
        case 7:
            monthValue = 6;
            break;
        case 8:
            monthValue = 2;
            break;
        case 9:
            monthValue = 5;
            break;
        case 10:
            monthValue = 0;
            break;
        case 12:
            monthValue = 5;
            break;

    }
    return monthValue;
}

int Date::getYearValue (int year)

{
    int lastTwo = year % 100;
    int d = lastTwo / 4;
    int r = lastTwo + d;
    return r;
}

int Date::getCenturyValue (int year)

{
    int firstTwo = year / 100;
    int in = firstTwo % 4;
    int ints = 3 - in;
    return ints * 2;
}

string Date::calculateWeekday ()
{
    string day;
    int sum = getDay() + getMonthValue (month, year) + getYearValue (year) + getCenturyValue (year);
    int rem = sum % 7;

    if (rem == 0)
        day = "Sunday";
    if (rem == 1)
        day = "Monday";
    if (rem == 2)
        day = "Tuesday";
    if (rem == 3)
        day = "Wednesday";
    if (rem == 4)
        day = "Thursday";
    if (rem == 5)
        day = "Friday";
    if (rem == 6)
        day = "Saturday";

    return day;

}

void Date::displayWeekday()
{
    cout << "\nYour date is:";
    cout << getMonth() << "/";
    cout << getDay() << "/";
    cout << getYear() << endl;
    cout << "The day of the given date is \n"; 
    cout<< calculateWeekday();

}

主     #include

using namespace std;
int main ()
{
    Date todaysDate (2,15,2015);

    todaysDate.displayWeekday();

    return 0;
}

1 个答案:

答案 0 :(得分:4)

您在构造函数调用中颠倒了月和日的顺序。您的构造函数需要订单中的参数:int day, int month, int year。您将参数传递为Date todaysDate (2,15,2015);我假设月份为2且日期为15.所以您应该拨打Date todaysDate (15,2,2015);