/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lab_3;
import [Link];
// if i want convert MAXINT&&MININT to a int number and
/**
*
* @author Kajetan
*/
public class HugeInteger {
private int[] Numbers = new int[1];
private int size;
public HugeInteger(){
size=1;
Numbers[0]=1;
}
public HugeInteger(int number){
if(number>=1){
Random random = new Random();
String HugeInt = [Link]([Link](number)+1); //string'
d to determine size of list!
size = [Link]();
Numbers = new int[size];
for(int i=0;i<size-1;i++){
Numbers[i]=[Link](""+[Link](i));}
}
else{throw new IllegalArgumentException("Number must be greater then 0")
;}
}
public HugeInteger(String val){
try{
String[] important = [Link]("E");
double Value = [Link](important[0]);//just to check boun
ds and positivity
int E = [Link](important[1]);
//error handling section
if(E>Integer.MAX_VALUE-5){
throw new IllegalArgumentException("Number must be format [Link]
where Z<2147483643");}
if((important[0].length()-1>E+2)||((important[0].length()-1>E+1)&&(i
mportant[0].charAt(0)!="-".charAt(0)))){
throw new IllegalArgumentException("Number must be a whole numbe
r!");}
if((E>0&&E+3<0)||(E<0&&E-3>0)){
throw new IllegalArgumentException("Number must be format [Link]
where Z<2147483643");}
if([Link](E).compareTo(important[1])!=0){
throw new IllegalArgumentException("E must be fixed point intege
r");}
if(Value<1&&Value>1){//is a decimal
throw new IllegalArgumentException("Number must be fixed point i
nteger (i.e >1 or <-1)");}
if(important[0].compareTo("")==0)
throw new IllegalArgumentException("String must contain numbers
in format '[Link]'");
try{important[3]="1";
throw new IllegalArgumentException("Error, too many 'E' in strin
g");
}
catch(IndexOutOfBoundsException e){}
if(Value>=10){
throw new IllegalArgumentException ("String must contain numbers
in format '[Link]' where -10>X>10");}
if(important[1].compareTo([Link](E))!=0){
throw new IllegalArgumentException ("String must contain numbers
in format '[Link] where Z is a whole number'");}
size = E+1;
//since E is 10^X there must be X+1 terms
int negShift;
int Val;
if(Value>1){//if positive dont need to do anything
Val= [Link](""+important[0].charAt(0));//can only pars
eInt strings
negShift=0;
}
else{//add the negative to the initial digit
Val= [Link](""+important[0].charAt(0)+important[0].cha
rAt(1));
negShift=1;
}
Numbers = new int[important[0].length()-1-negShift];
Numbers[0]=Val;
for(int i=1;i<important[0].length()-1-negShift;i++){
Numbers[i]= [Link](important[0].charAt(i+1+ne
gShift)); }
}
catch(NumberFormatException|IndexOutOfBoundsException e){
throw new IllegalArgumentException ("String must contain numbers in
format '[Link]' and limit of 2147483643");
}
}
public HugeInteger add(HugeInteger h){
HugeInteger newInt = new HugeInteger();
int newSize;
int newSizeCancel;
int[] newArray;
int[] shorts;
int[] longs;
int range = abs([Link]);//difference in terms
int signThis;
int signThat;
int signShort;
int signLong;
int shorter;
int front=0;
int end=0;
int longer;
if([Link][0]>0){signThis=1;}
else{signThis=-1;}
if([Link][0]>0){signThat=1;}
else{signThat=-1;}
if([Link]==[Link]){//determining size of final list
if([Link]>[Link]){
shorter = [Link];
longer = [Link];
shorts = [Link];
longs = [Link];
signShort = signThat;
signLong = signThis;
}
else{//even if same length doesnt matter
shorter=[Link];
longer=[Link];
shorts = [Link];
longs = [Link];
signShort = signThis;
signLong = signThat;
}
range =0;
if(signThis!=signThat){
for(int i=0;i<shorter;i++){
if([Link][i]!=[Link][i]){break;}
front++;//front values that zero out
}
}
newSize=longer;
}
else{
if([Link]>[Link]){
shorter = [Link];
shorts = [Link];
signShort = signThat;
signLong = signThis;
longs = [Link];
longer = [Link];
}
else{
shorter = [Link];
longer = [Link];
shorts = [Link];
longs = [Link];
signShort = signThis;
signLong = signThat;
}
if(shorter+range>longer){//check if shorter is shorter list (not ju
st number)
newSize = shorter+range;
}
else{newSize=longer;}
}
if(shorter+range==longer&&signThis!=signThat){//if this is true then if
statement above did not activate!
for(int i=newSize-1;i>=0;i++){
if(shorts[i-range]!=longs[i]){break;}
end++;//end values that zero out
}
}
// shorter use finished
newSizeCancel = front + end;
/*if(newSize>Integer.MAX_VALUE-5){ //making sure size is within arra
y limits
throw new IllegalArgumentException("Size of new list is too big!
");//exceeded max list*/
newSize-=newSizeCancel;
newArray = new int[newSize];
for(int i=newSize-1;i>0;i++){
if(shorter+range>i&&longer>i){newArray[i]=abs(shorts[i+front]*si
gnShort+longs[i+front-range]*signLong);}
else{
if(longer>i){
newArray[i]=longs[i+front];}
else if(shorter+range>i){
newArray[i]=longs[i+front];}
}
}
/*for(int i=newSize-1+front;i>front;i--){//asigning values
if(i>[Link]){
newArray[j]=[Link][i];}
else if(i>[Link]){
newArray[j]=[Link][i];}
newArray[j]+=abs([Link][i]*signThis+[Link][i]*signThat);
}
too ambitious trying to do in one forloop*/
for(int j=newSize-1;j>0;j++){
if(newArray[j]>=10){//if added term exceeded single digit(i.e carryo
ver)
newArray[j-1]+=1;
newArray[j]-=10;}
if(newArray[j]<0){
newArray[j-1]-=1;
newArray[j]+=10;
}
}
if(abs(newArray[0])>10){//if list is to small(addiion made size+1)
int[] tempArray = new int[newSize+1];
tempArray[0]=1*signThis;//same sign if adding
newArray[0]-=10;
for(int i=1;i<newSize+1;i++){
tempArray[i]=newArray[i-1];}
newArray = tempArray;
}
[Link] = newArray;
[Link] = newSize+1;
return newInt;
}
public HugeInteger subtract(HugeInteger h){//adding the negative version of
h
HugeInteger newInteger = h;
[Link][0]*=(-1);
return add(newInteger);
}
public int compareTo(HugeInteger h){//returns which integer is bigger
if([Link]>[Link]){return 1;}
else if ([Link]<[Link]){return -1;}
else{
int i=0;
int maxSize;
if([Link]>[Link]){maxSize=[Link];}
else{maxSize=[Link];}
while(i<maxSize){
if([Link][i]!=[Link][i]){break;}
}
return compareInt([Link][i],[Link][i]);
}
}
public int compareInt(int n, int m){
if(n>m){return 1;}
else if (n<m){return -1;}
else{return 0;}
}
public int abs(int n){//returns absolute value
if(n<0){
return n*(-1);
}
else{
return n;
}
}
public HugeInteger multiply(HugeInteger h){
String[] multiThis = [Link]().split("E");//gets individual decima
l and power
String[] multiThat = [Link]().split("E");
int totalSize = [Link](multiThis[1])+[Link](multiTha
t[1]);
if(totalSize<0){
throw new IllegalArgumentException("Number is to large for array to
handle!");
}
double Decimal = [Link](multiThis[0])*[Link](mul
tiThat[0]);
if(Decimal>10){//will only happen once as the max can be (9.9999...)*(9.
9999...) = 99.9999...
Decimal/=10;
totalSize+=1;
}
String newValue =[Link](Decimal)+"E"+[Link](totalSize);/
/creates string in correct foemat
return new HugeInteger(newValue);
}
@Override
public String toString(){
String Decimal = [Link](Numbers[0])+".";
if(size==1){
Decimal+="E0";
}
else{
for(int i=1;i<size;i++){
Decimal+=Numbers[i];
}
Decimal += "E" + (size-1);
}
return Decimal;
}
}