Problem with straightforward problem - Can you help?
1 回表示 (過去 30 日間)
古いコメントを表示
My code is as follows. It doesn't return the right answer. For 1556 it returns "XI". Its supposed to return "XVI" for the century number for 1556. The code is straightforward, but it doesn't work. What is wrong? Is it a matter of function dominance and putting parentheses into the code to correct? The math hierarchy?
function [ out] = centuries( year )
%CENTURIES Summary of this function goes here
% Detailed explanation goes here
YEAR=num2str(year);
l = length(YEAR);
if l ==4
A = YEAR(1);
B = YEAR(2);
C=YEAR(3);
D=YEAR(4);
if A=='1' && B =='0' && C~=0 || D~=0
out ='XI';
return;
end
if A=='1' && B =='0' && C==0 && D==0
out ='X';
return;
end
if A=='1' && B =='1' && C~=0 || D~=0
out ='XII';
return;
end
if A=='1' && B =='1' && C==0 && D==0
out ='XI';
return;
end
if A=='1' && B =='2' && C~=0 || D~=0
out ='XIII';
return;
end
if A=='1' && B =='2' && C==0 && D==0
out ='XII';
return;
end
if A=='1' && B =='3' && C~=0 || D~=0
out ='XIII';
return;
end
if A=='1' && B =='3' && C==0 && D==0
out ='XII';
return;
end
if A=='1' && B =='4' && C~=0 || D~=0
out ='XV';
return;
end
if A=='1' && B =='4' && C==0 && D==0
out ='XIV';
return;
end
if A=='1' && B =='5' && C~=0 || D~=0
out ='XVI';
return;
end
if A=='1' && B =='5' && C==0 && D==0
out ='XV';
return;
end
if A=='1' && B =='6' && C~=0 || D~=0
out ='XVII';
return;
end
if A=='1' && B =='6' && C==0 && D==0
out ='XVI';
return;
end
if A=='1' && B =='7' && C~=0 || D~=0
out ='XVIII';
return;
end
if A=='1' && B =='7' && C==0 && D==0
out ='XVII';
return;
end
if A=='1' && B =='8' && C~=0 || D~=0
out ='XIX';
return;
end
if A=='1' && B =='8' && C==0 && D==0
out ='XVIII';
return;
end
if A=='1' && B =='9' && C~=0 || D~=0
out ='XX';
return;
end
if A=='1' && B =='9' && C==0 && D==0
out ='XVIII';
return;
end
if A=='2' && B =='0' && C~=0 || D~=0
out ='XXI';
return;
end
if A=='2' && B =='0' && C==0 && D==0
out ='XX';
return;
end
if A=='2' && B =='1' && C~=0 || D~=0
out ='XXII';
return;
end
if A=='2' && B =='1' && C==0 && D==0
out ='XXI';
return;
end
if A=='2' && B =='2' && C~=0 || D~=0
out ='XXIII';
return;
end
if A=='2' && B =='2' && C==0 && D==0
out ='XXII';
return;
end
if A=='2' && B =='3' && C~=0 || D~=0
out ='XXIV';
return;
end
if A=='2' && B =='3' && C==0 && D==0
out ='XXIII';
return;
end
if A=='2' && B =='4' && C~=0 || D~=0
out ='XXV';
return;
end
if A=='2' && B =='4' && C==0 && D==0
out ='XXIV';
return;
end
if A=='2' && B =='5' && C~=0 || D~=0
out ='XXVI';
return;
end
if A=='2' && B =='5' && C==0 && D==0
out ='XXIV';
return;
end
if A=='2' && B =='6' && C~=0 || D~=0
out ='XXVII';
return;
end
if A=='2' && B =='6' && C==0 && D==0
out ='XXVI';
return;
end
if A=='2' && B =='7' && C~=0 || D~=0
out ='XXVIII';
return;
end
if A=='2' && B =='7' && C==0 && D==0
out ='XXVI';
return;
end
if A=='2' && B =='8' && C~=0 || D~=0
out ='XXIX';
return;
end
if A=='2' && B =='8' && C==0 && D==0
out ='XXVIII';
return;
end
if A=='2' && B =='9' && C~=0 || D~=0
out ='XXX';
return;
end
if A=='2' && B =='9' && C==0 && D==0
out ='XXIX';
return;
end
if A=='3' && B =='0' && C~=0 || D~=0
out ='XXXI';
return;
end
if A=='3' && B =='0' && C==0 && D==0
out = 'XXIX';
return;
end
end
if l==3
A = YEAR(1);
B = YEAR(2);
C=YEAR(3);
if A =='1' && C ~='0' || B ~='0'
out='II';
return;
end
if A=='1' && C =='0' && B=='0'
out ='I';
return;
end
if A =='2' && C ~='0' || B ~='0'
out='III';
return;
end
if A=='2' && C =='0' && B=='0'
out ='II';
return;
end
if A =='3' && C ~='0' || B ~='0'
out='IV';
return;
end
if A=='3' && C =='0' && B=='0'
out ='III';
return;
end
if A =='4' && C ~='0' || B ~='0'
out='V';
return;
end
if A=='4' && C =='0' && B=='0'
out ='IV';
return;
end
if A =='5' && C ~='0' && B ~='0'
out='VI';
return;
end
if A=='5' && C =='0' && B=='0'
out ='V';
return;
end
if A =='6' && C ~='0' || B ~='0'
out='VII';
return;
end
if A=='6' && C =='0' && B=='0'
out ='VI';
return;
end
if A =='7' && C ~='0' || B ~='0'
out='IIX';
return;
end
if A =='7' && C =='0' && B =='0'
out='VII';
return;
end
if A=='8' && C ~='0' || B~='0'
out ='IX';
return;
end
if A=='8' && C =='0' && B=='0'
out ='VIII';
return;
end
if A=='9' && C ~='0' || B~='0'
out ='X';
return;
end
if A=='9' && C =='0' && B=='0'
out ='IX';
return;
end
end
if l <3
out ='I';
return;
end
end
2 件のコメント
Stephen23
2016 年 12 月 15 日
編集済み: Stephen23
2016 年 12 月 15 日
You have ignored the fact that all operators are evaluated in a specific, well defined order. The order is defined here:
Once you learn about operator precedence, then think about what you really want to happen when you write something like this:
if A=='1' && B =='0' && C~=0 || D~=0
Also note that your solution of writing an explicit line of code for every possible output and using a thousand if statements is highly inefficient to write, and is also liable to make the buggy. Exactly as per one of your earlier tasks
you should think about how to encode the patterns of the task and this will make your code much simpler and easier to write and check. Do some research.
回答 (1 件)
James Tursa
2016 年 12 月 15 日
編集済み: Walter Roberson
2016 年 12 月 15 日
Use parentheses to separate your operations from your && operations as necessary. E.g., this line
if A=='1' && B =='0' && C~=0 || D~=0
As written, since && is higher operator precedence than ||, this is equivalent to:
if ( A=='1' && B =='0' && C~=0) || D~=0
I am guessing you really want this logic:
if A=='1' && B =='0' && ( C~=0 || D~=0 )
Similarly for all the other lines as well depending on what you really want the logic to do.
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!