当前位置: 代码迷 >> Java相关 >> 求助, 大整数相加程序!
  详细解决方案

求助, 大整数相加程序!

热度:494   发布时间:2008-04-26 17:22:00.0
求助, 大整数相加程序!
请大家帮我写下  大整数相加程序。我刚学 不怎么会,,

  随机生成两个100位以内的大整数,将两个数相加。 并格式化输出,  例如:每50位一行,5位一段。。

在控制台下输出就行!!不要求界面!。
我就写出了一点,  剩下的不会了, 请大家帮忙!谢谢大家啦!!(我写的代码如下)


程序代码:
import java.util.Random;
public class Xangjia
{

  
    public static void main(String[] args)
    {
       
        Random Rnd=new Random();
        
        int L=0;
        int L2=0;
        int max;
        int min;
        while(L ==0)
        {
           L=Rnd.nextInt(100);
          
        }
        while(L2 ==0)
        {
           L2=Rnd.nextInt(100);
          
        }
    
    
        System.out.println("生成的位数为"+L); // 生成随机位数!
        System.out.println("生成的第2个数的位数为"+L2);
    
        int number[]=new int[L];
        int number2[]=new int[L2];    
            
        
        for(int i=0;i<L;i++)
        {
           number[i]=Rnd.nextInt(10);  
           
           System.out.print(number[i]);
        }
        
        System.out.print("\n");
        System.out.print("\n"+"〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓"+"\n");
        System.out.print("\n");
    
        for(int ii=0;ii<L2;ii++)
        {
           number2[ii]=Rnd.nextInt(10);  
           
           System.out.print(number2[ii]);
           
        }
        System.out.print("\n");
   
        
  
   
   
}   
}
搜索更多相关的解决方案: 整数  程序  相加  

----------------解决方案--------------------------------------------------------
已经差不多了.
再把2个数组中的元素分别相加就得了.记得进位.
----------------解决方案--------------------------------------------------------
思想我知道,  关键是不知道  写出来,,  一直出错!!   请帮帮我好吗?
----------------解决方案--------------------------------------------------------
有没有试试 BigInteger 或是 BigDecimal 呢?!

很好的一个类啊

要充分利用!

若不写界面的话10行左右的代码就可以了吧


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.math.BigInteger;
import javax.swing.text.AttributeSet;   
import javax.swing.text.BadLocationException;   
import javax.swing.text.PlainDocument;

public class Bigint extends JFrame{
    private Container container;
    private JTextField num1,num2,sum;
    private JPanel panel,panel1;
    private JButton add;
    private BigInteger big1,big2,big3;
    
    public Bigint(){
        container = getContentPane();
        num1 = new JTextField(100);
        num1.setDocument(new NumOnly());
        num2 = new JTextField(100);
        num2.setDocument(new NumOnly());
        sum = new JTextField(101);
        sum.setEditable(false);
        panel = new JPanel();
        panel.setLayout(new GridLayout(3,1,5,5));
        panel.add(num1);
        panel.add(num2);
        panel.add(sum);
        container.add(new JScrollPane(panel));
        panel1 = new JPanel(new FlowLayout());
        add = new JButton("计算");
        add.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    if(num1.getText().equals("")||num2.getText().equals("")){
                        JOptionPane.showMessageDialog(container,"您没有输入完要计算的内容","提示",JOptionPane.INFORMATION_MESSAGE);
                    }
                    else{
                        big1 = new BigInteger(num1.getText());
                        big2 = new BigInteger(num2.getText());
                        big3 = big1.add(big2);
                        sum.setText(big3.toString());
                    }
                }
            }
        );
        panel1.add(add);
        container.add(panel1,BorderLayout.SOUTH);
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        Dimension frameSize = getSize();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);
        setVisible(true);
    }
    
    public static void main(String args[]){
        Bigint big = new Bigint();
    }
    
    public class NumOnly extends PlainDocument{        
        public void insertString(int offs,String str,AttributeSet a) throws BadLocationException{   
            for(int i=0;i<str.length();i++){  
                if(str.charAt(i)<'0'||str.charAt(i)>'9'){   
                    return;   
                }
            }   
            super.insertString(offs,str,a);  
            
        }
    }  
}

[[it] 本帖最后由 freish 于 2008-4-27 00:48 编辑 [/it]]
----------------解决方案--------------------------------------------------------
若是要随机生成数
可以用BigInteger构造方法中的
BigInteger(int numBits, Random rnd) 方法
它构造一个随机生成的 BigInteger,它是在 0 到 (2^numBits - 1)(包括)范围内均匀分布的值。
----------------解决方案--------------------------------------------------------
格式化输出用String的toCharArray方法转换成字符数组,然后输出就简单了
----------------解决方案--------------------------------------------------------
控制台的


import java.math.BigInteger;
import java.util.Random;

