Last active
August 29, 2015 14:10
-
-
Save h2rashee/8f96b12e89cefda42281 to your computer and use it in GitHub Desktop.
Arithmetic with string numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
String s = add("1234","567"); | |
1234 | |
567 | |
---- | |
1801 | |
=> "1801" | |
*/ | |
import java.util.*; | |
class Adder | |
{ | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
System.out.print("First number: "); | |
String t1 = sc.nextLine(); | |
System.out.print("Second number: "); | |
String t2 = sc.nextLine(); | |
Add ad = new Add(); | |
System.out.println("Sum: " + ad.add(t1, t2)); | |
} | |
} | |
class Add | |
{ | |
public String adding(String a, String b) { | |
int aLen = a.length(); | |
int bLen = b.length(); | |
boolean sameLen = aLen == bLen; | |
boolean aLonger = aLen > bLen; | |
// Length of the longest number so we iterate that much | |
int max = aLonger ? aLen : bLen; | |
StringBuilder sb = new StringBuilder(max + 1); | |
// What is the size difference between the two numbers | |
int diff = max - (aLonger ? bLen : aLen); | |
int carryover = 0; // No carryover by default | |
// Where we have digits in both rows, calculate the sum | |
for(int i = max - 1; i >= 0 && i-diff >= 0; i--) { | |
char chA = aLonger ? a.charAt(i) : a.charAt(i-diff); | |
char chB = aLonger ? b.charAt(i-diff) : b.charAt(i); | |
int total = add(chA, chB) + carryover; | |
// Compute the carryover as necessary | |
if(total > 9) { | |
carryover = 1; | |
total = total % 10; | |
} else { | |
carryover = 0; | |
} | |
sb.append(total); | |
} | |
// If the number of digits in both numbers weren't equal, we have a few | |
// trailing ones to deal with | |
if(!sameLen) { | |
for(int i = diff - 1; i >= 0; i--) { | |
// Work with the number that has the extra digits | |
int total = aLonger ? a.charAt(i) - '0' : b.charAt(i) - '0'; | |
total += carryover; | |
// Compute the carryover as necessary | |
if(total > 9) { | |
carryover = 1; | |
total = total % 10; | |
} else { | |
carryover = 0; | |
} | |
sb.append(total); | |
} | |
// Final case where we have the carryover hanging past the last digit | |
if(carryover == 1) { | |
sb.append(carryover); | |
} | |
} | |
return sb.reverse().toString(); | |
} | |
// Given two character digits, calculate the sum and return as an integer | |
private int add(char c1, char c2) { | |
return (c1 - '0') + (c2 - '0'); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
String s = add("1234","567"); | |
1234 | |
567 | |
---- | |
1801 | |
=> "1801" | |
*/ | |
import java.util.*; | |
class Arithmetic { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
System.out.print("First number: "); | |
String t1 = sc.nextLine(); | |
System.out.print("Second number: "); | |
String t2 = sc.nextLine(); | |
Add ad = new Add(); | |
System.out.println("Addition: " + ad.add(t1, t2)); | |
} | |
} | |
class Add { | |
public String add(String a, String b) { | |
// Reverse the strings so we can work from the units place up | |
return adding(reverse(a), reverse(b)); | |
} | |
private String adding(String a, String b) { | |
int aLen = a.length(); | |
int bLen = b.length(); | |
// Length of the longest number so we iterate that much | |
int max = aLen; | |
if(bLen > max) { | |
max = bLen; | |
} | |
int carryOver = 0; // No carryover by default | |
String result = ""; | |
// Iterate over the length of the longest number | |
for(int i = 0; i < max; i++) { | |
int addition; | |
// If the second number was shorter and we passed its length | |
// we consider the units place as zero | |
if(i >= bLen) { | |
addition = addDigits(Character.toString(a.charAt(i)), "0") | |
+ carryOver; | |
} else if(i >= aLen) { | |
// If the first number was shorter and we passed its length | |
// we consider the units place as zero | |
addition = addDigits("0", Character.toString(b.charAt(i))) | |
+ carryOver; | |
} else { | |
// Both digits need to be added | |
addition = addDigits(Character.toString(a.charAt(i)), | |
Character.toString(b.charAt(i))) | |
+ carryOver; | |
} | |
// Pre-pend the addition result | |
result = getUnit(addition) + result; | |
// Save new carryover | |
carryOver = getCarryOver(addition); | |
} | |
// Deal with carry-over, if any, at the very end | |
if(carryOver != 0) { | |
result = Integer.toString(carryOver) + result; | |
} | |
return result; | |
} | |
// Given a string, reverse it | |
private String reverse(String str) { | |
StringBuffer sb = new StringBuffer(str); | |
return sb.reverse().toString(); | |
} | |
// Given two string digits, add them and return the result as an integer | |
private int addDigits(String a, String b) { | |
int aNum = Integer.parseInt(a); | |
int bNum = Integer.parseInt(b); | |
return aNum + bNum; | |
} | |
// Given a number, get its unit place | |
private String getUnit(int num) { | |
return Integer.toString(num%10); | |
} | |
// Given a number, get everything but the units place | |
// In this particular calling case, it's the carryover | |
private int getCarryOver(int num) { | |
return (num/10); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Test cases: