1. 程式人生 > >大數相加,大數相除,大數相除,大數相減

大數相加,大數相除,大數相除,大數相減


public class BigNumberOper{
	private static final double b_ = 2.7182818284590452354;
	private static final double c_ = 3.14159265358979323846;

	public static String add_(String a1, String a2, int b) throws Exception {
		return d(add_(a1, a2), b);
	}

	public static String add_(String a1, String a2) throws Exception {
		if (a1 == null || a2 == null)
			throw new Exception("ParamNullException");
		a1 = a1.trim();
		a2 = a2.trim();
		int b1 = 0, b2 = 0;
		String c1 = "0", c2 = "0";
		String d = null;
		String e = null;
		int f = a1.indexOf('.');
		if (f != -1) {
			String[] g = a1.split("\\.");
			if (g.length != 2)
				throw new Exception("IllegalDotException");
			g[1] = e(g[1]);
			b1 = g[1].length();
			a1 = g[0];
			c1 = g[1];
		}
		f = a2.indexOf('.');
		if (f != -1) {
			String[] g = a2.split("\\.");
			if (g.length != 2)
				throw new Exception("IllegalDotException");
			g[1] = e(g[1]);
			b2 = g[1].length();
			a2 = g[0];
			c2 = g[1];
		}
		if (b1 > b2) {
			int h = b1 - b2;
			for (int i = 0; i < h; i++)
				c2 += "0";
		} else if (b1 < b2) {
			int h = b2 - b1;
			for (int i = 0; i < h; i++)
				c1 += "0";
		}
		int j = b1 > b2 ? b1 : b2;
		e = f(c1.toCharArray(), c2.toCharArray());
		d = add(a1, a2);
		if (e.length() > j) {
			String k = e.substring(0, 1);
			e = e.substring(1);
			d = add(d, k);
		}
		d = d + "." + e;
		d = e(d);
		if (d.charAt(d.length() - 1) == '.')
			d = d.substring(0, d.length() - 1);
		return d;
	}

	public static String add(String a1, String a2) throws Exception {
		if (a1 == null || a2 == null)
			throw new Exception("NullParamException");
		char[] b = a1.trim().toCharArray();
		char[] c = a2.trim().toCharArray();
		return add(b, c);
	}

	public static String add(char[] a1, char[] a2) throws Exception {
		if (!g(a1) || !g(a2))
			throw new Exception("IllegalCharacterException");
		a1 = h(a1);
		a2 = h(a2);
		return f(a1, a2);
	}

	private static String f(char[] a1, char[] a2) {
		int b1 = a1.length;
		int b2 = a2.length;
		int c = b1 > b2 ? b1 : b2;
		b1 -= 1;
		b2 -= 1;
		char[] d = new char[c + 2];
		int e = 0, f = 0, g, h, i;
		while (b1 >= 0 || b2 >= 0) {
			if (b1 < 0)
				g = '0';
			else
				g = a1[b1];
			if (b2 < 0)
				h = '0';
			else
				h = a2[b2];
			i = g - '0' + h - '0';
			if (f > 0)
				i += 1;
			if (i > 9) {
				f = 1;
				i %= 10;
			} else
				f = 0;
			d[e++] = (char) (i + '0');
			b1--;
			b2--;
		}
		if (f == 1)
			d[e++] = '1';
		StringBuffer j = new StringBuffer();
		while (e > 0)
			j.append(d[--e]);
		return j.toString();
	}

	public static String mult_(String a1, String a2) throws Exception {
		if (a1 == null || a2 == null)
			throw new Exception("ParamNullException");
		a1 = a1.trim();
		a2 = a2.trim();
		int b1 = 0, b2 = 0;
		String c = null;
		int d = a1.indexOf('.');
		if (d != -1) {
			String[] e = a1.split("\\.");
			if (e.length != 2)
				throw new Exception("IllegalDotException");
			e[1] = e(e[1]);
			b1 = e[1].length();
			a1 = e[0] + e[1];
		}
		d = a2.indexOf('.');
		if (d != -1) {
			String[] e = a2.split("\\.");
			if (e.length != 2)
				throw new Exception("IllegalDotException");
			e[1] = e(e[1]);
			b2 = e[1].length();
			a2 = e[0] + e[1];
		}
		c = mult(a1, a2);
		int f = c.length() - b1 - b2;
		if (b1 + b2 != 0) {
			c = c.substring(0, f) + "." + c.substring(f);
			c = e(c);
		}
		if (c.charAt(c.length() - 1) == '.')
			c = c.substring(0, c.length() - 1);
		return c;
	}

