Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert Roman numbers to Arabic

Status
Not open for further replies.

mutual

Programmer
Jun 8, 2000
6
US
I want to write a program that converts Roman numbers to Arabic from 1 to 3000 inclusive.<br>class RomanTest {&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String args[] ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int i=0,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j=Integer.parseInt(args[0]);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String s;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(i=0; i&lt;=j; i+=1)&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s = Roman.AtoR(i);<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;System.out.println(i + &quot;: &quot; + s +&nbsp;&nbsp;&quot; == &quot; + Roman.RtoA(s));&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;}<br><br>How to construct Roman class which should have RtoA function and vice versa ?<br>
 
for Roman -&gt; Arabic...<br><br>you want to parse it as a string. Have a method that returns an int value based on a single character....<br><br>static int convert(char c) throws IllegalNumberException {<br>&nbsp;&nbsp;int i = 0;<br>&nbsp;&nbsp;switch (c) {<br>&nbsp;&nbsp;&nbsp;&nbsp;case 'M': i = 1000; break;<br>&nbsp;&nbsp;&nbsp;&nbsp;case 'L': i = 500;&nbsp;&nbsp;break;<br>&nbsp;&nbsp;&nbsp;&nbsp;default : throw new IllegalNumberException(&quot;bad input, blah blah blah..&quot;); break;<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;return i;<br>}<br><br>Something like that... IllegalNumberException is just a made-up exception, it may or may not work.<br><br>Then have some kind of algorithm for taking care of the order... like <FONT FACE=monospace>j = i + 1; ((a =convert(yourString.charAt(i))) &gt; (b = convert(yourString.charAt(j))) ? result = a + b : result = a - b;</font><br><br>that's kind of cryptic... I'm not sure if the code is completely bug free, either; but it should be a start. If it doesn't make sense to you, let me know.<br><br>and for Arabic -&gt; Roman...<br>divide the number by 1000. If you get a number greater than 0, that's your number of M's. Then take the modulus for remainder and multiply that by 1000... divide that by 500, that's your L's, etc... If at any point you realize you have 4 of a particular letter, change it to one of that particular letter followed by one of the particular letter directly above. (Like if you have four I's, change it to one I followed by one V.) <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top