package andycpp; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ChangeAtoC { /** * @param args * @throws Exception */ private static Map map =null; static { map = new HashMap(); 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, "九"); } public static void main(String[] args) throws Exception { /*****以下为字符输入部分*****/ byte[] a = new byte[1024]; String str = null; String head= null; int size = 0; while (true) { int read = 0; try { read = System.in.read(); } catch (IOException e) { e.printStackTrace(); } a[size++] = (byte) read; // 为换行时 if (read == 10) { str = new String(a, 0, size); break; } } /*****输入完成,进行格式校验*****/ Pattern p = Pattern.compile("\\d+\\.\\d+"); Matcher mt = p.matcher(str); StringBuffer sb = new StringBuffer(); if (mt.find()) { String[] strs = mt.group().split("\\."); int ints = Integer.parseInt(strs[0]); if (ints == 0) { head = "零"; } else { for (int i = 1; ints != 0;) { sb.append(map.get(ints % 10)); unitChinese(sb, i); ints /= 10; i++; } // 细节处理 head = sb.reverse().substring(1, sb.length()); if (head.endsWith("零")) { head = head.substring(0, head.length() - 1); } head = replaceZero("零十", head); head = replaceZero("零百", head); head = replaceZero("零千", head); head = replaceZero("零万", head); while (head.startsWith("一十")) { head = head.substring(1, head.length()); } System.out.println(head); } String end =strs[1]; sb =new StringBuffer(); for (int i = 0; i < end.length(); i++) { sb.append(map.get(Integer.parseInt(end.substring(i,i+1)))); } //合并 System.out.println(head+"点"+sb.toString()); } else { throw new Exception("字符输入不合法"); } } //去掉语法中的零千、零万、零十、零百 private static String replaceZero(String str, String head) { while(head.lastIndexOf(str)!=-1){ head = head.replace(str, ""); } return head; } private static void unitChinese(StringBuffer sb, int i) { if(i==1){ sb.append("十"); }else if(i==2){ sb.append("百"); }else if(i==3){ sb.append("千"); }else if(i==4){ sb.append("万"); }else if(i==5){ sb.append("十"); }else if(i==6){ sb.append("百"); }else if(i==7){ sb.append("千"); }else if(i==8){ sb.append("亿"); } } }