用c语言编写一程序,用户输入日期,计算该日是今年的第几天

用c语言编写一程序,用户输入日期,计算该日是今年的第几天,第1张

#include"stdio.h"

#include"stdlib.h"

intmain()

{

intday,month,year,sum,leap

printf("\npleaseinputyear,month,day\n")

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

switch(month)/*先计算某月以前月份的总天数*/

{

case1:sum=0break

case2:sum=31break

case3:sum=59break

case4:sum=90break

case5:sum=120break

case6:sum=151break

case7:sum=181break

case8:sum=212break

case9:sum=243break

case10:sum=273break

case11:sum=304break

case12:sum=334break

default:printf("dataerror\n")break

}

sum=sum+day/*再加上某天的天数*/

if(year%400==0||(year%4==0&&year%100!=0))/*判断是不是闰年*/

{

leap=1

}

else

{

leap=0

}

if(leap==1&&month>2)/*如果是闰年且月份大于2,总天数应该加一天*/

{

sum++

}

printf("Itisthe%dthday.\n",sum)

return0

}

扩展资料

C语言输晌档入任意一个日期的年、月、日宴改乱的值,求出是星期几并输出。

#include<stdio.h>

voidmain()

{

inty,m,d,c,s,w

printf("请输入8位年月日,以空格隔开:")

scanf("%d%d%d",&y,&m,&d)

if(m==1)c=d

elseif(m==2)c=31+d

elseif(m==3)c=31+28+d

elseif(m==4)c=31+28+31+d

elseif(m==5)c=31+28+31+30+d

elseif(m==6)c=31+28+31+30+31+d

elseif(m==7)c=31+28+31+30+31+30+d

elseif(m==8)c=31+28+31+30+31+30+31+d

elseif(m==9)c=31+28+31+30+31+30+31+31+d

elseif(m==10)c=31+28+31+30+31+30+31+31+30+d

elseif(m==11)c=31+28+31+30+31+30+31+31+30+31+d

elseif(m==12)c=31+28+31+30+31+30+31+31+30+31+30+d

if(m>2)

{if(y%100==0){if(y%400==0)c=c+1}

elseif(y%4==0)c=c+1}

s=(y-1)*365+(y-1)/4-(y-1)/100+(y-1)/400+c

w=s%7

switch(w)

{

case0:printf("星期日")break

case1:printf("星期一")break

case2:printf("星期二")break

case3:printf("星期三")break

case4:printf("星期四")break

case5:printf("星期五歼芦")break

case6:printf("星期六")break

printf("\n")

}

}

源程序如下:

#include "pch.h"

#define  _CRT_SECURE_NO_WARNINGS

#include <iostream>

using namespace std

int main()

{

int s[2][13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31,

0,31,29,31,30,31,30,31,31,30,31,30,31, }

int year = 0

int month = 0

int day = 0

int n, i, daytemp

int flag = 0

int nyear = 0, nmonth = 0, nday = 0

printf("输入年月日正旦\n")

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

//printf("输入天数\n")

//scanf("%d",&n)

n = 1

if (year <0 || month <0 || month>12 || day <1 || n <0)

{

printf("核姿输入数据错误\n")

return 1

}

daytemp = day + n//累加天数

if ((year % 4 == 0 &&year % 100 != 0) || year % 400 == 0)//闰年置1

flag = 1

if (day >s[flag][month])

{

printf("输入日期与年月不符.\n")

return 1

}

if (daytemp <= s[flag][month])//当前日期加天数在本月

{

nyear = year

nmonth = month

nday = daytemp

}

else

{

i = month

nyear = year;

while (daytemp >s[flag][i])//循环递减,直到当前日期加天数在本月

{

daytemp = daytemp - s[flag][i]

i++

if (i >12)//超过一年,年累加

{

nyear++

if ((nyear % 4 == 0 &&nyear % 100 != 0) || nyear % 400 == 0)

flag = 1

else

flag = 0

i = i - 12

}

}

nmonth = i

nday = daytemp

}

printf("%d年%d月%d日第%d天后是:\n", year, month, day, n)

printf("%d年%d月%d日\n", nyear, nmonth, nday)

return 0

}

程序运行结果如下:

扩改清绝展资料:

其他实现方式:

int monthsize(int year, int month) {

int days

if (month == 2) {

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

return 29

return 28

}

switch (month) {

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:days = 31break

case 4:

case 6:

case 9:

case 11: days = 30break

}

return days

}

int main() 

int year, month, day, days

printf("年 月 日:")

while (scanf("%d%d%d", &year, &month, &day) == 3) {

days = monthsize(year, month)

if (days == day) {

if (month == 12) {

++year

month = 1

day = 1

}

else ++month

}

else ++day

printf("第二天是:%d/%02d/%02d\n", year, month, day)

printf("年 月 日(q to quit):")

}

return 0

}


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存