function lizi_doy

% 这个程序是用来计算输入的某一年,某一月,某一日在当年是第多少天

% 定义变量:

% year           --输入的年份

% month          --输入的月份

% day            --输入的日期

% leap_day       --闰年多出的那一天

% day_of_year    --一年中的第多少天

%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%提示输入年、月、日%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

year=input('Enter the value of Year:');

month=input('Enter the value of Month:');

day=input('Enter the value of Date:');

%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%判断输入的年份是不是闰年%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if mod(year, 400)==0

leap_day=1;%如果输入的年数能被400,整除,则说明这一年是闰年

elseif (mod(year,4)==0)&&(mod(year,100)~=0)

leap_day=1;%如果输入的年数能被4整除,但是不能被100整除,则说明这一年是闰年

elseif (mod(year,100)==0)&&(mod(year,400)~=0)

leap_day=0;%如果输入的年数能被100整除,但是不能被400整除,则说明这一年不是闰年

else

leap_day=0;

end

%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%输入月份的天数%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

switch(month)

case(1)

day_of_year=day;

case(2)

day_of_year=day+31;

case(3)

day_of_year=day+31+28+leap_day;

case(4)

day_of_year=day+2*31+28+leap_day;

case(5)

day_of_year=day+2*31+28+30+leap_day;

case(6)

day_of_year=day+3*31+28+30+leap_day;

case(7)

day_of_year=day+3*31+28+2*30+leap_day;

case(8)

day_of_year=day+4*31+28+2*30+leap_day;

case(9)

day_of_year=day+5*31+28+2*30+leap_day;

case(10)

day_of_year=day+5*31+28+3*30+leap_day;

case(11)

day_of_year=day+6*31+28+3*30+leap_day;

case(12)

day_of_year=day+6*31+28+4*30+leap_day;

end

%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%打印对应的年、月、日%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

fprintf('The date %2d %2d %4d is day of year %d.\n',month,day,year,day_of_year);

这里有点问题就是在月的转换的时候,我的算法比较古板,各位大侠!谁还有比较高明一点的算法,我可以学习学习!:loveliness:

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