java实现大整数数相加_求助, 大整数相加程序!

求助, 大整数相加程序!

请大家帮我写下  大整数相加程序。我刚学 不怎么会,,

随机生成两个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

{

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

{

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

if(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

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

{

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) 方法生成的随机数的位数太接近.

----------------解决方案--------------------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值