gpt4 book ai didi

c++ - 我怎样才能知道 13 号星期五在一年中出现了多少次?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:34:44 25 4
gpt4 key购买 nike

如标题所示,我试图找出给定年份中 13 号星期五出现了多少次。我必须使用 date类并以某种方式编写代码来解决这个问题。

日期.h

#ifndef DATE_H
#define DATE_H
#include <string>
#include <iostream>
using namespace std;

string Month[] = { "", "January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December" };

int daysInMonth[2][13] = { { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
{ 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } };
string DOW[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "" };
class Date {
private:
int *month;
int *day;
int *year;
public:
// constructors
Date(int month, int day, int year);
Date(); // default constructor

Date(Date &other); // copy constructor
Date(string mmddyyyy); // constructor that takes a string as parameter e.g "10/31/2012"

~Date();

// overload assignment operator

Date & operator=(Date & rhs);
Date & operator++();
Date & operator++(int);
Date & operator--();
Date & operator--(int);
bool operator ==(Date & rhs);
bool operator != (Date & rhs);
bool operator > (Date & rhs);
bool operator < (Date & rhs);

int operator-(Date & rhs);
Date & operator+(int);

// accessors

int getDay() {
return *this->day;
}

int getMonth() {
return *this->month;
}

int getYear() {
return *this->year;
}

static Date toDate(int doy, int yr) {
int dayOfMonth = doy;
int month = 1;
int isleap = isLeap(yr);

for (int i = 1; i < 13; i++) {
int daysThisMonth = daysInMonth[isleap][i];
if (dayOfMonth <= daysThisMonth) {
break;
}
else {
month++;
dayOfMonth -= daysThisMonth;
}
}
return *new Date(month, dayOfMonth, yr);
}

// display date in the format of mm/day/year e.g. 10/31/2012
void display() {
cout << *this->month << "/" << *this->day << "/" << *this->year;
}

// returns true if the given year is a leap year and false otherwise
static int isLeap(int year) {
return ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0));
}

// returns the day code the the new year day of the given year
static int get_day_code(int year) {
return (year + (year - 1) / 4 - (year - 1) / 100
+ (year - 1) / 400) % 7;
}

// overloading get_day_code that returns the day code of the specific day
static int get_day_code(int year, int month, int day) {
int dayCodeForYear = get_day_code(year);
Date temp(month, day, year);
int doy = temp.dayOfYear() - 1;
int dayCode = (dayCodeForYear + doy) % 7;
return dayCode;
}

int dayOfWeek() { // return the day code of this day
int dayOfYear = this->dayOfYear() - 1;
int firstDayOfTheYear = Date::get_day_code(*this->year);
dayOfYear += firstDayOfTheYear;
return dayOfYear % 7;
}

int dayOfYear() { // returns the day of the year, eg, the day of year for Feb 28 is 59.
int doy = 0;
int isleap = isLeap(*year);
for (int i = 1; i < *month; i++) {
doy += daysInMonth[isleap][i];
}
doy += *day;
return doy;
}
};

Date::Date(int month, int day, int year) {
this->month = new int(month);
this->day = new int(day);
this->year = new int(year);
}

Date::Date() {
this->month = new int(1);
this->day = new int(1);
this->year = new int(2000);
}

Date::Date(Date &other) {
this->month = new int(*(other.month));
this->day = new int(*(other.day));
this->year = new int(*(other.year));
}

Date::Date(string mmddyyyy) {
string mm = mmddyyyy.substr(0, 2);
string dd = mmddyyyy.substr(2, 2);
string yyyy = mmddyyyy.substr(4, 4);

this->month = new int(atoi(mm.c_str()));
this->day = new int(atoi(dd.c_str()));
this->year = new int(atoi(yyyy.c_str()));
}

Date::~Date() {
if (month) delete month;
if (day) delete day;
if (year) delete year;
}

