用数字计算出来结果,方便快速,容易理解。
public void matching(){
//^[^一二三四五六七八九十百千万块元][一二三四五六七八九十百千万块元]$/
int index = 0;
List<String> list = new ArrayList<String>();
String mParam = "";
while(true){
mParam = check(index);
if(mParam == null){
break;
}
if(!mParam.equals("")){
list.add(mParam);
}
index++;
}
String param = "";
for (int i = 0; i < list.size(); i++) {
param = list.get(i);
toConver(param);
}
}
//五百二十三
public int toConver(String param){
int index = 0,next,f =1,s =1,r=0;
char m,n;
while(index < param.length()){
//得到index位置的字符:三
m = param.charAt(index);
//一到十
if(com.indexOf(m) >= 0){
//初始化一下单位
s = 1;
//转换数字后的值:3
f = number[com.indexOf(m)];
next = index + 1;
if(next < param.length()){
n = param.charAt(next);
if(un.indexOf(n)>= 0) {
index++;
s = numberUn[un.indexOf(n)];
}
}
r = f * s + r;
}
index++;
}
System.out.println(r);
return r;
}
public String check(int index){
int i = index+1;
int j = i;
if(i >= count){
return null;
}
String f = String.valueOf(text.charAt(index));
String s = String.valueOf(text.charAt(i));
if(index == 0 && compare.indexOf(s) >= 0){
i = getLast(i,s);
return text.substring(index,i);
}else if(compare.indexOf(f) < 0 && compare.indexOf(s) >= 0){
i = getLast(i,s);
return text.substring(j,i);
}
return "";
}
/**
* 返回截止的位置
* @param i 下一个位置
* @param s 下一个位置对应的大写数字
* @return
*/
public int getLast(int i,String s){
while(true){
i++;
if(i >= text.length()){
break;
}
s = String.valueOf(text.charAt(i));
if(compare.indexOf(s) < 0){
break;
}
}
return i;
}
替换法,暂时没有实现零。比较麻烦,耗时,弃用
String text = "他有一块,我有十块钱,你有二十三块钱,我有二千三百二十一块,他有二百二十元钱。二千三百五十我有二万二千一百五十块钱";
int count = text.length();
String compare = "一二三四五六七八九十角元百千万块";
String number [] = new String[]{"1","2","3","4","5","6","7","8","9","10"};
// String com [] = new String[]{"一","二","三","四","五","六","七","八","九","十"};
String com = "一二三四五六七八九十";
String un = "角元百千万";
Map<String,String> data = new HashMap<String, String>();
public static void main(String[] args) {
Number n = new Number();
long s = System.nanoTime();
n.matching();
long e = System.nanoTime();
System.out.println(e-s);
}
public void matching(){
//^[^一二三四五六七八九十百千万块元][一二三四五六七八九十百千万块元]$/
int index = 0;
List<String> list = new ArrayList<String>();
String mParam = "";
while(true){
mParam = check(index);
if(mParam == null){
break;
}
if(!mParam.equals("")){
list.add(mParam);
}
index++;
}
String param = "";
StringBuffer sb = null;
for (int i = 0; i < list.size(); i++) {
sb = new StringBuffer();
param = list.get(i);
for (int j = 0; j < param.length(); j++) {
String m = String.valueOf(param.charAt(j));
if(com.indexOf(m) >= 0){
if(m.equals("十")){
sb.append(repT(param,j));
}else{
// param = param.replace(m, number[j]);
sb.append(number[com.indexOf(m)]);
}
}else{
if(j < un.length()){
// param = param.replace(String.valueOf(un.charAt(j)), "");
sb.append("");
}
}
}
System.out.println(sb.toString());
}
}
/**
*
*/
public String repT(String param,int j){
String l = "";
String r = "";
//如果十是最后一位返回0
if(j+1 >= param.length()){
// r = "0";
}else{
r = String.valueOf(param.charAt(j+1));
}
//如果十字开头
if(j == 0){
if(com.indexOf(r) >= 0){
return "1";
}else{
return "10";
}
}else if(j == param.length()-1){
return "0";
}else{
l = String.valueOf(param.charAt(j-1));
}
//十一元 二十元 二十三元 一百二十元
if(com.indexOf(l)>=0 && com.indexOf(r) >= 0){
return "";
}else if(com.indexOf(l) >= 0 && com.indexOf(r)<0){
return "0";
}else if(com.indexOf(l) < 0 && com.indexOf(r) >= 0){
return "1";
}
return "";
}
public String check(int index){
int i = index+1;
int j = i;
if(i >= count){
return null;
}
String f = String.valueOf(text.charAt(index));
String s = String.valueOf(text.charAt(i));
if(compare.indexOf(f) < 0 && compare.indexOf(s) >= 0){
while(true){
i++;
s = String.valueOf(text.charAt(i));
if(compare.indexOf(s) < 0){
break;
}
}
return text.substring(j,i);
}
return "";
}