Bigint 重载了'='和‘+’和‘-’和‘×’和‘<<’和‘>>’
来源:互联网 发布:科比巅峰数据 编辑:程序博客网 时间:2024/05/17 03:11
#include<cstdio> #include<iostream> #include<vector> #include<cstring> #define LL long long using namespace std; struct bigint{ static const int base=1000000000; static const int width=9; vector<LL>s; bigint (LL num=0){*this=num;} bigint operator = (LL num){ s.clear(); do{ s.push_back(num%base); num/=base; }while(num>0); return *this; } bigint operator = (const string& str){ s.clear(); int x,len=(str.length()-1)/width+1; for(int i=0;i<len;i++){ int end=str.length()-i*width; int start=max(0,end-width); sscanf(str.substr(start,end-start).c_str(),"%d",&x); s.push_back(x); } return *this; } bigint operator + (const bigint& b) const{ bigint c; c.s.clear(); for(int i=0,g=0;;i++){ if(g==0&&i>=s.size()&&i>=b.s.size())break; int x=g; if(i<s.size())x+=s[i]; if(i<b.s.size())x+=b.s[i]; c.s.push_back(x%base); g=x/base; } return c; } bigint operator - (const bigint& b) const{ bigint c; c.s.clear(); for(int i=0,g=0;;i++){ if(g==0&&i>=s.size())break; int x=g; if(i<s.size())x+=s[i]; if(i<b.s.size())x-=b.s[i]; if(x<0)x+=base,g=-1; else g=0; c.s.push_back(x); } return c; } bigint operator * (const bigint& b) const{ bigint c; c.s.clear(); LL i,j,g; LL temp,temp1; c.s.resize(s.size()+b.s.size()); for(i=0;i<s.size();i++){ g=0,temp1=0; for(j=0;j<b.s.size();j++){ LL x=c.s[i+j]; temp=s[i]*b.s[j]+g; g=temp/base; x+=temp%base+temp1; temp1=x/base; x%=base; c.s[i+j]=x; } if(g!=0) c.s[i+j]=g; } if(temp1!=0) c.s[i+j]=temp1; while(c.s.back()==0&&c.s.size()>1) c.s.pop_back(); return c; } bool operator < (const bigint&b) const{ if(s.size()!=b.s.size())return s.size()<b.s.size(); for(int i=s.size()-1;i>=0;i--) if(s[i]!=b.s[i])return s[i]<b.s[i]; return false; } bool operator > (const bigint&b) const{return b<*this;} bool operator <= (const bigint&b) const{return !(b<*this);} bool operator >= (const bigint&b) const{return !(*this>b);} bool operator != (const bigint&b) const{return b<*this|| *this<b;} bool operator == (const bigint&b) const{return !(b<*this)&&!(*this<b);} bigint operator *= (const bigint &b){*this=(*this)*b; return *this;} bigint operator -= (const bigint &b){*this=(*this)-b; return *this;} bigint operator += (const bigint &b){*this=(*this)+b; return *this;} bigint operator ++ (int){*this=*this+1; return *this;} bigint& operator ++ () {*this=*this+1; return *this;} bigint operator -- (int){*this=*this-1; return *this;} bigint& operator -- (){*this=*this-1; return *this;} }; ostream& operator << (ostream &out,const bigint& x){ out<<x.s.back(); for(int i=x.s.size()-2;i>=0;i--){ char buf[20]; sprintf(buf,"%09d",x.s[i]); for(int j=0;j<strlen(buf);j++)out<<buf[j]; } return out; } istream& operator >> (istream &in,bigint&x){ string s; if(!(in>>s))return in; x=s; return in; } int main(){ bigint a,b; cin>>a>>b; cout<<a*b; }
阅读全文
0 0
- Bigint 重载了'='和‘+’和‘-’和‘×’和‘<<’和‘>>’
- int、bigint、smallint 和 tinyint
- int、bigint、smallint 和 tinyint
- int、bigint、smallint 和 tinyint
- int、bigint、smallint 和 tinyint
- int、bigint、smallint 和 tinyint
- int、bigint、smallint 和 tinyint
- 重载++和--
- 重载<<和>>
- int、bigint、smallint 和 tinyint 区别
- int、bigint、smallint 和 tinyint范围
- int、bigint、smallint 和 tinyint (Transact-SQL)
- int、bigint、smallint 和 tinyint范围
- mysql int,bigint,smallint和tinyint理解
- int、bigint、smallint 和 tinyint范围
- int、bigint、smallint和tinyint范围
- 重载和多态
- 什么是覆盖和重载?
- Windows下Nginx的基本使用
- stm32 ADXL345传感器
- Java 访问Hbase数据库
- 接口学习
- mysql 行转列
- Bigint 重载了'='和‘+’和‘-’和‘×’和‘<<’和‘>>’
- 线上操作与线上问题排查实战
- 《C++ Primer》第五版课后习题解答_第三章(3)(21-29)
- Akka(19): Stream:组合数据流,组合共用-Graph modular composition
- 串口寄存器配置
- Bootstrap模态框使用WebUploader点击失效问题解决
- ASP.NET 安全认证(一)
- 一个圆圈两个边框
- USER_ERROR__missing_dlib_all_source_cpp_file__OR__inconsistent_use_of_DEBUG_or_ENABLE_ASSERTS_prepro