Write a function three positive integer scalar inputs year, month, day.

2 次查看(过去 30 天)
Write a function called valid_date that takes three positive integer scalar inputs year, month, day. If these three represent a valid date, return a logical true, otherwise false. The name of the output argument is valid. If any of the inputs is not a positive integer scalar, return false as well. Note that every year that is exactly divisible by 4 is a leap year, except for years that are exactly divisible by 100. However, years that are exactly divisible by 400 are also leap years. For example, the year 1900 was not leap year, but the year 2000 was.
%Code to call your function
valid = valid_date(2018,4,1)
valid = valid_ date(2018,4,31)
I only got 1 of 6 which is random leap years.
Here is my code.
function valid = valid_date(year,month,day)
if ~isscalar(year) || year <1 || year ~= fix(year);
valid = false;
end
if ~isscalar(month) || month <1 || month ~= fix(month) || month > 12;
valid = false;
end
if ~isscalar(day) || day <1 || day ~= fix(day) || day > 31;
valid = false;
end
if year/4 == 0 && year/100 == 0 && year/400 == 0;
if month == [2,4,6,9,11];
if month == 2 && day > 29;
valid = false;
elseif month ~= 2 && day > 30;
valid = false;
else
valid = true;
end
else month ~= [2,4,6,9,11];
if day > 31;
valid = false;
else
valid = true;
end
end
elseif year/4 == 0 && year/100 ~= 0 && year/400 ~= 0;
if month == [2,4,6,9,11];
if month == 2 && day > 29;
valid = false;
elseif month ~= 2 && day > 30;
valid = false;
else
valid = true;
end
else month ~= [2,4,6,9,11];
if day > 31;
valid = false;
else
valid = true;
end
end
elseif year/4 == 0 && year/100 == 0 && year/400 ~= 0;
if month == [2,4,6,9,11];
if month == 2 && day > 28;
valid = false;
elseif month ~= 2 && day > 30;
valid = false;
else
valid = true;
end
else month ~= [2,4,6,9,11];
if day > 31;
valid = false;
else
valid = true;
end
end
else year/4 ~= 0 && year/100 ~= 0 && year/400 ~= 0;
if month == [2,4,6,9,11];
if month == 2 && day > 28;
valid = false;
elseif month ~= 2 && day > 30;
valid = false;
else
valid = true;
end
else month ~= [2,4,6,9,11];
if day > 31;
valid = false;
else
valid = true;
end
end
end
  4 个评论
Taekyoung Lee
Taekyoung Lee 2020-2-2
@Mohammad Sami
Thank you for your support! I got 6 of 6!! Thank you so much ones agian.
function valid = valid_date(year,month,day)
if ~isscalar(year) || year <1 || year ~= fix(year);
valid = false;
return
end
if ~isscalar(month) || month <1 || month ~= fix(month) || month > 12;
valid = false;
return
end
if ~isscalar(day) || day <1 || day ~= fix(day) || day > 31;
valid = false;
return
end
if rem(year,4) == 0 && rem(year,100) == 0 && rem(year,400) == 0;
if month == 2 || month == 4 || month == 6 || month == 9 || month == 11;
if month == 2 && day > 29;
valid = false;
elseif month ~= 2 && day > 30;
valid = false;
else
valid = true;
end
else month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12;
if day > 31;
valid = false;
else
valid = true;
end
end
elseif rem(year,4) == 0 && rem(year,100) ~= 0 && rem(year,400) ~= 0;
if month == 2 || month == 4 || month == 6 || month == 9 || month == 11;
if month == 2 && day > 29;
valid = false;
elseif month ~= 2 && day > 30;
valid = false;
else
valid = true;
end
else month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12;
if day > 31;
valid = false;
else
valid = true;
end
end
elseif rem(year,4) == 0 && rem(year,100) == 0 && rem(year,400) ~= 0;
if month == 2 || month == 4 || month == 6 || month == 9 || month == 11;
if month == 2 && day > 28;
valid = false;
elseif month ~= 2 && day > 30;
valid = false;
else
valid = true;
end
else month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12;
if day > 31;
valid = false;
else
valid = true;
end
end
else rem(year,4) ~= 0 && rem(year,100) ~= 0 && rem(year,400) ~= 0;
if month == 2 || month == 4 || month == 6 || month == 9 || month == 11;
if month == 2 && day > 28;
valid = false;
elseif month ~= 2 && day > 30;
valid = false;
else
valid = true;
end
else month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12;
if day > 31;
valid = false;
else
valid = true;
end
end
end
Capture.PNG
Abhishek Kumar
Abhishek Kumar 2021-5-30
%This is the working code
function [valid] = valid_date(y, m, d)
t = isscalar(y) && isscalar(m) && isscalar(d)
if t ~= 1
valid = false;
else nargin == 3 && (y && m > 0)
p = rem(y,4);
q = rem(y,100);
r = rem(y,400);
if (p == 0 && q ~= 0) || r == 0
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) && (d > 0 && d <= 31)
valid = true;
elseif (m == 4 || m == 6 || m == 9 || m == 11) && (d > 0 && d <= 30)
valid = true;
elseif m == 2 && (d > 0 && d <= 29)
valid = true;
else
valid = false;
end
else
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) && (d > 0 && d <= 31)
valid = true;
elseif (m == 4 || m == 6 || m == 9 || m == 11) && (d > 0 && d <= 30)
valid = true;
elseif m == 2 && (d > 0 && d <= 28)
valid = true;
else
valid = false;
end
end
end

