Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class InfiniteInteger implements Comparable<InfiniteInteger>
- {
- // TO DO: Instance Variables
- // Note that it is a good idea to declare them final to
- // prevent you from accidentally modify them.
- int numDigits;
- boolean isNegative;
- int[] IInteger;
- boolean wasPassed = false;
- /**
- * Constructor: Constructs this infinite integer from a string
- * representing an integer.
- * @param s a string represents an integer
- */
- // TO DO: Constructor
- public InfiniteInteger(String s)
- {
- //remove negative
- if (s.contains("-")){
- isNegative = true;
- s = s.substring(1);
- }
- //remove leading 0s
- while(s.length() > 1 && s.charAt(0) == '0'){
- s = s.substring(1);
- }
- //split remaining string with "-" and leading 0s removed
- String[] string = s.split("");
- //give value to instance variables
- int []fuck = new int[string.length];
- //don't know why this is - 1
- numDigits = string.length - 1;
- //add digits from s to II
- for (int i = 1; i < string.length ; i++){
- fuck[i] = Integer.parseInt(string[i]);
- }
- IInteger = new int[string.length-1];
- for(int i = 1 ; i < fuck.length ; i++){
- IInteger[i-1] = (fuck[i]);
- }
- }
- /**
- * Constructor: Constructs this infinite integer from an integer.
- * @param anInteger an integer
- */
- // TO DO: Constructor
- public InfiniteInteger(int anInteger)
- {
- //convert from int to string
- String s = Integer.toString(anInteger);
- //remove negative
- if (s.contains("-")){
- isNegative = true;
- s = s.substring(1);
- }
- //remove leading 0s
- while(s.length() > 1 && s.charAt(0) == '0'){
- s = s.substring(1);
- }
- //split remaining string with "-" and leading 0s removed
- String[] string = s.split("");
- //give value to instance variables
- int []fuck = new int[string.length];
- //don't know why this is - 1
- numDigits = string.length - 1;
- //add digits from s to II
- for (int i = 1; i < string.length ; i++){
- fuck[i] = Integer.parseInt(string[i]);
- }
- IInteger = new int[string.length-1];
- for(int i = 1 ; i < fuck.length ; i++){
- IInteger[i-1] = (fuck[i]);
- }
- }
- /**
- * Gets the number of digits of this infinite integer.
- * @return an integer representing the number of digits
- * of this infinite integer.
- */
- public int getNumberOfDigits()
- {
- // TO DO
- return numDigits;
- }
- /**
- * Checks whether this infinite integer is a negative number.
- * @return true if this infinite integer is a negative number.
- * Otherwise, return false.
- */
- public boolean isNegative()
- {
- // TO DO
- if (isNegative == true){
- return true;
- } else{
- return false;
- }
- }
- /**
- * Calculates the result of this infinite integer plus anInfiniteInteger
- * @param anInfiniteInteger the infinite integer to be added to this
- * infinite integer.
- * @return a NEW infinite integer representing the result of this
- * infinite integer plus anInfiniteInteger
- */
- // TO DO
- public InfiniteInteger plus(final InfiniteInteger anInfiniteInteger)
- {
- //normal case
- if((this.isNegative == false && anInfiniteInteger.isNegative == false || ( this.isNegative == true && anInfiniteInteger.isNegative == true))) {
- //create sum array
- int bigSize;
- int smallSize;
- InfiniteInteger first = new InfiniteInteger("0");
- InfiniteInteger second = new InfiniteInteger("0");
- if(this.getNumberOfDigits() >= anInfiniteInteger.getNumberOfDigits()){
- bigSize = this.getNumberOfDigits();
- smallSize = anInfiniteInteger.getNumberOfDigits();
- //don't know why i have to do this but otherwise i get fk'd
- InfiniteInteger blah = new InfiniteInteger(this.toString());
- first = blah;
- InfiniteInteger blah1 = new InfiniteInteger(anInfiniteInteger.toString());
- second = blah1;
- }else{
- bigSize = anInfiniteInteger.getNumberOfDigits();
- smallSize = this.getNumberOfDigits();
- first = anInfiniteInteger;
- second = this;
- }
- int[] sum = new int[bigSize];
- //****BEGIN ADDING SHIT ****
- int indSum;
- int diff = bigSize - smallSize;
- int numIter = 1;
- for(int i = bigSize - 1; i >= 0; i--){
- //for the no. digs that the other doesnt have
- if(numIter > smallSize){
- indSum = first.IInteger[i];
- //for digs that the other has
- }else{
- indSum = first.IInteger[i] + second.IInteger[i - diff];
- }
- //change shit if more than 10 and not last dig
- if(indSum > 9 && i != 0){
- first.IInteger[i-1]++;
- indSum = indSum - 10;
- }
- //set and increment
- sum[i] = indSum;
- numIter++;
- }
- //****STOP ADDING SHIT ****
- //convert to string
- String strSum = "";
- for(int i : sum){
- Integer.toString(i);
- strSum = strSum + i;
- }
- //if double neg
- if (this.isNegative == true && anInfiniteInteger.isNegative == true){
- strSum = "-" + strSum;
- }
- //return that bad bitch
- InfiniteInteger IIadd = new InfiniteInteger(strSum);
- return IIadd;
- //if only one II is negative call
- }else{
- wasPassed = true;
- if(this.isNegative == true){
- anInfiniteInteger.isNegative = true;
- }
- if (this.isNegative == false){
- anInfiniteInteger.isNegative = false;
- }
- InfiniteInteger IIminus = minus(anInfiniteInteger);
- return IIminus;
- }
- }
- /**
- * Calculates the result of this infinite integer subtracted by anInfiniteInteger
- * @param anInfiniteInteger the infinite integer to subtract.
- * @return a NEW infinite integer representing the result of this
- * infinite integer subtracted by anInfiniteInteger
- */
- public InfiniteInteger minus(final InfiniteInteger anInfiniteInteger)
- {
- // TO DO
- //normal case
- if ( (this.isNegative == true && anInfiniteInteger.isNegative == true) || (this.isNegative == false && anInfiniteInteger.isNegative == false) ){
- int bigSize;
- int smallSize;
- InfiniteInteger first;
- InfiniteInteger second;
- InfiniteInteger temp;
- if(this.getNumberOfDigits() >= anInfiniteInteger.getNumberOfDigits()){
- bigSize = this.getNumberOfDigits();
- smallSize = anInfiniteInteger.getNumberOfDigits();
- first = this;
- second = anInfiniteInteger;
- //special cases since im really bad at coding
- if( this.isNegative == true && anInfiniteInteger.isNegative == true && (second.compareTo(first) == 1) ){
- wasPassed = true;
- }
- if( this.isNegative == true && anInfiniteInteger.isNegative == true && (second.compareTo(first) == -1) ){
- temp = first;
- first = second;
- second = temp;
- wasPassed = false;
- }
- if( this.isNegative == false && anInfiniteInteger.isNegative == false && (second.compareTo(first) == -1) ){
- wasPassed = false;
- }
- if( this.isNegative == false && anInfiniteInteger.isNegative == false && (second.compareTo(first) == 1) ){
- temp = first;
- first = second;
- second = temp;
- wasPassed = true;
- }
- }else{
- bigSize = anInfiniteInteger.getNumberOfDigits();
- smallSize = this.getNumberOfDigits();
- first = anInfiniteInteger;
- second = this;
- //some more fun cases
- if( this.isNegative == true && anInfiniteInteger.isNegative == true && (second.compareTo(first) == 1) ){
- wasPassed = false;
- }
- if( this.isNegative == false && anInfiniteInteger.isNegative == false && (second.compareTo(first) == 1) ){
- first = this;
- second = anInfiniteInteger;
- wasPassed = true;
- }
- if( this.isNegative == false && anInfiniteInteger.isNegative == false && (second.compareTo(first) == -1) ){
- wasPassed = true;
- }
- //didn't actually end up needing this but it seems like it would make sense
- if( this.isNegative == true && anInfiniteInteger.isNegative == true && (second.compareTo(first) == -1) ){
- first = this;
- second = anInfiniteInteger;
- wasPassed = false;
- }
- }
- int[] sum = new int[bigSize];
- //START SUBTRACTING SHIT
- int indSum;
- int diff = bigSize - smallSize;
- int numIter = 1;
- for(int i = bigSize - 1; i >= 0; i--){
- //digits where thisII doesn't have any
- if(numIter > smallSize){
- indSum = first.IInteger[i];
- sum[i] = indSum;
- //normal case
- }else{
- //if number at index being subtracted is less than the other
- if(second.IInteger[i-diff] > first.IInteger[i]){
- //if the number at index is greater than 0 no problemo
- if(first.IInteger[i-1] > 0){
- first.IInteger[i-1]--;
- first.IInteger[i] += 10;
- //but if the number is 0 we have special case
- }else{
- int index = i-1;
- while(first.IInteger[index] == 0){
- first.IInteger[index] = 9;
- index--;
- }
- first.IInteger[index]--;
- first.IInteger[i] += 10;
- }
- }
- indSum = first.IInteger[i] - second.IInteger[i-diff];
- sum[i] = indSum;
- }
- numIter++;
- }
- //END SUBTRACTING SHIT
- String strSum = "";
- for(int i : sum){
- Integer.toString(i);
- strSum = strSum + i;
- }
- if (wasPassed == true){
- strSum = "-" + strSum;
- }
- //returning
- InfiniteInteger IIsub = new InfiniteInteger(strSum);
- return IIsub;
- //if only one II is negative call
- }else{
- if(this.isNegative == false){
- anInfiniteInteger.isNegative = false;
- }
- if(this.isNegative == true){
- anInfiniteInteger.isNegative = true;
- }
- InfiniteInteger IIplus = plus(anInfiniteInteger);
- return IIplus;
- }
- }
- /**
- * Calculates the result of this infinite integer multiplied by anInfiniteInteger
- * @param anInfiniteInteger the multiplier.
- * @return a NEW infinite integer representing the result of this
- * infinite integer multiplied by anInfiniteInteger.
- */
- public InfiniteInteger multiply(final InfiniteInteger anInfiniteInteger)
- {
- // TO DO
- //create sum array
- int bigSize;
- int smallSize;
- InfiniteInteger first;
- InfiniteInteger second;
- if(this.getNumberOfDigits() > anInfiniteInteger.getNumberOfDigits()){
- bigSize = this.getNumberOfDigits();
- smallSize = anInfiniteInteger.getNumberOfDigits();
- first = this;
- second = anInfiniteInteger;
- }else{
- bigSize = anInfiniteInteger.getNumberOfDigits();
- smallSize = this.getNumberOfDigits();
- first = anInfiniteInteger;
- second = this;
- }
- //**LETS GET MULTIPLYING BABY**
- int indProd;
- int newAddBy = 0;
- int numIter = 0;
- int[] firstTerm = new int[bigSize *2];
- int[] secondTerm = new int[bigSize*2];
- String strProdFull = "";
- for(int i = smallSize - 1; i >= 0; i--){
- int count = 0;
- for(int j = bigSize - 1; j >= 0; j--){
- indProd = second.IInteger[i] * first.IInteger[j];
- //add excess from old indProd
- if (count > 0 && newAddBy != 0){
- indProd += newAddBy;
- newAddBy = 0;
- //System.out.println("first if exec");
- }
- //if more than 10 stuff
- if(indProd > 9 && j != 0){
- String strProd = Integer.toString(indProd);
- newAddBy = Integer.parseInt(strProd.substring(0,1));
- //get minusBy
- String add0 = Integer.toString(newAddBy);
- add0 = add0 + "0";
- int minusBy = Integer.parseInt(add0);
- indProd = indProd - minusBy;
- }
- //add
- if(numIter == 1){
- secondTerm[firstTerm.length - 1 - numIter - count] = indProd;
- }else{
- firstTerm[firstTerm.length - 1 - numIter - count] = indProd;
- }
- count++;
- }
- count = 0;
- //have to do this cause i suck at programming
- String strFirst1 = "";
- for(int l : firstTerm){
- Integer.toString(l);
- strFirst1 = strFirst1 + l;
- }
- if(smallSize == 1){
- if((this.isNegative == true && anInfiniteInteger.isNegative== false) || (this.isNegative == false && anInfiniteInteger.isNegative == true)){
- strFirst1 = "-" + strFirst1;
- }
- InfiniteInteger fuck = new InfiniteInteger(strFirst1);
- return fuck;
- }
- //add together
- if(numIter > 0){
- String strSecond = "";
- for(int k : secondTerm){
- Integer.toString(k);
- strSecond = strSecond + k;
- }
- String strFirst = "";
- for(int l : firstTerm){
- Integer.toString(l);
- strFirst = strFirst + l;
- }
- if(!strProdFull.equals("")){
- InfiniteInteger secondII = new InfiniteInteger(strProdFull);
- InfiniteInteger firstII = new InfiniteInteger(strFirst);
- secondII = secondII.plus(firstII);
- strProdFull = secondII.toString();
- }else{
- InfiniteInteger secondII = new InfiniteInteger(strSecond);
- InfiniteInteger firstII = new InfiniteInteger(strFirst);
- secondII = secondII.plus(firstII);
- strProdFull = secondII.toString();
- }
- //have to reset or it gets fucked up
- if(numIter>0){
- for(int q = 0; q < firstTerm.length; q++){
- firstTerm[q] = 0;
- }
- }
- }
- numIter++;
- }
- //**CALM UR DONGER FRIEND**
- if((this.isNegative == true && anInfiniteInteger.isNegative== false) || (this.isNegative == false && anInfiniteInteger.isNegative == true)){
- strProdFull = "-" + strProdFull;
- }
- InfiniteInteger prod = new InfiniteInteger(strProdFull);
- return prod;
- }
- /**
- * Generates a string representing this infinite integer. If this infinite integer
- * is a negative number a minus symbol should be in the front of numbers. For example,
- * "-12345678901234567890". But if the infinite integer is a positive number, no symbol
- * should be in the front of the numbers (e.g., "12345678901234567890").
- * @return a string representing this infinite integer number.
- */
- public String toString()
- {
- // TO DO
- //im bad with zeroes
- if(IInteger.length == 1 && IInteger[0] == 0){
- String s = "0";
- return s;
- }
- String val = "";
- for (int i = 0; i < IInteger.length ; i++){
- val = val + Integer.toString(IInteger[i]);
- }
- if (this.isNegative == true){
- val = "-" + val;
- }
- return val;
- }
- /**
- * Compares this infinite integer with anInfiniteInteger
- * @return either -1, 0, or 1 as follows:
- * If this infinite integer is less than anInfiniteInteger, return -1.
- * If this infinite integer is equal to anInfiniteInteger, return 0.
- * If this infinite integer is greater than anInfiniteInteger, return 1.
- */
- public int compareTo(final InfiniteInteger anInfiniteInteger){
- // TO DO
- //i tried using compareTo() trust me
- //if one or the other is neg
- if (this.isNegative && !anInfiniteInteger.isNegative){
- return -1;
- }else if(!this.isNegative && anInfiniteInteger.isNegative){
- return 1;
- //both positive
- }else if(!this.isNegative && !anInfiniteInteger.isNegative){
- if(this.getNumberOfDigits() > anInfiniteInteger.getNumberOfDigits()){
- return 1;
- }else if(this.getNumberOfDigits() < anInfiniteInteger.getNumberOfDigits()){
- return -1;
- }else{
- for(int i = 0; i < this.getNumberOfDigits(); i++){
- if (this.IInteger[i] > anInfiniteInteger.IInteger[i]){
- return 1;
- }
- if (this.IInteger[i] < anInfiniteInteger.IInteger[i]){
- return -1;
- }
- }
- return 0;
- }
- }
- //both negative
- else if(this.isNegative && anInfiniteInteger.isNegative){
- if(this.getNumberOfDigits() > anInfiniteInteger.getNumberOfDigits()){
- return -1;
- }else if(this.getNumberOfDigits() < anInfiniteInteger.getNumberOfDigits()){
- return 1;
- }else{
- for(int i = 0; i < this.getNumberOfDigits(); i++){
- if (this.IInteger[i] > anInfiniteInteger.IInteger[i]){
- return -1;
- }
- if (this.IInteger[i] < anInfiniteInteger.IInteger[i]){
- return 1;
- }
- }
- return 0;
- }
- }
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement