62,628
社区成员
发帖
与我相关
我的任务
分享
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//假设在汉译有效的情况下.
public class Test{
public static void main(String[] args){
String[] contents = {"一万三千零四十加二减一十","二十加一百乘二除五十减四","(一百一十一加九)乘 (五加九十五)"};
String result = null;
for(String content : contents){
result = process(content);
System.out.printf("'%s' = '%s' \n",content,result);
}
String content = "一万五千六百零二乘以五十八减六十六乘以九十";
System.out.printf("'%s' = '%s' \n",content,process(content));
}
private static String process(String content){
String arabic = chineseToArabic(content);
String result = processWithParenthese(arabic);
result = arabicToChinese(result);
return result;
}
private static String chineseToArabic(String chinese){
//去除空格,替换中文括号为英文括号
chinese = chinese.replaceAll("\\s+","");
chinese = chinese.replaceAll("(","(");
chinese = chinese.replaceAll(")",")");
chinese = chinese.replaceAll("加以?","+");
chinese = chinese.replaceAll("减以?","-");
chinese = chinese.replaceAll("乘以?","*");
chinese = chinese.replaceAll("除以?","/");
chinese = chinese.replaceAll("[一壹]","1");
chinese = chinese.replaceAll("[二贰]","2");
chinese = chinese.replaceAll("[三叁]","3");
chinese = chinese.replaceAll("[四肆]","4");
chinese = chinese.replaceAll("[五伍]","5");
chinese = chinese.replaceAll("[六陆]","6");
chinese = chinese.replaceAll("[七柒]","7");
chinese = chinese.replaceAll("[八捌]","8");
chinese = chinese.replaceAll("[九玖]","9");
chinese = chinese.replaceAll("零","0");
//仅处理一万以内的数字(正整数)
chinese = chinese.replaceAll("[万千仟百佰十拾](?!0)(?=\\d)","");
chinese = chinese.replaceAll("万0(?=\\d[百佰])","0");
chinese = chinese.replaceAll("万0(?=\\d[十拾])","00");
chinese = chinese.replaceAll("万0(?=\\d([-+*/)]|$))","000");
chinese = chinese.replaceAll("[千仟]0(?=\\d[十拾])","0");
chinese = chinese.replaceAll("[千仟]0(?=\\d([-+*/)]|$))","00");
chinese = chinese.replaceAll("[千仟](?=[-+*/)]|$)","000");
chinese = chinese.replaceAll("[百佰]0","0");
chinese = chinese.replaceAll("[百佰](?=[-+*/)]|$)","00");
chinese = chinese.replaceAll("[十拾]","0");
assert chinese.matches("[-+*/0-9()]+");
return chinese;
}
private static String arabicToChinese(String arabic){
arabic = arabic.replaceAll("^-","负");
arabic = arabic.replaceAll("(?<=\\d)(?=(\\d{4})+$)",",");
String[] tokens = {"万","亿","兆"};//结果仅处理到兆等量级
for(int i = 0 ; i < tokens.length ; i ++){
arabic = arabic.replaceAll(",(?!.*,)",tokens[i]);
}
arabic = arabic.replaceAll("(?<=[1-9])(?=\\d{3})","千");
arabic = arabic.replaceAll("(?<=[1-9])(?=\\d{2})","百");
arabic = arabic.replaceAll("(?<=[1-9])(?=\\d)","十");
arabic = arabic.replaceAll("0+(?=[万亿兆]|$)","");
arabic = arabic.replaceAll("0+","0");
String[] capitalNumbers = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
for(int i = 0 ; i < capitalNumbers.length ; i ++){
arabic = arabic.replaceAll(i+"",capitalNumbers[i]);
}
return arabic;
}
private static String processWithParenthese(String equation){
String regex = "(?<=[(])(?<middle>[^)]+)(?=[)])";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(equation);
String middle = null;
String result = null;
while(matcher.find()){
middle = matcher.group("middle");
result = processWithoutParenthese(middle);
equation = equation.substring(0,matcher.start() - 1) + result + equation.substring(matcher.end() + 1 , equation.length());
matcher = pattern.matcher(equation);
}
return processWithoutParenthese(equation);
}
//假设只存在小括号影响算式顺序.
private static String processWithoutParenthese(String equation){
//处理不带括号的情况
String regex = "(?<arg1>\\d+)(?<sign>[*/])(?<arg2>-?\\d+)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(equation);
int arg1 = 0;
int arg2 = 0;
String sign = null;
int result = 0;
//处理乘除法
while(matcher.find()){
sign = matcher.group("sign");
arg1 = Integer.valueOf(matcher.group("arg1"));
arg2 = Integer.valueOf(matcher.group("arg2"));
if(sign.equals("*")){
result = arg1 * arg2;
}else{
result = arg1 / arg2;//这里不算小数. 3/2=1
}
equation = equation.substring(0,matcher.start()) + result + equation.substring(matcher.end(),equation.length());
matcher = pattern.matcher(equation);
}
//处理加减
regex = "(?<arg1>\\d+)(?<sign>[-+])(?<arg2>-?\\d+)";//两个数相减可能存在 5-7 = -2的情况.需要考虑进来
pattern = Pattern.compile(regex);
matcher = pattern.matcher(equation);
while(matcher.find()){
sign = matcher.group("sign");
arg1 = Integer.valueOf(matcher.group("arg1"));
arg2 = Integer.valueOf(matcher.group("arg2"));
if(sign.equals("+")){
result = arg1 + arg2;
}else{
result = arg1 - arg2;
}
equation = equation.substring(0,matcher.start()) + result + equation.substring(matcher.end(),equation.length());
matcher = pattern.matcher(equation);
}
return equation;
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//假设在汉译有效的情况下.
public class Test{
public static void main(String[] args){
String[] contents = {"一万三千零四十加二减一十","二十加一百乘二除五十减四","(一百一十一加九)乘 (五加九十五)"};
String result = null;
for(String content : contents){
result = process(content);
System.out.printf("'%s' = '%s' \n",content,result);
}
String content = "一万五千六百零二乘以五十八减六十六乘以九十";
System.out.printf("'%s' = '%s' \n",content,process(content));
}
private static String process(String content){
String arabic = chineseToArabic(content);
String result = processWithParenthese(arabic);
result = arabicToChinese(result);
return result;
}
private static String chineseToArabic(String chinese){
//去除空格,替换中文括号为英文括号
chinese = chinese.replaceAll("\\s+","");
chinese = chinese.replaceAll("(","(");
chinese = chinese.replaceAll(")",")");
chinese = chinese.replaceAll("加以?","+");
chinese = chinese.replaceAll("减以?","-");
chinese = chinese.replaceAll("乘以?","*");
chinese = chinese.replaceAll("除以?","/");
chinese = chinese.replaceAll("[一壹]","1");
chinese = chinese.replaceAll("[二贰]","2");
chinese = chinese.replaceAll("[三叁]","3");
chinese = chinese.replaceAll("[四肆]","4");
chinese = chinese.replaceAll("[五伍]","5");
chinese = chinese.replaceAll("[六陆]","6");
chinese = chinese.replaceAll("[七柒]","7");
chinese = chinese.replaceAll("[八捌]","8");
chinese = chinese.replaceAll("[九玖]","9");
chinese = chinese.replaceAll("零","0");
//仅处理一万以内的数字(正整数)
chinese = chinese.replaceAll("[万千仟百佰十拾](?!0)(?=\\d)","");
chinese = chinese.replaceAll("万0(?=\\d[百佰])","0");
chinese = chinese.replaceAll("万0(?=\\d[十拾])","00");
chinese = chinese.replaceAll("万0(?=\\d([-+*/]|$))","000");
chinese = chinese.replaceAll("[千仟]0(?=\\d[十拾])","0");
chinese = chinese.replaceAll("[千仟]0(?=\\d([-+*/]|$))","00");
chinese = chinese.replaceAll("[千仟](?=[-+*/]|$)","000");
chinese = chinese.replaceAll("[百佰]0","0");
chinese = chinese.replaceAll("[百佰](?=[-+*/]|$)","00");
chinese = chinese.replaceAll("[十拾]","0");
assert chinese.matches("[-+*/0-9()]+");
return chinese;
}
private static String arabicToChinese(String arabic){
arabic = arabic.replaceAll("^-","负");
arabic = arabic.replaceAll("(?<=\\d)(?=(\\d{4})+$)",",");
String[] tokens = {"万","亿","兆"};//结果仅处理到兆等量级
for(int i = 0 ; i < tokens.length ; i ++){
arabic = arabic.replaceAll(",(?!.*,)",tokens[i]);
}
arabic = arabic.replaceAll("(?<=[1-9])(?=\\d{3})","千");
arabic = arabic.replaceAll("(?<=[1-9])(?=\\d{2})","百");
arabic = arabic.replaceAll("(?<=[1-9])(?=\\d)","十");
arabic = arabic.replaceAll("0+(?=[万亿兆]|$)","");
arabic = arabic.replaceAll("0+","0");
String[] capitalNumbers = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
for(int i = 0 ; i < capitalNumbers.length ; i ++){
arabic = arabic.replaceAll(i+"",capitalNumbers[i]);
}
return arabic;
}
private static String processWithParenthese(String equation){
String regex = "(?<=[(])(?<middle>[^)]+)(?=[)])";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(equation);
String middle = null;
String result = null;
while(matcher.find()){
middle = matcher.group("middle");
result = processWithoutParenthese(middle);
equation = equation.substring(0,matcher.start() - 1) + result + equation.substring(matcher.end() + 1 , equation.length());
matcher = pattern.matcher(equation);
}
return processWithoutParenthese(equation);
}
//假设只存在小括号影响算式顺序.
private static String processWithoutParenthese(String equation){
//处理不带括号的情况
String regex = "(?<arg1>\\d+)(?<sign>[*/])(?<arg2>\\d+)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(equation);
int arg1 = 0;
int arg2 = 0;
String sign = null;
int result = 0;
//处理乘除法
while(matcher.find()){
sign = matcher.group("sign");
arg1 = Integer.valueOf(matcher.group("arg1"));
arg2 = Integer.valueOf(matcher.group("arg2"));
if(sign.equals("*")){
result = arg1 * arg2;
}else{
result = arg1 / arg2;//这里不算小数. 3/2=1
}
equation = equation.substring(0,matcher.start()) + result + equation.substring(matcher.end(),equation.length());
matcher = pattern.matcher(equation);
}
//处理加减
regex = "(?<arg1>\\d+)(?<sign>[-+])(?<arg2>-?\\d+)";//两个数相减可能存在 5-7 = -2的情况.需要考虑进来
pattern = Pattern.compile(regex);
matcher = pattern.matcher(equation);
while(matcher.find()){
sign = matcher.group("sign");
arg1 = Integer.valueOf(matcher.group("arg1"));
arg2 = Integer.valueOf(matcher.group("arg2"));
if(sign.equals("+")){
result = arg1 + arg2;
}else{
result = arg1 - arg2;
}
equation = equation.substring(0,matcher.start()) + result + equation.substring(matcher.end(),equation.length());
matcher = pattern.matcher(equation);
}
return equation;
}
}