一个一百亿的计算器的实现(java代码实现)
源地址:http://blog.csdn.net/wilsonpeng3/article/details/22441009
网上一搜一大把,搜出来的结果几乎都是我很崇敬的张孝祥老师写的这道题的思路,甚至有的直接把原文copy paste过来,没有一个用代码实现了的。于是自己琢磨了下,这里发布出来。虽然标题是一百亿,但实现结果可用于任意大整数。
直接上代码。这里只实现了大整数相加。有了这个,不难实现减、乘等其他操作。代码复制粘帖即可运行。
MyBigInteger.java
import java.util.ArrayList;import java.util.regex.Matcher;import java.util.regex.Pattern; /** * Created by Rocky on 14-3-26. */public class MyBigInteger { private char sign = '0'; // 0 表示正数 - 表示负数 private byte[] data; public MyBigInteger() { this.data = "0".getBytes(); } public MyBigInteger(String value) throws Exception { //正则表达式,输入字符串要求以 零个或一个 - 开头,其余都是数字 Pattern pattern = Pattern.compile("^-?\\d+$"); if (value == null || value.length() <= 0) { value = "0"; } Matcher matcher = pattern.matcher(value); if (!matcher.find()) { throw new Exception("the value is not a number string :" + value); } //获取字符串的第一个字符 char firstChar = value.charAt(0); //data应该保存的是第一个非0数字后的字符串 if (firstChar == '-') { //说明输入的是个负数 if (value.length() >=2) { sign = firstChar; value = value.substring(1); value = getTemp(value); //得到value中第一个非0后的子字符串。 } } else { value = getTemp(value); } this.data = value.getBytes(); } /** * 得到一个字符串第一个非0后的字符串,如果没有找到,则返回 "0" 。如:00003435534,则返回3435534 * @return */ private String getTemp(String value){ Pattern pattern = Pattern.compile("[^0]{1}"); Matcher matcher = pattern.matcher(value); if (matcher.find()) { value = value.substring(matcher.start()); } else { value = "0"; } return value; } public MyBigInteger add(MyBigInteger other) { MyBigInteger result = new MyBigInteger(); int thisLength = this.data.length; int otherLength = other.data.length; int shorterLength = thisLength > otherLength ? otherLength : thisLength; ArrayList
MyBigIntegerTest.java
import junit.framework.TestCase;import java.math.BigInteger; /** * Created by Rocky on 14-3-26. */public class MyBigIntegerTest extends TestCase { public void test1() throws Exception { String a1 = "-5453450543044355356576980545345054545453453454344435353254545345054304435535657698087756454543454345454534534543444353532545453450543044355356454543454354353450136546534534545345345054353450136546534534545345345043044355356576980657698087756454543454354353450136546534534545345345054353450136546534534545345345043044355356576980877564545434543543534501877564545434543543534501"; String b1 = "4545453453454344435353254545345054304435535657698087756454543454354345454534534543444353532545453450543044355356576980877564545434545454534534564545434543543534501365465345345453453450543534501365465345345453453450430443553565769804344435353254545345054304435535657698087756454543454354353450136546534534545345345043543534501365465345345453453450534501365465345345453453450"; MyBigInteger a = new MyBigInteger(a1); MyBigInteger b = new MyBigInteger(b1); MyBigInteger c = a.add(b); System.out.println(c); BigInteger a2 = new BigInteger(a1); BigInteger b2 = new BigInteger(b1); BigInteger c2 = a2.add(b2); System.out.println(c2); System.out.println(c2.toString().equals(c.toString())); }