一个简单的C++大数类(希望可以得到各种建议)
2013-04-20 12:22
330 查看
暂时只有整形高精加减乘,希望大家可以指出不足之处。
#include<cstdio> #include<cstring> #include<iostream> #include<cmath> using namespace std; const int MAXN=4000; struct BIGNUM { int sgn,len,s[MAXN]; BIGNUM () {memset(s,0,sizeof(s)); len=1; sgn=1; } BIGNUM operator = (const char* num) { if (*num=='-'||*num=='+') { sgn=*num=='+'?1:-1; ++num; } len=strlen(num); for (int i=0;i<len;++i) s[i]=num[len-i-1]-'0'; return *this; } BIGNUM operator = (const int num) { char a[MAXN]; sprintf(a,"%d",num); *this = a; return *this; } BIGNUM (int num) { *this=num; } BIGNUM (const char * num) { *this=num; } BIGNUM (const BIGNUM & x) {*this = x; } bool operator < (const BIGNUM & x) const { if (sgn!=x.sgn) return sgn<x.sgn; if (len != x.len) return len<x.len; for (int i=len-1;i>=0;--i) { if (s[i] != x.s[i]) return s[i]<x.s[i]; } return false; } bool operator > (const BIGNUM & x) const { return x<*this; } bool operator <= (const BIGNUM & x) const { return !(x<*this); } bool operator >= (const BIGNUM & x) const { return !(*this<x); } bool operator == (const BIGNUM & x) const { return !(x<*this||*this<x); } bool operator != (const BIGNUM & x) const { return x<*this||*this<x; } BIGNUM operator + (const BIGNUM & a) const { BIGNUM c; c.len=max(len,a.len)+1; if (sgn==a.sgn) { c.sgn=sgn; for (int i=0;i<c.len;++i) { c.s[i]+=s[i]+a.s[i]; c.s[i+1]=c.s[i]/10; c.s[i]=c.s[i]%10; } } else { BIGNUM t1=*this,t2=a; t1.sgn=1; t2.sgn=1; if (t1<t2) { c.sgn=a.sgn; BIGNUM tmp=t1; t1=t2; t2=tmp; } else c.sgn=sgn; for (int i=0;i<c.len;++i){ c.s[i]+=t1.s[i]-t2.s[i]; if (c.s[i]<0) { c.s[i]+=10; --c.s[i+1]; } } } while (c.s[c.len-1]==0&&c.len>1) --c.len; if (c.s[0]==0&&c.len==1) c.sgn=1; return c; } BIGNUM operator += (const BIGNUM & a) { *this = *this+a; return *this; } BIGNUM operator - (const BIGNUM & a) const { BIGNUM c=a; c.sgn=-c.sgn; c+=*this; return c; } BIGNUM operator -= (const BIGNUM & a) { *this = *this-a; return *this; } BIGNUM operator * (const BIGNUM & x) const { BIGNUM c; c.sgn=sgn*x.sgn; c.len=len+x.len; for (int i=0;i<len;++i) { for (int j=0;j<x.len;++j) { c.s[i+j]+=s[i]*x.s[j]; c.s[i+j+1]+=c.s[i+j]/10; c.s[i+j]%=10; } } while (c.s[c.len-1]==0&&c.len>1) --c.len; if (c.s[0]==0&&c.len==1) c.sgn=1; return c; } BIGNUM operator *= (const BIGNUM & x) { *this = *this*x; return *this; } }; istream& operator >> (istream &in,BIGNUM &x) { char num[MAXN]; in>>num; x=num; return in; } ostream& operator << (ostream &out,const BIGNUM& x) { if (x.sgn==-1) cout<<'-'; for (int i=x.len-1;i>=0;--i) cout<<x.s[i]; return out; } BIGNUM f[5001]; int main() { BIGNUM a,b; cin>>a>>b; cout<<a*b<<endl; cout<<a+b<<endl; cout<<a-b<<endl; return 0; }转载请注明 蝼蚁在奔跑/article/11374640.html
相关文章推荐
- 介绍一个在线的Linux,没有安装Linux或者虚拟机不喜欢用的童鞋可以简单的用这个jslinux测试各种命令
- 用C++制作一个简单的文字游戏,纯属C++入门,有兴趣的可以看看
- C++实现的一个简单两个大数相加程序!
- 如何判断一个C++对象是否在堆栈上(通过VirtualQuery这个API来获取堆栈的起始地址,然后就可以得到答案了),附许多精彩评论
- C++制作一个泛型容器(可以盛放各种类型的对象)
- 介绍一个在线的Linux,没有安装Linux或者虚拟机不喜欢用的童鞋可以简单的用这个jslinux测试各种命令
- iOS自动生成各种icon的工具 (希望有时间写一个简单文档)
- C++创建一个类(1.只能在堆上创建; 2 只能在栈上创建; 3 不能被继承(堆上和栈上都可以创建))
- C++反汇编揭秘1 – 一个简单C++程序反汇编解析 (Rev. 3)
- 一个简单C++的调试代码
- 我曾经得到的一个最好的编程建议
- 【转】一个简单的线程池(c++版)
- 一个最简单的OSG例子源代码(C++)
- 一个简单的可以接收TCP服务器数据的TCP客户端
- 刚刚得到一个新的DNS服务器,觉得还不错,可以一试
- 使用简单的nltk的API 得到一系列字母可以组成的所有单词
- qt写的一个简单的tcp服务器程序,可以接受消息发送数据
- 请写一个简单的幻灯效果页面。如果不使用JS来完成,可以加分。
- 在C++中可以用3种方法访问一个字符串