2014-09-28 156 views
1

爲了不誤導任何人,這是一個硬件問題,我正試圖完成並需要一點幫助。問題本身是不言自明的。我必須編寫一個函數,在其中輸入一個字符串'Month Day,Year'並讓它告訴我它是否有效(true或false語句)。我已經爲幾乎所有的事情工作,除了現在似乎沒有認識到我可能的日子的限制。檢查日期在MATLAB中是否有效

function[valid] = isValidDate(str) 
[date, year] = strtok(str, ','); %Should give me the date and year 
[~, year2] = strtok(year, ' '); 
[month, day] = strtok(date, ' '); %Should give me month and the day 
day = round(day); 
if length(date) < 6 
valid = false; 
elseif month(1) == upper(month(1)) 
    valid = true; 
elseif length(date) >= 12 
    valid = false; 
    end 
if year2 >= 0 
    valid = true; 
else 
    valid = false; 
end 
leapyear = mod(year, 400) == 0 | (mod(year, 4) == 0 ~= mod(year, 100) == 0); 

switch month 
    case {'September','April','June','November'} 

day <= 30; 
    valid = true; 
    case {'February'} 
     if leapyear 
      day <= 29; 
      valid = true; 
     else 
      day <= 28; 
      valid = true; 
     end 
    case {'January', 'March', 'May', 'July', 'August', 'October', 'December'} 
     days <= 31; 
     valid = true; 
otherwise 
    valid = false; 

end 
end 

所以基本上

valid4 = isValidDate('December 29.9, -1005.7') 
valid = false 

注:會有一天後,沒有後綴。我現在唯一的問題是,我的功能沒有意識到我的日子受到限制。它喜歡想'2014年2月30日'是可能的

+0

日期的字符串格式是什麼? – Marcin 2014-09-29 00:27:46

+0

「月,日年」如此'2012年1月29日' – 2014-09-29 02:24:19

+0

您能詳細說明''一天之後沒有後綴嗎?'?任何可以解釋您的意思的例子嗎? – Divakar 2014-09-29 04:10:41

回答

1

代碼

function[valid] = isValidDate(str) 

[date1, year1] = strtok(str, ','); %Should give me the date and year 
[month1, day1] = strtok(date1, ' '); %Should give me month and the day 

%// 1. Take care of bad month strings 
all_months = {'January', 'February','March', 'April', 'May','June',... 
    'July', 'August', 'September','October','November' 'December'} ; 
if ~ismember(cellstr(month1),all_months) 
    valid = false; 
    return; 
end 

%// 2. Take care of negative or fraction days 
day1 = day1(isstrprop(day1,'digit')); %// Take care of suffixes after day string 
num_day = str2double(day1); 
if round(num_day)~=num_day || num_day<1 
    valid = false; 
    return; 
end 

%// 3. Take care of fraction or negative years 
num_year = str2double(strtok(year1,',')); 
if round(num_year)~=num_year || num_year<0 
    valid = false; 
    return; 
end 

lpyr =mod(num_year, 400) == 0 | (mod(num_year, 4) == 0 ~= mod(num_year, 100) == 0); 

switch month1 
    case {'September','April','June','November'} 
     if num_day > 30 
      valid = false; 
      return; 
     end 
    case {'February'} 
     if (lpyr && num_day > 29) | (~lpyr && num_day > 28) 
      valid = false; 
      return; 
     end 
    case {'January', 'March', 'May', 'July', 'August', 'October', 'December'} 
     if num_day > 31; 
      valid = false; 
      return; 
     end 
end 
valid = true; %// We made it through! 

return; 

如果你希望有一個緊湊的代碼 -

function valid = isValidDate(str) 

[date1, year1] = strtok(str, ','); %Should give me the date and year 
[month1, day1] = strtok(date1, ' '); %Should give me month and the day 

%// 1. Take care of bad month strings 
all_months = {'January', 'February','March', 'April', 'May','June',... 
    'July', 'August', 'September','October','November' 'December'} ; 
valid_month = ismember(cellstr(month1),all_months); 

%// 2. Take care of negative or fraction days 
day1 = day1(isstrprop(day1,'digit')); %// Take care of suffixes after day string 
num_day = str2double(day1); 
valid_day = round(num_day)==num_day && num_day>=1; 

%// 3. Take care of fraction or negative years 
num_year = str2double(strtok(year1,',')); 
valid_year = round(num_year)==num_year && num_year>=0; 

%// 4. Take care of valid days based on leap year and days in a month limits 
lpyr = mod(num_year, 400) == 0 | (mod(num_year, 4) == 0 ~= mod(num_year, 100) == 0); 
valid_leapyear = true; 
switch month1 
    case {'September','April','June','November'} 
     valid_leapyear = num_day<=30; 
    case {'February'} 
     valid_leapyear = ~((lpyr && num_day>29) || (~lpyr && num_day>28)); 
    case {'January', 'March', 'May', 'July', 'August', 'October', 'December'} 
     valid_leapyear = num_day<=31; 
end 
valid = valid_year & valid_month & valid_day & valid_leapyear; 
return; 
+0

這確實有用。我並不完全理解返回函數的工作原理,但我可以通過搜索來找出答案。我想結束這些言論會爲我節省一些心痛和壓力。 – 2014-09-29 04:10:16

+1

@JessicaMarie函數中的'return'基本上意思是'結束函數,不處理函數代碼的其餘部分',從而在那裏保存一些心跳,這是CPU的心跳。 – Divakar 2014-09-29 04:12:51

+0

....那解釋了很多東西 – 2014-09-29 04:13:38

1

我做了一些簡單的功能,使用matlab中的java接口。希望它會有用。

function [valid] = isValidDate(dateStr) 
    valid = true; 
    dateFormat = java.text.SimpleDateFormat('MMM dd, yyyy'); 
    dateFormat.setLenient(false); 
    try 
     dateFormat.parse(dateStr); 
     valid = true; 
    catch err 
     valid = false; 
    end  
end 

例子:

isValidDate('December 21, 1934');  % gives 1 
isValidDate('December 29.9, -1005.7'); % gives 0 
+0

我不應該在技術上使用它,因爲我不知道什麼關於Java或任何工作:/但我感謝幫助:) – 2014-09-29 03:08:49

+0

啊我明白了。但在未來的使用情況下,matlab具有本機[Java支持](http://goo.gl/kZvkMh)。所以它非常有用經常。有時候沒有意義重新發明輪子。 – Marcin 2014-09-29 03:13:42

+0

我也可以運行Python,但我必須按照老師要求的方式學習它 – 2014-09-29 03:19:43