	public static String mult_(String a1, String a2, int b) throws Exception {
		return d(mult_(a1, a2), b);
	}

	private static String d(String a, int b) throws Exception {
		if (b < 0)
			b = 0;
		int c = a.indexOf('.');
		if (c != -1) {
			String[] d = a.split("\\.");
			a = d[0];
			if (b == 0)
				return a;
			int d1 = d[1].length();
			if (d1 > b) {
				char e = d[1].charAt(b);
				d[1] = d[1].substring(0, b);
				int f = d[1].length();
				if ((e - '0') >= 5) {
					d[1] = f(d[1].toCharArray(), "1".toCharArray());
					if (d[1].length() > f) {
						a = add(a, "1");
						d[1] = d[1].substring(1);
					}
				}
			} else if (d1 < b) {
				for (int g = 0; g < b - d1; g++)
					d[1] += "0";
			}
			a += ".";
			a += d[1];
		}
		return a;
	}

	public static String mult(String a1, String a2) throws Exception {
		if (a1 == null || a2 == null)
			throw new Exception("NullParamException");
		char[] b = a1.trim().toCharArray();
		char[] c = a2.trim().toCharArray();
		return mult(b, c);
	}

	public static String mult(char[] a1, char[] a2) throws Exception {
		if (!g(a1) || !g(a2))
			throw new Exception("IllegalCharacterException");
		a1 = h(a1);
		a2 = h(a2);
		int b1 = a1.length;
		int b2 = a2.length;
		int c, d, e = 0, f = 0, g;
		c = b1 + b2;
		char[] h = new char[c + 1];
		while (c > 0) {
			h[--c] = '0';
		}
		for (c = b1 - 1; c >= 0; c--) {
			f = e;
			e++;
			g = 0;
			for (d = b2 - 1; d >= 0; d--) {
				g = (h[f] - '0') + g + (a2[d] - '0') * (a1[c] - '0');
				h[f] = (char) ((g % 10) + '0');
				f++;
				g /= 10;
			}
			if (g > 0)
				h[f++] = (char) (g + '0');
		}
		StringBuffer i = new StringBuffer();
		while (f > 0)
			i.append((h[--f]));
		return i.toString();
	}

	public static String div_(String a1, String a2) throws Exception {
		return div_(a1, a2, 0);
	}

	public static String div_(String a1, String a2, int b) throws Exception {
		if (a1 == null || a2 == null)
			throw new Exception("ParamNullException");
		a1 = a1.trim();
		a2 = a2.trim();
		int c1 = 0, c2 = 0;
		int d = a1.indexOf('.');
		if (d != -1) {
			String[] e = a1.split("\\.");
			if (e.length != 2)
				throw new Exception("IllegalDotException");
			e[1] = e(e[1]);
			c1 = e[1].length();
			a1 = e[0] + e[1];
		}
		d = a2.indexOf('.');
		if (d != -1) {
			String[] e = a2.split("\\.");
			if (e.length != 2)
				throw new Exception("IllegalDotException");
			e[1] = e(e[1]);
			c2 = e[1].length();
			a2 = e[0] + e[1];
		}
		if (c1 > c2) {
			int f = c1 - c2;
			for (int g = 0; g < f; g++)
				a2 += "0";
		} else if (c1 < c2) {
			int f = c2 - c1;
			for (int g = 0; g < f; g++)
				a1 += "0";
		}
		return div(a1, a2, b);
	}

	public static String div(String a1, String a2, int b) throws Exception {
		String[] c = i(a1, a2);
		if (c == null)
			throw new Exception("ResultNullException");
		;
		if (b <= 0)
			return c[0];
		String d = c[0] + ".";
		String e = c[1];
		while ((b--) > 0) {
			c = i(e + "0", a2);
			d += c[0];
			e = c[1];
		}
		return d;
	}

	public static String div(String a1, String a2) throws Exception {
		return i(a1, a2)[0];
	}

