导航:首页 > 编程语言 > c语言编程求某年第n天的日期

c语言编程求某年第n天的日期

发布时间:2024-05-28 10:53:00

❶ 用c语言中的数组编程,当填写年月日时,输出是这年的第几天.

方法一:

#include<stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
int day,month,year,sum,leap;
do{
printf("\nplease input year month day\n");
printf("\n请输入年 月 日\n");
scanf("%d %d %d",&year,&month,&day);
if((month==1&&day<=31&&day>=1)||
((year%4==0&&year%100!=0||year%400==0)&&(month==2&&day<=29&&day>=1))||
(!(year%4==0&&year%100!=0||year%400==0)&&(month==2&&day<=28&&day>=1))||
(month==3&&day<=31&&day>=1)||(month==4&&day<=30&&day>=1)||
(month==5&&day<=31&&day>=1)||(month==6&&day<=30&&day>=1)||
(month==7&&day<=31&&day>=1)||(month==8&&day<=31&&day>=1)||
(month==9&&day<=30&&day>=1)||(month==10&&day<=31&&day>=1)||
(month==11&&day<=30&&day>=1)||(month==12&&day<=31&&day>=1))
{ switch(month)//先计算某月以前月份的总天数
{
case 1:sum=0;break;
case 2:sum=31;break;
case 3:sum=59;break;
case 4:sum=90;break;
case 5:sum=120;break;
case 6:sum=151;break;
case 7:sum=181;break;
case 8:sum=212;break;
case 9:sum=243;break;
case 10:sum=273;break;
case 11:sum=304;break;
case 12:sum=334;break;
default:printf("data error");
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++;
//clrscr(); //清屏
system("cls"); //清屏
printf("%d-%d-%dis the year\'s first %d days.\n\n",year,month,day,sum);
printf("%d-%d-%d是这年的第%d天.\n\n",year,month,day,sum);
}
else printf("\tData errors!!!\n\n");
}while(1);
}

方法二:

#include<stdio.h>
#include <stdlib.h>
struct
{
int year;
int month;
int day;
}date;
void main()
{int i,days;
int day_tab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
do{
printf("Input year,month,day:\n");
scanf("%d %d %d",&date.year,&date.month,&date.day);
system("cls"); //清屏
days=0;
if((date.month==1&&date.day<=31&&date.day>=1)||
((date.year%4==0&&date.year%100!=0||date.year%400==0)&&(date.month==2&&date.day<=29&&date.day>=1))||
(!(date.year%4==0&&date.year%100!=0||date.year%400==0)&&(date.month==2&&date.day<=28&&date.day>=1))||
(date.month==3&&date.day<=31&&date.day>=1)||(date.month==4&&date.day<=30&&date.day>=1)||
(date.month==5&&date.day<=31&&date.day>=1)||(date.month==6&&date.day<=30&&date.day>=1)||
(date.month==7&&date.day<=31&&date.day>=1)||(date.month==8&&date.day<=31&&date.day>=1)||
(date.month==9&&date.day<=30&&date.day>=1)||(date.month==10&&date.day<=31&&date.day>=1)||
(date.month==11&&date.day<=30&&date.day>=1)||(date.month==12&&date.day<=31&&date.day>=1))
{
for(i=1;i<date.month;i++)
days+=day_tab[i];
days+=date.day;
if((date.year%4==0&&date.year%100!=0||date.year%400==0)&&date.month>=3)
days++;
printf("\n\n");
printf("\t%d/%d is the %dth day in %d.\n",date.month,date.day,days,date.year);
}
else printf("\tData errors!!!\n\n");
}while(1);
}

❷ 杈揿叆镆愪竴骞寸殑绗鍑犲ぉ锛岃$畻骞惰緭鍑哄畠鏄杩欎竴骞寸殑绗鍑犳湀绗鍑犳棩锛宑璇瑷锛屾庝箞鍐欙纴璋㈣阿

#include<stdio.h>
#include<stdlib.h>

voidMonthDay(intyear,intyearDay,int*pMonth,int*pDay);

intmain()
{
intyear,yearDay,pMonth,pDay;
scanf("%d",&year);
scanf("%d",&yearDay);

MonthDay(year,yearDay,&pMonth,&pDay);

printf(" %d%d",pMonth,pDay);
return0;
}

voidMonthDay(intyear,intyearDay,int*pMonth,int*pDay)
{
intday[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}};
intflag=0,i=0;
if(((year%4==0)&&(year%100)!=0)||(year%400==0))
flag=1;

if(flag==0&&yearDay>365)
{
printf("澶╂暟杈揿叆涓嶅悎娉昞n");
exit(0);
}
elseif(flag==1&&yearDay>366)
{
printf("澶╂暟杈揿叆涓嶅悎娉昞n");
exit(0);
}

while(yearDay>day[flag][i])
{
yearDay-=day[flag][i];
i++;
}
*pMonth=i+1;
*pDay=yearDay;
}

阅读全文

与c语言编程求某年第n天的日期相关的资料

热点内容
linuxmongodb服务启动 浏览:766
在哪里下载核酸检测app 浏览:310
esxi启动虚拟机命令 浏览:969
军工级单片机 浏览:113
服务器安全保护是什么意思 浏览:789
删除运行命令 浏览:720
龙之召唤服务器如何 浏览:119
linux目录跳转 浏览:368
程序员和老板称兄道弟 浏览:759
直播网络连接源码 浏览:736
用安卓手机怎么登录苹果手机id 浏览:710
论文查重工具源码 浏览:401
android银联demo 浏览:86
智能算法发展 浏览:351
房车露营地用什么app 浏览:70
spark编程指南python 浏览:553
phparray源码 浏览:1002
安卓手机反应有点慢怎么办 浏览:705
c语言怎么访问服务器并获取数据 浏览:114
怎么下载三维app 浏览:77