发布网友 发布时间:2023-05-03 23:24
共5个回答
热心网友 时间:2023-10-23 04:22
#include<iostream>
热心网友 时间:2023-10-23 04:22
Tool.h
#ifndef __TOOL_H__
#define __TOOL_H__
#define Sunday1
#define Monday2
#define Tuesday3
#define Wednesday4
#define Thursday5
#define Friday6
#define Saturday7
class Calendar
{
public:
Calendar(int num);
~Calendar();
void calculate(char* ret);
protected:
private:
bool if_leap_year(int year);
private:
int calc_num;
int start_year;
int start_week;
};
#endif//__TOOL_H__
Tool.cpp
#include <iostream>
#include "Tool.h"
Calendar::Calendar(int num)
{
calc_num = num;
start_year = 2001;
start_week = Sunday;
}
Calendar::~Calendar()
{
}
void Calendar::calculate(char* ret)
{
int day_tmp = 0;
int day_remain = 0;
int month_day_unleep[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int month_day_leep[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int act_year, act_month, act_day, act_week;
for (int i = start_year; ;i++)
{
if (day_tmp >= calc_num)
{
act_year = i - 1;
break;
}
day_remain = calc_num - day_tmp;
day_tmp += if_leap_year(i) ? 366 : 365;
}
day_tmp = 0;
for (int i = 0; i<13; i++)
{
if (day_tmp >= day_remain)
{
act_month = i;
break;
}
act_day = day_remain - day_tmp;
day_tmp += if_leap_year(act_year) ? month_day_leep[i] : month_day_unleep[i];
}
act_week = calc_num%7;
char tmp[30]= {0};
sprintf_s(tmp, "%d-%d-%d", act_year, act_month, act_day);
switch (act_week)
{
case 1:
sprintf_s(tmp, "%s %s", tmp, "Sunday");
break;
case 2:
sprintf_s(tmp, "%s %s", tmp, "Monday");
break;
case 3:
sprintf_s(tmp, "%s %s", tmp, "Tuesday");
break;
case 4:
sprintf_s(tmp, "%s %s", tmp, "Wednesday");
break;
case 5:
sprintf_s(tmp, "%s %s", tmp, "Thursday");
break;
case 6:
sprintf_s(tmp, "%s %s", tmp, "Friday");
break;
case 0:
sprintf_s(tmp, "%s %s", tmp, "Saturday");
break;
}
memcpy(ret, tmp, sizeof(tmp));
}
bool Calendar::if_leap_year(int year)
{
if (year%4 == 0)
{
if (year%100 == 0 && year%400 != 0)
{
return false;
}
return true;
}
return false;
}
void main()
{
int i;
char tmp[30];
char print[2048] = {0};
while (1)
{
scanf("%d", &i);
if (i == -1)
{
break;
}
Calendar cal(i);
cal.calculate(tmp);
sprintf_s(print, "%s%s\n", print, tmp);
}
printf("%s", print);
return;
}
热心网友 时间:2023-10-23 04:23
看看准标准库的 boost 库的date_time模块,里面就有你要的功能。热心网友 时间:2023-10-23 04:23
带有一点点注释,应该能看懂的
热心网友 时间:2023-10-23 04:24
先提一个问题,关于这个你是否要求关于历法的转变,我记得以前编这个的时候好像要求在1582年实现儒略历向格里历的转化。