/** * 辅助类 * 用于记载字符和位置 * */ class CharPos{ char c; int pos; public CharPos(char c,int pos){ this.c=c; this.pos=pos; } }
import java.lang.reflect.Array; /** * 泛型栈 * * @param <T> */ public class Stack<T>{ private Class<T> type;// 栈元素所属的类 private int size;// 栈深度 private T[] arr;// 用数组存储 private int top;// 栈顶元素的下标 public Stack(Class<T> type,int size){ this.type = type; this.size=size; arr=createArray(size); top=-1; } /** * 创建数组 * @param size * @return */ @SuppressWarnings("unchecked") private T[] createArray(int size) { return (T[]) Array.newInstance(type, size); } /** * 压栈 * @param t */ public void push(T t){ top++; arr[top]=t; } /** * 出栈 * @return */ public T pop(){ T t=arr[top]; top--; return t; } /** * 取栈顶元素 * @return */ public T peek(){ return arr[top]; } /** * 判断栈是否为空 * @return */ public boolean isEmpty(){ return top==-1; } /** * 判断栈是否满了 * @return */ public boolean isFull(){ return top==(size-1); } public static void main(String[] args){ Stack<String> s=new Stack<String>(String.class,100); s.push("以恒心为良友"); s.push("以经验为参谋"); s.push("以小心为兄弟"); s.push("以希望为哨兵"); while(!s.isEmpty()){ String str=s.pop(); System.out.println(str); } } }
/** * 将数字转化成汉字大写形式 * */ public class CnNumMaker { private String integerPart;// 整数部分 private String decimalPart;// 小数部分 private Stack<CharPos> stack;// 存放整数部分的栈 private char[] arr;// 存放万基单位的数组 public CnNumMaker(String strIn) { // 分离成整数部分和小数部分 if (strIn.contains(".")) { // 包含小数点 int dotIndex = strIn.indexOf("."); integerPart = strIn.substring(0, dotIndex); decimalPart = strIn.substring(dotIndex + 1); } else { // 不包含小数点 integerPart = strIn; } // 初始化栈 int length = integerPart.length(); stack = new Stack<CharPos>(CharPos.class, length); arr=new char[length]; // 向栈中填值 for (int i = 0; i < length; i++) { char c = integerPart.charAt(i); int pos = length - i - 1; if (c == '0') { if (stack.isEmpty()) { stack.push(new CharPos(c, pos)); } else { CharPos top = stack.peek(); if (top.c != '0') { stack.push(new CharPos(c, pos)); }else{ top = stack.pop(); stack.push(new CharPos(c, pos)); } } } else { stack.push(new CharPos(c, pos)); } // 插入逗号,作为单位 if (pos % 4 == 0 && pos != 0) { arr[pos]=','; } } // 清除尾后的零 CharPos top=stack.peek(); while(top.c=='0'){ stack.pop(); if(stack.isEmpty()==false){ top=stack.peek(); }else{ stack.push(top); break; } } } public String toString(){ StringBuilder sb=new StringBuilder(); // 整数部分处理 if(Long.parseLong(integerPart)==0){ sb.append("零"); }else{ // 这个数组用来放最终汉字,小单位和大单位 String[] arrStr=new String[arr.length]; while(stack.isEmpty()==false){ CharPos poped=stack.pop(); if('0'==poped.c){ arrStr[poped.pos]="零"; }else{ arrStr[poped.pos]=getCnUpperNumber(poped.c)+getUnit(poped.pos); } } // 万亿等单位 for(int i=0;i<arr.length;i++){ if(arr[i]==','){ String unit=getWanUnit(i); if("零".equals(arrStr[i])){ arrStr[i]=unit; }else{ if(arrStr[i]==null){ arrStr[i]=""; } arrStr[i]+=unit; } } } // 万基单位前面三位是空的话万基单位不该出现 for(int i=0;i<arrStr.length-3;i++){ if(arrStr[i]!=null && arrStr[i+1]==null && arrStr[i+2]==null && arrStr[i+3]==null){ arrStr[i]=null; } } // 整数部分拼接 for(int i=0;i<arrStr.length;i++){ if(arrStr[i]!=null ){ sb.insert(0, arrStr[i]); } } } // 小数部分处理 if(decimalPart!=null && decimalPart.length()>0){ sb.append("点"); for(int i=0;i<decimalPart.length();i++){ sb.append(getCnUpperNumber(decimalPart.charAt(i))); } } return sb.toString(); } // 得到数字的汉字大写 private static char getCnUpperNumber(char c){ final char[] Uppers = { '零', '壹', '贰', '叁', '肆', '伍', '陆','柒', '捌', '玖' }; String str=String.valueOf(c); int number=Integer.parseInt(str); return Uppers[number]; } // 得到十百千这种单位 private static String getUnit(int pos){ final String[] Units = { "", "拾", "百", "仟"}; int mod=pos%4; return Units[mod]; } // 得到万亿兆这种单位 private static String getWanUnit(int pos){ final String[] Wans = { "", "万", "亿", "兆", "京", "垓", "秭", "穰", "沟", "涧", "正", "载"}; return Wans[pos/4]; } public static void main(String[] args) { //CnNumMaker c=new CnNumMaker("100000000"); // c.toString(); String[] arr = {"0", "1", "10","100","1000", "10000","100000", "1000000", "10000000", "100000000", "1000000000","10000000000","1000000000000", "10123", "100123", "1000123", "10012003", "1234567890.043243", "567891234567890.043243" ,"3243243344567890.043243" ,"24","108","9234","9087","9008","10001","100001","10000001","1000000001","10000000001","100000000001","1000000000001","10000000000001","100000100000001","10007600089001"}; for (String str : arr) { try { CnNumMaker c = new CnNumMaker(str); System.out.println(str+"->"+c.toString()); } catch (Exception e) { e.printStackTrace(); System.out.println("Error:" + str); } } } }
从以下输出可以看到,这个类对于数字的汉字大写处理,已经可以了。如果你在使用中发现什么问题,请及时提醒我。
0->零 1->壹 10->壹拾 100->壹百 1000->壹仟 10000->壹万 100000->壹拾万 1000000->壹百万 10000000->壹仟万 100000000->壹亿 1000000000->壹拾亿 10000000000->壹百亿 1000000000000->壹兆 10123->壹万零壹百贰拾叁 100123->壹拾万零壹百贰拾叁 1000123->壹百万零壹百贰拾叁 10012003->壹仟零壹万贰仟零叁 1234567890.043243->壹拾贰亿叁仟肆百伍拾陆万柒仟捌百玖拾点零肆叁贰肆叁 567891234567890.043243->伍百陆拾柒兆捌仟玖百壹拾贰亿叁仟肆百伍拾陆万柒仟捌百玖拾点零肆叁贰肆叁 3243243344567890.043243->叁仟贰百肆拾叁兆贰仟肆百叁拾叁亿肆仟肆百伍拾陆万柒仟捌百玖拾点零肆叁贰肆叁 24->贰拾肆 108->壹百零捌 9234->玖仟贰百叁拾肆 9087->玖仟零捌拾柒 9008->玖仟零捌 10001->壹万零壹 100001->壹拾万零壹 10000001->壹仟万零壹 1000000001->壹拾亿零壹 10000000001->壹百亿零壹 100000000001->壹仟亿零壹 1000000000001->壹兆零壹 10000000000001->壹拾兆零壹 100000100000001->壹百兆零壹亿零壹 10007600089001->壹拾兆零柒拾陆亿零捌万玖仟零壹