c语言Nextdate函数

c语言Nextdate函数,第1张

NextDate(int year,int month,int day)

{

int a[12]={31,28,31,30,31,30,31,31,30,31,30,31}

int b[12]={31,29,31,30,31,30,31,31,30,31,30,31}

int y,m,d

if(year%4||year%100&&!(year%400))//闰年

{

if(b[month-1]>day)

{d=day+1m=monthy=year}

else

{d=1m=month+1y=year}

}

else

{

if(a[month-1]>day)

{d=day+1m=monthy=year}

else

{d=1m=month+1y=year}

}

if(m>12){m=m-12y++}

printf("%d年%d月%d日\n",y,m,d)

}

1. 输入是否合法

day_list[12] = {31,28,31,30,31,30,31,31,30,31,30,31}

// 判断条件:

( year>=1920 &&year<=2050 ) &&

(

( month>=1 &&month<=12 &&day>=1 &&day<=day_list[month-1] )||

( month==2 &&isLeap(year) &&day>=1 &&day<=29 )

)

2.

/*

函数说明: int NextDate(int month,int day,int year)

如果还有明天,返回1;

如果输入不合法,返回-1;

如果超出处理范围,返回-2

*/

int bLeapYear

int newY,newM,newD

newY = year

newM = month

newD = day

bLeapYear = ( (year%4)==0 &&(year%100)!=0 ) || (year%400)==0

if ( year>=1920 &&year<=2050 )

{

if ( month>=1 &&month<=12 &&day>=1 )

{

if ( day<day_list[month-1] )

{

newD += 1

}

else if ( day==day_list[month-1] &&!bLeapYear )

{

if ( month==12 &&day==31 )

{

if ( year==2050 ) return -2

else

{

newY += 1newM = 1newD = 1

}

}

else

{

newM += 1newD = 1

}

}

else if ( month==2 &&bLeapYear )

{

if ( day==28 ) newD = 29

else if( day==29 )

{ newM = 3newD = 1}

else return -1

} else return -1

}

}

// 输出

return 1

3. 测试方法:

3.1 闰年二月的最后一天

3.2 闰年二月的第28天

3.3 非闰年二月的第28天

3.4 每年的非二月的月末

3.5 每年的十二月的月末

3.6 2050年十二月的月末

3.7

year>2050 || year<1920 || month<1 || month>12 || day<1 ||

(day>day_list[month-1] &&month>=1 &&month<=12 &&month!=2 ) ||

(day>28 &&month==2 &&!bLeapYear ) ||

(day>29 &&month==2 &&bLeapYear )

3.8 闰年非二月的月末 (3.4的分支)

测试出程序错误:

4.1

else if ( day==day_list[month-1] &&!bLeapYear )

应改为:

else if ( day==day_list[month-1] &&month!=2 )

4.2

else if ( month==2 &&bLeapYear )

{

if ( day==28 ) newD = 29

else if( day==29 )

{ newM = 3newD = 1}

else return -1

}

应改为:

else if ( month==2 )

{

if ( day==28 &&bLeapYear ) newD = 29

else if( day==28 &&!bLeapYear || ( day==29 &&bLeapYear ) )

{ newM = 3newD = 1}

else return -1

}

/* 你问题中最核心的问题就是从一个日期计算一天后的日期的算法,

下面的程序中 核心函数 nextDate 实现了你所想要的功能.

设日期串 date 存储格式如下:

yyyymmdd

nextDate(date) 调用后会将 date 的内容按公历历法更改为下一天的日期的表示形式.

nextDate 的实现原理见其注解. */

#include "stdio.h"

#include "string.h"

char* nextDate(char *date)

void main(){

char date1[9] = "", date2[9] = ""

printf("Please input a date(YYYYMMDD): ")

gets(date2)

printf("\n")

strcpy(date1, nextDate(date2))

printf("The next Date is: %s\n", date1)

getchar()

}

/* 几个功能函数: */

int getYear(char *date){

int i, Y = 0

for (i = 0i <4i++, date++){

Y *= 10

Y += (*date - '0')

}

return Y

}

int getMonth(char *date){

int i, M = 0

for (date += 4, i = 0i <2i++, date++){

M *= 10

M += (*date - '0')

}

return M

}

int getDay(char *date){

int i, D = 0

for (date += 6, i = 0i <2i++, date++){

D *= 10

D += (*date - '0')

}

return D

}

/* 闰年份的判断 */

int isLeap(int year){

return ((year % 4 == 0) &&(year % 100 != 0)) || (year % 400 == 0)

}

int isBigMonth(int month){

return ((month % 2 == 1) &&(month <8)) || /* 小于 8 的奇数 */

((month % 2 == 0) &&(month >7))/* 大于 7 的偶数 */

}

int isSmallMonth(int month){

return (month != 2) &&!isBigMonth(month)

}

void toStr(char *Str, int n, int bit){

char Temp[255] = ""

strcpy(Str, "")

while ((bit--) >0){

strcpy(Temp, Str)

*Str = (char)(n % 10+'0')

*(Str + 1) = '\0'

strcat(Str, Temp)

n /= 10

}

}

void setDate(char *date, int y, int m, int d){

toStr(date, y, 4)

toStr(date + 4, m, 2)

toStr(date + 6, d, 2)

}

char* nextDate(char *date){

/*

大月 31 日(包括年末)

小月 30 日

平 2 月 28 日

闰 2 月 29 日.

对这些情况一律将 日段 变为 "01",

然后进一步判断当前月是否 12 月,

是则, 月段 变为 "01", 年段 + 1

否则, 月段 + 1.

其它的情况则只是简单的把 日段 加 1, 年, 月段不变. */

/* 大月末 */

if (getDay(date) >= 31){

setDate(date, getYear(date), getMonth(date) + 1, 1)

/* 年末 */

if (getMonth(date) >= 12) setDate(date, getYear(date) + 1, 1, 1)

}

/* 小月末 或 大月 30 日 */

else if (getDay(date) == 30){

if (isSmallMonth(getMonth(date))) setDate(date, getYear(date), getMonth(date) + 1, 1)

else setDate(date, getYear(date), getMonth(date), 31)/* 大月 30 日 */

}

/* 2 月末 */

else if (((getDay(date) == 29) &&(getMonth(date) == 2) &&isLeap(getYear

(date))) || ((getDay(date) == 28) &&(getMonth(date) == 2) &&!isLeap

(getYear(date))))

setDate(date, getYear(date), 3, 1)

/* 平日 */

else setDate(date, getYear(date), getMonth(date), getDay(date) + 1)

return date

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/7915775.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-11
下一篇 2023-04-11

发表评论

登录后才能评论

评论列表(0条)

保存