中文数字转阿拉伯数字

自己写得一个中文数字转阿拉伯数字代码,模拟写的,如果有badcase欢迎大家拍砖。

package org.x.cloud.rewrite;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class DigitalUtil {

    private String regex_al = null;
    private Pattern pattern_al = null;
    private String regex_ch = null;
    private Pattern pattern_ch = null;

    private HashMap<Character, Integer> count = new HashMap<Character, Integer>() {
        {
            put('零', 0);
            put('一', 1);
            put('壹', 1);
            put('二', 2);
            put('贰', 2);
            put('三', 3);
            put('叁', 3);
            put('四', 4);
            put('肆', 4);
            put('五', 5);
            put('伍', 5);
            put('六', 6);
            put('陆', 6);
            put('七', 7);
            put('柒', 7);
            put('八', 8);
            put('捌', 8);
            put('九', 9);
            put('玖', 9);
            put('十', 10);
            put('拾', 10);
            put('百', 100);
            put('佰', 100);
            put('千', 1000);
            put('仟', 1000);
        }
    };
    private HashMap<Character, Integer> unit = new HashMap<Character, Integer>() {
        {
            put('万', 10000);
            put('萬', 10000);
            put('亿', 100000000);
        }
    };

    public DigitalUtil() {
        regex_al = "^[.0-9]+$";
        pattern_al = Pattern.compile(regex_al);
        regex_ch = "^[零一壹二贰两三叁四肆五伍六陆七柒八捌九玖十拾百佰千仟万萬亿]+$";
        pattern_ch = Pattern.compile(regex_ch);
    }

    public Integer curNumTran(ArrayList<Integer> CNT, ArrayList<Integer> UNT) {
        Integer res = 0;
        for (int i = 0; i < CNT.size(); i++) {
            res += CNT.get(i) * UNT.get(i);
        }
        return res;
    }

    public Integer parseCh2Num(char[] ChArr) {
        Integer result = 0;
        int base = 1;
        int tmpbase = 1;
        int c = -1;
        ArrayList<Integer> UNT = new ArrayList<Integer>();
        ArrayList<Integer> CNT = new ArrayList<Integer>();
        boolean flag = false;
        if(ChArr.length==3){
            if(this.unit.containsKey(ChArr[1]) && this.unit.get(ChArr[1])%10==0){
                tmpbase = this.unit.get(ChArr[1])/10;
            }
            else if (this.count.get(ChArr[1])> 0 && this.count.get(ChArr[1])%10==0){
                tmpbase = this.count.get(ChArr[1])/10;
            }
        }
        for (int i = ChArr.length - 1; i >= -1; i--) {
            if (i == -1) {
                if(flag){
                    UNT.add(tmpbase);
                    CNT.add(1);
                }
                if(UNT.size() > 0 && CNT.size() > 0){
                result += base * curNumTran(CNT, UNT);
                }
            } else if (this.unit.containsKey(ChArr[i])) {
                if (UNT.size() != 0 && CNT.size() != 0) {
                    result += base * curNumTran(CNT, UNT);
                    UNT = new ArrayList<Integer>();
                    CNT = new ArrayList<Integer>();
                }
                base = this.unit.get(ChArr[i]);
                tmpbase = 1;
            } else if (this.count.containsKey(ChArr[i])) {
                if (this.count.get(ChArr[i]) % 10 == 0) {
                    tmpbase = this.count.get(ChArr[i]);
                    flag = true;
                } else {
                    c = this.count.get(ChArr[i]);
                    UNT.add(tmpbase);
                    CNT.add(c);
                    flag = false;
                }
            }
        }
        return result;
    }

    public Integer parse(String ChNum) {
        Matcher matcher_al = pattern_al.matcher(ChNum);
        Matcher matcher_ch = pattern_ch.matcher(ChNum);
        if (matcher_al.find()) {
            return Integer.parseInt(ChNum);
        } else if (matcher_ch.find()) {
            return parseCh2Num(ChNum.toCharArray());
        }
        return 0;
    }

    public static void main(String[] args) {
        DigitalUtil digitalParse = new DigitalUtil();
        Integer num = digitalParse.parse("九十");
        System.out.println(num);
    }
}

 

posted on 2013-12-03 17:22 _Clarence 阅读(...) 评论(...) 编辑 收藏

导航

统计