62,614
社区成员
发帖
与我相关
我的任务
分享
public class Solution {
public static Map<Integer, String> map = new HashMap<Integer, String>();
static {
map.put(0,"零");
map.put(1,"一");
map.put(2,"二");
map.put(3,"三");
map.put(4,"四");
map.put(5,"五");
map.put(6,"六");
map.put(7,"七");
map.put(8,"八");
map.put(9,"九");
map.put(10,"十");
map.put(11,"十一");
map.put(12,"十二");
}
public static void main(String[] args)throws Exception{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String name = reader.readLine();
FileReader fis = new FileReader(name);
BufferedReader file = new BufferedReader(fis);
String tem = null;
while (file.ready()){
tem = file.readLine();
for(Map.Entry<Integer,String>pair : map.entrySet()) {
tem = tem.replace("*.\\b" +pair.getKey().intValue()+"*.\\b", pair.getValue());
}
System.out.println(tem);
}
reader.close();
file.close();
}
}
public class NumberConverter {
public static String convert(String value) {
StringBuilder buffer = new StringBuilder(value);
for (int i=0; i<buffer.length(); i++) {
int index = buffer.indexOf("美元", i);
if (index > 0) { // 定位 "美元" 两个字符的位置。
int start = index-1;
int end = index;
// 定位 数字 的开始位置
for (;start>0 && end >= start && buffer.charAt(start)>='0' && buffer.charAt(start)<='9'; start--);
start++;
// 找到 美元 前面的数字
String target = buffer.substring(start, end);
// 替换成中文数字
String replace = replace(target);
buffer.delete(start,end);
buffer.insert(start,replace);
// 确定继续查找替换的位置
i = start + replace.length();
}
}
return buffer.toString();
}
private static char[] Nums = new char[]{'零','一','二','三','四','五','六','七', '八', '九'};
private static String[] Unit = new String[]{"","十","百","千","万","十万","百万","千万","亿"};
public static String replace(String number) {
if (number.length()>Unit.length) {
throw new IllegalArgumentException("超出能转换的数字大小");
}
StringBuilder buffer = new StringBuilder();
char lastChar = '\0';
for(int i=0; i< number.length(); i++) {
if (lastChar == Nums[0]){
continue;
}
int nd = number.length()-i-1;
char n = Nums[(number.charAt(i)-'0')];
if (!(number.length()==2 && n==Nums[1])){
buffer.append(n);
}
buffer.append(Unit[nd]);
}
return buffer.toString();
}
public static void main(String[] args) {
final String example =
"这个价值为1美元,但这个为12美元。\n" +
"变量名为 file1。\n" +
"110 是一个数字。";
String result = convert(example);
System.out.println(result);
}
}
....
while (file.ready()){
tem = file.readLine();
Pattern p = Pattern.compile("\\d+美元");
Matcher m = p.matcher(tem);
tem=tem.replaceAll("\\d+美元","%s美元");
while (m.find()){
String price = m.group().replaceAll("美元", "").trim();
tem=tem.replaceFirst("%s",map.get(Integer.valueOf(price)));
}
System.out.println(tem);
}
.....
String reg = "[a-zA-Z\\d]+";
String str = "这个价值为1美元,但这个为12美元。变量名为 file1。110 是一个数字。";
Pattern p = Pattern.compile(reg);
Matcher matcher = p.matcher(str);
while (matcher.find()) {
String tempKey = matcher.group();
if (tempKey.length() < 3) {
System.out.println(tempKey);
str = str.replaceFirst(matcher.group(), map.get(Integer.parseInt(tempKey)));
}
}
System.out.println(str);
// 这个价值为一美元,但这个为十二美元。变量名为 file1。110 是一个数字。