Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-18-2008, 03:41 AM
Member
 
Join Date: Nov 2008
Posts: 50
Rep Power: 0
matzahboy is on a distinguished road
Default Convert roman numerals
I made a program to simplify roman numerals (projecteuler.net/index.php?section=problems&id=89]Problem 89 - Project Euler). I think that my method works, but I'm not sure if it is the most efficient method. Is there a way to shorten it and improve the time it takes to run it? Here's my code:

Code:
/*
 Converts Roman Numerals
 */
 
 import java.io.*;
import java.util.Scanner;

public class Problem_89 {
    
    
    public static String westernToRoman(int western) {
    	int level=7;
    	String roman = "";
    	while(level>0) {
    		if(western - levelToValue(level)>=0) {
    			roman += String.valueOf(levelToNumeral(level));
    			western -= levelToValue(level);
    		} else {
    			//Check to see if you should use subtraction
    			int subtractorLevel = (int)Math.floor(level/2)*2-1;
    			if(subtractorLevel>0 && western-(levelToValue(level)-levelToValue(subtractorLevel))>=0) {
    				western -= (levelToValue(level)-levelToValue(subtractorLevel));
    				roman = roman + levelToNumeral(subtractorLevel) + levelToNumeral(level);
    			}
    			level--;
    				
    		}
    	}
    	return roman;
    }
    
    public static int romanToWestern(String roman) {
    	
    	int western = 0; //the numerical version
    	char currentChar;
    	char nextChar;
    	
    	int i=0;
    	
    	while(i<roman.length()) {
    		currentChar = roman.charAt(i);
    		if(i<roman.length()-1) {
    			nextChar = roman.charAt(i+1);
    			if(getValue(currentChar)<getValue(nextChar)) {
    				western += (getValue(nextChar) - getValue(currentChar));
    				i+=2;
    			} else {
    				western += getValue(currentChar);
    				i++;
    			}
    		} else {
    			western += getValue(currentChar);
    			i++;
    		}
    	}
    	return western;
    	
    }
    
    private static int getValue(char l) { //Converts the numeral to a number
    	String letter = String.valueOf(l);
    	if(letter.equalsIgnoreCase("I")) return 1;
    	if(letter.equalsIgnoreCase("V")) return 5;
    	if(letter.equalsIgnoreCase("X")) return 10;
    	if(letter.equalsIgnoreCase("L")) return 50;
    	if(letter.equalsIgnoreCase("C")) return 100;
    	if(letter.equalsIgnoreCase("D")) return 500;
    	if(letter.equalsIgnoreCase("M")) return 1000;
    	return 0;
    }
    
    private static char levelToNumeral(int level) {
    	String character;
    	if(level == 1)
    		character = "I";
    	else if(level == 2)
    		character = "V";
    	else if(level == 3)
    		character = "X";
    	else if(level == 4)
    		character = "L";
    	else if(level == 5)
    		character = "C";
    	else if(level == 6)
    		character = "D";
    	else if (level == 7)
    		character = "M";
    	else
    		character = "E";
    	return character.charAt(0);
    }
    
    private static int levelToValue(int level) {
    	if(level == 1)
    		return 1;
    	if(level == 2)
    		return 5;
    	if(level == 3)
    		return 10;
    	if(level == 4)
    		return 50;
    	if(level == 5)
    		return 100;
    	if(level == 6)
    		return 500;
    	if(level == 7)
    		return 1000;
    	//error
    	System.out.println("Error in the getLevel sub. level = " + level);
    	return 1;
    }
    
    public static void main(String[] args) throws FileNotFoundException  {
    	
    	String[] numerals = new String[1000];
    	int charactersBefore=0;
    	int charactersAfter=0;
    	
    	//Read the txt file:
    	File fFile = new File("C:\\roman.txt");
    	Scanner scanner = new Scanner(fFile);
    	int i=0;
    	while ( scanner.hasNextLine() ){
        	numerals[i] = scanner.nextLine();
        	i++;
    	}
    	
    	String simplified;
    	
    	//Calculate "charactersBefore" and then simplify the numerals
    	for (i=0; i<1000; i++) {
    		charactersBefore += numerals[i].length();
    		int western = Problem_89.romanToWestern(numerals[i]);
    		String roman = Problem_89.westernToRoman(western);
    		charactersAfter += roman.length();
    		System.out.println(numerals[i] + " becomes " + western + " which becomes " + roman);
    	}
    	
    	System.out.println("We gained " + (charactersBefore - charactersAfter) + " characters");
    }
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-18-2008, 03:50 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 3,195
Rep Power: 5
Fubarable is on a distinguished road
Default
The "best" way is the way that 1) works for you, 2) is reliable and testable, and 3) is understandable to you and to others.

so there are many best and efficient ways to do this. One way is like so:
Code:
public enum Roman
{
  M(1000), CM(900), D(500), CD(400), C(100), 
  XC(90), L(50), XL(40), X(10), IX(9), V(5), 
  IV(4), I(1);

  private int number;

  private Roman(int number)
  {
    this.number = number;
  }

  public int getNumber()
  {
    return number;
  }

  public static String toRoman(int arabicInt)
  {
    StringBuilder result = new StringBuilder();
    for (int i = 0; i < values().length; i++)
    {
      int termCount = arabicInt / values()[i].getNumber();
      for (int j = 0; j < termCount; j++)
      {
        result.append(values()[i].toString());
      }
      arabicInt %= values()[i].getNumber();
    }
    return result.toString();
  }

}
Code:
public class TestRoman
{
  public static void main(String[] args)
  {
    System.out.println(Roman.toRoman(1975));
    for (int i = 0; i < 400; i++)
    {
      // note empty string "" if int is 0
      System.out.println(Roman.toRoman(i));
    }

  }
}

Last edited by Fubarable; 11-18-2008 at 04:05 AM.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
convert XML using XSL rajjan4u XML 2 03-30-2009 01:25 AM
Convert to Applet Urgle New To Java 1 11-12-2008 01:15 PM
Convert the application from 9i to 10g rahul999 Database 5 09-03-2008 03:55 PM
how to convert from BigInteger to Hex nanaji Advanced Java 10 05-22-2008 12:44 PM
Convert roman numerals to integers Felissa Advanced Java 2 07-01-2007 11:27 PM


All times are GMT +2. The time now is 03:10 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org