Guest User

Roman Numerals

a guest
Oct 11th, 2011
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class RomanNumerals {
  5.     public static void main(String[] args) throws IOException {
  6.         testRomanNumerals();
  7.         interactiveRomanNumerals();
  8.     }
  9.  
  10.     private static void interactiveRomanNumerals() throws IOException {
  11.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  12.         String input;
  13.         String output;
  14.         while ((input = br.readLine()) != null && input.length() > 0) {
  15.             System.out.println(toRomanNumerals(input));
  16.         }
  17.     }
  18.  
  19.     private static String toRomanNumerals(String input) {
  20.         String output;
  21.         StringBuffer data = new StringBuffer(input).reverse();
  22.         output = "";
  23.         for (int i = 0; i < data.length(); i++) {
  24.             output = getRomanNumeral(data.charAt(i), i) + output;
  25.         }
  26.         return output;
  27.     }
  28.  
  29.     private static String getRomanNumeral(char value, int placeIdx) {
  30.         String[][] romanChars = {
  31.                 {"I", "V", "X"},
  32.                 {"X", "L", "C"},
  33.                 {"C", "D", "M"},
  34.                 {"M", "?", "?"}
  35.         };
  36.         String one = romanChars[placeIdx][0];
  37.         String five = romanChars[placeIdx][1];
  38.         String ten = romanChars[placeIdx][2];
  39.        
  40.         switch (value - '0') {
  41.             case 1:
  42.                 return one;
  43.             case 2:
  44.                 return one + one;
  45.             case 3:
  46.                 return one + one + one;
  47.             case 4:
  48.                 return one + five;
  49.             case 5:
  50.                 return five;
  51.             case 6:
  52.                 return five + one;
  53.             case 7:
  54.                 return five + one + one;
  55.             case 8:
  56.                 return five + one + one + one;
  57.             case 9:
  58.                 return one + ten;
  59.             default:
  60.                 return "";
  61.         }
  62.     }
  63.  
  64.     private static void testRomanNumerals() {
  65.         testEquals("I", toRomanNumerals("1"));
  66.         testEquals("VI", toRomanNumerals("6"));
  67.         testEquals("XXXI", toRomanNumerals("31"));
  68.         testEquals("XXIV", toRomanNumerals("24"));
  69.         testEquals("L", toRomanNumerals("50"));
  70.         testEquals("CCCLXIX", toRomanNumerals("369"));
  71.         testEquals("CD", toRomanNumerals("400"));
  72.         testEquals("CDXLVIII", toRomanNumerals("448"));
  73.         testEquals("MCMXCVIII", toRomanNumerals("1998"));
  74.         testEquals("MMDCCLI", toRomanNumerals("2751"));
  75.         System.out.println("All test cases passed.");
  76.     }
  77.  
  78.     private static void testEquals(String left, String right) {
  79.         if (!left.equals(right))
  80.             throw new RuntimeException("Test failed. left != right (" + left + " != " + right + ")");
  81.     }
  82. }
  83.  
  84.  
Advertisement
Add Comment
Please, Sign In to add comment