博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
高精——模板
阅读量:4507 次
发布时间:2019-06-08

本文共 8308 字,大约阅读时间需要 27 分钟。

紫书:

#include 
#include
#include
#include
using namespace std; const int maxn = 1000; struct bign{ int d[maxn], len; void clean() { while(len > 1 && !d[len-1]) len--; } bign() { memset(d, 0, sizeof(d)); len = 1; } bign(int num) { *this = num; } bign(char* num) { *this = num; } bign operator = (const char* num){ memset(d, 0, sizeof(d)); len = strlen(num); for(int i = 0; i < len; i++) d[i] = num[len-1-i] - '0'; clean(); return *this; } bign operator = (int num){ char s[20]; sprintf(s, "%d", num); *this = s; return *this; } bign operator + (const bign& b){ bign c = *this; int i; for (i = 0; i < b.len; i++){ c.d[i] += b.d[i]; if (c.d[i] > 9) c.d[i]%=10, c.d[i+1]++; } while (c.d[i] > 9) c.d[i++]%=10, c.d[i]++; c.len = max(len, b.len); if (c.d[i] && c.len <= i) c.len = i+1; return c; } bign operator - (const bign& b){ bign c = *this; int i; for (i = 0; i < b.len; i++){ c.d[i] -= b.d[i]; if (c.d[i] < 0) c.d[i]+=10, c.d[i+1]--; } while (c.d[i] < 0) c.d[i++]+=10, c.d[i]--; c.clean(); return c; } bign operator * (const bign& b)const{ int i, j; bign c; c.len = len + b.len; for(j = 0; j < b.len; j++) for(i = 0; i < len; i++) c.d[i+j] += d[i] * b.d[j]; for(i = 0; i < c.len-1; i++) c.d[i+1] += c.d[i]/10, c.d[i] %= 10; c.clean(); return c; } bign operator / (const bign& b){ int i, j; bign c = *this, a = 0; for (i = len - 1; i >= 0; i--) { a = a*10 + d[i]; for (j = 0; j < 10; j++) if (a < b*(j+1)) break; c.d[i] = j; a = a - b*j; } c.clean(); return c; } bign operator % (const bign& b){ int i, j; bign a = 0; for (i = len - 1; i >= 0; i--) { a = a*10 + d[i]; for (j = 0; j < 10; j++) if (a < b*(j+1)) break; a = a - b*j; } return a; } bign operator += (const bign& b){ *this = *this + b; return *this; } bool operator <(const bign& b) const{ if(len != b.len) return len < b.len; for(int i = len-1; i >= 0; i--) if(d[i] != b.d[i]) return d[i] < b.d[i]; return false; } bool operator >(const bign& b) const{
return b < *this;} bool operator<=(const bign& b) const{
return !(b < *this);} bool operator>=(const bign& b) const{
return !(*this < b);} bool operator!=(const bign& b) const{
return b < *this || *this < b;} bool operator==(const bign& b) const{
return !(b < *this) && !(b > *this);} string str() const{ char s[maxn]={}; for(int i = 0; i < len; i++) s[len-1-i] = d[i]+'0'; return s; } }; istream& operator >> (istream& in, bign& x) { string s; in >> s; x = s.c_str(); return in; } ostream& operator << (ostream& out, const bign& x) { out << x.str(); return out; }

