C语言:求日期的下一天程序

C语言:求日期的下一天程序,第1张

已经修改。另外要想理想运行,输入就用cin>>,scanf输入的会造成显示混乱

#include

#include

#include

using

namespace

std

int

IsLeapYear(int

year)

{

if(year%4==0)

{

if(year%100==0&&year%400==0)

return

1

else

return

0

}

else

return

0

}

int

getlast_day(int

month,

int

year)

{

switch

(month)

{

case

1:

case

3:

case

5:

case

7:

case

8:

case

10:

case

12:

return

31

break

case

2:

if(

IsLeapYear(year))

return

29

else

return

28

break

default:

return

30

break

}

}

int

check_day(int

last_day,

int

day)

{

if(day>last_day||day<=0)//invalid

return

0

else

return

1

}

void

next_day(int

year,

int

month,

int

day)

{

if(day+1<=getlast_day(month,year))

printf("Next

Day:

%d/%d/%d

",year,month,day+1)

else

{

if(month+1<=12)

printf("Next

Day:

%d/%d/%d

",year,month+1,1)

else

printf("Next

Day:

%d/%d/%d

",year+1,1,1)

}

}

void

main()

{

int

year,month,day

char

flag

do

{

printf("Input

Year\n")

scanf("%d",&year)

printf("Input

Month\n")

scanf("%d",&month)

printf("Input

Day\n")

scanf("%d",&day)

if(!check_day(getlast_day(month,

year),

day))

{

printf("Day

Input

Error.

Input

Please[1

-

31]

integerInput

Day\n")

scanf("%d",&day)

}

else

{

next_day(year,

month,

day)

printf("要终止程序吗?(Y/N)

\n")

scanf("%c",&flag)

if(flag=='Y'||flag=='y')

break

}

}

while(1)

system("pause")

}

#include

"stdio.h"

int

isleapyear(int

year)//判断是不是闰年

int

iscurrentdate(int

year,int

month,int

day)//是不是正确日期

int

returnnextday(int

year,int

month,int

day)//返回下一天,也有判断是不是正确的日期。

int

leapyear_month_sumday[12]={31,29,31,30,31,30,31,31,30,31,30,31}//闰年数组,保存各月天数。

void

main()

{

int

year=0,month=0,day=0,nextday=0

printf("pleace

enter

right

date(example:2005

6

27):\n")

scanf("%d%d%d",&year,&month,&day)

nextday=returnnextday(year,month,day)

switch

(nextday)

{

case

0:

printf("not

a

current

date\n")

break

case

1:

if

(month==12)

{

year++

month=1

}

else

{

month++

}

break

}

if

(nextday!=0)

printf("the

you

input

next

date

is

%d-%d-%d.thank

use

bye

bye!\n",year,month,nextday)

}

int

isleapyear(int

year)

{

if

(year%4==0)

{

if

(year%400)

return

1

else

{

if

(year%100==0)

return

0

else

return

1

}

}

else

{

return

0

}

}

int

iscurrentdate(int

year,int

month,int

day)

{

if

((year<0)

&&

(year>9999)

&&

(month>12)

&&

(month<1)

&&

(day<1)

&&(day>31)

)return

0

else

return

1

}

int

returnnextday(int

year,int

month,int

day)

{

int

thismonthsumday

if

(iscurrentdate(year,month,day)==1)

{

thismonthsumday=leapyear_month_sumday[month-1]

if

(month==2)

{

if

(isleapyear(year)==0)

thismonthsumday--

}

if

(day<thismonthsumday)

{

return

++day

}

else

{

if

(day==thismonthsumday)

{

return

1

}

else

{

return

0

}

}

}

else

{

return

0

}

}


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

原文地址: http://outofmemory.cn/yw/11324333.html

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

发表评论

登录后才能评论

评论列表(0条)

保存