为什么不阅读我的日子C
Why is it not reading my days c++
我是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);
。
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 我的字符计数代码计算错误.为什么
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- cmake在我的项目中所需的所有静态库都不成功
- 为什么我的代码在输出中增加了93天
- 我的简单if-else语句是如何无法访问的代码
- 为什么我的for循环不能正确获取argv
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 0-1背包代码中的错误.我的代码中有什么错误
- 当我的阵列太大时出现分段错误
- 如何确认我的constexpr表达式实际上已经在编译时执行
- 为什么二进制搜索在我的测试中不起作用
- 如何指定我希望我的LIB链接到的DLL文件?-Visual Studio 2019
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- OpenGL在启用深度测试时不会丢弃我的碎片
- 为什么我的 std::ref 无法按预期工作?
- clang整洁10忽略了我的NOLINT命令
- 为什么不阅读我的日子C