`
cheer_nice
  • 浏览: 98827 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

罗马数字转成阿拉伯数字

    博客分类:
  • j2se
阅读更多

首先得知道罗马数字是怎么回事:

http://520920.blog.51cto.com/126264/114533

 

得验证 输入的字符串 是不是 规范的罗马数字

http://hi.baidu.com/dryg/blog/item/58fb7df33357b7c80b46e0f3.html

 

然后就是转换了 

 

http://ralf0131.blogbus.com/logs/38528077.html

 

唉,对正则表达式忘完了 得复习复习 

 

然后写了 java的 记在这里 以后看看...

 

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Convert {

	/**首先是检查输入字符串是否是一个合法的罗马数字,
	 * 使用正则表达式来实现,
	 * 
	 * 接下来就是分别获得罗马数字的千位、百位、十位和个位数字,
	 * 并将它转化为阿拉伯数字,
	 * 最后相加即可。
	 *  I=1|V=5|X=10|L=50|C=100|D=500|M=1000
	 */
	private static boolean isValid(String romaNum) {
		
		//String matchStr = "^IVLCDM";
		String matchStr = "^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$";
		boolean isRoma = romaNum.matches(matchStr);
		return isRoma;
	}
	

	private static int toArabic(String romaNum) {
		int result=0;//返回的结果
		
		Map<String , Integer> hundredsMap = new HashMap<String,Integer>();//百位map
		Map<String , Integer> tensMap = new HashMap<String,Integer>();//十位map
		Map<String , Integer> bitsMap = new HashMap<String,Integer>();//个位map
		
		String[] str1 = "C=100,CC=200,CCC=300,CD=400,D=500,DC=600,DCC=700,DCCC=800,CM=900".split(","); 
	    for(String split : str1) { 
		    String[] pair = split.split("="); 
		    hundredsMap.put(pair[0], Integer.parseInt(pair[1])); 
	    }
	    
	    String[] str2 = "X=10,XX=20,XXX=30,XL=40,L=50,LX=60,LXX=70,LXXX=80,XC=90".split(","); 
	    for(String split : str2) { 
		    String[] pair = split.split("="); 
		    tensMap.put(pair[0], Integer.parseInt(pair[1])); 
	    }
	    
	    String[] str3 = "I=1,II=2,III=3,IV=4,V=5,VI=6,VII=7,VIII=8,IX=9".split(","); 
	    for(String split : str3) { 
		    String[] pair = split.split("="); 
		    bitsMap.put(pair[0], Integer.parseInt(pair[1])); 
	    }
		 
	    
	    Pattern thousandsPattern = Pattern.compile("^M{0,3}");
	    Pattern hundredsPattern = Pattern.compile("CM|CD|D?C{0,3}");
	    Pattern tensPattern = Pattern.compile("XC|XL|L?X{0,3}");
	    Pattern bitsPattern = Pattern.compile("IX|IV|V?I{0,3}$");
	    
	    Matcher match1 = thousandsPattern.matcher(romaNum);
	    match1.find();
	    if(!match1.group().equals(""))//千位有多少个M 就成相应的1000被
	    	result = match1.group().length()*1000;
	   
	    /*
	     * 下面 百位 十位 个位 分别按照各自的map去匹配  如果有就加上相应的value
	     */
	    Matcher match2 = hundredsPattern.matcher(romaNum.substring(match1.end()));
	    match2.find();
	    if(!match2.group().equals(""))
	    	result = result + hundredsMap.get(match2.group());
	    
	    Matcher match3 = tensPattern.matcher(romaNum.substring(match2.end()+match1.end()));
	    match3.find();
	    
	    if(!match3.group().equals(""))
	    {	
	    	result = result + tensMap.get(match3.group());
	    }
	    Matcher match4 = bitsPattern.matcher(romaNum.substring(match2.end()+match1.end()+match3.end()));
	    match4.find();
	    if(!match4.group().equals(""))
	    	result = result + bitsMap.get(match4.group());

		return result;
	}
	
	
	
	public static void main(String[] args) {
		while(true)
		{
			//MMMDCCCLXXXVIII 
			System.out.println("请输入罗马数字:");
			Scanner scan=new Scanner(System.in); 
		    String romaNum = scan.nextLine().trim(); 
		    
		    if(!isValid(romaNum))
		    {
		    	System.out.println("输入无效,请重新输入");
		    	continue;
		    
		    }else
		    {
		    	System.out.print("对应的阿拉伯数字是:");
		    	System.out.println(toArabic(romaNum));
		    	break;
		    }
		}
	}
}
 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics