function dd = day_diff(m1, d1, m2, d2)
input_all = [m1, d1, m2, d2];
input_mth = [m1, m2];
input_day = [d1, d2];
if m1 <= 2
N1 = 1461*(2014)/4 + 153*(m1 + 13)/5 + d1;
else
N1 = 1461*(2015)/4 + 153*(m1 + 1)/5 + d1;
end
if m2 <= 2
N2 = 1461*(2014)/4 + 153*(m2 + 13)/5 + d2;
else
N2 = 1461*(2015)/4 + 153*(m2 + 1)/5 + d2;
end
if sum(rem(input_all,1)==0) ~= 4
disp('Input is not integer.');
elseif sum(input_all>0) ~= 4
disp('Input is not positive.');
elseif sum(input_mth > [12, 12]) ~= 0
disp('Month input exceeds 12.');
else
switch m1
case {1,3,5,7,8,10,12}
d1_max = 31;
case 2
d1_max = 28;
otherwise
d1_max = 30;
end
switch m2
case {1,3,5,7,8,10,12}
d2_max = 31;
case 2
d2_max = 28;
otherwise
d2_max = 30;
end
if sum(input_day > [d1_max, d2_max]) ~= 0
disp('Day input exceeds its limit.');
elseif N1 - N2 > 0
dd = floor(N1 - N2);
str = ['Person 1 is ', num2str(dd), ' days older than Person 2.'];
disp(str);
elseif N1 - N2 < 0
dd = floor(N2 - N1);
str = ['Person 2 is ', num2str(dd), ' days older than Person 1.'];
disp(str);
else
disp('The two persons are of the same age.');
end
end
姐要革命
发表于 2022-06-22 22:24:08