首页 > 代码库 > 动手动脑6

动手动脑6

1.请编写一个程序将一个整数转换为汉字读法字符串。

比如“1123”转换为“一千一百二十三”。

更进一步,能否将数字表示的金额改为“汉字表达?

代码:

import java.util.Scanner;
public class Dongnao {

public static void main(String[] args) 
    {
// TODO Auto-generated method stub
    Scanner in=new Scanner(System.in);
    System.out.println("输入一个数:");
    double a;
    a=in.nextDouble();
    System.out.println(digitUppercase(a));
    }
    public static String digitUppercase(double n) 
    { 
        String fraction[] = { "", ""}; 
        String digit[] = { "", "", "", "", "", "", "", "", "", ""}; 
        String unit[][] = { { "", "", "亿"}, { "", "", "", ""}}; 

        String head = n < 0 ? "" : ""; 
        n = Math.abs(n); 

        String s = ""; 
        for (int i = 0; i < fraction.length; i++) { 
            s += (digit[(int) (Math.floor(n * 10 * Math.pow(10, i)) % 10)] + fraction[i]).replaceAll("(零.)+", ""); 
    } 
        if (s.length() < 1) 
        { 
            s = ""; 
        } 
        int integerPart = (int) Math.floor(n); 
        for (int i = 0; i < unit[0].length && integerPart > 0; i++)
        { 
            String p = ""; 
            for (int j = 0; j < unit[1].length && n > 0; j++) 
            { 
                p = digit[integerPart % 10] + unit[1][j] + p; 
                integerPart = integerPart / 10; 
            } 
            s = p.replaceAll("(零.)*零$", "").replaceAll("^$", "") + unit[0][i] + s; 
        } 
        return head + s.replaceAll("(零.)*零元", "").replaceFirst("(零.)+", "").replaceAll("(零.)+", "").replaceAll("^整$", "零元整"); 
    }

}

 

技术分享

 

 

2.

前面几讲介绍过JDK所提供的BigInteger能完成大数计算,如果不用它,直接使用数组表达大数,你能实现相同的功能吗? 要求: (1)用你的大数类实现加和减两个功能 (2)阅读BigInteger类源码,弄清楚它是使用什么算法实现加减乘除四种运算的? (3)通过互联网查找大数运算的相关资料,给你的大数类添加乘、除、求阶乘等其它功能。

    import java.util.Scanner;

    public class Dongnao {

        public static void main(String[] args) {
            String s1,s2;
            Scanner in=new Scanner(System.in);
            System.out.println("输入第一个大数:");
            s1=in.next();
            System.out.println("输入第二个大数:");
            s2=in.next();
            System.out.println(add(s1,s2));
            System.out.println(sub(s1,s2));
        }
        private static String add(String a, String b) { 

            System.out.print("加法:" + a+ "+" + b + "=");
            char[] aa = new StringBuffer(a).reverse().toString().toCharArray(); 
            char[] bb = new StringBuffer(b).reverse().toString().toCharArray(); 
            int aLen = aa.length; 
            int bLen = bb.length; 

            int len = aLen > bLen ? aLen : bLen; 

            int[] result = new int[len + 1]; 
            for (int i = 0; i < len + 1; ++i) { 
                int aint = i < aLen ? aa[i] - 0 : 0; 
                int bint = i < bLen ? bb[i] - 0 : 0; 
                result[i] = aint + bint; 
            } 

            for(int i=0;i<result.length-1;++i){ 
                if(result[i]>=10){ 
                    result[i+1] += result[i]/10; 
                    result[i] %= 10; 
                } 
            } 

            boolean flag = true; 
            StringBuffer sb = new StringBuffer(len); 
            for(int i=len;i>=0;--i){ 
                if(result[i]==0&&flag){ 
                        continue; 
                }    
                else{ 
                    flag=false; 
                } 
                sb.append(result[i]); 
            } 
            return sb.toString(); 
        } 
        public static String sub(String f, String s) { 
            System.out.print("减法:" + f + "-" + s + "="); 
            char[] a = new StringBuffer(f).reverse().toString().toCharArray(); 
            char[] b = new StringBuffer(s).reverse().toString().toCharArray(); 
            int lenA = a.length; 
            int lenB = b.length; 
            int len = lenA > lenB ? lenA : lenB; 
            int[] result = new int[len]; 
            char sign = +; 
 
            if (lenA < lenB) { 
                sign = -; 
            } 
            else if (lenA == lenB) { 
                int i = lenA - 1; 
                while (i > 0 && a[i] == b[i])
                { 
                    i--; 
                } 
                if (a[i] < b[i]) 
                { 
                    sign = -; 
                } 
            } 
 
            for (int i = 0; i < len; i++) 
            { 
                int aint = i < lenA ? (a[i] - 0) : 0; 
                int bint = i < lenB ? (b[i] - 0) : 0; 
                if (sign == +) { 
                    result[i] = aint - bint; 
                } 
                else 
                { 
                    result[i] = bint - aint; 
                } 
            } 
            for (int i = 0; i < result.length - 1; i++) { 
                if (result[i] < 0) { 
                    result[i + 1] -= 1; 
                    result[i] += 10; 
                } 
            } 

            StringBuffer sb = new StringBuffer(); 
            if (sign == -) { 
                sb.append(-); 
            } 
            boolean flag = true; 
            for (int i = len - 1; i >= 0; i--) { 
                if (result[i] == 0 && flag) { 
                    continue; 
                } 
                else 
                { 
                    flag = false; 
                } 
                sb.append(result[i]); 
            } 
            if (sb.toString().equals("")) { 
                sb.append("0"); 
            } 
            System.out.println(sb.toString()); 
            return sb.toString(); 
        } 
}

 

技术分享

 

3.

随机生成10个数,填充一个数组,然后用消息框显示数组内容,接着计算数组元素的和,将结果也显示在消息框中。

 

import javax.swing.JOptionPane;
public class Dongnao {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a[],sum=0;
        a=new int[10];
        String output=new String();
        for(int i=0;i<a.length;i++)
        {
            a[i]=(int)(Math.random()*100);
        }
        output+="a[10]={";
        for(int i=0;i<a.length;i++)
        {
            output+=a[i]+" ";
        }
        output+="}";
        for(int i=0;i<a.length;i++)
        {
            sum+=a[i];
        }
        output+="\nsum:"+sum;
        JOptionPane.showMessageDialog(null, output,"结果",
                JOptionPane.PLAIN_MESSAGE);
    }
}

 

源代码:

 

 技术分享

 

动手动脑6