Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Arrays;
- final class BigInt{
- final ArrayList<Integer> digits = new ArrayList<>();
- final boolean sign; //true - >=0, false - <0
- public BigInt(String str_digits) {
- String[] str = str_digits.split("");
- if (str_digits.contains("-")){
- sign = false;
- for(int i = str_digits.length()-1;i>=1;--i){
- digits.add(Integer.parseInt(str[i]));
- }
- }
- else{
- sign = true;
- for(int i = str_digits.length()-1;i>=0;--i){
- digits.add(Integer.parseInt(str[i]));
- }
- }
- }
- public BigInt(Boolean sign,ArrayList<Integer> digits){
- this.sign = sign;
- this.digits.addAll(digits);
- }
- @Override
- public String toString() {
- StringBuilder str = new StringBuilder();
- if (!sign){
- str.append("-");
- }
- for (int i = digits.size()-1;i>=0;--i) {
- str.append(digits.get(i));
- }
- return str.toString();
- }
- public static BigInt valueOf(long num){
- return new BigInt(String.valueOf(num));
- }
- public int compareTo(BigInt num){
- if (num.sign && !this.sign){
- return -1;
- }
- if (!num.sign && this.sign){
- return 1;
- }
- if (num.digits.size()>this.digits.size()){
- if (num.sign){
- return -1;
- }
- else{
- return 1;
- }
- }
- if (num.digits.size()<this.digits.size()){
- if (num.sign){
- return 1;
- }
- else{
- return -1;
- }
- }
- for (int i =num.digits.size()-1;i>=0;--i){
- if (this.digits.get(i)>num.digits.get(i)){
- if (num.sign){
- return 1;
- }
- else{
- return -1;
- }
- }
- if (this.digits.get(i)<num.digits.get(i)){
- if (num.sign){
- return -1;
- }
- else{
- return 1;
- }
- }
- }
- return 0;
- }
- public BigInt invert(BigInt num){
- return new BigInt(!num.sign, num.digits);
- }
- public BigInt add(BigInt num){
- ArrayList<Integer> new_num_digits = new ArrayList<>();
- if ((num.sign && this.sign) || (!num.sign && !this.sign)){
- int count = 0;
- for (int i = 0;i<Math.max(num.digits.size(),this.digits.size());++i){
- if (num.digits.size()>i && this.digits.size()>i){
- count+=num.digits.get(i)+this.digits.get(i);
- }
- else{
- if (this.digits.size()<=i){
- count+=num.digits.get(i);
- }
- else{
- count+=this.digits.get(i);
- }
- }
- new_num_digits.add(count%10);
- count = count/10;
- }
- if (count!=0){
- new_num_digits.add(count);
- }
- }
- return new BigInt(num.sign,new_num_digits);
- }
- public BigInt subtract(BigInt num){
- ArrayList<Integer> subtructed = new ArrayList<>();
- ArrayList<Integer> subtractor = new ArrayList<>();
- if (this.compareTo(num)==0){
- return new BigInt("0");
- }
- }
- }
- public class Main {
- public static void main(String[] args) {
- BigInt b = new BigInt("-11");
- BigInt a = new BigInt("-1");
- System.out.println(a.compareTo(b));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement