Trouble getting code to output properly

1 次查看(过去 30 天)
I am having trouble getting this to output the code properly, for some reason when I run this I just get a value of 0. I If I leave
days = d1+d2
unsupressed farther up in the code it will give me a number that is close to the value that I need, but this won't show up in the final answer for running the function. We're supposed to test it with the date 6/21/2011 and with that we're supposed to get 449,786 days since 1/1/780. I keep getting 448,951 though so I think that something is messed up with the way that it is calculating the days in the leap years as well as it not adding up the right number of days in the current year.
function d = daycount(day, month, year)
%start counter at 0
d = 0;
%add either 365 or 366 for each year from 780 to year-1
%call function leap (defined below) to determine leap years
years = leap(year-1);
d1 = years*366;
d2 = (((year-1)-780)-years)*365;
days = d1+d2
%add either 28, 29, 30, or 31 days each month in current year from January to month-1
%call function leap (defined below) to determine leap years
if leap(year) == 1
Feb = 28;
elseif leap(year) == 0
Feb =29;
%add days in current month
if month ==1
days = days+0;
elseif month == 2
days = days+31;
elseif month == 3
days = 31+Feb;
elseif month == 4
days = days +31+Feb;
elseif month == 5
days =days + (2*31)+Feb;
elseif month == 6
days = days + (2*31)+30+Feb;
elseif month == 7
days = days + (3*31)+30+Feb;
elseif month == 8
days = days + (3*31)+(2*30)+Feb;
elseif month == 9
days = days + (4*31)+(2*30)+Feb;
elseif month == 10
days = days + (5*31)+(2*30)+Feb;
elseif month == 11
days = days + (5*31)+(3*30)+Feb;
elseif month == 12
days = days + (6*31)+(3*30)+Feb;
end
days = days + day
end
function yesno = leap(year)
%set yesno to 1 if year is a leap year
%set yesno to 0 if year is not a leap year
x=[1:1:year];
for i = 1:year;
if mod(x(i),400) == 0;
yesno=1;
elseif (mod(x(i),100) ~= mod(x(i),400));
yesno= 0;
else (mod(x(i),4) ~= mod(x(i),100));
yesno= 1;
end;
end
  3 个评论
David Hill
David Hill 2022-2-12
Leap years are defined as those years that are divisible by 4, except for those that ends in , in which case a year is a leap year only if it is divisible by . This rule was introduced only starting . Before , the rule is much simpler, namely, except for , all years that are multiples of 4, are leap years.

请先登录,再进行评论。

回答(1 个)

David Hill
David Hill 2022-2-12
编辑:David Hill 2022-2-12
datenum('6/21/2011')-datenum('1/1/780')+1

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by