public class Big{
    private static BigInteger num1,num2,sum;
    
    public Big(){
        num1 = new BigInteger(334,new Random());
        num2 = new BigInteger(334,new Random());
        sum = num1.add(num2);

    }
    public static void main(String args[]){
        Big b = new Big();
        System.out.println("加数一为:");
        output(num1);
        System.out.println("加数二为:");
        output(num2);
        System.out.println("和为:");
        output(sum);
    }
    
    private static void output(BigInteger big){
        if(big!=null){
            String out = big.toString();
            char cout[] = out.toCharArray();
            for(int i = 1;i<cout.length;i++){
                System.out.print(cout[i-1]);
                if(i%5 == 0){
                    System.out.print(" ");
                }
                if(i%50 == 0){
                    System.out.println();
                }
            }

        }
    }
}
----------------解决方案--------------------------------------------------------
以下是控制台输出:

Process started >>>
加数一为:
15017 64068 48275 66141 54800 47537 71245 01117 38511 04132
61067 94079 17924 39481 41010 70592 55799 50415 92719 25204
加数二为:
28065 62205 90443 05182 05963 86086 34338 07289 56251 18136
09135 57779 11284 63146 53178 17791 84368 60334 79702 35818
和为:
43083 26274 38718 71323 60764 33624 05583 08406 94762 22268
70203 51858 29209 02627 94188 88384 40168 10750 72421 61022
<<< Process finished.
================ READY ================
----------------解决方案--------------------------------------------------------
[bo]以下是引用 [un]hengheng516[/un] 在 2008-4-26 18:55 的发言:[/bo]

思想我知道,  关键是不知道  写出来,,  一直出错!!   请帮帮我好吗?

import java.util.Random;
public class Xangjia
{
    public static int control(int count)//格式控制
    {
        if(count%5 == 0)System.out.print(" ");
        if(count%50 == 0)System.out.println();
        count ++;
        return count;
    }
    public static void create(int n[],int len)//生成随机数
    {
        int count = 1;
        Random Rnd=new Random();
        for(int i=0;i<len;i++)
        {                 
           n[i]=Rnd.nextInt(10);            
           System.out.print(n[i]);
           count = control(count);
        }      
    }
    public static void add(int a[],int b[],int sum[],int sign[],int minlen)//相加
    {
        for(int i = 0; i < minlen; i++)
        {                        
            sum[sum.length - 1 - i] = a[a.length - 1 - i] + b[b.length - 1 - i];
            if(sum[sum.length - i - 1] > 9)
                {                    
                    sign[sign.length - 1 - i] += 1;                      
                    sum[sum.length - 1 - i] %= 10;
                }       
        }
    }
    public static void in(int sum[],int sign[])//进位
    {
            for(int i = 1, j = 0; i < sum.length; i++,j++)
              {
                sum[sum.length - 1 - i] += sign[sign.length - 1 - j];
                if(sum[sum.length - 1 - i] > 9)
                    {                        
                      sign[sign.length - 2 - j] += 1;                       
                      sum[sum.length - 1 - i] %= 10;
                    }
              }        
    }
    public static void output(int sum[])//输出
    {
        int count = 1;
        int i = 0;
        try//捕获当生成的随机数都为0时产生的异常
        {
            while(sum[i] == 0)//去除前面是0的数
            i++;  
        }
        catch(Exception e){}                
        while(i < sum.length)//输出结果
        {
            System.out.print(sum[i]);
            count = control(count);
            i++;
        }
    }
    public static void main(String[] args)
    {  
        Random Rnd=new Random();      
        int L1=Rnd.nextInt(100);
        int L2=Rnd.nextInt(100);
        int max = L1>L2?L1:L2;
        int min = L1>L2?L2:L1;
        System.out.println("生成的第1个数的位数为"+L1);
        System.out.println("生成的第2个数的位数为"+L2);
        int a[] = new int[L1];
        int b[] = new int[L2];   
        int sum[] = new int[max+1];   
        int sign[] = new int[max];
        create(a,L1);
        System.out.println("\n"+"〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓");
        create(b,L2);
        System.out.println("\n它们的和是:");        
        //数组复制,把位数大的数前面不用加的数先存起来
        if(L1 > L2)
            System.arraycopy(a, 0, sum, 1, (a.length - b.length));
        else
            System.arraycopy(b, 0, sum, 1, (b.length - a.length));
        add(a,b,sum,sign,min);
        in(sum,sign);
        output(sum);
     }   
}
----------------解决方案--------------------------------------------------------
这是顺着你(LZ)的思路写的.
如果没有什么特别要求的.还是7楼的容易实现.
只是BigInteger(int numBits, Random rnd) 方法生成的随机数的位数太接近.
----------------解决方案--------------------------------------------------------
  相关解决方案