Write a function called day_diff that takes four scalar positive integer inputs, month1, day1, month2, day2. These represents the birthdays of two children who were born in 2015. The function returns a positive integer scalar that is equal to the dif

I can't deal it anymore. I have trouble to check an integer number or an array number in order to input argument in this problem .
function dd = day_diff(month1, day1, month2, day2)
if (month1 && day1 >0)&&(month1
if true
% code
end <= 12)&&(month2 && day2 >0) && month2 <= 12
if isinteger(month1 &&
if (month1 == 1 && month2 == 1)||(month1 == 3 && month2 == 3)||(month1 == 5 && month2 == 5)||(month1 == 7 && month2 == 7)||(month1 == 8 && month2 == 8)||(month1 == 10 && month2 == 10)||(month1 == 12 && month2 == 12) && day1<=31 && day2<=31
if day1 == day2
total1 = day1;
total2 = day2;
elseif day1 ~= day2
total1 = max(day1,day2);
total2 = min(day1,day2);
end
elseif (month1 == 4 && month2 == 4) ||(month1 == 6 && month2 == 6)||(month1 == 9 && month2 == 9)||(month1 == 11 && month2 == 11) % months have 30 days
if day1 == day2 && day1<=30 && day2<30
total1 = day1;
total2 = day2;
elseif day1 ~= day2 && day1<=30 && day2<30
total1 = max(day1,day2);
total2 = min(day1,day2);
else
dd=-1; return
end
elseif month1 == 1 && month2 ==2
total1 = day1;
total2 = day2+31;
elseif (month1 == 2 && day1<=28) && month2 == 1
total1 = day1 + 31;
total2 = day2;
elseif (month1 == 2 && day1>28) && month2 == 1
dd=-1;
return
elseif month1 == 1 && month2 == 12
total1 = day1;
total2 = day2 + 334;
elseif month1 == 2 && month2 == 3
total1 = day1 + 31;
total2 = day2 + 59;
elseif month1 == 1 && day1<=31&& month2 == 4
total1 = day1;
total2 = day2 + 90;
elseif month1 == 1 && day1>31 && month2 == 4
dd=-1; return
elseif month1 == 11 && day1<=30 && month2 == 12
total1 = day1 ;
total2 = day2 + 30;
elseif month1 == 11 && day1>30 && month2 == 12
dd=-1;
return
elseif month1 == 7 && month2 == 9
total1 = day1 + 181;
total2 = day2 + 243;
elseif month1 == 2 && day1<=28 && month2 == 6 && day2>30
dd=-1; return
elseif month1 == 2 && day1<=28 && month2 == 6 && day2<=30
total1 = day1;
total2 = day2 + 92;
end
dd = (max(total1,total2)) - (min(total1,total2));
else
dd = -1;
return
end
end

답변 (4개)

Why not make your function just be a wrapper for etime()? Did they specifically prohibit using that function?

댓글 수: 4

I have dealt with it. and actually it really simple than I thought before .
function diff = day_diff(m1,d1,m2,d2)
days = [31 28 31 30 31 30 31 31 30 31 30 31];
diff = -1;
if ~isscalar(m1) || m1 < 1 || m1 > 12 || m1 ~= floor(m1), return;
elseif ~isscalar(m2) || m2 < 1 || m2 > 12 || m2 ~= floor(m2), return;
elseif ~isscalar(d1) || d1 < 1 || d1 > days(m1) || d1 ~= floor(d1), return;
elseif ~isscalar(d2) || d2 < 1 || d2 > days(m2) || d2 ~= floor(d2), return;
end
if m2 < m1 || (m1 == m2 && d2 < d1) % make sure m1/d1 is the earlier date
tmp = m1; m1 = m2; m2 = tmp;
tmp = d1; d1 = d2; d2 = tmp;
end
diff = sum(days(m1:m2-1)) + d2 - d1;
end
whats the 'floor' for? what difference does it make with using 'fix'?
Floor rounds down to minus infinity. Fix rounds towards zero. They give different results when operating on negative numbers.

댓글을 달려면 로그인하십시오.

Ugur Ulas
Ugur Ulas 2017년 12월 29일
편집: Ugur Ulas 2017년 12월 29일
Here there is more basic solution:
function dd = day_diff(m1, d1, m2, d2)
A = [31 28 31 30 31 30 31 31 30 31 30 31]';
day1 = d1 + sum(A(1:(m1-1)));
day2 = d2 + sum(A(1:(m2-1)));
if prod(size(m1)) ~= 1 || prod(size(m2)) ~= 1 || prod(size(d1)) ~= 1 || prod(size(d2)) ~= 1
dd = -1;
elseif m1 < 1 || m2 < 1 || d1 < 1 || d2 < 1 || m1 ~= floor(m1) || m2 ~= floor(m2) || d1 ~= floor(d1) || d2 ~= floor(d2)
dd = -1;
elseif A(m1) < d1 || A(m2) < d2
dd = -1;
else
dd = abs(day2-day1);
end
end

댓글 수: 4

%to avoid matrix input for day and month
if prod(size(m1)) ~= 1 || prod(size(m2)) ~= 1 || prod(size(d1)) ~= 1 || prod(size(d2)) ~= 1
dd = -1;
%To avoid negative and rational numbers as input for month & day
elseif m1 < 1 || m2 < 1 || d1 < 1 || d2 < 1 || m1 ~= floor(m1) || m2 ~= floor(m2) || d1 ~= floor(d1) || d2 ~= floor(d2)
dd = -1;
elseif A(m1) < d1 || A(m2) < d2
dd = -1;
What case would the last one be? I do not understand the logic. Please help!
If you want to check if something is a scalar, don't compute prod(size(...)). Use the isscalar function instead.
Thank you for you response but I actually wanted to know the logic behind this:
elseif A(m1) < d1 || A(m2) < d2
dd = -1;
It says to set dd equal to minus one if A is less than d1 at the m1 index, or if A is less than d2 at the m2 index.

댓글을 달려면 로그인하십시오.

if true
function [ dd ] = day_diff( m1,d1,m2,d2 )
% Short circuiting!
N = [31,28,31,30,31,30,31,31,30,31,30,31];
if isscalar(m1) && isscalar(d1) && isscalar(m2) && isscalar(d2) && ... m1 == fix(m1) && d1 == fix(d1) && m2 == fix(m2) && d2 == fix(d2) &&... m1 > 0 && m1 <= 12 && m2 > 0 && m2 <= 12 && ... d1 > 0 && d1 <= N(m1) && d2 > 0 && d2 <= N(m2)
day1 = d1 + sum(N(1:1:m1-1));
day2 = d2 + sum(N(1:1:m2-1));
dd = abs(day1-day2)
else
dd = -1
end
end
function dd=day_diff(m1,d1,m2,d2)
num_of_days=[31,28,31,30,31,30,31,31,30,31,30,31];
dd=-1;
mmm1=round(m1);mmm2=round(m2);ddd1=round(d1);ddd2=round(d2);
if(isscalar(d1)&&isscalar(d2)&&isscalar(m1)&&isscalar(m2)) % checking for valid input
if(d1-ddd1 || d2-ddd2 || m1-mmm1 || m2-mmm2) % checking for valid input
dd=-1;
else
if (m1<1||m1>12||m2<1||m2>12) %checking months
dd=-1;
else
if(d1>num_of_days(m1) || d2>num_of_days(m2) || d1<1 || d2<1) %validating days of respective month
dd=-1;
else
dd1=sum(num_of_days(1:m1));
dd1=dd1+d1-num_of_days(m1);
dd2=sum(num_of_days(1:m2));
dd2=dd2+d2-num_of_days(m2);
if(dd1>=dd2)
dd=dd1-dd2;
else
dd=dd2-dd1;
end
end
end
end
end

카테고리

도움말 센터File Exchange에서 Functions에 대해 자세히 알아보기

질문:

2016년 11월 26일

답변:

2019년 2월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by