Advertisement
Egonau

Untitled

Apr 9th, 2023
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.80 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3.  
  4. final class BigInt{
  5.     final ArrayList<Integer> digits = new ArrayList<>();
  6.     final boolean sign; //true - >=0, false - <0
  7.     public BigInt(String str_digits) {
  8.         String[] str = str_digits.split("");
  9.         if (str_digits.contains("-")){
  10.             sign = false;
  11.             for(int i = str_digits.length()-1;i>=1;--i){
  12.                 digits.add(Integer.parseInt(str[i]));
  13.             }
  14.         }
  15.         else{
  16.             sign = true;
  17.             for(int i = str_digits.length()-1;i>=0;--i){
  18.                 digits.add(Integer.parseInt(str[i]));
  19.             }
  20.         }
  21.     }
  22.     public BigInt(Boolean sign,ArrayList<Integer> digits){
  23.         this.sign = sign;
  24.         this.digits.addAll(digits);
  25.     }
  26.  
  27.     @Override
  28.     public String toString() {
  29.         StringBuilder str = new StringBuilder();
  30.         if (!sign){
  31.             str.append("-");
  32.         }
  33.         for (int i = digits.size()-1;i>=0;--i) {
  34.             str.append(digits.get(i));
  35.         }
  36.         return str.toString();
  37.     }
  38.     public static BigInt valueOf(long num){
  39.         return new BigInt(String.valueOf(num));
  40.     }
  41.     public int compareTo(BigInt num){
  42.         if (num.sign && !this.sign){
  43.             return -1;
  44.         }
  45.         if (!num.sign && this.sign){
  46.             return 1;
  47.         }
  48.         if (num.digits.size()>this.digits.size()){
  49.             if (num.sign){
  50.                 return -1;
  51.             }
  52.             else{
  53.                 return 1;
  54.             }
  55.         }
  56.         if (num.digits.size()<this.digits.size()){
  57.             if (num.sign){
  58.                 return 1;
  59.             }
  60.             else{
  61.                 return -1;
  62.             }
  63.         }
  64.         for (int i =num.digits.size()-1;i>=0;--i){
  65.             if (this.digits.get(i)>num.digits.get(i)){
  66.                 if (num.sign){
  67.                     return 1;
  68.                 }
  69.                 else{
  70.                     return -1;
  71.                 }
  72.             }
  73.             if (this.digits.get(i)<num.digits.get(i)){
  74.                 if (num.sign){
  75.                     return -1;
  76.                 }
  77.                 else{
  78.                     return 1;
  79.                 }
  80.             }
  81.         }
  82.         return 0;
  83.     }
  84.     public BigInt invert(BigInt num){
  85.         return new BigInt(!num.sign, num.digits);
  86.     }
  87.     public BigInt add(BigInt num){
  88.         ArrayList<Integer> new_num_digits = new ArrayList<>();
  89.         if ((num.sign && this.sign) || (!num.sign && !this.sign)){
  90.             int count = 0;
  91.             for (int i = 0;i<Math.max(num.digits.size(),this.digits.size());++i){
  92.                 if (num.digits.size()>i && this.digits.size()>i){
  93.                     count+=num.digits.get(i)+this.digits.get(i);
  94.                 }
  95.                 else{
  96.                     if (this.digits.size()<=i){
  97.                         count+=num.digits.get(i);
  98.                     }
  99.                     else{
  100.                         count+=this.digits.get(i);
  101.                     }
  102.                 }
  103.                 new_num_digits.add(count%10);
  104.                 count = count/10;
  105.             }
  106.             if (count!=0){
  107.                 new_num_digits.add(count);
  108.             }
  109.         }
  110.         return new BigInt(num.sign,new_num_digits);
  111.     }
  112.     public BigInt subtract(BigInt num){
  113.         ArrayList<Integer> subtructed = new ArrayList<>();
  114.         ArrayList<Integer> subtractor = new ArrayList<>();
  115.         if (this.compareTo(num)==0){
  116.             return new BigInt("0");
  117.         }
  118.     }
  119.  
  120. }
  121. public class Main {
  122.  
  123.     public static void main(String[] args) {
  124.         BigInt b = new BigInt("-11");
  125.         BigInt a = new BigInt("-1");
  126.         System.out.println(a.compareTo(b));
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement