gpt4 book ai didi

c++ - 星期二 + 3 = 星期五? C++编程问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:07:52 28 4
gpt4 key购买 nike

查看 main 函数,我们可以看到我已将“星期一”硬编码到我的 setDay 公共(public)函数中。使用 c 字符串很容易从用户那里获取星期几(就像我在 setDay 中所做的那样),但是我如何要求用户将 n 添加到设置的日期“星期一”并提出“周四”?这很难,因为 typdef enum { INVALID, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY} 不会将 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 到 6,您可以使用模数运算符 % 对其进行简单的操作;任何时候你不知道一个操作是否在那个范围内,你可以做

nDay = nDay % 7;

它会达到您期望的值;例如,如果它从 6 上升到 9,它将回到 2。

关于c++ - 星期二 + 3 = 星期五? C++编程问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2406622/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com