无符号大整数类,喜欢的拿去
顺带一提,这是我第一次交作业。
我写了一个无符号大整数类,被我拿来AC了OJ中所有涉及高精度的题。
挺粗糙的,不保证无bug,但由于当初是给OJ用的,所以安全性上还是不错的,喜欢的拿去。
简单粗暴的介绍:
构造函数:可以用字符串或无符号整数初始化,也可以不初始化。
析构函数:没有
其他功能:
利用字符串或无符号整数重新初始化
打印整数
用来屏蔽前导0的函数,做过高精度的应该知道,模拟竖式可能会产生前导0
支持的运算:(出于安全性考虑我没有重载运算符)
比较大小
加法
乘法
减法:提供了两个减法,有一个会先调用判断大小再作减法,有一个不会判断大小(OJ上只用到了后者)
//defs.h
#ifndef DEFS_H_INCLUDED #define DEFS_H_INCLUDED #include <iostream> #include <vector> #include <string> using namespace std; ///无符号大整数类 class BigInt { private: vector<int> integer;///存储每个位的向量 int lenth;///实际的整数长度,不包括前导0 BigInt(vector<int> newdata); ///直接用向量初始化 public: BigInt();///构造函数 BigInt(string iteral_integer);///带初始化的构造函数 BigInt(long long unsigned int true_integer); void init(string iteral_integer);///重新初始化函数 void init(long long unsigned int true_integer); int larger_than(const BigInt& other);///比较大小 void standardize();///更新lenth,保障安全的函数 vector<int> get_data();///返回向量 void display();///打印 int get_lenth(){return lenth;}///返回长度值 BigInt add(const BigInt& other);///大整数加法 BigInt subabs(const BigInt& other);///绝对值减法,会自动判断大小 BigInt subs(const BigInt& smaller);///不判断大小的减法 BigInt mul(const BigInt& other);///大整数乘法 }; #endif // DEFS_H_INCLUDED
#include"defs.h" #include<iostream> #include<vector> #include<string> using namespace std; BigInt::BigInt(vector<int> newdata){integer = newdata;standardize();} BigInt::BigInt() { lenth=0; } BigInt::BigInt(string iteral_integer)///字符串构造函数 { for(int i=iteral_integer.size()-1; i>=0; --i) integer.push_back(iteral_integer[i]-'0'); lenth=iteral_integer.size(); ///输入时不带前导0,则函数安全 } BigInt::BigInt(long long unsigned int true_integer)///整数构造函数 { int cnt=0; if(true_integer==0) { integer.push_back(0); cnt+=1; } while(true_integer!=0) { integer.push_back(true_integer%10); cnt+=1; true_integer/=10; } lenth=cnt; } void BigInt::init(string iteral_integer)///初始化函数 { for(int i=iteral_integer.size()-1; i>=0; --i) integer.push_back(iteral_integer[i]-'0'); lenth=iteral_integer.size(); } void BigInt::init(long long unsigned int true_integer) { int cnt=0; if(true_integer==0) { integer.push_back(0); cnt+=1; } while(true_integer!=0) { integer.push_back(true_integer%10); cnt+=1; true_integer/=10; } lenth=cnt; } int BigInt::larger_than(const BigInt &other)///判断大小,返回1大于,返回0等于,返回-1小于 { int br=0; if(lenth>other.lenth) br=1; else if(lenth<other.lenth) br=-1; else { for(int i=lenth-1;i>=0;--i) { if(integer[i]==other.integer[i]) continue; else { if(integer[i]>other.integer[i]) { br=1; break; } else { br=-1; break; } } } } return br; } void BigInt::display()///打印函数 { int i; for(i=lenth-1; i>0;--i) cout<<integer[i]; cout<<integer[i]<<endl; } void BigInt::standardize()///去除前导0以更新lenth { int j=integer.size()-1; while(j>0) { if(integer[j]==0) --j; else break; } lenth=j+1; } vector<int> BigInt::get_data(){return integer;} BigInt BigInt::add(const BigInt& other)///加法 { int len = max(lenth, other.lenth)+1; vector<int> added(len,0); const vector<int> &bignum1 = integer; const vector<int> &bignum2 = other.integer; for(int i=0;i<lenth;++i) added[i]=bignum1[i]; int tmp; for(int i=0;i<other.lenth;++i) { tmp=added[i]+bignum2[i]; if(tmp>9) { added[i]=tmp%10; added[i+1]+=1; } else added[i]=tmp; } BigInt ans(added); ans.lenth = added[len-1]==0?len-1:len; return ans; } BigInt BigInt::subabs(const BigInt& other)///减法 { int flag=larger_than(other); const vector<int> &bignum1=integer; const vector<int> &bignum2=other.integer; int len1,len2; vector<int> diff(max(lenth, other.lenth)); if(flag==0) { BigInt ans(vector<int>(1,0)); ans.lenth=1; return ans; } else if(flag==1) { len1=lenth; len2=other.lenth; int i; for(i=0;i<len2;++i) { diff[i] += bignum1[i]-bignum2[i]; } for(;i<len1;++i) { diff[i] +=bignum1[i]; } for(i=0;i<len1-1;++i) { if(diff[i]<0) { diff[i]+=10; diff[i+1]-=1; } } } else { len2=lenth; len1=other.lenth; int i; for(i=0;i<len1;++i) { diff[i] += bignum2[i]-bignum1[i]; } for(;i<len2;++i) { diff[i] +=bignum2[i]; } for(i=0;i<len2-1;++i) { if(diff[i]<0) { diff[i]+=10; diff[i+1]-=1; } } } BigInt ans(diff); ans.standardize(); return ans; } BigInt BigInt::subs(const BigInt& smaller) { const vector<int> &bignum1=integer; const vector<int> &bignum2=smaller.integer; int len1=lenth; int len2=smaller.lenth; vector<int> diff(len1); int i; for(i=0;i<len2;++i) { diff[i] = bignum1[i]-bignum2[i]; } for(;i<len1;++i) { diff[i] = bignum1[i]; } for(i=0;i<len1-1;++i) { if(diff[i]<0) { diff[i]+=10; diff[i+1]-=1; } } BigInt ans(diff); ans.standardize(); return ans; } BigInt BigInt::mul(const BigInt& other)///乘法 { const vector<int> &bignum1 = integer; const vector<int> &bignum2 = other.integer; int len1 = lenth; int len2 = other.lenth; vector<int> product(len1+len2,0); int mul_tmp = 0; int t=1; for(int i=0; i<len1; ++i) { for(int j=0;j<len2; ++j) { mul_tmp = bignum1[i]*bignum2[j]+product[i+j]; if(mul_tmp>9) { t = mul_tmp/10; product[i+j] = mul_tmp%10; product[i+j+1] += t; } else product[i+j] = mul_tmp; } } BigInt result(product); result.lenth = product[len1+len2-1]==0?len1+len2-1:len1+len2;///排除前导0,函数安全 return result; }
浙公网安备 33010602011771号