gpt4 book ai didi

c++ - 在循环中递增日期

转载 作者:行者123 更新时间:2023-11-28 02:38:44 24 4
gpt4 key购买 nike

这是一个练习;我禁止使用 C++ 的内置日期函数或任何现成的库。

我有这个代码:

#include <iostream>
#include <vector>

using namespace std;


bool IsLeap(unsigned int year)
{
if (year % 4 != 0) return false;
else
if (year % 100 != 0) return true;
else
if (year % 400 != 0) return false;
else
return true;
}

class Date
{
public:
unsigned short int Day;
unsigned short int Month;
unsigned short int Year;
unsigned short int DayOfWeek;
};

int MonthLimit(int Month, int year)
{
switch (Month)
{
case 4:
case 6:
case 9:
case 11:
{
return 30;
break;
}
case 1:
case 3:
case 5:
case 7:
case 8:
case 12:
{
return 31;
break;
}
case 2:
if (IsLeap(year))
{
return 29;
break;
}
else
{
return 28;
break;
}

}
}



const Date FirstDayEver = { 1, 1, 1900, 1 }; //January 1, 1900 was on a Monday.

int main()
{
unsigned int years;
cin >> years;

vector<int> counters(7); //whenever a 13th falls on a day X, we increment the Xth counter. It's that simple


for (Date i = FirstDayEver; !((i.Day == 31) && (i.Month == 12) && (i.Year == 1900 + years - 1)); i.Day++)
{
i.DayOfWeek = (i.DayOfWeek + 1) % 7;
if (i.Month == MonthLimit(i.Month, i.Year))
{
i.Month++;
i.Day = 1;
cout << "Proceeded to " << i.Day << "." << i.Month << "." << i.Year << "\n";
}
if ((i.Day == 31) && (i.Month == 12))
{
i.Year++;
i.Day = 1;
i.Month = 1;
cout << "Proceeded to " << i.Day << "." << i.Month << "." << i.Year << "\n";
}

if (i.Day == 13)
{
counters[i.DayOfWeek]++;
cout << i.Day << "." << i.Month << "." << i.Year << " was a " << i.DayOfWeek << "\n";
}
}

cout << counters[6] << " " << counters[7] << " " << counters[1] << " " << counters[2] << " " << counters[3] << " " << counters[4] << " " << counters[5] << "\n";

exit(0);
}

调试信息 (couts) 用于查看循环中是否发生任何事情。到目前为止,唯一发生的事情是很多行说“13.1.1900 是一个”不同的数字。我认为从代码中可以清楚地看出增加日期所需的逻辑,但我试图增加一些错误的东西。我的错误在哪里?

最佳答案

你的增量程序太复杂了。
实际上,我仍在尝试弄清楚它们是如何组合在一起的(边缘情况等),但您似乎试图独立地完成每一天/每月/每年。

您可以像这样在一个简单的函数中完成所有递增操作:

