给定日期的c++星期几
c++ day of week for given date
我正在尝试用c++编写一个简单的程序,该程序返回给定日期的星期几。
输入格式为日、月、年。我没法用闰年。当输入年份是闰年时,我尝试从a
变量中减去一个,但程序最终崩溃而没有错误消息。
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
int d;
int m;
int y;
string weekday(int d, int m, int y){
int LeapYears = (int) y/ 4;
long a = (y - LeapYears)*365 + LeapYears * 366;
if(m >= 2) a += 31;
if(m >= 3 && (int)y/4 == y/4) a += 29;
else if(m >= 3) a += 28;
if(m >= 4) a += 31;
if(m >= 5) a += 30;
if(m >= 6) a += 31;
if(m >= 7) a += 30;
if(m >= 8) a += 31;
if(m >= 9) a += 31;
if(m >= 10) a += 30;
if(m >= 11) a += 31;
if(m == 12) a += 30;
a += d;
int b = (a - 2) % 7;
switch (b){
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday";
case 7:
return "Sunday";
}
}
int main(){
cin >> d >> m >> y;
cout << weekday(d, m, y);
}
第一:如果已经有可以处理相同问题的标准化函数,则不要编写自己的函数。问题是,您可能很容易犯错误(我已经在您的weekday()
函数的第一行看到了现在的错误),而标准化函数的实现已经经过了彻底的测试,您可以确信它们交付了您期望得到的结果。
话虽如此,这里有一种可能的方法,使用std::localtime和std::mktime:
#include <ctime>
#include <iostream>
int main()
{
std::tm time_in = { 0, 0, 0, // second, minute, hour
9, 10, 2016 - 1900 }; // 1-based day, 0-based month, year since 1900
std::time_t time_temp = std::mktime(&time_in);
//Note: Return value of localtime is not threadsafe, because it might be
// (and will be) reused in subsequent calls to std::localtime!
const std::tm * time_out = std::localtime(&time_temp);
//Sunday == 0, Monday == 1, and so on ...
std::cout << "Today is this day of the week: " << time_out->tm_wday << "n";
std::cout << "(Sunday is 0, Monday is 1, and so on...)n";
return 0;
}
老问题的新答案,因为它们是一种不断变化的工具…
c++ 20规范指出,以下代码将具有与问题中代码意图相同的功能:
#include <chrono>
#include <format>
#include <iostream>
int
main()
{
using namespace std;
using namespace std::chrono;
year_month_day dmy;
cin >> parse("%d %m %Y", dmy);
cout << format("{:%A}", weekday{dmy}) << 'n';
}
今天可以使用这个免费的、开源的日期/时间库来试验这种语法,只是日期对象在namespace date
而不是namespace std::chrono
中,并且格式字符串的语法略有改变。
#include "date/date.h"
#include <iostream>
int
main()
{
using namespace std;
using namespace date;
year_month_day dmy;
cin >> parse("%d %m %Y", dmy);
cout << format("%A", weekday{dmy}) << 'n';
}
你对闰年的理解是错误的:
闰年是每4年一次除了如果它能被100整除,但是即使如此如果它能被400整除它仍然是闰年
关于如何计算"天数"(dn)的清晰简明的解释可以在这里找到。
一旦您有了天数(dn),只需执行模数7。结果将是星期几(下跌)。
下面是一个示例实现(不检查date是否是有效输入):
#include <iostream>
#include <iomanip>
typedef unsigned long ul;
typedef unsigned int ui;
// ----------------------------------------------------------------------
// Given the year, month and day, return the day number.
// (see: https://alcor.concordia.ca/~gpkatch/gdate-method.html)
// ----------------------------------------------------------------------
ul CalcDayNumFromDate(ui y, ui m, ui d)
{
m = (m + 9) % 12;
y -= m / 10;
ul dn = 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + (d - 1);
return dn;
}
// ----------------------------------------------------------------------
// Given year, month, day, return the day of week (string).
// ----------------------------------------------------------------------
std::string CalcDayOfWeek(int y, ul m, ul d)
{
std::string day[] = {
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday",
"Monday",
"Tuesday"
};
ul dn = CalcDayNumFromDate(y, m, d);
return day[dn % 7];
}
// ----------------------------------------------------------------------
// Program entry point.
// ----------------------------------------------------------------------
int main(int argc, char **argv)
{
ui y = 2017, m = 8, d = 29; // 29th August, 2017.
std::string dow = CalcDayOfWeek(y, m, d);
std::cout << std::setfill('0') << std::setw(4) << y << "/";
std::cout << std::setfill('0') << std::setw(2) << m << "/";
std::cout << std::setfill('0') << std::setw(2) << d << ": ";
std::cout << dow << std::endl;
return 0;
}
您可以使用Boost c++库中的公历日期系统来查找给定日期的星期几。下面是一个简单的例子:
#include <boost/date_time.hpp>
#include <string>
#include <iostream>
const static std::string daysOfWeek[] = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
};
int getDayOfWeekIndex(int day, int month, int year) {
boost::gregorian::date d(year, month, day);
return d.day_of_week();
}
int main()
{
const int index = getDayOfWeekIndex(30, 07, 2018);
std::cout << daysOfWeek[index] << 'n';
}
此代码打印Monday
。
我也遇到过同样的问题,但我找到了一个简单的解决方案。
根据这篇文章:
下面是Sakamoto, Lachman, Keith和Craver提出的计算日的简单函数。下面的函数星期天返回0,星期一返回1,等等。"
int dayofweek(int d, int m, int y)
{
static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
y -= m < 3;
return ( y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7;
}
当一个数能被7整除时会发生什么?
14/7 = 214 % 7 = 0
模运算符(% n)将返回一个从0到n的数字-1
如果n除以7,余数永远不可能是7所以
int b = (a - 2) % 7;
switch (b){
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday";
case 7:
return "Sunday";
}
}
在这种情况下,它不可能是星期天
试试这个
int b = (a - 2) % 7;
switch (b){
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";
default:
return "Error";
}
尝试使用CTime类
为例:
const CTime currTime = CTime::GetCurrentTime();
const int nWeekDay = currTime.GetDayOfWeek();
switch (nWeekDay)
{
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday";
case 7:
return "Sunday";
}
在上面的例子中,我使用的是当前时间,但你可以用不同的方式,用你想要的时间,例如:
const CTime currTime = CTime(year,month, day, hours, minutes, seconds );
int dayofweek(int day,int month,int year)
{
int arr[] = {0,3,2,5,3,5,1,4,6,2,4};
if(month<3)
year--;
return ((year+year/4-year/100+year/400+arr[month-1]+day)%7);
}
int main()
{
int day,month,year;
cout<<"Enter the Date for which day of the week need to be find (DD/MM/YYYY)."<<endl;
cin>>day>>month>>year;
int x = dayofweek(day,month,year);
if(x==0)
cout<<"Sunday"<<endl;
else if(x==1)
cout<<"Monday"<<endl;
else if(x==2)
cout<<"Tuesday"<<endl;
else if(x==3)
cout<<"Wednesday"<<endl;
else if(x==4)
cout<<"Thursday"<<endl;
else if(x==5)
cout<<"Friday"<<endl;
else if(x==6)
cout<<"Saturday"<<endl;
}
- 查询SQLite数据库中的日期
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- Log4cpp:以UTC/GMT时区打印日期
- 使用C++的日期库读取时间
- 日期格式为C++
- 如何在 C++20 计时中为日期添加天数?
- 如何在QTableView中排序和更改日期格式
- 如何在cpp中使用地图显示给定日期范围内(在下面的问题中)的费率?
- Gtkmm 会不断检查 Gtk::日历上的所选日期是否发生变化
- fmt 与 Howard Hinnant 的日期:为什么从 fmt::to_string "{}"?FMT 和日期的最佳实践?
- 使程序检查当前日期是否=预期日期
- 如何找到两个日期之间的时间差异(以秒和纳秒为单位)?
- JNI 日期值转换问题,在C++中获取不同的长整型值
- 确定夏令时是否适用于特定日期
- C++日期库因时区而失败
- 向日期运算符添加天数+
- 为什么在C++中获取日期和/或时间如此复杂?
- 如何验证用户输入月份的日期?
- 从编译时已知的日历日期创建"std::chrono::time_point"
- 在 MacOS 上C++:显示日期和时间问题