我正在尝试让重载的postfix运算符make day = 1每当第365天第一次剪切是我创建原型时,第二个片段是实际的代码定义,我似乎无法解决这个问题。任何帮助,将不胜感激。 基本上该程序会询问用户一年中哪一天他们想要日期。
所以这一天可能是364,即12月30日。
我希望++运算符在每天使用它来增加当天为365的对象时将这一天带回第1天。如果对象不是365,我只希望它增加1天
每当我将其更改为if(day == 365) 它做同样的事情,它仍然增加1,但我最终得到366,而不是1。
我很感激帮助。这是一个家庭作业,但它让我st脚。 编辑:发布更多代码。再次感谢帮助人员
#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;
}
答案 0 :(得分:5)
if(day = 365)
{day = 1;}
else
{day ++;}
这不符合你的想法。 =
是赋值运算符。 ==
将是比较运算符。目前,该表达式将值365
分配给day
变量,并返回365
,执行day = 1;
(任何不0
的内容都是{{} 1}})
这是你想要做的:
true
您的代码存在的问题是,虽然您要为该类定义修复后if (day == 365) {
day = 1;
} else {
day++;
}
,但您在该类的operator++
成员上使用此运算符。
在int day
中,你不应该int main
,你应该做day.day++
。
答案 1 :(得分:2)
您没有在任何地方调用过载的operator++
。
DayOfYear day;
cout << day.day << endl;
cout << day.day ++ << endl;
cout << day.day << endl;
day.day
是day
类的成员DayOfYear
,因此day.day++
只是int
的普通增量。你想这样做:
DayOfYear day;
cout << day.day << endl;
cout << (day++).day << endl;
cout << day.day << endl;