how to make a function that convert roman number to arabic number

3 ビュー (過去 30 日間)
Muhammad Usman Saleem
Muhammad Usman Saleem 2015 年 6 月 2 日
コメント済み: Walter Roberson 2015 年 8 月 17 日
Hi everyone;
I am going to attempt that question:
"function called roman that takes a string representing an integer between 1 and 20 inclusive using Roman numerals and returns the Arabic equivalent as a number of type uint8. If the input is illegal or its value is larger than 20, roman returns 0 instead. The rules for Roman numerals can be found here: http://en.wikipedia.org/wiki/Roman_numerals. Use the definition at the beginning of the page under the “Reading Roman Numerals” heading. In order to have unambiguous one-to-one mapping from roman to Arabic numbers, consider only the shortest possible roman representation as legal. Therefore, only three consecutive symbols can be the same (IIII or VIIII are illegal, but IV and IX are fine), and a subtractive notation cannot be followed by an additive one using the same symbols making strange combinations, such as IXI for 10 or IXX for 19, illegal also."
i am using that code:
function myarabic=roman(n)
switch n
case 'I'
myarabic=1;
case 'II'
myarabic=2;
case 'III'
myarabic=3;
case 'IV'
myarabic=4;
case 'V'
myarabic=5;
case 'VI'
myarabic=6;
case 'VII'
myarabic=7;
case 'VIII'
myarabic=8;
case 'IX'
myarabic=9;
case 'X'
myarabic=10;
case 'XI'
myarabic=11;
case 'XII'
myarabic=12;
case 'XIII'
myarabic=13;
case 'XIV'
myarabic=14;
case 'XV'
myarabic=15;
case 'XVI'
myarabic=16;
case 'XVII'
myarabic=17;
case 'XVIII'
myarabic=18;
case 'XIX'
myarabic=19;
case 'XX'
myarabic=20;
otherwise
myarabic=0;
end
end
but when i test this code i got an error :
Feedback: Your function made an error for argument(s) 'I'
Your solution is _not_ correct.
Thanks in advance for assistance....
  1 件のコメント
Muhammad Usman Saleem
Muhammad Usman Saleem 2015 年 6 月 2 日
Guide me about my corrections. Very thanks in advance...

サインインしてコメントする。

採用された回答

Muhammad Usman Saleem
Muhammad Usman Saleem 2015 年 6 月 2 日
I find the error : we have to return uint8 arabic number in output..
  4 件のコメント
Christos Vyzantios
Christos Vyzantios 2015 年 6 月 8 日
No the problem still exists...The grader does not accept the solution and i dont know why...
Walter Roberson
Walter Roberson 2015 年 6 月 9 日
The code there does not return the numbers as uint8() numbers, and the code there does not return uint8(0) for invalid strings.

サインインしてコメントする。

その他の回答 (2 件)

Ingrid
Ingrid 2015 年 6 月 2 日
how do you test your code?
When I do
roman('I')
ans =
1
or
roman('XX')
ans =
20
the answer is as expected so it is not really clear what your problem is?
  1 件のコメント
Muhammad Usman Saleem
Muhammad Usman Saleem 2015 年 6 月 2 日
@ Ingrid thanks for contributions.. Actually i have grader which will run and test whether my program makes sense or not? I have posted complete question above. Please read this and check whether i am doing correct or not?

サインインしてコメントする。


Tai Kieu
Tai Kieu 2015 年 8 月 16 日
Can you explain more about making the ouput uint8? I don't really quite understand about it.

カテゴリ

Help Center および File ExchangeCall MATLAB from Java についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by