JAVA第十周实验封装一类Java对象,计算两个大整数(如123456789123456789123456789和987654321987654321987654321)的和、差、积和商,并计算一个大

时间:2023-01-02 10:46:11

问题描述:封装一类Java对象,计算两个大整数(如123456789123456789123456789和987654321987654321987654321)的和、差、积和商,并计算一个大整数的因子个数(因子中不包括1和大整数本身)。

package slj.com;
import java.math.*;
public class Maxnumber {
public static void main(String[] args) {
BigInteger a=new BigInteger("123456789123456789123456789"),
b=new BigInteger("987654321987654321987654321"),
c=new BigInteger("0"),
d=new BigInteger("0"),
e=new BigInteger("0"),
f=new BigInteger("0"),
S=new BigInteger("0"),
ONE=new BigInteger("1"),
ZERO=new BigInteger("0"),
i=ONE;
c=a.add(b);
d=a.subtract(b);
e=a.multiply(b);
f=a.divide(b);
System.out.println(a+"+"+b+"="+c);
System.out.println(b+"-"+a+"="+d);
System.out.println(a+"*"+b+"="+e);
System.out.println(a+"和"+b+"的商:"+f);
System.out.println(a+"的因子为:");
while(i.compareTo(a)<0){
i=i.add(ONE);
if(ZERO.equals(a.remainder(i))){
System.out.print(i+" ");
}
}

}
}


运行结果:

123456789123456789123456789+987654321987654321987654321=1111111111111111111111111110
987654321987654321987654321-123456789123456789123456789=-864197532864197532864197532
123456789123456789123456789*987654321987654321987654321=121932631356500531591068431581771069347203169112635269
123456789123456789123456789和987654321987654321987654321的商:0
123456789123456789123456789的因子为:
3 9 27 757 2271 3607 3803 6813 10821 11409 20439 32463 34227 97389 102681 2730499 2878871 8191497 8636613 13717421 24574491 25909839 41152263 73723473 77729517 123456789