	private static String[] i(String a1, String a2) throws Exception {
		if (a1 == null || a2 == null)
			throw new Exception("NullParamException");
		if (!g(a1) || !g(a2))
			throw new Exception("IllegalCharacterException");
		a1 = h(a1);
		a2 = h(a2);
		int b1 = a1.length();
		int b2 = a2.length();
		String[] c = new String[2];
		if (a2.charAt(0) == '0' && b2 == 1)
			throw new Exception("DivIsZeroException");
		if (a2.charAt(0) == '1' && b2 == 1) {
			c[0] = a1;
			c[1] = "0";
			return c;
		}
		if (j(a1, a2) < 0) {
			c[0] = "0";
			c[1] = a1;
			return c;
		}
		if (j(a1, a2) == 0) {
			c[0] = "1";
			c[1] = "0";
			return c;
		}
		String d = "0";
		while (true) {
			b1 = a1.length();
			b2 = a2.length();
			int e = b1 - b2;
			int f = j(a1, a2);
			if (e <= 0 && f < 0)
				break;
			String g2 = a2;
			String h = "1";
			if (e > 0) {
				for (int i = 0; i < e; i++) {
					g2 += "0";
					h += "0";
				}
				f = j(a1, g2);
				if (f < 0) {
					g2 = g2.substring(0, g2.length() - 1);
					h = h.substring(0, h.length() - 1);
				}
			}
			a1 = sub(a1, g2);
			d = add(d, h);
		}
		c[0] = d;
		c[1] = a1;
		return c;
	}

	public static String div(char[] a1, char[] a2) throws Exception {
		if (!g(a1) || !g(a2))
			throw new Exception("IllegalCharacterException");
		;
		return div(new String(a1), new String(a2));
	}

	public static String factorial(int a) {
		int b = k(a);
		char[] c = new char[b + 1];
		while (b > 0) {
			c[b--] = '0';
		}
		c[0] = '1';
		b = 1;
		int d, e;
		long f = 0;
		for (d = 2; d <= a; d++) {
			for (e = 0; e < b; e++) {
				f = f + (c[e] - '0') * d;
				c[e] = (char) ((f % 10) + '0');
				f /= 10;
			}
			while (f > 0) {
				c[b++] = (char) ((f % 10) + '0');
				f /= 10;
			}
		}
		StringBuffer g = new StringBuffer();
		while (b > 0)
			g.append((c[--b]));
		return g.toString();
	}

	private static int k(int a) {
		int b = 1;
		if (a > 3)
			b = (int) (Math.log10(2 * c_ * a) / 2 + a * Math.log10(a / b_) + 1);
		return b;
	}

	private static Boolean g(String a) {
		return g(a.toCharArray());
	}

	private static Boolean g(char[] a) {
		int b = a.length;
		if (b == 0)
			return false;
		for (int c = 0; c < b; c++)
			if (a[c] > '9' || a[c] < '0')
				return false;
		return true;
	}

	public static String sub_(String a1, String a2, int b) throws Exception {
		return d(sub_(a1, a2), b);
	}

	public static String sub_(String a1, String a2) throws Exception {
		if (a1 == null || a2 == null)
			throw new Exception("ParamNullException");
		a1 = a1.trim();
		a2 = a2.trim();
		int b1 = 0, b2 = 0;
		String c1 = "0", c2 = "0";
		String d = null;
		String e = null;
		int f = a1.indexOf('.');
		if (f != -1) {
			String[] g = a1.split("\\.");
			if (g.length != 2)
				throw new Exception("IllegalDotException");
			g[1] = e(g[1]);
			b1 = g[1].length();
			a1 = g[0];
			c1 = g[1];
		}
		f = a2.indexOf('.');
		if (f != -1) {
			String[] g = a2.split("\\.");
			if (g.length != 2)
				throw new Exception("IllegalDotException");
			g[1] = e(g[1]);
			b2 = g[1].length();
			a2 = g[0];
			c2 = g[1];
		}
		if (b1 > b2) {
			int h = b1 - b2;
			for (int i = 0; i < h; i++)
				c2 += "0";
		} else if (b1 < b2) {
			int h = b2 - b1;
			for (int i = 0; i < h; i++)
				c1 += "0";
		}
		if (j(a1, a2) > 0) {
			if (l(c1, c2) < 0) {
				c1 = "1" + c1;
				a1 = sub(a1, "1");
				e = m(c1.toCharArray(), c2.toCharArray());
				e = e.substring(1);
			} else {
				e = m(c1.toCharArray(), c2.toCharArray());
			}
			d = sub(a1, a2);
			d += "." + e;
		} else if (j(a1, a2) < 0) {
			d = "-";
			if (l(c2, c1) < 0) {
				c2 = "1" + c2;
				a2 = sub(a2, "1");
				e = m(c2.toCharArray(), c1.toCharArray());
				e = e.substring(1);
			} else {
				e = m(c2.toCharArray(), c1.toCharArray());
			}
			d += sub(a2, a1);
			d += "." + e;
		} else {
			if (l(c1, c2) < 0) {
				d = "-0.";
				e = m(c2.toCharArray(), c1.toCharArray());
				d += e;
			} else {
				e = m(c1.toCharArray(), c2.toCharArray());
				d += "0." + e;
			}
		}
		d = e(d);
		if (d.charAt(d.length() - 1) == '.')
			d = d.substring(0, d.length() - 1);
		return d;
	}