请先登录,再进行评论。

回答(3 个)

Subhadeep Koley
Subhadeep Koley 2020-2-1
Refer the code below. Hope this helps!
function valid = validateDate(year, month, day)
year1 = year/4;
leap1 = year1 == round(year1); % Condition 1
year2 = year/100;
leap2 = year2 == round(year2); % Condition 2
year3 = year/400;
leap3 = year3 == round(year3); % Condition 3
if leap1
leapYear = 1;
elseif leap2
leapYear = 0;
elseif leap3
leapYear = 1;
else
leapYear = 0;
end
if isscalar(year) && year >= 1 && isscalar(month) && month >= 1 && month <= 12 && isscalar(day) && day >= 1 && day <= 31
if leapYear
if month == 2
valid = day <= 29;
elseif month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12
valid = day <= 31;
else
valid = day <= 30;
end
else
if month == 2
valid = day <= 28;
elseif month == 1 || month ==3 || month ==5 || month ==7 || month == 8 || month == 10 || month == 12
valid = day <= 31;
else
valid = day <= 30;
end
end
else
valid = false;
end
end

Ahmed BENTALEB
Ahmed BENTALEB 2023-5-10
编辑:Ahmed BENTALEB 2023-5-10
My solution, it passed all tests and for sure it could be optimized more. hope it helps
function valid = valid_date(year, month, day)
valid = false;
if checkinput(year) && checkinput(month) && checkinput(day)
if isleap(year)
valid = (month31(month) && day<=31) || (month30(month) && day<=30) || (month==2 && day<=29);
else
valid = (month31(month) && day<=31) || (month30(month) && day<=30) || (month==2 && day<=28);
end
end
% check correct input
function check = checkinput(in)
check = isscalar(in) && in >=1 && in == fix(in);
% check for leap year to handel month 2
function leap = isleap(y)
leap = (mod(y,4)==0 && ~mod(y,100)==0) || mod(y,400)==0;
% check for lmonths where days are 31 or less
function is31 = month31(month)
is31 = (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12);
% check for lmonths where days are 30 or less
function is30 = month30(month)
is30 = (month==4 || month==6 || month==9 || month==11);
Edit: another solution combined with the solution from cours
function valid = valid_date(y, m, d)
valid = false;
% check all input conditions one time
if checkinput(y) && (checkinput(m) && m<=12) && (checkinput(d) && d<=31)
% days of month in a year
daysInMonth = [31 28 31 30 31 30 31 31 30 31 30 31];
if isleap(y)
daysInMonth(2) = 29; % change month 2 days to 29
end
valid = d<=daysInMonth(m);
end
% check correct input
function check = checkinput(in)
check = isscalar(in) && in >=1 && in == fix(in);
% check for leap year to handel month 2
function leap = isleap(y)
leap = (mod(y,4)==0 && ~mod(y,100)==0) || mod(y,400)==0;

Paul
Paul 2023-5-15
编辑:DGM 2023-8-23
function valid = valid_date(y,m,d);
if ~isscalar(y)||~isscalar(m)||~isscalar(d)||y~=fix(y)||m~=fix(m)||d~=fix(d);
valid = false;
else
if y > 0 && (m == 12 || m == 1 || m == 3 || m == 5|| m == 7 || m == 8 || m == 10) && d >= 1 && d <= 31;
valid = true;
elseif (m== 4 || m == 6 || m ==9 || m == 11) && d >=1 && d <= 30;
valid = true;
elseif m==2 && d>=1 && d<=28;
valid = true;
elseif ((mod(y,4)==0 && mod(y,100)~=0)||mod(y,400)==0) && m==2 && d>=1 && d<=29;
valid = true;
else
valid = false;
end
end
end

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by