大整数相乘------java实现

大数相乘算法实现:(Java版,使用BigInteger)(含有大数的加、减、乘、除)

package com.bigInteger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;

/*******************************************************************
 * 功能描述:
 * 创建信息:jtm 2016-4-1
 * 修改信息:
 ********************************************************************/

public class BigIntegerFunc {

	public static void main(String[] args) throws IOException {	//获取用户的输入信息	
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));		
		String str1=null;
		String str2=null;
		System.out.print("第一个乘数是:");
		str1 =br.readLine();		
		System.out.print("第二个乘数是:");
		str2 =br.readLine();
		BigNumMutiply(str1, str2);
	}
	private static void BigNumMutiply(String str1, String str2) {
		// TODO Auto-generated method stub
		BigInteger bigNum1 =new BigInteger(str1);
		BigInteger bigNum2 =new BigInteger(str2);
		BigInteger sumNum =bigNum1.add(bigNum2);//大数相加
		BigInteger subNum =bigNum1.subtract(bigNum2);//大数相减
		BigInteger mutiNum =bigNum1.multiply(bigNum2);//大数相乘
		BigInteger divNum =bigNum1.divide(bigNum2);//大数相除
		BigInteger remNum =bigNum1.remainder(bigNum2);//大数取余
		System.out.println("加"+sumNum+"减"+subNum+"乘积"+mutiNum+"除"+divNum+"余"+remNum);
	}

}

大数相乘算法实现2-----不是用BigInteger
package com.bigInteger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/***
 * 大数相乘
 * *****************************************************************
 */

public class bigInteger {

	public static void main(String[] args) throws IOException{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));		
		String str1=null;
		String str2=null;
		System.out.print("第一个乘数是:");
		str1 =br.readLine();		
		System.out.print("第二个乘数是:");
		str2 =br.readLine();	
		BigNumMutiply(str1, str2);

	}
	public static void BigNumMutiply(String str1,String str2){
		System.out.print("大数相乘:\n" + str1 + "*" + str2 + "="); 
		int temp=0;//用于记录输入大数的正负号个数
		if(str1.charAt(0)=='-'){//判断str1代表的数值的正负,如果负,删除负号,temp++
			StringBuffer sb1=new StringBuffer(str1);
			sb1.delete(0, 1);
			str1=sb1.toString();
			temp++;
		}
		if(str2.charAt(0)=='-'){//判断str2代表的数值的正负,如果负,删除负号,temp++
			StringBuffer sb2=new StringBuffer(str2);
			sb2.delete(0, 1);
			str2=sb2.toString();
			temp++;
		}
		//将字符串转为字符型数组,并反转
		char[] a=new StringBuffer(str1).reverse().toString().toCharArray(); 
		char[] b=new StringBuffer(str2).reverse().toString().toCharArray();		
		int lenA =a.length;
		int lenB =b.length;
		//常识:一般两个数相乘,结果长度不大于两个乘数的长度之和
		int len =lenA+lenB;
		//初始化乘数结果
		int[] result =new int[len];
		// 计算结果集合 
        for (int i = 0; i < a.length; i++) {  
            for (int j = 0; j < b.length; j++) {  
                result[i + j] += (int) (a[i] - '0') * (int) (b[j] - '0');  
            }  
        } 
        //处理结果集合,如果是大于10的就向前一位进位,本身进行除10取余
        for (int i = 0; i < result.length; i++) {  
            if (result[i] > 10) {  
                result[i + 1] += result[i] / 10;  
                result[i] %= 10;  
            }  
        }         
        StringBuffer sb = new StringBuffer();  
        //判断有没有前置的0,有的话跳过
        for (int i = len - 1; i >= 0; i--) {  
        	if (result[i] == 0) {  
                continue;  
            } 
            sb.append(result[i]);  
        } 
        if(temp==0||temp==2){//判断最后大数相乘的正负     	
        	System.out.println(sb.toString());
        }else{
        	sb.insert(0,'-');
        	System.out.println(sb.toString());
        }
	}
}


你可能感兴趣的:(编程基础)