查看main函数,我们可以看到我已经将“星期一”硬编码到我的setDay公共函数中。使用c-string(就像我在setDay中所做的那样)很容易从用户那里获取一周中的某一天,但是我如何要求用户将n添加到设置的日期,“星期一”并提出“星期四”?这很难,因为typdef enum {无效,星期一,星期二,星期三,星期四,星期五,星期六,星期日}不解释9是0和/或10为1。
#include <iostream>
using std::cout;
using std::endl;
class DayOfTheWeek //class is encapsulation of functions and members that manipulate the data.
{
public:
DayOfTheWeek(); // Constructor
virtual ~DayOfTheWeek(); // Destructor
void setDay(const char * day); // Function to set the day
void printDay() const; // Function to Print the day.
const char * getDay() const; // Function to get the day.
const char * plusOneDay(); // Next day function
const char * minusOneDay(); // Previous day function
const char * addDays(int addValue); // function that adds days based on parameter value
private:
char * day; // variable for the days of the week.
};
DayOfTheWeek::DayOfTheWeek() : day(0)
{
// Usually I would allocate pointer member variables
// Here in the construction of the Object
}
const char * DayOfTheWeek::getDay() const
{
return day; // we can get the day simply by returning it.
}
const char * DayOfTheWeek::minusOneDay()
{
if ( strcmp( day, "Monday" ) == 0)
{
cout << "The day before " << day << " is ";
return "Sunday";
}
else if ( strcmp( day, "Tuesday" ) == 0 )
{
cout << "The day before " << day << " is ";
return "Monday";
}
else if ( strcmp( day, "Wednesday" ) == 0 )
{
cout << "The day before " << day << " is ";
return "Tuesday";
}
else if ( strcmp( day, "Thursday" ) == 0 )
{
cout << "The day before " << day << " is ";
return "Wednesday";
}
else if ( strcmp( day, "Friday" ) == 0 )
{
cout << "The day before " << day << " is ";
return "Thursday";
}
else if ( strcmp( day, "Saturday" ) == 0 )
{
cout << "The day before " << day << " is ";
return "Friday";
}
else if ( strcmp( day, "Sunday" ) == 0 )
{
cout << "The day before " << day << " is ";
return "Saturday";
}
else
{
cout << "'" << day << "'";
return "is an invalid day of the week!";
}
}
const char * DayOfTheWeek::plusOneDay()
{
if ( strcmp( day, "Monday" ) == 0)
{
cout << "The day after " << day << " is ";
return "Tuesday";
}
else if ( strcmp( day, "Tuesday" ) == 0 )
{
cout << "The day after " << day << " is ";
return "Wednesday";
}
else if ( strcmp( day, "Wednesday" ) == 0 )
{
cout << "The day after " << day << " is ";
return "Thursday";
}
else if ( strcmp( day, "Thursday" ) == 0 )
{
cout << "The day after " << day << " is ";
return "Friday";
}
else if ( strcmp( day, "Friday" ) == 0 )
{
cout << "The day after " << day << " is ";
return "Saturday";
}
else if ( strcmp( day, "Saturday" ) == 0 )
{
cout << "The day after " << day << " is ";
return "Sunday";
}
else if ( strcmp( day, "Sunday" ) == 0 )
{
cout << "The day after " << day << " is ";
return "Monday";
}
else
{
cout << "'" << day << "'";
return " is an invalid day of the week!";
}
}
const char * DayOfTheWeek::addDays(int addValue)
{
if ( addValue < 0 )
{
if ( strcmp( day, "Monday" ) == 0)
{
cout << day << " - " << -addValue << " = ";
return "Friday";
}
else if ( strcmp( day, "Tuesday" ) == 0 )
{
cout << day << " - " << -addValue << " = ";
return "Saturday";
}
else if ( strcmp( day, "Wednesday" ) == 0 )
{
cout << day << " - " << -addValue << " = ";
return "Sunday";
}
else if ( strcmp( day, "Thursday" ) == 0 )
{
cout << day << " - " << -addValue << " = ";
return "Monday";
}
else if ( strcmp( day, "Friday" ) == 0 )
{
cout << day << " - " << -addValue << " = ";
return "Tuesday";
}
else if ( strcmp( day, "Saturday" ) == 0 )
{
cout << day << " - " << -addValue << " = ";
return "Wednesday";
}
else if ( strcmp( day, "Sunday" ) == 0 )
{
cout << day << " - " << -addValue << " = ";
return "Thursday";
}
else
{
cout << "'" << day << "' ";
return "is an invalid day of the week! ";
}
}
else // if our parameter is greater than 0 (positive)
{
if ( strcmp( day, "Monday" ) == 0)
{
cout << day << " + " << addValue << " = ";
return "Thursday";
}
else if ( strcmp( day, "Tuesday" ) == 0 )
{
cout << day << " + " << addValue << " = ";
return "Friday";
}
else if ( strcmp( day, "Wednesday" ) == 0 )
{
cout << day << " + " << addValue << " = ";
return "Saturday";
}
else if ( strcmp( day, "Thursday" ) == 0 )
{
cout << day << " + " << addValue << " = ";
return "Sunday";
}
else if ( strcmp( day, "Friday" ) == 0 )
{
cout << day << " + " << addValue << " = ";
return "Monday";
}
else if ( strcmp( day, "Saturday" ) == 0 )
{
cout << day << " + " << addValue << " = ";
return "Tuesday";
}
else if ( strcmp( day, "Sunday" ) == 0 )
{
cout << day << " + " << addValue << " = ";
return "Wednesday";
}
else
{
cout << "'" << day << "' ";
return "is an invalid day of the week! ";
}
}
}
void DayOfTheWeek::printDay() const
{
cout << "The Value of the " << day;
}
void DayOfTheWeek::setDay(const char * day)
{
if (day)
{// Here I am allocating the object member char day pointer
this->day = new char[strlen(day)+1];
size_t length = strlen(day)+1; // +1 for trailing null char
strcpy_s(this->day , length , day); // copying c-strings
}
else day = NULL; // If their was a problem with the parameter 'day'
}
DayOfTheWeek::~DayOfTheWeek()
{
delete day; // Free the memory allocated in SetDay
}
int main()
{
DayOfTheWeek MondayObject; // declare an object
MondayObject.setDay("Monday"); // Call our public function 'setDay' to set a day of the week
MondayObject.printDay(); // Call our public function 'printDay' to print the day we set
cout << " object is " << MondayObject.getDay() << endl; // Print the value of the object
cout << MondayObject.plusOneDay() << endl;
cout << MondayObject.minusOneDay() << endl;
cout << MondayObject.addDays(3) << endl;
MondayObject.printDay();
cout << " object is still " << MondayObject.getDay() << endl; // Print the value of the object
cout << MondayObject.addDays(-3) << endl;
return 0;
}
答案 0 :(得分:9)
您可能只想在类中使用整数来存储星期几,而不是尝试使用字符串存储它的当天。整数和模数运算最常用于存储此类数据。
如果您存储星期几,例如星期日到星期一是0到6,您可以使用模数运算符%
对其进行简单的操作;只要你不知道某个操作是否在该范围内,你就可以
nDay = nDay % 7;
它将达到您期望的价值;例如,如果它从6上升到9,它将回绕到2。
答案 1 :(得分:6)
您可以使用枚举并封装星期一到星期一的翻转:
enum Weekday {
INVALID,
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
};
Weekday operator+(Weekday w, int n) {
if (w == INVALID) return w;
n %= 7;
if (n < 0) n += 7;
return Weekday((n + (w - 1)) % 7 + 1);
}
Weekday operator+(int n, Weekday w) { return w + n; }
Weekday operator-(Weekday w, int n) { return w + -n; }
Weekday operator-(int n, Weekday w); // not defined -- doesn't make sense and not
// to be used
std::string str(Weekday d) {
assert(INVALID <= d && d <= SUNDAY); // function precondition
static char const* const days[] = {
"INVALID",
"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY"
};
return days[d];
}
// you would probably not call this directly, but it shows how you simply have to
// pick what day int(0) means, and add any int to that day to get a Weekday
Weekday Weekday_from_num(int n, Weekday zero=MONDAY) {
return zero + n;
}
Test:
int main() {
assert(MONDAY + 1 == TUESDAY);
assert(MONDAY + 8 == TUESDAY);
assert(MONDAY - 1 == SUNDAY);
assert(MONDAY - 8 == SUNDAY);
cout << "MONDAY + 3 is " << str(MONDAY + 3) << '\n';
return 0;
}
但是,我会删除INVALID(这也简化了数学运算)。你已经写了一个Weekday_from_str函数,你只需要调整它就可以返回这个枚举。
答案 2 :(得分:2)
这种(经常遇到的)问题不需要编写类,包含枚举的命名空间和两个函数就足够了:
#include <iostream>
namespace day {
enum e_DAY { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
inline e_DAY GetDay(unsigned int i)
{
return (e_DAY)(i % 7);
}
inline const wchar_t* GetName(unsigned int i)
{
static wchar_t* Names[] = { L"Sunday", L"Monday", L"Tuesday", L"Wednesday", L"Thursday", L"Friday", L"Saturday" };
return Names[GetDay(i)];
}
} // namespace day
int main(int argc, wchar_t* argv[])
{
std::wcout << day::Tue + 3 << "\t" << day::GetName(day::Tue + 3) << std::endl;
return 0;
}