为什么不阅读我的日子C

Why is it not reading my days c++

本文关键字:日子 我的 为什么不      更新时间:2023-10-16

我是C 的新手,我在准备好之前准备好了代码并放在一周的某个日期时遇到一些麻烦。当我放下代码时,它说您的日期是:15/2/2015给定日期的一天是关于如何解决此问题的任何建议?

#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;
}

您在构造函数呼叫中扭转了每日和日的顺序。您的构造函数期望以下顺序:int day, int month, int year。您将参数传递为Date todaysDate (2,15,2015);,我认为这个月是2个,一天为15。因此,您应该致电Date todaysDate (15,2,2015);