	public static String sub(String a1, String a2) throws Exception {
		if (a1 == null || a2 == null)
			throw new Exception("ParamNullException");
		if (!g(a1) || !g(a2))
			throw new Exception("IllegalCharacterException");
		char[] b1 = a1.trim().toCharArray();
		char[] b2 = a2.trim().toCharArray();
		return sub(b1, b2);
	}

	public static String sub(char[] a1, char[] a2) {
		if (j(a1, a2) == 0)
			return "0";
		if (j(a1, a2) > 0)
			return n(a1, a2);
		return "-" + n(a2, a1);
	}

	private static String m(char[] a, char[] b) {
		int c = a.length - 1;
		int d = b.length - 1;
		int e = 0, f, g, h;
		char[] i = new char[c + 1];
		while (c >= 0) {
			g = a[c] - '0' + e;
			if (d >= 0)
				h = b[d] - '0';
			else
				h = 0;
			f = g - h;
			if (f < 0) {
				e = -1;
				f += 10;
			} else
				e = 0;
			i[c] = (char) (f + '0');
			c--;
			d--;
		}
		return new String(i);
	}

	private static String n(char[] a, char[] b) {
		return h(m(a, b));
	}

	private static int j(String a1, String a2) {
		return j(a1.toCharArray(), a2.toCharArray());
	}

	private static int j(char[] a1, char[] a2) {
		a1 = h(a1);
		a2 = h(a2);
		return l(a1, a2);
	}

	private static int l(String a1, String a2) {
		return l(a1.toCharArray(), a2.toCharArray());
	}

	private static int l(char[] a1, char[] a2) {
		int b1 = a1.length;
		int b2 = a2.length;
		if (b1 > b2)
			return 1;
		if (b1 < b2)
			return -1;
		int c = 0;
		while (c < b1) {
			if (a1[c] > a2[c])
				return 1;
			if (a1[c] < a2[c])
				return -1;
			c++;
		}
		return 0;
	}

	private static String h(String a) {
		return new String(h(a.toCharArray()));
	}

	private static char[] h(char a[]) {
		int b = 0;
		while (b < a.length)
			if (a[b++] != '0')
				break;
		if (b == 1)
			return a;
		if (b == a.length + 1) {
			char[] c = { '0' };
			return c;
		} else {
			b--;
			char c[] = new char[a.length - b];
			for (int d = 0; d < c.length; b++, d++)
				c[d] = a[b];
			return c;
		}
	}

	private static String e(String a) {
		return new String(e(a.toCharArray()));
	}

	private static char[] e(char[] a) {
		int b = 0, c = a.length - 1;
		while (c >= 0) {
			if (a[c] != '0')
				break;
			b++;
			c--;
		}
		if (b == 0)
			return a;
		if (b == a.length)
			return new char[] { '0' };
		int d = a.length - b;
		char[] e = new char[d];
		for (c = 0; c < d; c++) {
			e[c] = a[c];
		}
		return e;
	}
}


原始碼被壓縮過,不過執行都正常,大數任何操作都可以,還帶小數運算沒問題.

都是靜態方法,類名直接呼叫.