void increment_Date(Date &d)
{
d.DayOfWeek = (d.DayOfWeek + 1) % 7; //increase weekday
if (++d.Day > MonthLimit(d.Month, d.Year)//increase day, if larger than month size...
{
d.Day = 1; //reset day to 1
if (++d.Month > 12) //increase month, if larger than year size
{
d.Month = 1; //reset month to 1
d.Year++; //increase year
}
}
}

逻辑更简单,因为我处理它的方式与您似乎采用的方式略有不同。
在上面的函数中,我首先增加最小单位(天),检查溢出,然后根据需要向上移动到月(依此类推)。

这与汽车里程盘的数字仅在表盘达到 10 并返回 0 时才会上升的方式相同。或者,将 1 加到 9 并“溢出”到 10 的数字的数量。

否则你的代码是好的。 MonthLimit 的 Switch 语句是一个不错的选择。我会在函数中添加一个 switch 语句以返回星期几的字符串:

std::String printable_DOW(int DOW)
{
switch (DOW)
case 0:
return "Sunday";
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday";
}

还有一个 print_date 函数:

void print_date(Date d)
{
std::cout<< printable_DOW(d.DayOfWeek)
<< ", " << d.Day << "." << d.Month << "." d.Year;
}

将它们组合起来使用也很容易:

int main
{

Date my_date;
my_date.Day = 1;
my_date.Month = 1;
my_date.Year = 2000;
my_date.DayOfWeek = 0; //assuming 0 == Sunday, 1 = Monday...
for (int daycount = 0; daycount < 5114 /*days since 1/1/2000 to today*/; daycount++)
{
increment_date(my_date);
//debug:
print_date(my_date);
}
//print "today's" date:
print_date(my_date);
}

作为一个类(class):它大部分都在这里并且(大部分)有效。随意编辑人物!

class date
{
private:
unsigned short int day;
unsigned short int month;
unsigned short int year;
unsigned short int day_of_week;
int month_limit();
bool is_leap_year();
public:
date();
date(int in_day, int in_month, int in_year, int in_day_of_week);
date& date::operator=(date rhs)
date& operator++(); //pre-increment (++date)
date operator++(int); //post-increment (date++)

//these are "special" they don't work exactly as you might think and are slightly broken right now,
//but I'm putting them in regardless
//they also don't work on DOW right now.
date& operator+=(const date& rhs)
date& operator-=(const date& rhs)
inline date operator+(date lhs, const date& rhs)
inline date operator-(date lhs, const date& rhs)
}
//phwew

date::date(int in_day, int in_month, int in_year, int in_day_of_week)
{
this->day = in_day;
this->month = in_month;
this->year = in_year;
this->day_of_week = in_doay_of_week;
}

date::month_limit()
{
switch (this->month)
{
case 4:
case 6:
case 9:
case 11:
{
return 30;
break;
}
case 1:
case 3:
case 5:
case 7:
case 8:
case 12:
{
return 31;
break;
}
case 2:
if (is_leap_year(this->year))
{
return 29;
break;
}
else
{
return 28;
break;
}

}
}

bool is_leap_year()
{
if (this->year % 4 != 0) return false;
else
if (this->year % 100 != 0) return true;
else
if (this->year % 400 != 0) return false;
else
return true;
}

date& date::operator=(date rhs)
{
swap(rhs);
return *this;
}

date& date::operator++()
{
this->day_of_week = (this->day_of_week + 1) % 7; //increase weekday
this->day++;
if (++(this->day) > month_limit()) //increase day, if larger than month size...
{
this->day = 1; //reset day to 1
if (++this->month > 12) //increase month, if larger than year size
{
this->month = 1; //reset month to 1
this->year++; //increase year
}
}
return *this;
}

date date::operator++(int)
{
date tmp(*this);
operator++();
return tmp;
}

//adds years on, then months, then days
date& date::operator+=(const date& rhs)
{
this->year += rhs.year;
this->month += rhs.month;
this->day += rhs.day;
if (this->month > 12) //get to the right month
{
this->year = this->month / 12;
this->month = this->month % 12;
}
if (this->day > month_limit())
{
this->month = this->day / month_limit();
this->day = this->day % month_limit();
if (this->month > 12) //recalculate **yes, I know this is currently wrong if more than one month is added on in days**
{
this->year = this->month / 12;
this->month = this->month % 12;
}
}
return *this;
}

inline date date::operator+(date lhs, const date& rhs)
{
lhs += rhs;
return lhs;
}

//subtracts years, then months, then days
date& date::operator-=(const date& rhs)
{
if ((rhs.year < this->year) || ((rhs.year == this->year) && (rhs.month < this->month))
|| (((rhs.year == this->year) && (rhs.month == this->month)) && (rhs.day < this->day)
{
swap(rhs);
}
this->year -= rhs.year;
this->month -= rhs.month;
this->day -= rhs.day;

return *this;
}

inline date date::operator+(date lhs, const date& rhs)
{
lhs += rhs;
return lhs;
}

关于c++ - 在循环中递增日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26672431/

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