Problem with straightforward problem - Can you help?

조회 수: 1 (최근 30일)
DJ V
DJ V 2016년 12월 15일
댓글: DJ V 2016년 12월 16일
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
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.
DJ V
DJ V 2016년 12월 16일
Ahh, but I'm done, and the code works and is found to be correct by the software that checks it.

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

답변 (1개)

James Tursa
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.
  댓글 수: 2
DJ V
DJ V 2016년 12월 15일
I also need quotation marks around the zeroes....
James Tursa
James Tursa 2016년 12월 15일
Yes ... that too!

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by