C语言:输入年份和天数,输出对应的年、月、日

如题所述

下面是这段代码,配合注释体会一下吧

#include <stdio.h>

void MonthDay(int year,int yearDay,int *pMonth,int *pDay);

int main()
{
int year,yearDay,month,day;
scanf("%d%d",&year,&yearDay);//输入年份和天数
MonthDay(year,yearDay,&month,&day);
printf("%d-%d-%d",year,month,day);
return 0;
}

void MonthDay(int year,int yearDay,int *pMonth,int *pDay)
{
int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};//这个数组保存每月的天数
if(year%4==0&&year%100!=0||year%400==0)//判断是否为闰年
days[1]=29;//闰年的二月有29天
*pMonth=1;//从一月开始看
while(yearDay>days[*pMonth-1])//如果哪一天超出这个月
{
yearDay-=days[*pMonth-1];//求出超出此月的天数
(*pMonth)++;//继续考虑下一月
}
*pDay=yearDay;
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2015-06-13
#include <stdio.h>
#define ISLEAP(y) ((y)%400==0||(y)%4==0&&(y)%100!=0)
void MonthDay(int year,int yearDay,int *pMonth,int *pDay);
int main(void) 
{
int year,yearDay,month,day;
while(scanf("%d%d",&year,&yearDay)!=EOF)
{
MonthDay(year,yearDay,&month,&day);
printf("%d-%d-%d\n",year,month,day);
}
return 0;
}
void MonthDay(int year,int yearDay,int *pMonth,int *pDay)
{
int i,date[]={31,28,31,30,31,30,31,31,30,31,30,31};
for(i=0,*pMonth=1;yearDay>date[i];++i,++*pMonth)
{
yearDay-=i==1?date[i]+ISLEAP(year):date[i];
}
if(yearDay==0)
{
--*pMonth;
*pDay=*pMonth-1==1?date[*pMonth-1]+ISLEAP(year):date[*pMonth-1];
}
else
{
*pDay=yearDay;
}
}

本回答被提问者采纳

C语言输入年份和天数输出对应的年月日
C语言输入年份和天数输出对应的年月日的源代码如下:include <iostream> int day(int &year, int &month);int main(){ int year{};int month{};std::cout << "请输入年和月(空格隔开):";std::cin >> year >> month;std::cout << "该月天数:" << day(year, month) << '\\n';...

【悬赏】程序编写,输入年月日和一个天数,再输出年月日,c语言
int days[]={31,28,31,30,31,30,31,31,30,31,30,31};int main(){ int year , mon , day , num;printf("input yyyy\/mm\/dd:");scanf("%d\/%d\/%d" , &year , &mon , &day );\/\/请确保输入的数据是有效的日期,不然出错 \/\/根据年份确定当年是否闰年 if ( (year%4==0 && ye...

c语言输出年月日
pMonth=1;\/\/从一月开始看 while(yearDay>days[*pMonth-1])\/\/如果哪一天超出这个月 { yearDay-=days[*pMonth-1];\/\/求出超出此月的天数 (*pMonth)++;\/\/继续考虑下一月 } pDay=yearDay;}

求助!输入年份和天数算年月日的问题
printf("是%d年%d月%d号\\n",year,i+1,date);}

c语言万年历程序。编写程序打印任意给定的年份的日历或者某年某月的月 ...
以下是一个示例的 C 代码,实现了打印给定年份的日历、某年某月的月历以及查询某一天是星期几的功能:include <stdio.h>\/\/ 判断某年是否为闰年int isLeapYear(int year) { if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { return 1;} return 0;}\/\/ 获取某年某...

C语言改错:输入某年的第几天,输出该天为该年的第几月第几日
30,31,30,31,31,30,31,30,31},{31,28,31,30,31,30,31,31,30,31,30,31}};printf("请输入年份:");scanf("%d",&year);printf("请输入该年的第几天:");scanf("%d",&day);func(a);printf("该天为该年的第%d月,第%d天\\n",month,day);\/\/system("pause");return 0;} ...

给出年份和天数,求出相应的月、日
printf("请输入年份和天数:");scanf("%d%d",&year,&days);if(year%4==0 && year%100!=0 ||year%400==0){ day_s[2]=29;} day=1;month=1;for(i=1;i<13;i++){ if(days<=day_s[i]){ day=days;break;} days-=day_s[i];month++;} printf("日期:%d年%d月%d日\\n",...

c语言;输入一个年份,输出该年各月份天数以及总天数。
include <stdio.h>main(){int i,year,month[12] ={31,28,31,30,31,30,31,31,30,31,30,31};scanf("%d",&year);if((year%4==0)&&(year%100!=0)||(year%400==0))month[1]++;year=0;for (i=0;i<12;i++)printf("month[%d]:day[%d]\\n",i+1,month[i]),year+=...

C语言输入年份月份,输出天数。
){int year,day,d,month,leap;int i;int Month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};printf("输入年份:\\n");scanf("%d",&year);printf("输入月份:\\n");scanf("%d",&month);if( (year%4!=0) ||( (year%100==0)&& (year%400!=0))) Month[2] = 29;...

c语言设计万年历 输入任意年份,输出该年12个月份 输入任意年份月份...
程序直接输入年份和月份,打印对应日历。一年12个月是固定的,我就不写单独输入年份打印月份了。你想就把我月份数组打印一遍好了。include<stdio.h>#include#include<malloc.h>enum MONTH{January=1,February,March,April,May,June,July,August,September,October,November,December};struct tm * getDate(...

相似回答