转换中文数字成为阿拉伯数字

package converter;

import java.util.HashMap;

//转换中文数字成为阿拉伯数字
public class chineseConverter {
    
    //万以下的单位
    static HashMap<Character, Integer> m_mapUnit = new HashMap<Character, Integer>(); 

    //万以上的单位
    static HashMap<Character, Integer> m_wUnit = new HashMap<Character, Integer>(); 

    //数字
    static HashMap<Character, Integer> m_mapNum = new HashMap<Character, Integer>(); 
    
    static{
        //万以下的单位
        m_mapUnit.put(Character.valueOf('十'), Integer.valueOf(1));
        m_mapUnit.put(Character.valueOf('拾'), Integer.valueOf(1));
        m_mapUnit.put(Character.valueOf('百'), Integer.valueOf(2));
        m_mapUnit.put(Character.valueOf('佰'), Integer.valueOf(2));
        m_mapUnit.put(Character.valueOf('千'), Integer.valueOf(3));
        m_mapUnit.put(Character.valueOf('仟'), Integer.valueOf(3));

        //万以上的单位
        m_wUnit.put(Character.valueOf('万'), Integer.valueOf(4));
        m_wUnit.put(Character.valueOf('亿'), Integer.valueOf(8));
        m_wUnit.put(Character.valueOf('兆'), Integer.valueOf(16));

        //数字
        m_mapNum.put(Character.valueOf('零'), Integer.valueOf(0));
        m_mapNum.put(Character.valueOf('○'), Integer.valueOf(0));
        m_mapNum.put(Character.valueOf('〇'), Integer.valueOf(0));
        m_mapNum.put(Character.valueOf('一'), Integer.valueOf(1));
        m_mapNum.put(Character.valueOf('二'), Integer.valueOf(2));
        m_mapNum.put(Character.valueOf('三'), Integer.valueOf(3));
        m_mapNum.put(Character.valueOf('四'), Integer.valueOf(4));
        m_mapNum.put(Character.valueOf('五'), Integer.valueOf(5));
        m_mapNum.put(Character.valueOf('六'), Integer.valueOf(6));
        m_mapNum.put(Character.valueOf('七'), Integer.valueOf(7));
        m_mapNum.put(Character.valueOf('八'), Integer.valueOf(8));
        m_mapNum.put(Character.valueOf('九'), Integer.valueOf(9));
        m_mapNum.put(Character.valueOf('壹'), Integer.valueOf(1));
        m_mapNum.put(Character.valueOf('贰'), Integer.valueOf(2));
        m_mapNum.put(Character.valueOf('叁'), Integer.valueOf(3));
        m_mapNum.put(Character.valueOf('肆'), Integer.valueOf(4));
        m_mapNum.put(Character.valueOf('伍'), Integer.valueOf(5));
        m_mapNum.put(Character.valueOf('陆'), Integer.valueOf(6));
        m_mapNum.put(Character.valueOf('柒'), Integer.valueOf(7));
        m_mapNum.put(Character.valueOf('捌'), Integer.valueOf(8));
        m_mapNum.put(Character.valueOf('玖'), Integer.valueOf(9));
    }
    
    static int end = 0; //下次开始接收的位置

    //接收一个万以下的小数字
    public static int getFigure(String input ,int offset){
        int figure = 0;
        char c = input.charAt(offset);
        Integer ret = m_mapNum.get(c);
        if(ret == null){
            return 0;
        }
        figure = ret;
        
        offset++;
        if(offset>=input.length()){
            end = offset;
            return figure;
        }
        c = input.charAt(offset);
        int unitLevel = 0;
        ret = m_mapUnit.get(c);
        
        if(ret!=null){
            offset++;
            unitLevel = ret;
        }
        
        figure *= Math.pow(10.0D, unitLevel);
        end = offset;
        return figure;
    }
    
    public static void main(String[] args){
        int figure = 0;
        String input = "九百一十四";
        for(int i=0;i<input.length();i = end){
            int unitNum = getFigure(input,i);
            //TODO 如果不是小数字,再判断是否万级单位
            figure += unitNum;
            //System.out.println(end + " "+ figure + " " +  unitNum);
        }
        
        System.out.println(figure);
    }
    

}

posted on 2012-09-27 09:54  雨渐渐  阅读(228)  评论(0编辑  收藏  举报

导航