【正整数模板运算(C++手写BigInteger类和java.math.BigInteger以及java.math.BigDecimal)】
2014-07-12 12:28
513 查看
正整数大数加减乘比较运算C++模版;转载请说明出处http://write.blog.csdn.net/postedit/37724829
【JAVA BigInteger】
【JAVA BigDecimal】
String a = in.next();
BigDecimal num = new BigDecimal(a);
res = num.setScale(2,BigDecimal.ROUND_HALF_EVEN).toString();//四舍五入保留两位小数,大多数和BigInteger差不多
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; /* * 完全大数模板 * 输出cin>>a * 输出a.print(); * 注意这个输入不能自动去掉前导0的,可以先读入到char数组,去掉前导0,再用构造函数。 */ #define MAXN 9999 #define MAXSIZE 1010 #define DLEN 4 struct BigInter{ int a[500]; //可以控制大数的位数 int len; BigInter(){ len = 1; memset(a, 0, sizeof(a)); } //构造函数 BigInter(const int); //将一个int类型的变量转化成大数 BigInter(const char*); //将一个字符串类型的变量转化为大数 BigInter(const BigInter &); //拷贝构造函数 BigInter &operator=(const BigInter &); //重载赋值运算符,大数之间进行赋值运算 friend istream& operator>>(istream&, BigInter&); //重载输入运算符 friend ostream& operator<<(ostream&, BigInter&); //重载输出运算符 BigInter operator+(const BigInter &)const; //重载加法运算符,两个大数之间的相加运算 BigInter operator-(const BigInter &)const; //重载减法运算符,两个大数之间的相减运算 BigInter operator*(const BigInter &)const; //重载乘法运算符,两个大数之间的相乘运算 BigInter operator/(const int &)const; //重载除法运算符,大数对一个整数进行相除运算 BigInter operator^(const int &)const; //大数的n次方运算 int operator%(const int &)const; //大数对一个int类型的变量进行取模运算 bool operator>(const BigInter &T)const; //大数和另一个大数的大小比较 bool operator>(const int &t)const; //大数和一个int类型的变量的大小比较 void print(); //输出大数 }; BigInter::BigInter(const int b) //将一个int类型的变量转化为大数 { int c, d = b; len = 0; memset(a, 0, sizeof(a)); while (d > MAXN) { c = d - (d / (MAXN + 1))*(MAXN + 1); d = d / (MAXN + 1); a[len++] = c; } a[len++] = d; } BigInter::BigInter(const char *s) //将一个字符串类型的变量转化为大数 { int t, k, index, L, i; memset(a, 0, sizeof(a)); L = strlen(s); len = L / DLEN; if (L%DLEN)len++; index = 0; for (i = L - 1; i >= 0; i -= DLEN) { t = 0; k = i - DLEN + 1; if (k < 0)k = 0; for (int j = k; j <= i; j++) t = t * 10 + s[j] - '0'; a[index++] = t; } } BigInter::BigInter(const BigInter &T) :len(T.len) //拷贝构造函数 { int i; memset(a, 0, sizeof(a)); for (i = 0; i < len; i++) a[i] = T.a[i]; } BigInter & BigInter::operator=(const BigInter &n) //重载赋值运算符,大数之间赋值运算 { int i; len = n.len; memset(a, 0, sizeof(a)); for (i = 0; i < len; i++) a[i] = n.a[i]; return *this; } istream& operator>>(istream &in, BigInter &b) { char ch[MAXSIZE * 4]; int i = -1; in >> ch; int L = strlen(ch); int count = 0, sum = 0; for (i = L - 1; i >= 0;) { sum = 0; int t = 1; for (int j = 0; j < 4 && i >= 0; j++, i--, t *= 10) { sum += (ch[i] - '0')*t; } b.a[count] = sum; count++; } b.len = count++; return in; } ostream& operator<<(ostream& out, BigInter& b) //重载输出运算符 { int i; cout << b.a[b.len - 1]; for (i = b.len - 2; i >= 0; i--) { printf("%04d", b.a[i]); } return out; } BigInter BigInter::operator+(const BigInter &T)const //两个大数之间的相加运算 { BigInter t(*this); int i, big; big = T.len > len ? T.len : len; for (i = 0; i < big; i++) { t.a[i] += T.a[i]; if (t.a[i] > MAXN) { t.a[i + 1]++; t.a[i] -= MAXN + 1; } } if (t.a[big] != 0) t.len = big + 1; else t.len = big; return t; } BigInter BigInter::operator-(const BigInter &T)const //两个大数之间的相减运算 { int i, j, big; bool flag; BigInter t1, t2; if (*this > T) { t1 = *this; t2 = T; flag = 0; } else { t1 = T; t2 = *this; flag = 1; } big = t1.len; for (i = 0; i < big; i++) { if (t1.a[i]<t2.a[i]) { j = i + 1; while (t1.a[j] == 0) j++; t1.a[j--]--; while (j>i) t1.a[j--] += MAXN; t1.a[i] += MAXN + 1 - t2.a[i]; } else t1.a[i] -= t2.a[i]; } t1.len = big; while (t1.a[len - 1] == 0 && t1.len > 1) { t1.len--; big--; } if (flag) t1.a[big - 1] = 0 - t1.a[big - 1]; return t1; } BigInter BigInter::operator*(const BigInter &T)const //两个大数之间的相乘 { BigInter ret; int i, j, up; int temp, temp1; for (i = 0; i < len; i++) { up = 0; for (j = 0; j < T.len; j++) { temp = a[i] * T.a[j] + ret.a[i + j] + up; if (temp > MAXN) { temp1 = temp - temp / (MAXN + 1)*(MAXN + 1); up = temp / (MAXN + 1); ret.a[i + j] = temp1; } else { up = 0; ret.a[i + j] = temp; } } if (up != 0) ret.a[i + j] = up; } ret.len = i + j; while (ret.a[ret.len - 1] == 0 && ret.len > 1)ret.len--; return ret; } BigInter BigInter::operator/(const int &b)const //大数对一个整数进行相除运算 { BigInter ret; int i, down = 0; for (i = len - 1; i >= 0; i--) { ret.a[i] = (a[i] + down*(MAXN + 1)) / b; down = a[i] + down*(MAXN + 1) - ret.a[i] * b; } ret.len = len; while (ret.a[ret.len - 1] == 0 && ret.len > 1) ret.len--; return ret; } int BigInter::operator%(const int &b)const //大数对一个 int类型的变量进行取模 { int i, d = 0; for (i = len - 1; i >= 0; i--) d = ((d*(MAXN + 1)) % b + a[i]) % b; return d; } BigInter BigInter::operator^(const int &n)const //大数的n次方运算 { BigInter t, ret(1); int i; if (n < 0)exit(-1); if (n == 0)return 1; if (n == 1)return *this; int m = n; while (m > 1) { t = *this; for (i = 1; (i << 1) <= m; i <<= 1) t = t*t; m -= i; ret = ret*t; if (m == 1)ret = ret*(*this); } return ret; } bool BigInter::operator>(const BigInter &T)const //大数和另一个大数的大小比较 { int ln; if (len > T.len)return true; else if (len == T.len) { ln = len - 1; while (a[ln] == T.a[ln] && ln >= 0) ln--; if (ln >= 0 && a[ln] > T.a[ln]) return true; else return false; } else return false; } bool BigInter::operator>(const int &t)const //大数和一个int类型的变量的大小比较 { BigInter b(t); return *this > b; } void BigInter::print() //输出大数 { int i; printf("%d", a[len - 1]); for (i = len - 2; i >= 0; i--) printf("%04d", a[i]); printf("\n"); }
【JAVA BigInteger】
1.valueOf(parament); 将参数转换为制定的类型 比如 int a=3; BigInteger b=BigInteger.valueOf(a); 则b=3; String s=”12345”; BigInteger c=BigInteger.valueOf(s); 则c=12345; 2.add(); 大整数相加 BigInteger a=new BigInteger(“23”); BigInteger b=new BigInteger(“34”); a.add(b); 3.subtract(); 相减 4.multiply(); 相乘 5.divide(); 相除取整 6.remainder();取余 7.pow(); a.pow(b)=a^b 8.gcd(); 最大公约数 9.abs(); 绝对值 10.negate();取反数 11.mod(); a.mod(b)=a%b=a.remainder(b); 12.max(); min(); 13.punlic int comareTo(); 14.boolean equals(); 是否相等 15.BigInteger构造函数: 一般用到以下两种: BigInteger(String val); 将指定字符串转换为十进制表示形式; BigInteger(String val,int radix); 将指定基数的BigInteger的字符串表示形式转换为BigInteger Ⅱ.基本常量: A=BigInteger.ONE 1 B=BigInteger.TEN 10 C=BigInteger.ZERO 0 Ⅲ.基本操作 1.读入: 用Scanner类定义对象进行控制台读入,Scanner类在java.util.*包中 Scanner cin=new Scanner(System.in);// 读入 while(cin.hasNext()) //等同于!=EOF { int n; BigInteger m; n=cin.nextInt(); //读入一个int; m=cin.BigInteger();//读入一个BigInteger; System.out.print(m.toString()); } BigInteger(byte[]) 把一个包含着(带正负号)整数的二进制补码的字节数组翻译为 BigInteger 。 BigInteger(int, byte[]) 把一个整数的 sign-magnitude 表示法翻译为 BigInteger 。 BigInteger(int, int, Random) 返回有指定 bitLength(可能是素数)的随机选择的 BigInter 。 BigInteger(int, Random) 返回一个随机数,均匀分布在 [0, 2**numBits - 1] 之间 (假设由 rndSrc 提供一个公平的随机源) 。 BigInteger(String) 把一个字符串翻译为一个 BigInteger ,该字符串包含可选的负号,后面跟着一个或多个十进制数字序列。 BigInteger(String, int) 把一个字符串翻译为一个 BigInteger ,该字符串包含可选的负号,后面跟着一个或多个指定进制的数字序列。
方法摘要 | |
BigInteger | abs() 返回其值是此 BigInteger 的绝对值的 BigInteger。 |
BigInteger | add(BigInteger val) 返回其值为 (this + val) 的 BigInteger。 |
BigInteger | and(BigInteger val) 返回其值为 (this & val) 的 BigInteger。 |
BigInteger | andNot(BigInteger val) 返回其值为 (this & ~val) 的 BigInteger。 |
int | bitCount() 返回此 BigInteger 的二进制补码表示形式中与符号不同的位的数量。 |
int | bitLength() 返回此 BigInteger 的最小的二进制补码表示形式的位数,不包括 符号位。 |
BigInteger | clearBit(int n) 返回其值与清除了指定位的此 BigInteger 等效的 BigInteger。 |
int | compareTo(BigInteger val) 将此 BigInteger 与指定的 BigInteger 进行比较。 |
BigInteger | divide(BigInteger val) 返回其值为 (this / val) 的 BigInteger。 |
BigInteger[] | divideAndRemainder(BigInteger val) 返回包含 (this / val) 后跟 (this % val) 的两个 BigInteger 的数组。 |
double | doubleValue() 将此 BigInteger 转换为 double。 |
boolean | equals(Object x) 比较此 BigInteger 与指定的 Object 的相等性。 |
BigInteger | flipBit(int n) 返回其值与对此 BigInteger 进行指定位翻转后的值等效的 BigInteger。 |
float | floatValue() 将此 BigInteger 转换为 float。 |
BigInteger | gcd(BigInteger val) 返回一个 BigInteger,其值是 abs(this) 和 abs(val) 的最大公约数。 |
int | getLowestSetBit() 返回此 BigInteger 最右端(最低位)1 比特的索引(即从此字节的右端开始到本字节中 最右端 1 比特之间的 0 比特的位数)。 |
int | hashCode() 返回此 BigInteger 的哈希码。 |
int | intValue() 将此 BigInteger 转换为 int。 |
boolean | isProbablePrime(int certainty) 如果此 BigInteger 可能为素数,则返回 true,如 果它一定为合数,则返回 false。 |
long | longValue() 将此 BigInteger 转换为 long。 |
BigInteger | max(BigInteger val) 返回此 BigInteger 和 val 的最大值。 |
BigInteger | min(BigInteger val) 返回此 BigInteger 和 val 的最小值。 |
BigInteger | mod(BigInteger m) 返回其值为 (this mod m) 的 BigInteger。 |
BigInteger | modInverse(BigInteger m) 返回其值为 (this-1 mod m) 的 BigInteger。 |
BigInteger | modPow(BigInteger exponent, BigInteger m) 返回其值为 (thisexponent mod m) 的 BigInteger。 |
BigInteger | multiply(BigInteger val) 返回其值为 (this * val) 的 BigInteger。 |
BigInteger | negate() 返回其值是 (-this) 的 BigInteger。 |
BigInteger | nextProbablePrime() 返回大于此BigInteger的可能为素数的第一个整数。 |
BigInteger | not() 返回其值为 (~this) 的 BigInteger。 |
BigInteger | or(BigInteger val) 返回其值为 (this | val) 的 BigInteger。 |
BigInteger | pow(int exponent) 返回其值为 (thisexponent) 的 BigInteger。 |
static BigInteger | probablePrime(int bitLength, Random rnd) 返回有可能是素数的、具有指定长度的正 BigInteger。 |
BigInteger | remainder(BigInteger val) 返回其值为 (this % val) 的 BigInteger。 |
BigInteger | setBit(int n) 返回其值与设置了指定位的此 BigInteger 等效的 BigInteger。 |
BigInteger | shiftLeft(int n) 返回其值为 (this << n) 的 BigInteger。 |
BigInteger | shiftRight(int n) 返回其值为 (this >> n) 的 BigInteger。 |
int | signum() 返回此 BigInteger 的正负号函数。 |
BigInteger | subtract(BigInteger val) 返回其值为 (this - val) 的 BigInteger。 |
boolean | testBit(int n) 当且仅当设置了指定的位时,返回 true。 |
byte[] | toByteArray() 返回一个字节数组,该数组包含此 BigInteger 的二进制补码表示形式。 |
String | toString() 返回此 BigInteger 的十进制字符串表示形式。 |
String | toString(int radix) 返回此 BigInteger 的给定基数的字符串表示形式。 |
static BigInteger | valueOf(long val) 返回其值等于指定 long 的值的 BigInteger。 |
BigInteger | xor(BigInteger val) 返回其值为 (this ^ val) 的 BigInteger。 |
【JAVA BigDecimal】
String a = in.next();
BigDecimal num = new BigDecimal(a);
res = num.setScale(2,BigDecimal.ROUND_HALF_EVEN).toString();//四舍五入保留两位小数,大多数和BigInteger差不多
相关文章推荐
- java 大整数运算以及BigInteger的用法
- 重踏学习Java路上_Day14(正则表达式,Math,Random,System,BigInteger,BigDecimal,Date,Calendar)
- Java中正则表达式和常用的Math、Random、System、BigInteger、BigDecimal、Date/DateFormat、Calendar类的使用
- java 大数类 总结 整数小数(BigInteger和BigDecimal)
- Java常用类-Math、BigInteger、BigDecimal
- Java常用类库--Math、Random、NumberFormat、DecimalFormat、BigInteger、BigDecimal
- java.math包中的BigInteger和BigDecimal
- 【java】BigDecimal、BigInteger、大数相乘、数值的整数次方、快速乘法
- JAVA基础复习十四-Math、Random、System、BigInteger、BigDecimal、Date、SimpleDateFormat类
- Java300StudyNote(12)-Java浮点数以及浮点数误差问题、BigInteger、BigDecimal、float与long谁更大
- 大整数运算 (java BigInteger类)
- Java基础 - 正则,Math,Random,System,Date,SimpleDateFormat,Calendar,BigInteger,BigDecimal
- 大数类java.math.BigInteger
- C++大数模板 BigInteger
- java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Integer解决
- 【java基础】大数据的处理:java.math.Number/BigInteger/BigDecimal
- java.math.BigInteger使用心得总结
- java BigInteger和BigDecimal
- java.BigInteger(java表示大整数)
- java里的BigInteger和BigDecimal