hqt

Java Big Num Processing

hqt
Jul 16th, 2013
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. package com.DTT;
  2.  
  3. public class BigNumOperator {
  4.    
  5.     public static String AddPositiveNum(String a, String b) {
  6.         // normalize two string
  7.         if (a.length() < b.length()) {
  8.             String tmp = a; a = b; b = tmp;
  9.         }
  10.         // make same length
  11.         while(a.length() != b.length()) b = "0" + b;
  12.        
  13.         int remain = 0;
  14.         StringBuilder res = new StringBuilder();
  15.         for (int i = a.length() - 1; i >= 0; i--) {
  16.             int add = (a.charAt(i) - 48) + (b.charAt(i) - 48) + remain;
  17.             res.append(add % 10);
  18.             remain = add / 10;
  19.         }
  20.         if (remain != 0) res.append(remain);
  21.         return res.reverse().toString();
  22.     }
  23.    
  24.     public static String MultiplyPositive(String num1, char num2) {
  25.         int remain = 0;
  26.         int num = num2 - 48;
  27.         StringBuilder res = new StringBuilder();
  28.         for (int i = num1.length() - 1; i >= 0; i--) {
  29.             int mul = num * (num1.charAt(i)-48) + remain;
  30.             res.append(mul % 10);
  31.             remain = mul / 10;
  32.         }
  33.         if (remain != 0) res.append(remain);
  34.         return res.reverse().toString();
  35.     }
  36.    
  37.     public static String MultiplyPositive(String num1, String num2) {
  38.         String res = "";
  39.         for (int i = num2.length() - 1; i >= 0; i--) {
  40.             String mul = MultiplyPositive(num1, num2.charAt(i));
  41.             for (int j = 0; j < num2.length() - 1 - i; j++) mul += "0";
  42.             res = AddPositiveNum(res, mul);
  43.         }
  44.         return res;
  45.     }
  46.    
  47.     public static String Multiply(String num1, String num2) {
  48.         int op = 1;
  49.         if (num1.charAt(0) == '-') { num1 = num1.substring(1, num1.length()); op *= -1; }
  50.         if (num2.charAt(0) == '-') { num2 = num2.substring(1, num2.length()); op *= -1; }
  51.         String res = MultiplyPositive(num1, num2);
  52.         if (op == -1) res = '-' + res;
  53.         return Normalize(res);
  54.     }
  55.    
  56.     public static String Normalize(String num) {
  57.         if (num.length() > 1 && num.charAt(0) == '0') return Normalize(num.substring(1, num.length()));
  58.         else return num;
  59.     }
  60.    
  61.     public static void main(String[] args) {
  62.         String a = "999";
  63.         char b = '9';
  64.         String res = MultiplyPositive(a, b);
  65.         System.out.println("Mul: " + res);
  66.        
  67.         String add1 = "3";
  68.         String add2 = "333";
  69.         res = AddPositiveNum(add1, add2);
  70.         System.out.println("Add: " + res);
  71.        
  72.         String big1 = "999";
  73.         String big2 = "012";
  74.         res = MultiplyPositive(big1, big2);
  75.         System.out.println("Mul Big Num: " + res);
  76.        
  77.         String gen1 = "999";
  78.         String gen2 = "-012";
  79.         res = Multiply(gen1, gen2);
  80.         System.out.println("Mul General: " + res);
  81.        
  82.         System.out.println("Normalize: " + Normalize("0000"));
  83.     }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment