Conversion of Roman numerals to Arabic numerals (Java edition, more comprehensive)

Source: Internet
Author: User

I. Preface

My friend wanted to find a change on the Internet. However, after google and baidu, either the Java version is not found, or the considerations are not comprehensive enough. Therefore, I wrote it myself. If you have not fully considered it, I hope you can point it out. Next, go to the topic.

 

Ii. Rome digital rules

 

The number of Roman numerals indicates zero.

The basic method to represent numbers: Except for I, X, and C, after a large number, they are used as the Addons and before a large number as the subtraction. Generally, they write several basic Roman numbers together, it represents the sum of numbers.

1. Rewrite: III (3) XX (20) CC (200)

2. Left subtraction: IX (9) XL (40) CD (400)

3. Add right: VII (7) XI (11) LX (60)

4. Synthesis of the first three methods: XLV (L-X + V, 45) LXII (L + X + I, 62)

If you add a horizontal line above the number, the number is increased by 1000 times. (This rule is not implemented in the program because it is input)

Note:

1. I, X, and C cannot exceed three consecutive values on the right side of a large number (that is, when they are added together). Only one value can be used on the left side of a large number (that is, when the values are subtracted.

2. V, L, and D cannot be used on the left side of a large number (subtraction), but only on the right side of a large number (addition.

3. The left decimal places of V and X are only I; the left decimal places of L and C are only X; the left decimal places of D and m are only C.

 

Iii. Source Code

There are three classes: Roman, romanconverter, and romanexception.

Roman class: analyzes whether the input conforms to the rules of the Roman numerals, and converts the roman numerals to Arabic numerals. The Code is as follows:

Public class Roman <br/>{</p> <p> private final int num; // Arabic numerals (decimal) after conversion) </p> <p>/* <br/> private static int [] numbers = {1000,900,500,400,100, 90, <br/> 50, 40, 10, 9, 5, 4, 1 }; </p> <p> private static String [] letters = {"M", "CM", "D", "CD", "C ", "XC", <br/> "L", "XL", "X", "IX", "V", "IV", "I "}; <br/> */<br/> public Roman (String roman) throws RomanException <br/> {</p> <p> if (roman. le Ngth () = 0) // The input is null <br/>{< br/> throw new RomanException ("null character cannot be entered "); <br/>}</p> <p> roman = roman. toUpperCase (); // all Roman numerals are converted to uppercase </p> <p> int I = 0; // record the location of each character in the roman numerals <br/> int arabic = 0; // The converted arabic numerals </p> <p> while (I <roman. length () <br/>{< br/> char letter = roman. charAt (I); // character of the current position of the Roman numerals <br/> int number = letterToNumber (letter ); // convert the character to an Arabic number </p> <p> if (number <0) <br/>{< br/> throw new Roma NException ("excluding Roman numerals" + letter); <br/>}</p> <p> I ++; // move to the next position of the string </p> <p> if (I = roman. length () // The roman numerals have been processed <br/>{< br/> arabic + = number; <br/>}< br/> else <br/> {<br/> char nextLetter = roman. charAt (I); <br/> int nextNumber = letterToNumber (nextLetter); </p> <p> if (nextNumber> number) // The character at the backend is greater than that at the front end. <br/>{< br/> int result = nextNumber-number; </p> <p> if (result = 4 | result = 9 | result = 40 | result = 90 | result = 400 | result = 900) <br/>{< br/> arabic + = result; <br/> I ++; </p> <p> if (I = roman. length () // The roman numerals have been processed <br/>{< br/> break; <br/>}< br/> else <br/> {<br/> char afterNextLetter = roman. charAt (I); <br/> int afterNextNumber = letterToNumber (afterNextLetter); </p> <p> if (afterNextNumber> result) <br/>{< br/> throw new RomanException ("invalid roman numerals" + letter + nextLett Er + afterNextLetter ); <br/>}< br/> else <br/>{< br/> throw new RomanException ("invalid roman numerals" + letter + nextLetter ); <br/>}< br/> else <br/> {<br/> if (number = 5 | number = 50 | number = 500) & number = nextNumber) // V, L, and D are used on the right side of a large number (sum up). They are used more than once. <Br/>{< br/> throw new RomanException ("invalid roman numerals" + letter + nextLetter ); <br/>}</p> <p> if (number = nextNumber) <br/>{< br/> I ++; // check the next character again. </p> <p> if (I = roman. length () // The roman numerals have been processed <br/>{< br/> arabic + = number + nextNumber; <br/> break; <br/>}</p> <p> char afterNextLetter = roman. charAt (I); <br/> int afterNextNumber = letterToNumber (afterNextLetter); </p> <p> if (afterNextNumber> nextNumber) // I, X, and C are on the left side of the large number (that is, the subtraction time) use more than 2 <br/>{< br/> throw new RomanException ("invalid roman numerals" + letter + nextLetter + afterNextLetter ); <br/>}< br/> else if (afterNextNumber = nextNumber) // when all three characters are the same, for example, III <br/>{< br/> I ++; // you need to check the next character again. IIII may occur. (if this is not allowed, an exception should be thrown) </p> <p> if (I = roman. length () // The roman numerals have been processed <br/>{< br/> arabic + = number + nextNumber + afterNextNumber; <br/> break; <br/>}</p> <p> char afterNextNextLetter = roman. charAt (I); <br/> int afterNextNextNumber = letterToNumber (afterNextNextLetter); </p> <p> if (afterNextNextNumber = afterNextNumber) // IIII <br/> {<br/> throw new RomanException ("invalid roman numerals" + letter + nextLetter + afterNextLetter ); <br/>}< br/> else <br/> {<br/> arabic + = number; <br/> I = I-2; // roll back 2 characters (because 4 characters are considered) <br/>}< br/> else <br/> {<br/> arabic + = number + nextNumber; <br/>}< br/> else <br/> {<br/> arabic + = number; <br/>}</p> <p> if (arabic> 3999) <br/>{< br/> throw new RomanException ("the number cannot exceed 3999"); <br/>}</p> <p> num = arabic; </p> <p >}</p> <p>/** <br/> * convert Rome to Arabic numerals <br/> * @ param letter <br/> * @ return: normal Roman character, returns an Arabic number. Otherwise,-1 <br/> */<br/> private int letterToNumber (char letter) <br/>{< br/> switch (letter) <br/>{< br/> case 'I': return 1; <br/> case 'V': return 5; <br/> case 'X ': return 10; <br/> case 'l': return 50; <br/> case 'C': return 100; <br/> case 'D': return 500; <br/> case 'M': return 1000; <br/> default: return-1; <br/>}</p> <p> public int getNum () <br/>{< br/> return num; <br/>}</p> <p>

Romanconverter class -- main function entry class, used to test the conversion of Roman numerals. The Code is as follows:

Import java. util. topology; </p> <p> public class RomanConverter <br/> {<br/> public static void main (String [] args) <br/>{< br/> while (true) <br/>{< br/> System. out. println (); <br/> System. out. print ("Enter the roman numerals (Press Q to exit):"); </p> <p> export cin = new partition (System. in); <br/> String romanStr = cin. nextLine (); </p> <p> if (romanStr. equals ("q") | romanStr. equals ("Q") // exit the loop <br/>{< br/> break; <br/>}< br/> Roman roman = null; <br/> try <br/>{< br/> roman = new Roman (romanStr); <br/> System. out. println ("the converted Arabic numerals are:" + roman. getNum (); <br/>}< br/> catch (RomanException e) <br/>{< br/> System. out. println (e); <br/>}< br/> System. out. println ("welcome to use ^-^" again); <br/>}</p> <p>

Romanexception class-exception handling class. The Code is as follows:

Import java. util. topology; </p> <p> public class RomanConverter <br/> {<br/> public static void main (String [] args) <br/>{< br/> while (true) <br/>{< br/> System. out. println (); <br/> System. out. print ("Enter the roman numerals (Press Q to exit):"); </p> <p> export cin = new partition (System. in); <br/> String romanStr = cin. nextLine (); </p> <p> if (romanStr. equals ("q") | romanStr. equals ("Q") // exit the loop <br/>{< br/> break; <br/>}< br/> Roman roman = null; <br/> try <br/>{< br/> roman = new Roman (romanStr); <br/> System. out. println ("the converted Arabic numerals are:" + roman. getNum (); <br/>}< br/> catch (RomanException e) <br/>{< br/> System. out. println (e); <br/>}< br/> System. out. println ("welcome to use ^-^" again); <br/>}</p> <p>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.