MATLAB:为什么函数不能在给定的日期输入中处理错误?

时间:2019-01-09 16:59:17

标签: matlab

我正在Coursera上学习MATLAB,并遇到了以下问题:

  

编写一个名为day_diff的函数,该函数需要四个标量正整数输入,month1,day1,month2,day2。这些代表两个孩子在2015年出生的生日。该函数返回一个正整数标量,该整数等于两个孩子的天数之差。确保检查输入值的类型正确,并且它们代表有效的日期。如果它们是错误的,则返回-1。该函数的示例调用为
  dd = day_diff(1,30,2,1);
  这将使dd等于2。不允许使用内置函数datenum或datetime。提示:将2015个月中的天数存储在12个元素的向量中(例如31、28、31、30…),并以简单的公式使用。

使用预先创建的评估代码测试多个案例。 到目前为止,我的代码:

function answer = day_diff(month1, day1, month2,day2)
    answer = -1;
    days_in_months = [31,28,31,30,31,30,31,31,30,31,30,31]; %days in every month array
    flag1 = days_in_months(month1); %to check if day1 is valid
    flag2 = days_in_months(month2); %to check if day2 is valid
    %Non valid values handing
    if nargin < 4
        error('Must have four arguments');
    end
    if ~isscalar(month1) || month1 < 1 || month1 ~= fix(month1) || month1 > 12
        error('month1 needs to be a positive integer and not greater than 12.');
    end
    if ~isscalar(month2) || month2 < 1 || month2 ~= fix(month2) || month2 > 12        
        error('month2 needs to be a positive integer and not greater than 12.');
    end
    if ~isscalar(day1) || day1 < 1 || day1 ~= fix(day1)|| day1 > flag1        
        error('day1 needs to be a positive integer and a valid date.');
    end
    if ~isscalar(day2) || day2 < 1 || day2 ~= fix(day2)|| day2 > flag2        
        error('day1 needs to be a positive integer and a valid date.');
    end
    %end of error handler
    %to find the age in days
    if (month1 == month2)
        inbetween_days = 0;
        if day1 == day2
            first_last_days = 0;
        elseif day1 < day2
            first_last_days = day2 - day1;
        else
            first_last_days = day1 - day2;
        end
    elseif month1 < month2
            inbetween_days = sum(days_in_months(month1+1:month2-1));
            first_last_days = (days_in_months(month1)-day1) + day2;
        else
            inbetween_days = sum(days_in_months(month2+1:month1-1));
            first_last_days = day1 + (days_in_months(month2)-day2);
    end
     answer = first_last_days +  inbetween_days;
end
到目前为止,

很好,但是当它是day_diff(2,29,1,22)时,评估文件会给出错误,而不是处理错误的预定义输入(day1为29,最大值为28),这是问题所在。如何返回-1?

Problem 4 (day_diff):
        Feedback: Your function performed correctly for argument(s) 1, 30, 2, 1
        Feedback: Your function performed correctly for argument(s) 1, 1, 1, 1
        Feedback: Your function performed correctly for argument(s) 1, 1, 1, 2
        Feedback: Your function performed correctly for argument(s) 1, 2, 1, 1
        Feedback: Your function performed correctly for argument(s) 1, 1, 2, 1
        Feedback: Your function performed correctly for argument(s) 2, 1, 1, 1
        Feedback: Your function performed correctly for argument(s) 1, 31, 2, 1
        Feedback: Your function performed correctly for argument(s) 2, 1, 1, 31
        Feedback: Your function performed correctly for argument(s) 1, 1, 12, 31
        Feedback: Your function performed correctly for argument(s) 2, 1, 3, 1
        Feedback: Your function performed correctly for argument(s) 7, 1, 9, 30
        Feedback: Your program made an error for argument(s) 2, 29, 1, 22

    Your solution is _not_ correct.

2 个答案:

答案 0 :(得分:1)

是的,您永远不会返回他们要求的-1。删除错误消息,在这种情况下,什么也不做,让您的答案保持-1。 以下是您所拥有的更多代码行,您可以对其进行改进,但这应该可以通过失败的测试

    function answer = day_diff(month1, day1, month2,day2)
    answer = -1;
    days_in_months = [31,28,31,30,31,30,31,31,30,31,30,31]; %days in every month array
    flag1 = days_in_months(month1); %to check if day1 is valid
    flag2 = days_in_months(month2); %to check if day2 is valid
    %Non valid values handing
    if nargin < 4
        answer=-1;

    elseif ~isscalar(month1) || month1 < 1 || month1 ~= fix(month1) || month1 > 12
        answer=-1;
    elseif ~isscalar(month2) || month2 < 1 || month2 ~= fix(month2) || month2 > 12        
        answer=-1;
    elseif ~isscalar(day1) || day1 < 1 || day1 ~= fix(day1)|| day1 > flag1        
        answer=-1;
    elseif ~isscalar(day2) || day2 < 1 || day2 ~= fix(day2)|| day2 > flag2        
        answer=-1;
    %end of error handler
    %to find the age in days
    elseif (month1 == month2)
        inbetween_days = 0;
        if day1 == day2
            first_last_days = 0;
        elseif day1 < day2
            first_last_days = day2 - day1;
        else
            first_last_days = day1 - day2;
        end
        answer = first_last_days +  inbetween_days;
    elseif month1 < month2
            inbetween_days = sum(days_in_months(month1+1:month2-1));
            first_last_days = (days_in_months(month1)-day1) + day2;
            answer = first_last_days +  inbetween_days;
    else
            inbetween_days = sum(days_in_months(month2+1:month1-1));
            first_last_days = day1 + (days_in_months(month2)-day2);
            answer = first_last_days +  inbetween_days;
    end

end

答案 1 :(得分:0)

我将删除第一行中的answer = -1并处理一个if块中的所有错误,以避免在可能介于elseif块之间的情况下出现错误:

% Non valid values handing:
if (nargin ~= 4) ||...
        ~all([isscalar(month1) isscalar(month2) isscalar(day1) isscalar(day2)]) ||... % only scalars
        any([month1,month2,day1,day2]<1) ||... % all input greater then zero
        any(fix([month1,month2,day1,day2]) ~= [month1,month2,day1,day2]) ||... % all are integers
        any([month1,month2,day1,day2] > [12 12 flag1 flag2]) % all are valid dates
    answer = -1;
    return;
end
% end of error handler

如果条件为真,则该函数将-1分配给answer并退出(通过return)。

我认为,这种错误处理方式更易于遵循且不易出错,但是我不确定它能否解决原始问题。

您还可以删除return语句,并将所有其余功能放在else块中,即:

if (nargin ~= 4) ||... % all the condition...           
    answer = -1;
else
    % all the calculation...
    answer = first_last_days +  inbetween_days;
end