Guest User

Roman Numerals II

a guest
Oct 11th, 2011
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.98 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class RomanNumerals {
  5.     // Holds expanded roman character representations of each digit, for the first four places
  6.     static String[][] lookupTable = new String[4][10];
  7.  
  8.     static void initLookupTable() {
  9.         // Roman representation of One, Five, Ten for each place
  10.         String[][] romanChars = {
  11.             {"I", "V", "X"},  // ---X ones
  12.             {"X", "L", "C"},  // --X- tens
  13.             {"C", "D", "M"},  // -X-- hundreds
  14.             {"M", "?", "?"}   // X--- thousands
  15.         };
  16.  
  17.         // Compute lookup table for each of 4 places (ones, tens, hundreds, thousands)
  18.         for (int i = 0; i < 4; i++) {
  19.             String one = romanChars[i][0];
  20.             String five = romanChars[i][1];
  21.             String ten = romanChars[i][2];
  22.  
  23.             lookupTable[i][0] = "";
  24.             lookupTable[i][1] = one;
  25.             lookupTable[i][2] = one + one;
  26.             lookupTable[i][3] = one + one + one;
  27.             lookupTable[i][4] = one + five;
  28.             lookupTable[i][5] = five;
  29.             lookupTable[i][6] = five + one;
  30.             lookupTable[i][7] = five + one + one;
  31.             lookupTable[i][8] = five + one + one + one;
  32.             lookupTable[i][9] = one + ten;
  33.         }
  34.     }
  35.  
  36.     // Initialize the lookup table when the class loads
  37.     static { initLookupTable(); }
  38.  
  39.     //  Main; run tests, then interactively convert numerals
  40.     public static void main(String[] args) throws IOException {
  41.         testRomanNumerals();
  42.         interactiveRomanNumerals();
  43.     }
  44.  
  45.     // Read numbers from stdin, output roman numeral representation of input to stdout
  46.     private static void interactiveRomanNumerals() throws IOException {
  47.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  48.         String input;
  49.         String output;
  50.         while ((input = br.readLine()) != null && input.length() > 0) {
  51.             System.out.println(toRomanNumerals(input));
  52.         }
  53.     }
  54.  
  55.     /**
  56.      * Converts a number, as an input string, into the roman numeral representation
  57.      * @param input a 0-4 digit number, represented as a string
  58.      * @return the roman numeral representation of the input
  59.      */
  60.     private static String toRomanNumerals(String input) {
  61.         String output;
  62.     // Reverse string order, it's easier to think of the "places" this way. index 0 == ones, 1 == tens, etc.
  63.         StringBuffer data = new StringBuffer(input).reverse();
  64.         output = "";
  65.         for (int i = 0; i < data.length(); i++) {
  66.             // For each char in the input, get the Roman representation, prepend it to the output string (to reverse the order again)
  67.             output = getRomanNumeral(data.charAt(i), i) + output;
  68.         }
  69.         return output;
  70.     }
  71.  
  72.     /**
  73.      * @param value one digit of a number, as a character (eg: '1')
  74.      * @param placeIdx the digit's "place" (0 == ones, 1 == tens, 2 == hundreds, 3 == thousands)
  75.      * @return the roman numeral representation of the one input digit
  76.      */
  77.     private static String getRomanNumeral(char value, int placeIdx) {
  78.         return lookupTable[placeIdx][value - '0'];
  79.     }
  80.  
  81.     private static void testRomanNumerals() {
  82.         testEquals("I", toRomanNumerals("1"));
  83.         testEquals("VI", toRomanNumerals("6"));
  84.         testEquals("XXXI", toRomanNumerals("31"));
  85.         testEquals("XXIV", toRomanNumerals("24"));
  86.         testEquals("L", toRomanNumerals("50"));
  87.         testEquals("CCCLXIX", toRomanNumerals("369"));
  88.         testEquals("CD", toRomanNumerals("400"));
  89.         testEquals("CDXLVIII", toRomanNumerals("448"));
  90.         testEquals("MCMXCVIII", toRomanNumerals("1998"));
  91.         testEquals("MMDCCLI", toRomanNumerals("2751"));
  92.         System.out.println("All test cases passed.");
  93.     }
  94.  
  95.     private static void testEquals(String left, String right) {
  96.         if (!left.equals(right))
  97.             throw new RuntimeException("Test failed. left != right (" + left + " != " + right + ")");
  98.     }
  99. }
  100.  
  101.  
Advertisement
Add Comment
Please, Sign In to add comment