所以这就是我想要实现的目标
这是我的代码。我现在完全陷入困境,我在这里错过了什么,就在我面前?我觉得我已经完成了所有功能,但我认为它不会正确计算它。我在这里有一些错误,我无法修复甚至尝试运行它。 谢谢你的帮助; -
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
class dayType
{
public:
void setDay(int dayNum);
// set the day with the dayNum as parameter
void printd(dayNum);
// print the dayNum
int returnDay();
// return the day of the week
void dayAfter();
// return next day
void dayBefore();
// return previous day
void randomDay(int dayNum);
// function to return a day after a certain number of days
dayType(int dayNum);
// Constructor with parameters setting dayNum according to parameters
dayType();
//Default constructor
private:
int today;
int yest;
int tom;
int dayN;
};
void dayType::printd(int dayNum)
{
if (dayNum == 1)
cout << "Monday" << endl;
if (dayNum == 2)
cout << "Tuesday" << endl;
if (dayNum == 3)
cout << "Wednesday" << endl;
if (dayNum == 4)
cout << "Thursday" << endl;
if (dayNum == 5)
cout << "Friday" << endl;
if (dayNum == 6)
cout << "Saturday" << endl;
if (dayNum == 7)
cout << "Sunday" << endl;
}
void dayType::setDay(int dayNum)
{
today = dayNum;
};
int dayType::returnDay()
{
return today;
};
void dayType::printd(<#int dayNum#>);
{
cout << "The current day is: " << today << endl;
}
void dayType::dayBefore()
{
if(today == 0)
yest = 6;
else today--;
};
void dayType::dayAfter()
{
if(today == 6)
tom = 0;
};
void dayType::randomDay(int dayNum)
{
dayN=(today+dayNum);
today =(dayN%7);
};
dayType::dayType()
{
today = 0;
}
dayType::dayType(int daynum)
{
today = daynum;
}
// do I need these constructors here doing this?
int main()
{
int dayWeek;
cout << "Please enter a number for the day of the week: " << endl;
cout << "1 - Monday" << endl;
cout << "2 - Tuesday" << endl;
cout << "3 - Wednesday" << endl;
cout << "4 - Thursday" << endl;
cout << "5 - Friday" << endl;
cout << "6 - Saturday" << endl;
cout << "7 - Sunday" << endl;
while (dayWeek<= 7)
cin >> dayWeek;
dayType thisDay;
cout << "Today is: ";
thisDay.returnDay();
thisDay.printd(int dayNum);
cout << "Yesterday was: ";
thisDay.dayBefore();
thisDay.printd(int dayNum);
cout << "Tomorrow is: ";
thisDay.dayAfter();
thisDay.printd(int dayNum);
cout << "Type a number of days from today and it will be: ";
thisDay.randomDay(dayNum);
return 0;
};
}
答案 0 :(得分:1)
您的代码应该更像这样:
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
class dayType
{
public:
void setDay(int dayNum);
void printd();
// set the day with the dayNum as parameter
void printd(int dayNum);
// print the dayNum
int returnDay();
// return the day of the week
int dayAfter();
// return next day
int dayBefore();
// return previous day
int randomDay(int dayNum);
// function to return a day after a certain number of days
dayType(int dayNum);
// Constructor with parameters setting dayNum according to parameters
dayType();
//Default constructor
private:
int today;
};
void dayType::printd(int dayNum)
{
if (dayNum == 1)
cout << "Monday" << endl;
if (dayNum == 2)
cout << "Tuesday" << endl;
if (dayNum == 3)
cout << "Wednesday" << endl;
if (dayNum == 4)
cout << "Thursday" << endl;
if (dayNum == 5)
cout << "Friday" << endl;
if (dayNum == 6)
cout << "Saturday" << endl;
if (dayNum == 7)
cout << "Sunday" << endl;
}
void dayType::setDay(int dayNum)
{
today = dayNum;
}
int dayType::returnDay()
{
return today;
}
void dayType::printd()
{
cout << "The current day is: " << today << endl;
}
int dayType::dayBefore()
{
int yest;
if(today == 0)
yest = 6;
else
yest = today - 1;
return yest;
};
int dayType::dayAfter()
{
int tom;
if(today == 6)
tom = 0;
else
tom = today + 1;
return tom;
};
int dayType::randomDay(int dayNum)
{
int dayN = (today+dayNum);
return (dayN % 7)
};
dayType::dayType()
{
today = 0;
}
dayType::dayType(int daynum)
{
today = daynum;
}
// do I need these constructors here doing this?
int main()
{
int dayWeek = -1;
int random = -1;
cout << "Please enter a number for the day of the week: " << endl;
cout << "1 - Monday" << endl;
cout << "2 - Tuesday" << endl;
cout << "3 - Wednesday" << endl;
cout << "4 - Thursday" << endl;
cout << "5 - Friday" << endl;
cout << "6 - Saturday" << endl;
cout << "7 - Sunday" << endl;
while (dayWeek >= 7 || dayWeek < 0)
cin >> dayWeek;
dayType thisDay;
thisDay.today = dayWeek;
thisDay.printd();
cout << "Yesterday was: ";
thisDay.printd(thisDay.dayBefore());
cout << "Tomorrow is: ";
thisDay.printd(thisDay.dayAfter());
cout << "Type a number of days : ";
while (random < 0)
cin >> random;
cout << "Now we are ";
thisDay.printd(thisDay.randomDay(random));
return 0;
}
当你调用这样的函数:void fonction(int a, int b)
时,你可以这样调用它:fonction(1, 2);
。您不必编写参数的类型。在您的课程中,仅声明您的功能所需的值。在之前的代码中,您声明了tom
,yes
和dayN
,但您无需保留其值。
我没有测试代码,所以如果你有错误,请写下来,我会编辑这篇文章。
答案 1 :(得分:1)
就我个人而言,我建议您使用enum
而不是int
来跟踪当天,以及必要的操作员,让您可以循环使用这些日子。这样您就可以更轻松地定义dayType
并与之互动,轻松修复您遇到的错误。
#include <iostream>
#include <cmath>
#include <string>
#include <limits>
using namespace std;
class dayType
{
public:
// Days enum:
enum Days {
D_MONDAY = 1,
D_TUESDAY,
D_WEDNESDAY,
D_THURSDAY,
D_FRIDAY,
D_SATURDAY,
D_SUNDAY
};
void setDay(int dayNum);
// set the day with the dayNum as parameter
void printd(int dayNum);
// print the dayNum
void printd();
// Prints the current day.
Days returnDay();
// return the day of the week
Days dayAfter();
// return next day
Days dayBefore();
// return previous day
Days randomDay(int dayNum);
// function to return a day after a certain number of days
dayType(int dayNum);
// Constructor with parameters setting dayNum according to parameters
dayType();
//Default constructor
private:
Days today;
};
// Compound assignment addition:
dayType::Days& operator+=(dayType::Days& left, int right)
{
typedef dayType::Days Days;
int temp = static_cast<int>(left) + right;
// In case of adding negative numbers.
while (temp < dayType::D_MONDAY)
temp += dayType::D_SUNDAY;
while (temp > dayType::D_SUNDAY)
temp -= dayType::D_SUNDAY;
left = static_cast<Days>(temp);
return left;
}
// Compound assignment subtraction:
dayType::Days& operator-=(dayType::Days& left, int right)
{
typedef dayType::Days Days;
int temp = static_cast<int>(left) - right;
while (temp < dayType::D_MONDAY)
temp += dayType::D_SUNDAY;
// In case of subtracting negative numbers.
while (temp > dayType::D_SUNDAY)
temp -= dayType::D_SUNDAY;
left = static_cast<Days>(temp);
return left;
}
// Addition. Uses compound assignment addition internally.
dayType::Days operator+(dayType::Days left, int right)
{
return left += right;
}
// Subtraction. Uses compound assignment subtraction internally.
dayType::Days operator-(dayType::Days left, int right)
{
return left -= right;
}
// Prefix increment (++day):
dayType::Days operator++(dayType::Days& day)
{
return day += 1;
}
// Postfix increment (day++):
dayType::Days operator++(dayType::Days& day, int)
{
typedef dayType::Days Days;
Days temp = day;
++day;
return temp;
}
// Prefix decrement (--day):
dayType::Days operator--(dayType::Days& day)
{
return day -= 1;
}
// Postfix decrement (day--):
dayType::Days operator--(dayType::Days& day, int)
{
typedef dayType::Days Days;
Days temp = day;
--day;
return temp;
}
void dayType::printd(int dayNum)
{
if (dayNum == D_MONDAY)
cout << "Monday" << endl;
if (dayNum == D_TUESDAY)
cout << "Tuesday" << endl;
if (dayNum == D_WEDNESDAY)
cout << "Wednesday" << endl;
if (dayNum == D_THURSDAY)
cout << "Thursday" << endl;
if (dayNum == D_FRIDAY)
cout << "Friday" << endl;
if (dayNum == D_SATURDAY)
cout << "Saturday" << endl;
if (dayNum == D_SUNDAY)
cout << "Sunday" << endl;
}
void dayType::printd()
{
cout << "The current day is: ";
printd(today);
}
void dayType::setDay(int dayNum)
{
today = static_cast<Days>(dayNum);
};
dayType::Days dayType::returnDay()
{
return today;
};
dayType::Days dayType::dayBefore()
{
return today - 1;
};
dayType::Days dayType::dayAfter()
{
return today + 1;
};
dayType::Days dayType::randomDay(int dayNum)
{
return today + dayNum;
};
dayType::dayType()
{
today = D_SUNDAY;
}
dayType::dayType(int daynum)
{
today = static_cast<Days>(daynum);
}
int main()
{
int dayWeek = 0;
cout << "Please enter a number for the day of the week: " << endl;
cout << "1 - Monday" << endl;
cout << "2 - Tuesday" << endl;
cout << "3 - Wednesday" << endl;
cout << "4 - Thursday" << endl;
cout << "5 - Friday" << endl;
cout << "6 - Saturday" << endl;
cout << "7 - Sunday" << endl;
while (!((dayWeek > 0) && (dayWeek < 8)))
cin >> dayWeek;
dayType thisDay(dayWeek);
// -----
cout << "Today is: ";
thisDay.printd(thisDay.returnDay());
// -----
cout << "Yesterday was: ";
thisDay.printd(thisDay.dayBefore());
// -----
cout << "Tomorrow is: ";
thisDay.printd(thisDay.dayAfter());
// -----
cout << "Type a number of days: ";
int random;
cin >> random;
cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
cout << "In " << random << " days, it will be: ";
thisDay.printd(thisDay.randomDay(random));
};
目前,这应该适用于您想要的内容,但需要注意的是,如果您想将int
用作Days
(例如在{{中初始化today
1}}),你必须明确地把它变成一个。 (相反,dayType::dayType(int daynum)
可以隐含地用作Days
,通过将int
的返回值传递给dayType::returnDay()
或者传递dayType::printd()
来证明} Days
。)
如果您想在cout
之外使用Days
,可以将其用作dayType
(例如在运算符&#39;返回类型中)。您还可以使用dayType::Days
将其带入当前范围,从而可以将其简化为typedef dayType::Days Days;
(例如Days
或dayType::Days operator++(dayType::Days& day, int)
)。同样,要使用枚举值本身,请在其名称前加上dayType::Days operator--(dayType::Days& day, int)
。
当你正在使用这个练习和你的问题时,作为一个学习机会,我留下了一些可以改进的地方。它目前工作正常,但有进行修改的空间,可以使其更好地工作。