- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
如标题所示,我试图找出给定年份中 13 号星期五出现了多少次。我必须使用 date
类并以某种方式编写代码来解决这个问题。
#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
这是我弄乱了一段时间的代码:
#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/
我正在尝试执行不是很复杂的图像分析,以尝试找到不同的形状并计算它们的一些参数,如面积和周长(以像素为单位),我正在尝试在 Haskell 中执行此操作(我想这样做是为了尝试并使用函数式编程语言)。 第
查看 main 函数,我们可以看到我已将“星期一”硬编码到我的 setDay 公共(public)函数中。使用 c 字符串很容易从用户那里获取星期几(就像我在 setDay 中所做的那样),但是我如何
我正在寻找两个做两件事: 检查一周是否过去(基于星期几) 检查一天是否过去(基于日期) 我不确定解决这个问题的最佳方法是什么,因为比较两个 NSDates(一个在 2009 年 1 月 1 日 21:
伙计们,我想创建一个 13 日即将到来的 13 个星期五的列表我该怎么做? 我尝试了一年: public static void getFridayThirteen() { for (in
我想知道这个日期格式是什么 Friday, December 18th 也是这个标准日期格式,或者我必须努力工作才能得到这个。 谢谢。 最佳答案 我认为th 不可能通过格式化程序实现。虽然你可以这
我正在使用 UILocalNotification 进行报警。我有一个基于工作日(星期日、星期一、星期二、星期三、星期四、星期五、星期六)重复的自定义选项。这么多应用程序都做了这个过程。我尽力了。但我
下面的函数返回图表数据 上述函数返回的输出格式如下 [Object { date=Date, value=112, volume=1469}, Object { date=Date, value
我是一名优秀的程序员,十分优秀!