高精度四则运算
来源:互联网 发布:怎么重新注册知乎 编辑:程序博客网 时间:2024/06/05 08:43
class HighPrecisionOperation{public: static int Compare(string a, string b) //a>b 1 a==b 0 a<b -1 { if (a[0] == '-'&&b[0] == '-') return -Compare(a.substr(1), b.substr(1)); else if (a[0] == '-'&&b[0] != '-') return -1; else if (a[0] != '-'&&b[0] == '-') return 1; else { a = IntegerSimplify(a); b = IntegerSimplify(b); if (a.length() > b.length()) return 1; else if (a.length() < b.length()) return -1; else { if (a > b) return 1; else if (a < b) return -1; else return 0; } } } static string Add(string a, string b) { if (a[0] == '-'&&b[0] == '-') return "-" + Add(a.substr(1), b.substr(1)); else if (a[0] == '-' &&b[0] != '-') return Sub(b, a.substr(1)); else if (a[0] != '-'&& b[0] == '-') return Sub(a, b.substr(1)); else { a = IntegerSimplify(a); b = IntegerSimplify(b); int M = a.length(), N = b.length(); int L = max(M, N); int *result = new int[L + 1]{ 0 }; for (size_t i = 0; i < L; i++) { if (i < a.length()) result[i] += a[M - 1 - i] - '0'; if (i < b.length()) result[i] += b[N - 1 - i] - '0'; if (result[i] >= 10) { result[i + 1] += result[i] / 10; result[i] %= 10; } } string c(""); for (int i = result[L] == 0 ? L - 1 : L; i >= 0; i--) c += result[i] + '0'; delete[] result; return c; } } static string Sub(string a, string b) { if (a[0] == '-'&&b[0] == '-') //-A-(-B)= B-A return Sub(b.substr(1), a.substr(1)); else if (a[0] == '-'&&b[0] != '-') //-A-B=-(A+B) return "-" + Add(a.substr(1), b); else if (a[0] != '-' && b[0] == '-') //A-(-B)=A+B return Add(a, b.substr(1)); else //A-B { a = IntegerSimplify(a); b = IntegerSimplify(b); int Comp = Compare(a, b); if (Comp == 0) return "0"; else if (Comp < 0) return "-" + Sub(b, a); else { int M = a.length(), N = b.length(); int L = M, highestDigit = 0; int *result = new int[L] {0}; for (size_t i = 0; i < L; i++) { if (i < b.length()) result[i] += a[M - 1 - i] - b[N - 1 - i]; else result[i] += a[M - 1 - i] - '0'; if (result[i] < 0) { result[i + 1] -= 1; result[i] += 10; } if (result[i] != 0) highestDigit = i; } string c(""); for (int i = highestDigit; i >= 0; i--) c += result[i] + '0'; delete[] result; return c; } } } static string Multiply(string a, string b) { if (a[0] == '-'&&b[0] != '-') return "-" + Multiply(a.substr(1), b); else if (a[0] != '-'&&b[0] == '-') return "-" + Multiply(a, b.substr(1)); else if (a[0] == '-' &&b[0] == '-') return Multiply(a.substr(1), b.substr(1)); else { a = IntegerSimplify(a); b = IntegerSimplify(b); int M = a.length(), N = b.length(); int L = M + N; int *result = new int[L] {0}; for (size_t i = 0; i < N; i++) for (size_t j = 0; j < M; j++) result[i + j] += (b[N - 1 - i] - '0')*(a[M - 1 - j] - '0'); int highestDigit = 0; for (size_t i = 0; i < L; i++) { if (result[i] >= 10) { result[i + 1] += result[i] / 10; result[i] %= 10; } if (result[i]) highestDigit = i; } string c = ""; for (int i = highestDigit; i >= 0; i--) c += result[i] + '0'; delete[] result; return c; } } static string Divide(string a, string b) { if (b == "0") { throw string("Divisor Is 0"); } if (a[0] == '-'&&b[0] != '-') return "-" + Divide(a.substr(1), b); else if (a[0] != '-' && b[0] == '-') return "-" + Divide(a, b.substr(1)); else if (a[0] == '-'&&b[0] == '-') return Divide(a.substr(1), b.substr(1)); else { int Comp = Compare(a, b); if (Comp<0) return "0"; else { int M, N ,L=a.length()-b.length()+1; string tmpDividend=a.substr(0,b.length()), tmpDifference, multiResult, c; while (L--) { if (Compare(tmpDividend, b) < 0) { c += "0"; tmpDividend = a.substr(0, tmpDividend.length() + 1); continue; if (N + 1 <= M) tmpDividend = a.substr(0, ++N); } char i; for (i = '9'; i >= '0'; i--) { multiResult = Multiply(b, string() + i); if (Compare(multiResult, tmpDividend) <= 0) break; } c += i; tmpDifference = Sub(tmpDividend, multiResult); if (tmpDifference != "0") { a = tmpDifference + a.substr(tmpDividend.length()); tmpDividend = a.substr(0, tmpDifference.length() + 1); } else { a = a.substr(tmpDividend.length()); tmpDividend = a.substr(0, 1); } } return IntegerSimplify(c); } } } private: static string IntegerSimplify(string a) { return a.erase(0, a.find_first_not_of('0')); }};
0 0
- 高精度四则运算
- 大数的高精度四则运算
- 高精度四则运算---加减
- 高精度四则运算 完整版
- 高精度整数的四则运算
- 高精度四则运算模板
- 高精度四则运算模板
- 大数四则运算(高精度)
- 高精度四则运算模板
- 2017.7.14. 高精度四则运算
- 高精度四则运算模板
- 【高精度】【高精度四则运算模板】高精度除法
- 任意长度大数 高精度四则运算
- 高精度数的四则运算+N!
- 大数加法 减法 乘法 除法 高精度四则运算
- 大数加法 减法 乘法 除法 高精度四则运算
- 2013-BIT程序设计 7. 四则运算之加减法 -- 高精度加减
- 高精度(大数)的四则运算与逻辑运算---c++ struct版
- hdu 1280 前m大的数
- 剑指offer 复杂链表的复制
- Java例子:判断输入的年份是否为闰年
- 小游戏-连连看
- Codeforces Beta Round #3 D. Least Cost Bracket Sequence(贪心,想法,好题)
- 高精度四则运算
- 内部类与异常
- 剑指offer面试题14~调整数组顺序使奇数位于偶数前面
- Jetson TX1 开发教程(4)--TensorRT加速Caffe初探
- 符号执行
- 6.4.3树和森林的遍历
- 在甲骨文培训,菜鸟的遇袭
- Java IO(二)
- 工厂模式的三种形式