JAVA--第九周实验--计算两个大整数的和、差、积和商,并计算一个大整数的因子个数
/* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved. * 文件名称:判断两个日期的大小关系 * 作 者: 雷恒鑫 * 完成日期: 2012 年 10 月 31 日 * 版 本 号: V1.0 * 对任务及求解方法的描述部分 * 输入描述: * 问题描述: * 程序输出: * 程序头部的注释结束 */ public class Test {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubBigInt big = new BigInt("123456", "9876543219");big.add();big.cut();big.multiply();big.divide();big.factorCount();//new MyJFrame();}}
BigIntegerBigInteger类
import java.math.BigInteger;import java.math.*;public class BigInt {BigInteger m1;BigInteger m2;BigInteger m3;BigInt(String s1, String s2) {m1 = new BigInteger(s1);m2 = new BigInteger(s2);}public void add() {m3 = m1.add(m2);System.out.println("两个数的和为:" + m3);}public void cut() {m3 = m1.subtract(m2);System.out.println("两个数的差为:" + m3);}public void multiply() {m3 = m1.multiply(m2);System.out.println("两个数的积为:" + m3);}public void divide() {m3 = m1.divide(m2);System.out.println("两个数的商为:" + m3);}public void factorCount() {int count = 0;for (BigInteger i = BigInteger.valueOf(2); i.compareTo(m1) < 0; i = i.add(BigInteger.ONE)) {if (m1.remainder(i).equals(BigInteger.ZERO)) {++count;}}System.out.println(m1 + "的因子个数为:" + count);}}
运行结果: