Borrar filtros
Borrar filtros

*** 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

5 visualizaciones (últimos 30 días)
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.

Respuestas (1)

Stephen23
Stephen23 el 15 de Jun. de 2015
Editada: Stephen23 el 15 de Jun. de 2015
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...

Categorías

Más información sobre Data Types en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by