hqt

Untitled

hqt
Jul 16th, 2013
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.13 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 res;
  54.     }
  55.    
  56.     public static void main(String[] args) {
  57.         String a = "999";
  58.         char b = '9';
  59.         String res = MultiplyPositive(a, b);
  60.         System.out.println("Mul: " + res);
  61.        
  62.         String add1 = "3";
  63.         String add2 = "333";
  64.         res = AddPositiveNum(add1, add2);
  65.         System.out.println("Add: " + res);
  66.        
  67.         String big1 = "999";
  68.         String big2 = "012";
  69.         res = MultiplyPositive(big1, big2);
  70.         System.out.println(res);
  71.        
  72.         String gen1 = "999";
  73.         String gen2 = "-012";
  74.         res = Multiply(gen1, gen2);
  75.         System.out.println(res);
  76.     }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment