我试图让重载的后缀运算符 make day = 1 每当它的第 365 天剪断是我创建原型时,第二个片段是实际的代码定义,我似乎无法弄清楚这一点。任何帮助,将不胜感激。基本上,程序会询问用户他们想要日期是一年中的哪一天。
所以这一天可能是 364,即 12 月 30 日。
我希望 ++ 运算符在我使用它来增加天为 365 的对象时将这一天带回第 1 天。如果对象不是 365,我只希望它增加 1 天
每当我将它更改为 if(day == 365) 时,它都会做同样的事情,它仍然会增加 1,但我最终得到的是 366,而不是 1。
我很感激帮助。这是一个家庭作业,但它让我跺着脚。编辑:发布更多代码。再次感谢大家的帮助
#include<iostream>
#include<string>
using namespace std;
class DayOfYear {
public:
DayOfYear(int d);
void print();
static string month;
DayOfYear();
DayOfYear(string m, int d);
DayOfYear operator++(int);
int day;
};
string DayOfYear::month;
DayOfYear DayOfYear::operator++(int) {
DayOfYear temp = *this;
if (day == 365) {
day = 1;
} else {
day++;
}
return temp;
}
DayOfYear::DayOfYear(string m, int d) {
month = m;
day = d;
if (m == "January" && d > 31) {
cout << "ERROR: PROGRAM TERMINATING!" << endl;
system("pause");
exit(0);
} else if (m == "February" && d > 28) {
cout << "ERROR INVALID DATE: PROGRAM TERMINATING!" << endl;
system("pause");
exit(0);
} else if (m == "March" && d > 31) {
cout << "ERROR INVALID DATE: PROGRAM TERMINATING!" << endl;
system("pause");
exit(0);
} else if (m == "April" && d > 30) {
cout << "ERROR INVALID DATE: PROGRAM TERMINATING!" << endl;
system("pause");
exit(0);
} else if (m == "May" && d > 31) {
cout << "ERROR INVALID DATE: PROGRAM TERMINATING!" << endl;
system("pause");
exit(0);
} else if (m == "June" && d > 30) {
cout << "ERROR INVALID DATE: PROGRAM TERMINATING!" << endl;
system("pause");
exit(0);
} else if (m == "July" && d > 31) {
cout << "ERROR INVALID DATE: PROGRAM TERMINATING!" << endl;
system("pause");
exit(0);
} else if (m == "August" && d > 31) {
cout << "ERROR INVALID DATE: PROGRAM TERMINATING!" << endl;
system("pause");
exit(0);
} else if (m == "September" && d > 30) {
cout << "ERROR INVALID DATE: PROGRAM TERMINATING!" << endl;
system("pause");
exit(0);
} else if (m == "October" && d > 31) {
cout << "ERROR INVALID DATE: PROGRAM TERMINATING!" << endl;
system("pause");
exit(0);
} else if (m == "November" && d > 30) {
cout << "ERROR INVALID DATE: PROGRAM TERMINATING!" << endl;
system("pause");
exit(0);
} else if (m == "December" && d > 31) {
cout << "ERROR INVALID DATE: PROGRAM TERMINATING!" << endl;
system("pause");
exit(0);
}
cout << month << " " << day << endl;
}
DayOfYear::DayOfYear() {
cout << "Enter a day number." << endl;
cin >> day;
print();
}
DayOfYear::DayOfYear(int d) {
day = d;
while (d > 365 || d < 1) {
cout << "Day can't be higher than 365 and must be at least one. " << endl;
cin >> d;
day = d;
}
print();
}
void DayOfYear::print() {
if (day > 0 && day < 32) //jan 31
{
month = "January";
cout << month << " " << 31 + day - 31 << endl;
}
else if (day > 31 && day < 60) // feb 28
{
month = "February";
cout << month << " " << day - 31 << endl;
}
else if (day > 59 && day < 91) // march 31
{
month = "March";
cout << month << " " << day - 59 << endl;
}
else if (day > 90 && day < 121) // april 30
{
month = "April";
cout << month << " " << day - 90 << endl;
}
else if (day > 120 && day < 152) //may 31
{
month = "May";
cout << month << " " << day - 120 << endl;
}
else if (day > 151 && day < 182) //june 30
{
month = "June";
cout << month << " " << day - 151 << endl;
}
else if (day > 181 && day < 213) //july 31
{
month = "July";
cout << month << " " << day - 181 << endl;
}
else if (day > 212 && day < 244) // august 31
{
month = "August";
cout << month << " " << day - 212 << endl;
}
else if (day > 243 && day < 274) // sept 30
{
month = "September";
cout << month << " " << day - 243 << endl;
}
else if (day > 273 && day < 305) // oct 31
{
month = "October";
cout << month << " " << day - 273 << endl;
}
else if (day > 304 && day < 335) // nov 30
{
month = "November";
cout << month << " " << day - 304 << endl;
}
else if (day > 334 && day < 366) //dec 31
{
month = "December";
cout << month << " " << day - 334 << endl;
}
}
int main() {
DayOfYear day;
cout << day.day << endl;
cout << day.day ++ << endl;
cout << day.day << endl;
system("pause");
return 0;
}