当前位置:网站首页>N天前的日期
N天前的日期
2022-07-28 05:30:00 【▀】
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<memory.h>
#define DEBUG
int g_year[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };//非闰年
struct Date
{
int Year;
int Month;
int Day;
};
Date g_data;
bool re_Leap(int year)
{
if (year % 4 == 0)
{
if (year % 100 == 0)
{
// 这里如果被 400 整数是闰年
if (year % 400 == 0)
return true;
else
return false;
}
else
return true;
}
else
return false;
}
void re_year(time_t t)
{
struct tm *tp;
time(&t);
tp = gmtime(&t);
g_data.Year = 1900 + tp->tm_year;
g_data.Month = 1 + tp->tm_mon;
g_data.Day = tp->tm_mday;
}
Date ne_year(int days,int month,int year)
{
days = days - g_data.Day ;
int j = month - 1;
while (days - g_year[j -1 ] >= 0 && j > 0)
{
days -= g_year[j -1];
j--;
}
j = 12;//跨年了
if (re_Leap(year - 1)) //是闰年
{
g_year[1] = 29;
}
else
{
g_year[1] = 28;
}
//days++;
while (days - g_year[j-1] > 0 && j > g_data.Month)
{
days -= g_year[j-1];
j--;
}
days = g_year[j-1] - days;
if (days != 0)
{
Date d = { year - 1,j,days };
return d;
}
else
{
Date d = { year - 1,j-1 ,g_year[j - 1] };
return d;
}
}
Date no_ne_year(int days, int month, int year)
{
days = days - g_data.Day;
int j = month-1;
while (days - g_year[j-1] > 0)
{
days -= g_year[j-1];
j--;
}
days = g_year[j-1] - days ;
if (days != 0)
{
Date d = { year,j,days };
return d;
}
else
{
Date d = { year,j-1,g_year[j - 2] };
return d;
}
}
Date no_one_month(int year,int days)
{
int day = g_data.Day - days;
if (day != 0)
{
Date d = { year,g_data.Month,day };
return d;
}
else
{
Date d = { year,g_data.Month-1,g_year[g_data.Month-2] };
return d;
}
}
Date re_col(int year ,int days)
{
int ny = 0;
int month = 0;
int day = 0;
int sum = 0;
if (days < 0)
{
return *(Date*)(0);
}
else
{
if (re_Leap(year))//是闰年
{
g_year[1] = 29;
if (days >= 0 && days < 366)
{
month = g_data.Month;
day = g_data.Day;
for (int i = 1;i < month;i++)
{
sum += g_year[i];
}
sum += day;
if (days > sum) //过年的
{
return ne_year(days,month,year);
}
else
{
if (days > g_data.Day)
{
return no_ne_year(days, month, year);
}
else //不足一个月
{
return no_one_month(year,days);
}
}
}
if (days >= 366)
{
return re_col(year-1, days - 366);
}
}
else
{
if (days >= 0 && days < 365)
{
month = g_data.Month;
day = g_data.Day;
for (int i = 1;i < month;i++)
{
sum += g_year[i];
}
sum += day;
if (days > sum) //过年的
{
return ne_year(days, month, year);
}
else
{
if (days > g_data.Day)
{
return no_ne_year(days, month, year);
}
else //不足一个月
{
return no_one_month(year,days);
}
} //不跨年
}
if (days >= 365)
{
return re_col(year - 1, days - 365);
}
}
}
}
int main()
{
re_year(NULL);
int s;
#ifdef DEBUG
FILE *f = fopen("test.txt", "a+");
char buf[256] = { 0 };
for (int i=0;i< 9999;i++)
{
Date p = re_col(g_data.Year, i);
sprintf(buf,"\n %d---------------->%d-%d-%d", i, p.Year, p.Month, p.Day);
fputs(buf,f);
}
fclose(f);
#endif // DEBUG
#ifndef DEBUG
while (1)
{
printf("\n|===========================BEGIN===============================|\n");
scanf_s("%d", &s);
Date p = re_col(g_data.Year, s);
printf("\n%d-%d-%d", p.Year, p.Month, p.Day);
printf("\n|============================END===============================|\n");
}
#endif // !DEBUG
getchar();
}边栏推荐
猜你喜欢

Easypoi one to many, merge cells, and adapt the row height according to the content

easypoi导出隔行样式设置

rsync+inotify实现远程实时同步

easypoi一对多,合并单元格,并且根据内容自适应行高

一个定时任务提醒工具

MOOC翁恺 C语言 第三周:判断与循环:2.循环

Joern's code uses -devign

MOOC Weng Kai C language week 3: judgment and circulation: 2. circulation

MySQL excludes holidays and calculates the date difference

freemarker导出word,带表格和多张图片,解决图片重复和变形
随机推荐
[learning records of erudite Valley] Super summary, attentive sharing | collection
Install Nessus under Kali
Standard C language learning summary 3
kali下安装nessus
Canvas drawing 2
freemarker合并单元格,if、else标签的使用,null、空字符串处理
Asynchronous programming promise
Basic knowledge of functions and special points
Nrf51822 review summary
Blueberry pie Bluetooth debugging process
Easypoi one to many, merge cells, and adapt the row height according to the content
DHCP服务
主动扫描技术nmap详解
ELK日志分析系统的部署
Media set up live broadcast server
开虚拟机KALI2022.2下安装GVM
PXE unattended installation management
Uniapp monitor whether the app has a network connection
Shell--- circular statement practice
Standard C language learning summary 8