*** Write a function called roman2 that takes a string input representing an integer between 1 and 399 inclusive using Roman numerals and returns the Arabic equivalent as a uint16. If the input is illegal, or its value is larger than 399, roman2 retu
조회 수: 2 (최근 30일)
이전 댓글 표시
function num = roman(rom)
romans = { 'I' 'II' 'III' 'IV' 'V' 'VI' 'VII' 'VIII' 'IX' 'X' ...
'XI' 'XII' 'XIII' 'XIV' 'XV' 'XVI' 'XVII' 'XVIII' 'XIX' 'XX' 'XXX'...
'XL' 'L' 'LX' 'LXX' 'LXXX' 'XC' 'C' 'CX' 'CXX' 'CXX' 'CXL' 'CL' 'CLX' ...
'CLXX' 'CLXXX' 'CXC' 'CC' 'CCX' 'CCXX' 'CCXXX' 'CCXL' 'CCL' 'CCLX' ...
'CCLXX' 'CCLXXX' 'CCXC' 'CCC' 'CCCX' 'CCCXX' 'CCCXXX' 'CCCXL' 'CCCL' ...
'CCCLX' 'CCCLXX' 'CCCLXXX' 'CCCXC' 'CCCXCIX' 'CD' };
num = uint16(0);
for ii = 1:399
if strcmp(rom,romans{ii})
num = uint16(ii);
break
end
end
end
error
Feedback: Your function performed correctly for argument(s) 'I'
Feedback: Your function performed correctly for argument(s) 'V'
Feedback: Your function performed correctly for argument(s) 'X'
Feedback: Your function made an error for argument(s) 'C'
Your solution is _not_ correct.
댓글 수: 0
답변 (1개)
Stephen23
2015년 6월 15일
편집: Stephen23
2015년 6월 15일
Your algorithm could work, but it is not yet complete...
If you want to get the numeric value for any of the 399 required possible inputs, then your look-up cell array romans will have to have (atleast) 399 elements in it. Currently it has 59 elements in it, and the string 'C' is only in position 28... so when it (correctly!) matches 'C' it returns that value: 28. Where should 'C' be located to get the correct output?
While you could extend this cell array to contains all 399 elements, this would not be the easiest to program and is quite likely to be buggy: how would you fill in all the other strings? By hand?
Perhaps instead you should consider the meaning of the letters, and how they are arranged...
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!