bool Date::operator == (Date & rhs) {
return (*year == *(rhs.year) && *month == *(rhs.month) && *day == *(rhs.day));
}

bool Date::operator != (Date & rhs) {
return !(*this == rhs);
}

bool Date::operator > (Date & rhs) {
if (*year > *(rhs.year)) return true;
else if (*year < *(rhs.year)) return false;
else if (*month > *(rhs.month)) return true;
else if (*month < *(rhs.month)) return false;
else if (*day > *(rhs.day)) return true;
return false;
}

bool Date::operator < (Date & rhs) {
if (*year < *(rhs.year)) return true;
else if (*year > *(rhs.year)) return false;
else if (*month < *(rhs.month)) return true;
else if (*month > *(rhs.month)) return false;
else if (*day < *(rhs.day)) return true;
return false;
}

Date & Date::operator=(Date & rhs) {
*this->month = *rhs.month;
*this->day = *rhs.day;
*this->year = *rhs.year;
return *this;
}

Date & Date::operator++() {
Date tmp = *this + 1;
*this->month = *tmp.month;
*this->day = *tmp.day;
*this->year = *tmp.year;
return *this;
}
Date & Date::operator++(int) {
Date tmp = *this + 1;
Date * output = new Date(tmp);
*this->month = *tmp.month;
*this->day = *tmp.day;
*this->year = *tmp.year;
return *output;
}

Date & Date::operator--() {
Date tmp = *this + -1;
*this->month = *tmp.month;
*this->day = *tmp.day;
*this->year = *tmp.year;
return *this;
}

Date & Date::operator--(int) {
Date tmp = *this + -1;
Date * output = new Date(tmp);
*this->month = *tmp.month;
*this->day = *tmp.day;
*this->year = *tmp.year;
return *output;
}

int Date::operator-(Date & rhs) {
int yearsDiff = *this->year - *rhs.year;
int daysDiff = this->dayOfYear() - rhs.dayOfYear();
daysDiff += yearsDiff * 365;
return daysDiff;
}

Date & Date::operator+(int) {
int n = 0;
int doy = dayOfYear();
int newDoy = doy + n;
int yearsDiff = newDoy / 365;
newDoy = newDoy % 365;
int newYear = *this->year + yearsDiff;
Date newDate = Date::toDate(newDoy, newYear);
return *new Date(newDate);
}
#endif

这是我弄乱了一段时间的代码:

源代码.cpp

#include <iostream>
#include <iomanip>
#include "Date.h"

using namespace std;

