⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 largeintegeroperation.java

📁 这是一个用java编写的大整数加减的算法
💻 JAVA
字号:
import java.io.*;
class LargeInteger{
	String s1;
	String s2;
	String str;
	char c1[];
	char c2[];
	String sig = "";
	LargeInteger(String s1,String s2){
		this.s1=s1;
		this.s2=s2;
	}
	public static int charToDecimal(char c){
		int decimal = c;
		if (decimal <= 57) //0~9
			decimal -= 48;
		else //a~f
			decimal -= 87;
		return decimal;
	}
	public static char decimalToChar(int decimal){
		if (decimal < 10){ //0~9
			decimal += 48;
			return (char)decimal;
		}
		else{ //a~f
			decimal += 87;
			return (char)decimal;
		}
	}
	public void add(){
        if (s1.length() >= s2.length()) {
            c1=s1.toCharArray();
            c2=s2.toCharArray();
        }
        else{
            c2=s1.toCharArray();
            c1=s2.toCharArray();
        }
        for(int i=0;i<c1.length/2;i++){
            char temp=c1[i];
            c1[i]=c1[c1.length-1-i];
            c1[c1.length-1-i]=temp;
        }
        for(int i=0;i<c2.length/2;i++){
            char temp = c2[i];
            c2[i] = c2[c2.length-1-i];
            c2[c2.length-1-i] = temp;
        }
        char r[]=new char[c1.length+1];
        char cQuotient='0';
        for(int i=0;i<c2.length;i++){
        	int iFirst = charToDecimal(c1[i]);
    		int iSecond = charToDecimal(c2[i]);
    		int iThird = charToDecimal(cQuotient);
    		int sum = iFirst + iSecond + iThird;
    		int iquotient = sum / 16;                  //商、进位
    		int iresidual = sum % 16;                  //余数
    		cQuotient = decimalToChar(iquotient);      //商、进位
    		char cResidual = decimalToChar(iresidual); //余数
            r[i]=cResidual;
        }
        for(int i=c2.length;i<c1.length;i++){
        	int iFirst = charToDecimal(c1[1]);
    		int iThird = charToDecimal(cQuotient);
    		int sum = iFirst + iThird;
    		int iquotient = sum / 16;                  //商、进位
    		int iresidual = sum % 16;                  //余数
    		cQuotient = decimalToChar(iquotient);      //商、进位
    		char cResidual = decimalToChar(iresidual); //余数
    		
            r[i]=cResidual;
        }
        r[c1.length]=cQuotient;
        if(r[c1.length]=='0')
            str="";
        else
            str="1";
        for(int j=r.length-2;j>=0;j--){
            str+=r[j];
        }
	}
	public void sub(){
		if (s1.length() > s2.length()) {
            c1=s1.toCharArray();
            c2=s2.toCharArray();
        }
        else if(s1.length() < s2.length()){
        	sig = "-";
            c2 = s1.toCharArray();
            c1 = s2.toCharArray();
        }
        else{
        	c1=s1.toCharArray();
            c2=s2.toCharArray();
        	for(int i=0;i<s1.length();i++){
                while(c1[i]<c2[1]){
                	sig = "-";
                	c2 = s1.toCharArray();
                    c1 = s2.toCharArray();
                    break;
                }
        	}
        }
        for(int i=0;i<c1.length/2;i++){
            char t=c1[i];
            c1[i]=c1[c1.length-1-i];
            c1[c1.length-1-i]=t;
        }
        for(int i=0;i<c2.length/2;i++){
            char temp=c2[i];
            c2[i]=c2[c2.length-1-i];
            c2[c2.length-1-i]=temp;
        }
        
        char r[]=new char[c1.length];
        char cQuotient='0';
        for(int i=0;i<c2.length;i++){
            int iFirst = charToDecimal(c1[i]);
    		int iSecond = charToDecimal(c2[i]);
    		int iThird = charToDecimal(cQuotient);
    		int sum = iFirst - iSecond - iThird;
            if(sum<0){
                r[i]=decimalToChar(iFirst - iSecond - iThird + 16);
                cQuotient='1';
            }
            else{
                r[i]=decimalToChar(sum);
                cQuotient='0';
            }
        }
        for(int i=c2.length;i<c1.length;i++){
            int iFirst = charToDecimal(c1[i]);
    		int iThird = charToDecimal(cQuotient);
    		int sum = iFirst - iThird; 
            if(sum<0){
                cQuotient='1';
                r[i]='f';
            }
            else{
                r[i]=decimalToChar(sum);
                cQuotient='0';
            }
        }
        if(r[c1.length-1]==0){
            str="";
        }
        else{
            str=""+r[c1.length-1];
        }
        for(int j=r.length-2;j>=0;j--){
            str+=r[j];
        }
	}
	public String toString(){
		String result = sig+str;
		return result;
	}
}
public class LargeIntegerOperation {
	public static void main(String[] args) {
		while (true){
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			String line = "";
	        System.out.println("请输入两个大整数,中间用 and 或 sub 分割。 finish表示退出:");
	        try{
	        	line = br.readLine();//读取一行数据
	        }catch(IOException ioe){}
	        if (line.equals("finish")) //如果用户的一行输入是“finish”,则程序全部退出。
	            System.exit(0);
	        String[] s = line.split(" ");
	        boolean sig1=true;
	        boolean sig2=true;
	        //System.out.println(s[0]+","+s[1]+","+s[2]);
	        if(s[0].substring(0,1).equals("-")){
	        	s[0]=s[0].substring(1,s[0].length());
	        	sig1=false;
	        }
	        if(s[2].substring(0,1).equals("-")){
	        	s[2]=s[2].substring(1,s[2].length());
	        	sig2=false;
	        }
	        LargeInteger li1 = new LargeInteger(s[0],s[2]);
	        LargeInteger li2 = new LargeInteger(s[2],s[1]);
	        if(sig1&&sig2){
	        	if(s[1].equals("and")){
		        	li1.add();
		        	System.out.println("+"+li1);
		        }
		        if(s[1].equals("sub")){
		        	li1.sub();
		        	System.out.println(li1);
		        }
	        }
	        else if(!sig1&&!sig2){
	        	if(s[1].equals("and")){
		        	li1.add();
		        	System.out.println("-"+li1);
		        }
		        if(s[1].equals("sub")){
		        	li2.sub();
		        	System.out.println(li2);
		        }
	        }
	        else if(!sig1&&sig2){
	        	if(s[1].equals("and")){
		        	li2.sub();
		        	System.out.println(li2);
		        }
		        if(s[1].equals("sub")){
		        	li1.add();
		        	System.out.println("-"+li1);
		        }
	        }
	        else {
	        	if(s[1].equals("and")){
		        	li1.sub();
		        	System.out.println(li1);
		        }
		        if(s[1].equals("sub")){
		        	li1.add();
		        	System.out.println("+"+li1);
		        }
	        }
		}	
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -