Vous êtes sur la page 1sur 7

package bigInteger_256;

import java.util.ArrayList;

public class BI256 implements Comparable<BI256>{


public ArrayList<Integer> numberDigits = new ArrayList<Integer>();
public int[] NumberDigits = new int[80];
// String representation as given by the user
private String stringNumber;

//Constructors
BI256(String number) {
if (number.equals("")){
stringNumber = "0";
numberDigits.add(0);
}
else {
// Regex to remove zeros at the beginning of the number
number = number.replaceFirst("^0+(?!$)", "");
stringNumber = number;
for (int index = 0; index < number.length(); index++) {
int curDigNumericVal =
Character.getNumericValue(number.charAt(index));
if (curDigNumericVal == -1)
throw new IllegalArgumentException();
numberDigits.add(curDigNumericVal);
}
}
}

BI256(String number,int z){


if (number.equals("") || number.equals("0")){
for(int y=0;y<80;y++) {
//zero sur 256 bit :)
this.NumberDigits[y]=0;
}
}
else {
// Saving the digits
int numIndex=79;
for (int index =number.length()-1; index>=0; index--) {
//int curDigNumericVal = Integer.parseInt(number.substring(index,
index+1));
int
curDigNumericVal=Character.getNumericValue(number.charAt(index));
// Current char is not a valid digit
if (curDigNumericVal == -1) throw new IllegalArgumentException();
this.NumberDigits[numIndex]= curDigNumericVal;
numIndex--;
}
for(int y=numIndex; y>=0; y--) {
this.NumberDigits[y]=0;
}
}
}

BI256 plus(BI256 otherNumber) {


BI256 finalResult = new BI256("0",0);
int DIGIT, som;
int carry=0;
for(int y=79;y>=0;y--) {
som=this.NumberDigits[y]+otherNumber.NumberDigits[y];
if(som+carry>=10) {
DIGIT=carry+som-10;
carry=(som+carry-DIGIT)/10;
}
else {
DIGIT=som+carry;
carry=0;
}
finalResult.NumberDigits[y]= DIGIT;
}
return finalResult;
}

public String to_string() {


StringBuilder ss= new StringBuilder();
for(int y=0;y<this.numberDigits.size();y++) {
ss.append(this.numberDigits.get(y));
}
String final_s=ss.toString();
final_s = final_s.replaceFirst("^0+(?!$)", "");

return final_s;
}
public String to_string(int i) {
StringBuilder ss= new StringBuilder();
for(int y=0;y<80;y++) {
ss.append(this.NumberDigits[y]);
}
String final_s=ss.toString();
final_s = final_s.replaceFirst("^0+(?!$)", "");
return final_s;
}

BI256 minus(BI256 otherNumber){


int lengthsDifferences = numberDigits.size() -
otherNumber.numberDigits.size();

StringBuilder resultString = new StringBuilder();

int carry = 0;

for (int index = otherNumber.numberDigits.size() - 1; index >=0 ; index--)


{
int biggerNumDig = numberDigits.get(index + lengthsDifferences) -
carry;
int smallerNumDig = otherNumber.numberDigits.get(index);

carry = 0;

if (biggerNumDig < smallerNumDig){


carry = 1;
biggerNumDig += 10;
}

resultString.append(biggerNumDig - smallerNumDig);
}
for (int index = lengthsDifferences - 1; index >=0 ; index--) {
int currDig = numberDigits.get(index);

// Check if carry is needed


if (carry > currDig){
resultString.append(currDig + 10 - carry);
carry = 1;
} else {
resultString.append(currDig - carry);
carry = 0;
}
}

return new BI256(resultString.reverse().toString());


}

BI256 multiply(BI256 otherNumber){


BI256 finalResult = new BI256("0",0);
BI256 the_bigger;
BI256 the_smaller;
if(otherNumber.NumberDigits.length> NumberDigits.length) {
the_bigger = new BI256(otherNumber.to_string(0),0);
the_smaller = new BI256(this.to_string(0),0);
}
else {
the_bigger = new BI256(this.to_string(0),0);
the_smaller = new BI256(otherNumber.to_string(0),0);
}
BI256 fianalResult= new BI256("0",0);
for (int otherNumIndex = the_smaller.NumberDigits.length - 1;
otherNumIndex >=0; otherNumIndex--) {
BI256 rawResult = new BI256("0",0);
int currentNumDigit = the_smaller.NumberDigits[otherNumIndex];
int resultIndex=79;
if(otherNumIndex!=79) {
for(int h=0;h<79-otherNumIndex;h++) {
rawResult.NumberDigits[resultIndex]= 0;
resultIndex--;
}
}
int carry=0;
for(int Index=the_bigger.NumberDigits.length-1; Index>=0; Index--) {
int currentOtherNumDigit = the_bigger.NumberDigits[Index];
int mul= currentNumDigit*currentOtherNumDigit;
int DIGIT=0;
mul+=carry;
if(mul>=10) {
DIGIT=mul-10;
carry=(mul-DIGIT)/10;
}
else {
DIGIT=mul;
carry=0;
}
rawResult.NumberDigits[resultIndex]=DIGIT;
if(resultIndex>0) resultIndex--;
else break;
}
finalResult=finalResult.plus(rawResult);
}
return finalResult;
}