最全:

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;const int MAX_L=2005; //最大长度,可以修改class bign{public: int len, s[MAX_L];//数的长度,记录数组//构造函数 bign(); bign(const char*); bign(int); bool sign;//符号 1正数 0负数 string toStr() const;//转化为字符串,主要是便于输出 friend istream& operator>>(istream &,bign &);//重载输入流 friend ostream& operator<<(ostream &,bign &);//重载输出流//重载复制 bign operator=(const char*); bign operator=(int); bign operator=(const string);//重载各种比较 bool operator>(const bign &) const; bool operator>=(const bign &) const; bool operator<(const bign &) const; bool operator<=(const bign &) const; bool operator==(const bign &) const; bool operator!=(const bign &) const;//重载四则运算 bign operator+(const bign &) const; bign operator++(); bign operator++(int); bign operator+=(const bign&); bign operator-(const bign &) const; bign operator--(); bign operator--(int); bign operator-=(const bign&); bign operator*(const bign &)const; bign operator*(const int num)const; bign operator*=(const bign&); bign operator/(const bign&)const; bign operator/=(const bign&);//四则运算的衍生运算 bign operator%(const bign&)const;//取模(余数) bign factorial()const;//阶乘 bign Sqrt()const;//整数开根(向下取整) bign pow(const bign&)const;//次方//一些乱乱的函数 void clean();};#define max(a,b) a>b ? a : b#define min(a,b) a
>(istream &in, bign &num){ string str; in>>str; num=str; return in;}ostream &operator<<(ostream &out, bign &num){ out<
= 0; i--) if (s[i] != num.s[i]) return sign ? (s[i] < num.s[i]) : (!(s[i] < num.s[i])); return !sign;}bool bign::operator>(const bign&num)const{ return num < *this;}bool bign::operator<=(const bign&num)const{ return !(*this>num);}bool bign::operator>=(const bign&num)const{ return !(*this
num || *this < num;}bool bign::operator==(const bign&num)const{ return !(num != *this);}bign bign::operator+(const bign &num) const{ if (sign^num.sign) { bign tmp = sign ? num : *this; tmp.sign = 1; return sign ? *this - tmp : num - tmp; } bign result; result.len = 0; int temp = 0; for (int i = 0; temp || i < (max(len, num.len)); i++) { int t = s[i] + num.s[i] + temp; result.s[result.len++] = t % 10; temp = t / 10; } result.sign = sign; return result;}bign bign::operator++(){ *this = *this + 1; return *this;}bign bign::operator++(int){ bign old = *this; ++(*this); return old;}bign bign::operator+=(const bign &num){ *this = *this + num; return *this;}bign bign::operator-(const bign &num) const{ bign b=num,a=*this; if (!num.sign && !sign) { b.sign=1; a.sign=1; return b-a; } if (!b.sign) { b.sign=1; return a+b; } if (!a.sign) { a.sign=1; b=bign(0)-(a+b); return b; } if (a
= 0) g = 0; else { g = 1; x += 10; } result.s[result.len++] = x; } result.clean(); return result;}bign bign::operator * (const bign &num)const{ bign result; result.len = len + num.len; for (int i = 0; i < len; i++) for (int j = 0; j < num.len; j++) result.s[i + j] += s[i] * num.s[j]; for (int i = 0; i < result.len; i++) { result.s[i + 1] += result.s[i] / 10; result.s[i] %= 10; } result.clean(); result.sign = !(sign^num.sign); return result;}bign bign::operator*(const int num)const{ bign x = num; bign z = *this; return x*z;}bign bign::operator*=(const bign&num){ *this = *this * num; return *this;}bign bign::operator /(const bign&num)const{ bign ans; ans.len = len - num.len + 1; if (ans.len < 0) { ans.len = 1; return ans; } bign divisor = *this, divid = num; divisor.sign = divid.sign = 1; int k = ans.len - 1; int j = len - 1; while (k >= 0) { while (divisor.s[j] == 0) j--; if (k > j) k = j; char z[MAX_L]; memset(z, 0, sizeof(z)); for (int i = j; i >= k; i--) z[j - i] = divisor.s[i] + '0'; bign dividend = z; if (dividend < divid) { k--; continue; } int key = 0; while (divid*key <= dividend) key++; key--; ans.s[k] = key; bign temp = divid*key; for (int i = 0; i < k; i++) temp = temp * 10; divisor = divisor - temp; k--; } ans.clean(); ans.sign = !(sign^num.sign); return ans;}bign bign::operator/=(const bign&num){ *this = *this / num; return *this;}bign bign::operator%(const bign& num)const{ bign a = *this, b = num; a.sign = b.sign = 1; bign result, temp = a / b*b; result = a - temp; result.sign = sign; return result;}bign bign::pow(const bign& num)const{ bign result = 1; for (bign i = 0; i < num; i++) result = result*(*this); return result;}bign bign::factorial()const{ bign result = 1; for (bign i = 1; i <= *this; i++) result *= i; return result;}void bign::clean(){ if (len == 0) len++; while (len > 1 && s[len - 1] == '\0') len--;}bign bign::Sqrt()const{ if(*this<0)return -1; if(*this<=1)return *this; bign l=0,r=*this,mid; while(r-l>1) { mid=(l+r)/2; if(mid*mid>*this) r=mid; else l=mid; } return l;}bign num0,num1,res;int main() { num0 = 1, num1 = 2; res=num0-num1; cout << res << endl; return 0;}

出处:

紫书和

转载于:https://www.cnblogs.com/Menteur-Hxy/p/9248016.html

你可能感兴趣的文章
Kafka集群副本分配算法解析
查看>>
vue单页面条件下添加类似浏览器的标签页切换功能
查看>>
lambda表达式10个示例——学习笔记
查看>>
python 文件操作
查看>>
Java多线程之后台线程
查看>>
浏览器兼容性
查看>>
flask 第四篇 模板语言jinja2
查看>>
非均衡分类问题的思考与问题与解决思路
查看>>
头文件与extern
查看>>
python开发技术详解(三) 进阶的语法
查看>>
LeetCode Missing Number
查看>>
Linux 网络(连接)相关参数作用
查看>>
鼠标事件先后顺序
查看>>
洛谷P2756 飞行员配对方案问题
查看>>
在java中删除数组元素的练习
查看>>
[No0000B7]If else 与 三元表达式? : 效率对比
查看>>
【PSR规范专题(5)】PSR-4 改进后的自动加载规范
查看>>
01单例模式(创建型模式)
查看>>
Java 组播
查看>>
java_泛型,设置类型通配符的上限
查看>>