#include<conio.h>
#include<stdlib.h>
main()
{
int year,month,day
int leapyear=0//闰年时使用
int daytime=0
int sum
static int month_date[12]={31,28,31,30,31,30,31,31,30,31,30,31}
printf("请输入日期:(例如:2010 5 27)")
scanf("%d%d%d",&year,&month,&day)
if(year<=0||month>12||month<1)//判断输入是否正确
{
printf("Error!\n")
getch()
exit(0)
}
if(year%400==0||(year%4==0&&year%100!=0))//判断是否为闰年
leapyear=1//是就加1
if(month==2)//判断日是否输入正确,2月是特殊的因为分闰年和平年
{ if((month_date[month-1]+leapyear)<day||day<=0)
{
printf("Error!\n")
getch()
exit(0)
}
}
if(month!=2)//当输入不是2月时,判断输入
{
if(month_date[month-1]<day||day<=0)
{
printf("Error!\n")
getch()
exit(0)
}
}
printf("\n请输入天数:")
scanf("%d",&daytime)//输入第N天后
if(daytime<0)
{
printf("Error!\n")
getch()
exit(0)
}
sum=daytime+day//当前日期与N天相加
do
{
if(month==2)//判断当月是不是二月份
month_date[month-1]+=leapyear
if(sum>month_date[month-1])
{
sum-=month_date[month-1]
month++//超过当月天数,加一个月
if(month==13)//当月份超过12月时,重新返到1月
{
year++//加一年
if(year%400==0||(year%4==0&&year%100!=0))//判断加一年后是不是闰年
leapyear=1
else
leapyear=0//不是闰年则为0
month=1//因为12月的下一个月是1月
}
}
}while(sum>month_date[month-1])//当加起来的天数少于当月的天数就停止循环
day=sum
printf("\n第%d天后是%d %d %d",daytime,year,month,day)//输出
getch()
}
// sts.cpp : Defines the entry point for the console application.//
#include "stdafx.h"
#include <time.h>
///输入时间 (1970.1.1---3000.12.31),否则无法转换
void convert( char* chOutDate, const char *chInDate, const char* chInc )
{
char dateFormat[40]=""
int inDateLen = (int)strlen(chInDate)
//取出输入日期格式
int l = 0
for(int i=0i<inDateLen)
{
if( chInDate[i] <'0' || chInDate[i] >'9' )
{
dateFormat[l++] = chInDate[i++]
continue
}
dateFormat[l++] = '%'
dateFormat[l++] = 'd'
++i
while( chInDate[i]>='0'&&chInDate[i]<='9' )
{
++i
}
}
//天数
int incDays = 0
sscanf(chInc, "%d", &incDays )
//获取输入日期
tm inData
memset( &inData, 0, sizeof(inData) )
sscanf( chInDate, dateFormat, &inData.tm_year, &inData.tm_mon, &inData.tm_mday )
inData.tm_year -= 1900
inData.tm_mon -= 1
__time64_t currentTime = _mktime64( &inData )+incDays*3600*24
tm *newDate = _localtime64( currentTime)
sprintf(chOutDate, dateFormat, newDate->tm_year+1900, newDate->tm_mon+1, newDate->tm_mday)
}
main()
{
char out[200]
convert( out, "2007-10-5", "-20")
printf(out) printf("\n")
convert( out, "2007年10月5日", "-120")
printf(out) printf("\n")
convert( out, "2007.10.5", "30")
printf(out) printf("\n")
printf("按任意键退出")
getchar()
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)