Guest User

Roman Numeral Complete

a guest
Oct 12th, 2011
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. import java.util.Map;
  2. import java.util.SortedMap;
  3. import java.util.TreeMap;
  4.  
  5. /**
  6.  * @author deliveryninja
  7.  *
  8.  * Given a numeral from 1 to 3000 this will return the Roman
  9.  * numberal equivalent.
  10.  *  
  11.  * @param numeral
  12.  * @return
  13.  */
  14. public class Roman {
  15.    
  16.     public enum RomanNumeral{
  17.         M(1000),
  18.         CM(900),
  19.         D(500),
  20.         CD(400),
  21.         C(100),
  22.         XC(90),
  23.         L(50),
  24.         XL(40),
  25.         X(10),
  26.         IX(9),
  27.         V(5),
  28.         IV(4),
  29.         I(1);
  30.        
  31.         private int value;
  32.        
  33.         RomanNumeral(int value){
  34.             this.value = value;
  35.         }
  36.  
  37.         public int getIntegerValue() {
  38.             return value;
  39.         }      
  40.     }
  41.    
  42.     public String romanize(int numeral) {
  43.         StringBuffer result = new StringBuffer();
  44.         for(RomanNumeral romanNumeral : RomanNumeral.values()){
  45.             while(numeral >= romanNumeral.getIntegerValue()){
  46.                 result.append(romanNumeral);
  47.                 numeral -= romanNumeral.getIntegerValue();
  48.             }
  49.         }
  50.         return result.toString();
  51.     }
  52.  
  53.     public int convertToNumeral(String input) {
  54.         int result = 0;
  55.        
  56.         //Loop left to right across the input number
  57.         for(int index = 0; index < input.length() ; index++){
  58.             //Check the current value with all known roman values
  59.             for(RomanNumeral romanNumeral : RomanNumeral.values()){
  60.                 //Check if the next value is 1 or 2 characters long
  61.                 int adjustedIndex = calculateIndexPosition(romanNumeral, index+1, input.length());
  62.                 //Compare the input and current roman numeral
  63.                 if(input.substring(index, adjustedIndex).equals(romanNumeral.toString())){
  64.                     result += romanNumeral.getIntegerValue(); //Increment the result
  65.                     //Increment the index so we don't re-evaluate the same characters
  66.                     index = adjustedIndex - 1;
  67.                 }
  68.             }
  69.         }
  70.         return result;
  71.     }
  72.    
  73.     private int calculateIndexPosition(RomanNumeral romanNumeral, int index, int inputLength){
  74.         //Check if the length is larger than 1 and there is more than 1 character in the input
  75.         if(romanNumeral.toString().length() > 1 && index < inputLength){
  76.                 index++; //Increment index to check 2 characters
  77.         }
  78.         return index;
  79.     }
  80. }
  81.  
  82.  
Advertisement
Add Comment
Please, Sign In to add comment