用c语言完成输入一个日期,输出下一天

如题所述

#include <stdio.h>

int main(void)
{
int year, month, day;
int m[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

printf("Input year month day:");
scanf("%d%d%d", &year, &month, &day);

if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
m[1]++;

(month == 12 && day == 31) ? year++ : year;
month = (month + day / m[month - 1]) % 12;
day = day % m[month - 1] + 1;

printf("Next day: %d年%d月%d号\n", year, month, day);
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-10-20
一般只要把day+1就好了对于月底就用swith-case吧
第2个回答  2017-11-04
#include <stdio.h>
int main(void)
{
int year, month, day;
int a,b,c;
int m[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
printf("Input year month day:");
scanf("%d-%d-%d", &year, &month, &day);
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
m[1]++;
(month == 12 && day == 31) ? year++ : year;
if(day==m[month-1]&&month<12&&0<month)
b = (month+1)%12;
else if(day==m[month-1]&&month==12)
b=1;
else
b= month;
c = day % m[month - 1] + 1;

printf("Next day: %d年%d月%d号\n", year, b, c);

return 0;
}
第3个回答  2011-10-20
/*
http://zhidao.baidu.com/question/331958824.html?seed=0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

/*
指定日期的格式。可以修改日期的格式以应对不同的需要。

%y
年(0-99)
%Y
用CCYY表示的年(如:2004)
%m
月份 (1-12)
%d
月中的第几天(1-31)
%H
小时, 24小时格式 (0-23)
%M
分钟(0-59)
%S
秒钟(0-59)
*/

// 年月日格式
char DATE_FORMAT[] = "%Y-%m-%d";
char DATE_FORMAT_SAMPLE[] = "2011-10-21";

/*
// 年月日时分秒格式(替换:年月日格式 )
char DATE_FORMAT[] = "%Y-%m-%d %H:%M:%S";
char DATE_FORMAT_SAMPLE[] = "2011-10-21 14:15:16";
*/

/*
用 dateForamt 的格式转换字符串 strDate

返回:保存时间的结构体
*/
time_t transFromStringToDate(char dateForamt[],char strDate[])
{
int i,j;
/*
struct tm {
int tm_sec; // seconds after the minute - [0,59]
int tm_min; // minutes after the hour - [0,59]
int tm_hour; // hours since midnight - [0,23]
int tm_mday; // day of the month - [1,31]
int tm_mon; // months since January - [0,11]
int tm_year; // years since 1900
int tm_wday; //days since Sunday - [0,6]
int tm_yday; // days since January 1 - [0,365]
int tm_isdst; // daylight savings time flag
};
*/

struct tm t;
t.tm_sec = t.tm_min = t.tm_hour = t.tm_mday = t.tm_mon = t.tm_year = 0;

/*
从字符串中提取日期的内容,如年月日,时分秒
保存到结构体 struct tm 中
*/
for(i=0,j=0;i<strlen(dateForamt);i++,j++)
{
// 日期内容用标识 "%" 开始
if(dateForamt[i] == '%')
{
switch(dateForamt[i+1])
{
// %y 年(0-99)
case 'y':
{
sscanf(&strDate[j],"%d",&t.tm_year);
j+=(int)log10(t.tm_year) ;
break;
}
// %Y 用CCYY表示的年(如:2004)
case 'Y':
{
sscanf(strDate+j,"%d",&t.tm_year);
j+=(int)log10(t.tm_year) ;
t.tm_year -= 1900;
break;
}
// %m 月份 (0-11)
case 'm':
{
sscanf(strDate+j,"%d",&t.tm_mon);
j+=(int)log10(t.tm_mon);
t.tm_mon --;
break;
}
// %d月中的第几天(1-31)
case 'd':
{
sscanf(strDate+j,"%d",&t.tm_mday);
j+=(int)log10(t.tm_mday) ;
break;
}
//%H 小时, 24小时格式 (0-23)
case 'H':
{
sscanf(strDate+j,"%d",&t.tm_hour);
j+=(int)log10(t.tm_hour) ;
break;
}
// %M 分钟(0-59)
case 'M':
{
sscanf(strDate+j,"%d",&t.tm_min);
j+=(int)log10(t.tm_min) ;
break;
}
// %S秒钟(0-59)
case 'S':
{
sscanf(strDate+j,"%d",&t.tm_sec);
j+=(int)log10(t.tm_sec);
break;
}
default :
{
}
} // end switch

i++;
}
else
{
// 格式错误
if(dateForamt[i] != strDate[j])
{
return 0;
}
}
}

return mktime(&t);
}
/*
用 dateForamt 的格式转换时间结构体 time

返回:时间字符串
*/
char* transFromDateToString(char dateForamt[],time_t time)
{
char *buffer = (char*)malloc(sizeof(char)*100);
struct tm *t = localtime(&time);
strftime(buffer,100,dateForamt,t);
return buffer;
}

/*
返回 oneDay 的明天同一时刻
*/
time_t toTomorrow(time_t oneDay)
{
return oneDay + 24*60*60;
}

int main(int argc, char *argv[])
{

char date[100];
printf("输入日期(示例:\"%s\"):",DATE_FORMAT_SAMPLE);
while(gets(date)!=NULL)
{
printf
(
"%s 的明天是:%s\n",
date,
transFromDateToString
(
DATE_FORMAT,
toTomorrow(
transFromStringToDate (
DATE_FORMAT,
date
)
)
)
);
printf("输入日期(示例:\"%s\"):",DATE_FORMAT_SAMPLE);
}
return 0;
}
/*
输入日期(示例:"2011-10-21"):2011-10-21
2011-10-21 的明天是:2011-10-22
输入日期(示例:"2011-10-21"):2011-10-31
2011-10-31 的明天是:2011-11-01
输入日期(示例:"2011-10-21"):2011-12-31
2011-12-31 的明天是:2012-01-01
*/
第4个回答  2011-10-20
楼上整得太详细了。
相似回答