BI256 divide(BI256 otherNumber) {

if (isBigIntZero(otherNumber)) throw new ArithmeticException();


BI256 Other = new BI256(otherNumber.to_string(0));
BI256 First = new BI256(this.to_string(0));
int compareResult = First.compareTo(Other);
if (compareResult == 0)
return new BI256("1",0);
else if (compareResult < 0)
return new BI256("0",0);

BI256 result = new BI256("0",0);


BI256 tempNumber = new BI256("0",0);
BI256 Temp=new BI256(tempNumber.to_string(0));
BI256 ca= new BI256(this.to_string(0));
while (Temp.compareTo(ca) < 0) {
tempNumber = tempNumber.plus(otherNumber);
Temp= new BI256(tempNumber.to_string(0));
if(Temp.compareTo(ca)<=0) result = result.plus(new BI256("1",0));
}

return result;
}

BI256 addMod (BI256 first ,BI256 second) {


return(first.add(second));
}
BI256 SubstractMod (BI256 first ,BI256 second) {
return(first.Substract(second));
}
BI256 multiplyMod (BI256 first ,BI256 second) {
return(first.multiply(second));
}
BI256 divideMod (BI256 first ,BI256 second) {
return(first.divide(second));
}

private boolean isBigIntZero(BI256 number) {


boolean flag=true;
for(int y=79;y>=0;y--) {
if(number.NumberDigits[y]!=0) {
flag=false;
break;
}
}
return flag;
}

private boolean isNegative() {


return false;
}
@Override
public int compareTo(BI256 other) {
// Current is negative, other is positive
if (isNegative() && !other.isNegative())
return -1;

// Current is positive, other is negative


else if (!isNegative() && other.isNegative()){
return 1;
}

// Both are negative


else if (isNegative()){
// Current is negative and has more digits - therefore it is smaller
if (numberDigits.size() > other.numberDigits.size())
return -1;
// Current is negative and has less digits - Therefore it is bigger
else if (numberDigits.size() < other.numberDigits.size())
return 1;

// Both have same number of digits - need to iterate them


else
for (int index = 0; index < numberDigits.size(); index++) {

// Current has bigger negative digit - therefore it is smaller


if (numberDigits.get(index) > other.numberDigits.get(index))
return -1;

// Current has smaller negative digit - therefore it is smaller


else if (numberDigits.get(index) <
other.numberDigits.get(index))
return 1;
}

// If we have reached here, the numbers are completely identical


return 0;
}

// If we have reached here, both numbers are positive

// Current is positive and has more digits - Therefore it is bigger


if (numberDigits.size() > other.numberDigits.size()) {
return 1;
}

// Current is positive and has less digits - Therefore it is smaller


else if (numberDigits.size() < other.numberDigits.size())
return -1;

// Both have same number of digits - need to iterate them


else
for (int index = 0; index < numberDigits.size(); index++) {

// Current has bigger positive digit - therefore it is bigger


if (numberDigits.get(index) > other.numberDigits.get(index))
return 1;

// Current has smaller positive digit - therefore it is smaller


else if (numberDigits.get(index) < other.numberDigits.get(index))
return -1;
}
// If we have reached here, the numbers are completely identical
return 0;
}

@Override
public boolean equals(Object o) {
// self check
if (this == o)
return true;

// null check
if (o == null)
return false;

// type check and cast


if (getClass() != o.getClass())
return false;

BI256 other = (BI256) o;


// field comparison

return other.toString().equals(stringNumber);
}

//main()

package bigInteger_256;
import java.util.ArrayList;
import java.util.Scanner;

public class main {


public static void main(String[] args) {
String firstNumber,secondNumber;
Scanner scanf = new Scanner (System.in);
System.out.println("Enter first number(It should be positive :) : ");
firstNumber=scanf.next();
BI256 firstNumberBI = new BI256(firstNumber,0) ;
System.out.println("Enter second number(It should be positive :)
: ");
secondNumber =scanf.next();
BI256 secondNumberBI = new BI256(secondNumber,0);
System.out.println("The result of plus is: " +
firstNumberBI.plus(secondNumberBI).to_string(0));
System.out.println("The result of multiply is: " +
firstNumberBI.multiply(secondNumberBI).to_string(0));
BI256 f_firstNumberBI= new BI256(firstNumber);
BI256 f_secondNumberBI= new BI256(secondNumber);
System.out.println("The result of minus is: " +
f_firstNumberBI.minus(f_secondNumberBI).to_string());
try {
System.out.println("The result of divide is: " +
firstNumberBI.divide(secondNumberBI).to_string(0));
} catch (ArithmeticException ex){
System.out.println("Can not divide by zero");
}

}
}

Vous aimerez peut-être aussi