void iterateMonth(int);
bool isFriday13th();
string dayofweek[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "" };
string month[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", "" };
//int dayInMonth[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
//int dayInMonthLeap[13] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };


void iterateMonth(int year) {
int dayCode, month, daysInMonth, day, u;
bool leapYear;
cout << year;

// returns the day on which January 1 of year begins.
dayCode = Date::getDayCode(year);
// returns true if year is a leap year, false otherwise
leapYear = Date::isLeap(year);

// month is 0 for Jan, 1 for Feb, etc.
for (month = 1; month <= 12; month++)
{
switch (month)
{
case 1:
cout << "\n\nJanuary\n";
daysInMonth = 31;
break;
case 2:
cout << "\n\nFebruary\n";
if (leapYear)
daysInMonth = 29;
else
daysInMonth = 28;
break;
case 3:
cout << "\n\nMarch\n";
daysInMonth = 31;
break;
case 4:
cout << "\n\nApril\n";
daysInMonth = 30;
break;
case 5:
cout << "\n\nMay\n";
daysInMonth = 31;
break;
case 6:
cout << "\n\nJune\n";
daysInMonth = 30;
break;
case 7:
cout << "\n\nJuly\n";
daysInMonth = 31;
break;
case 8:
cout << "\n\nAugust\n";
daysInMonth = 31;
break;
case 9:
cout << "\n\nSeptember\n";
daysInMonth = 30;
break;
case 10:
cout << "\n\nOctober\n";
daysInMonth = 31;
break;
case 11:
cout << "\n\nNovember\n";
daysInMonth = 30;
break;
case 12:
cout << "\n\nDecember\n";
daysInMonth = 31;
break;
}
//cout << "\n\nSun Mon Tue Wed Thu Fri Sat\n";
for (int i = 0; i < (sizeof(dayofweek) / sizeof(dayofweek[0])); i++)
{
cout << dayofweek[i] << " ";
}
cout << endl;
for (day = 1; day <= dayCode * 5; day++)
{
cout << " ";
}
for (day = 1; day <= daysInMonth; day++)
{
cout << setw(3) << day;
if ((day + dayCode) % 7 > 0)
{
cout << " ";
}
else
cout << endl;
}
dayCode = (dayCode + daysInMonth) % 7;
}
}
//bool isFriday13th() {
//
//}

int main() {
int year;
//cout << "Please enter a year: ";
cout << "Final ";
cin >> year;
iterateMonth(year);
}

我从某人那里得到的一个想法是创建一个 Date 1 月 13 日的对象,使用 dayOfWeek检查是否是星期五的方法,如果不是,递增直到我到达星期五,然后向前跳转 7 并使用 getDay检查它是否为 13。我用这个试了一下:

Date tmp(1, 13, year);
int dc = tmp.dayOfWeek(); // return day code

tmp.display();
cout << endl;
++tmp;
tmp.display();
cout << endl;

我期待 tmp.display();显示 1/13/2013(以 2013 年为年份)的行,它确实如此,但是 ++tmp线给出了相同的结果。我还以为我必须以某种方式找到对应于星期五的日期代码(现在 dc 是 0),但我无法弄清楚。

此外,与同一问题相关的另一篇帖子说,每个从星期日开始的月份都会有 13 号星期五,所以我试着考虑如何实现它。也许在第一个 for 的某个地方循环(在 switch 语句之上)我可以检查类似 if (firstDay = Sunday) { cout << "Friday13th exists in " << month << endl; } 的内容

这有点可怕的伪代码,但这是我的想法。有人有想法吗?如有任何帮助,我们将不胜感激,在此先感谢您。

最佳答案

您可以执行此操作,而不必多次调用获取年中日期的函数。如果您知道 1 月第一个星期日的日期,那就没有什么可说的了。只是一些基础数学。

对于 2013 年,一月的第一个星期日是第 6 个星期日。所以 13 号是星期天。我们知道,如果一月的第一个星期日是第6个,那么二月的第一个星期日就是第3个。 13号加10天。那是星期三。

我怎么知道二月的第一个星期日是第三个星期日?因为日历就是这样工作的。如果您取一月第一个星期日的日期,加上 4 和 mod 7,就会得到二月第一个星期日的日期。日历就是这样固定的;给定 1 月的第一个星期日,您可以使用模 7 算法轻松计算任何月份的第一个星期日。

偏移量是:

Normal Year: 0 4 4 1 6 3 1 5 2 0 4 2 
Leap Year: 0 4 3 0 5 2 0 4 1 6 3 1

如果你想知道第 13 号星期五是几月,那么下一个星期日是第 15 号,这意味着该月的第一个星期日必须是第 1 号。

鉴于上表,如果 1 月的第一个星期日是第一个星期日,那么在正常年份中,您将有两个月(13 日星期五):1 月和 10 月。如果是闰年,那么你会有一月、四月和七月。今年的第一个星期日是第 6 个星期日,因此您需要偏移量为 2 的月份。即 9 月和 12 月。

如果月份偏移量加上 1 月第一个星期日的日期等于 8,则该月的星期五是 13 号。

前段时间我把它当作内存技巧玩过。参见 Now what day is that?以获得更长的解释。

关于c++ - 我怎样才能知道 13 号星期五在一年中出现了多少次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20169947/

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