一个cpp大整数类示例

v1.0.0
实现了:
构造函数和类型转换构造函数(int, long long int, char[], string)
复制构造函数(int, long long int, 同类, char[], string)(重载赋值运算符
重载一元运算符(-) 算术运算符(+ -) 比较运算符(== != > < >= <=)
实现了cout输出(重载流提取运算符
bool isZero() 是否为0
修正类型转换构造函数和复制构造函数string对于前面没有正负号的数读取为负数的错误

v1.0.1
重载运算符 *(同类 , int)
修正类型转换构造函数char []对于前面没有正负号的数读取为负数的错误
增加类型转换构造函数(const char []) 用于实现MInt m="666";
重载左移右移运算符(10)
复制构造函数增加const char[] 这个是什么时候写的?刚看见
bool getflag, void setflag
int length,int size
int at
重载[]
重载赋值运算符<<=,>>=
简单重载赋值运算符 -= += *= (调用重载的-+*函数,没有重写为直接修改*this)

v1.0.2
abscmp 小于返回-1 等于返回0 大于返回1
int absCmp
重载运算符 / (未测试运算速度)
简单重载了 /= 赋值运算符 (调用 / )
重载自增自减运算符(++n n++ --n n--)
修正了重载减法函数(operator-())的一个错误(150-1 -> 49)
MInt factorial_ 递归求阶乘 测试MAXL=1140时 500!可行,当MAXL太大时内存不够出错
MInt factorial  循环求阶乘 测试MAXL=6000时 2000!可算,且用时较短(2s内)

v1.0.3
重载运算符 %
cin输入(重载流插入运算符
一些函数加上了const,重载加法减法的函数稍微优化
新增大整数类和int的比较
--析构函数好像不需要自己写?
重载强制类型转换 int() long long int()
增加 operator + - * / (const int n) const{...}
注意:
  • MAXL 为 数组的长度,即数字的位数,可根据要计算的数字最多有多少位和运行内存的大小调整
  • 其中除法和取模运算可能比较慢
  • 强制类型转换int()是按int有32位写死的,longlongint 也是
  • 有错误或者建议欢迎评论

主要代码如下


#include<iostream>
#include<string>
#include<cstring>

using namespace std;

const int MAXL=6000;

class MInt{
public:
	bool fl;
	int len;
	int num[MAXL];
	MInt(){fl=true;len=1;memset(num,0,sizeof(num));}
	MInt(int n){
		if(n>=0){fl=true;}else{fl=false;n=-n;}
		len=(n==0?1:0);memset(num,0,sizeof(num));
		while(n){num[len++]=n%10;n/=10;}
	}
	MInt(long long int n){
		if(n>=0){fl=true;}else{fl=false;n=-n;}
		len=(n==0?1:0);memset(num,0,sizeof(num));
		while(n){num[len++]=n%10;n/=10;}
	}
	MInt(const char n[]){
		int l=strlen(n);
		len=0;
		memset(num,0,sizeof(num));
		if(l<=0){
			fl=true;
		}else {
			bool start=false;
			for(int i=0;i<l;++i){
				if(n[i]=='\0') break;
				if(start){
					num[len++] = (n[i]-'0');
				} else {
					if(n[i]==' '){}
					else if(n[i]=='-'){
						fl=false;start=true;
					} else if(n[i]=='+'){
						fl=true;start=true;
					} else if('0'<=n[i] && n[i]<='9'){
						fl=true;
						num[len++] = (n[i]-'0');
						start=true;
					}
				}
			}
			for(int i=0;i<len/2;++i){
				int tp=num[i];
				num[i]=num[len-1-i];
				num[len-1-i]=tp;
			}
		}
	}
	MInt(char n[], int l){
		len=0;
		memset(num,0,sizeof(num));
		if(l<=0){
			fl=true;
		}else {
			bool start=false;
			for(int i=0;i<l;++i){
				if(n[i]=='\0') break;
				if(start){
					num[len++] = (n[i]-'0');
				} else {
					if(n[i]==' '){}
					else if(n[i]=='-'){
						fl=false;start=true;
					} else if(n[i]=='+'){
						fl=true;start=true;
					} else if('0'<=n[i] && n[i]<='9'){
						fl=true;
						num[len++] = (n[i]-'0');
						start=true;
					}
				}
			}
			for(int i=0;i<len/2;++i){
				int tp=num[i];
				num[i]=num[len-1-i];
				num[len-1-i]=tp;
			}
		}
	}
	MInt(string n){
		int l=n.length();
		len=0;
		memset(num,0,sizeof(num));
		if(l==0){
			fl=true;
		}else {
			bool start=false;
			for(int i=0;i<l;++i){
				if(start){
					if('0'<=n[i] && n[i]<='9')
						num[len++] = (n[i]-'0');
					else 
						break;
				} else {
					if(n[i]==' '){}
					else if(n[i]=='-'){
						fl=false;start=true;
						while(!('0'<=n[i+1] && n[i+1]<='9')) ++i;
					} else if(n[i]=='+'){
						fl=true;start=true;
						while(!('0'<=n[i+1] && n[i+1]<='9')) ++i;
					} else if('0'<=n[i] && n[i]<='9'){
						fl=true;
						num[len++] = (n[i]-'0');
						start=true;
					}
				}
			}
			for(int i=0;i<len/2;++i){
				int tp=num[i];
				num[i]=num[len-1-i];
				num[len-1-i]=tp;
			}
		}
	}
	MInt(const MInt & n){
		fl=n.fl;len=n.len;memset(num,0,sizeof(num));
		for(int i=0;i<len;++i)num[i]=n.num[i];
	}
	MInt & operator=(int n){
		if(n>=0){fl=true;}else{fl=false;n=-n;}
		len=(n==0?1:0);memset(num,0,sizeof(num));
		while(n){num[len++]=n%10;n/=10;}
		return *this;
	}
	MInt & operator=(long long int n){
		if(n>=0){fl=true;}else{fl=false;n=-n;}
		len=(n==0?1:0);memset(num,0,sizeof(num));
		while(n){num[len++]=n%10;n/=10;}
		return *this;
	}
	MInt & operator=(const MInt & n){
		if(this!=&n){
			memset(num,0,sizeof(num));
			fl=n.fl;len=n.len;
			for(int i=0;i<len;++i)num[i]=n.num[i];
		}
		return *this;
	}
	MInt & operator=(const char n[]){
		int l=strlen(n);
		len=0;
		memset(num,0,sizeof(num));
		if(l<=0){
			fl=true;
		}else{
			bool start=false;
			for(int i=0;i<l;++i){
				if(n[i]=='\0') break;
				if(start){
					num[len++] = (n[i]-'0');
				} else {
					if(n[i]==' '){}
					else if(n[i]=='-'){
						fl=false;start=true;
					} else if(n[i]=='+'){
						fl=true;start=true;
					} else if('0'<=n[i] && n[i]<='9'){
						num[len++] = (n[i]-'0');
						start=true;
					}
				}
			}
			for(int i=0;i<len/2;++i){
				int tp=num[i];
				num[i]=num[len-1-i];
				num[len-1-i]=tp;
			}
		}
		return *this;
	}
	MInt & operator=(string n){
		int l=n.length();
		len=0;
		memset(num,0,sizeof(num));
		if(l==0){
			fl=true;
		}else {
			bool start=false;
			for(int i=0;i<l;++i){
				if(start){
					if('0'<=n[i] && n[i]<='9')
						num[len++] = (n[i]-'0');
					else 
						break;
				} else {
					if(n[i]==' '){}
					else if(n[i]=='-'){
						fl=false;start=true;
						while(!('0'<=n[i+1] && n[i+1]<='9')) ++i;
					} else if(n[i]=='+'){
						fl=true;start=true;
						while(!('0'<=n[i+1] && n[i+1]<='9')) ++i;
					} else if('0'<=n[i] && n[i]<='9'){
						fl=true;
						num[len++] = (n[i]-'0');
						start=true;
					}
				}
			}
			for(int i=0;i<len/2;++i){
				int tp=num[i];
				num[i]=num[len-1-i];
				num[len-1-i]=tp;
			}
		}
		return *this;
	}
	bool operator<(const MInt & n)const{
		if(!fl && n.fl) return true;
		else if(fl && !n.fl) return false;
		else {
			if(len<n.len) return fl;
			else if(len>n.len) return !fl;
			else {
				for(int i=len;i>=0;--i){
					if(num[i]<n.num[i]) return fl;
					else if(num[i]>n.num[i]) return !fl;
				}
				return false;
			}
		}
	}
	bool operator<(const int n)const{
		return *this<MInt(n);
	}
	bool operator<=(const MInt & n)const{
		if(!fl && n.fl) return true;
		else if(fl && !n.fl) return false;
		else {
			if(len<n.len) return fl;
			else if(len>n.len) return !fl;
			else {
				for(int i=len;i>=0;--i){
					if(num[i]<n.num[i]) return fl;
					else if(num[i]>n.num[i]) return !fl;
				}
				return true;
			}
		}
	}
	bool operator<=(const int n)const{
		return *this<=MInt(n);
	}
	bool operator>(const MInt & n)const{
		if(!fl && n.fl) return false;
		else if(fl && !n.fl) return true;
		else {
			if(len<n.len) return !fl;
			else if(len>n.len) return fl;
			else {
				for(int i=len;i>=0;--i){
					if(num[i]<n.num[i]) return !fl;
					else if(num[i]>n.num[i]) return fl;
				}
				return false;
			}
		}
	}
	bool operator>(const int n)const{
		return *this>MInt(n);
	}
	bool operator>=(const MInt & n)const{
		if(!fl && n.fl) return false;
		else if(fl && !n.fl) return true;
		else {
			if(len<n.len) return !fl;
			else if(len>n.len) return fl;
			else {
				for(int i=len;i>=0;--i){
					if(num[i]<n.num[i]) return !fl;
					else if(num[i]>n.num[i]) return fl;
				}
				return true;
			}
		}
	}
	bool operator>=(const int n)const{
		return *this>=MInt(n);
	}
	bool operator==(const MInt & n)const{
		if(fl!=n.fl || len!=n.len) return false;
		for(int i=0;i<len;++i){
			if(num[i]!=n.num[i]) return false;
		}
		return true;
	}
	bool operator==(const int n)const{  // 用于MInt(1)==1 其他!=,>=等也可以
		return *this==MInt(n);
	}
	bool operator!=(const MInt & n)const{
		if(fl!=n.fl || len!=n.len) return true;
		for(int i=0;i<len;++i){
			if(num[i]!=n.num[i]) return true;
		}
		return false;
	}
	bool operator!=(const int n)const{
		return *this!=MInt(n);
	}
	bool isZero()const{
		return (len==0 && num[0]==0);
	}
	MInt operator-()const{
		MInt tp(*this);
		tp.fl=!tp.fl;
		return tp;
	}
	MInt operator+(const MInt & n)const{
		MInt tp;
		if((fl&&n.fl) || (!fl&&!n.fl)){
			tp.fl=fl;
			int carry=0;
			int m=max(len,n.len);
			int w;
			for(int i=0;i<m;++i){
				w=num[i]+n.num[i]+carry;
				if(w<10){
					tp.num[i]=w;
					carry=0;
				} else {
					carry=w/10;
					tp.num[i]=w-10;
				}
			}
			if(carry){
				tp.len=m+1;
				tp.num[m]=1;
			} else {
				tp.len=m;
			}
		} else {
			/*MInt tp2;
			if(fl){
				tp=-n;
				tp2=*this;
				tp=tp2 - tp;
			} else {
				tp= - *this;
				tp2=n;
				tp=tp2-tp;
			}  //改为下两行  */
			if(fl) tp = *this - (-n);
			else tp = n - (-*this);
		}
		return tp;
	}
	MInt operator+(const int n)const{return *this + MInt(n);}
	MInt operator-(const MInt & n)const{
		MInt tp;
		if((fl&&n.fl) || (!fl&&!n.fl)){
			if((fl&&*this>=n)||(!fl&&*this<n)){
				tp.fl=fl;
				int carry=0;
				int w;
				int ma=len,mi=n.len;
				if(len>n.len){
					bool isend=false;
					for(int i=0;i<ma;++i){
						if(isend) {tp.num[i]=num[i];continue;}
						w=num[i]-n.num[i]+carry;
						if(w<0){
							tp.num[i]=w+10;
							carry=-1;
						} else {
							tp.num[i]=w;
							carry=0;
						}
						if(i>=mi && carry==0) isend=true;
					}
				} else {
					for(int i=0;i<ma;++i){
						w=num[i]-n.num[i]+carry;
						if(w<0){
							tp.num[i]=w+10;
							carry=-1;
						} else {
							tp.num[i]=w;
							carry=0;
						}
					}
				}
				while(ma>0 && tp.num[ma-1]==0) --ma;
				if(ma==0) tp.len=1;
				else tp.len=ma;
			} else {
				tp.fl=!fl;
				int carry=0;
				int w;
				int ma=n.len,mi=len;
				if(ma>mi){
					bool isend=false;
					for(int i=0;i<ma;++i){
						if(isend) {tp.num[i]=num[i];continue;}
						w=n.num[i]-num[i]+carry;
						if(w<0){
							tp.num[i]=w+10;
							carry=-1;
						} else {
							tp.num[i]=w;
							carry=0;
						}
						if(i>=mi && carry==0) break;
					}
				} else {
					for(int i=0;i<ma;++i){
						w=n.num[i]-num[i]+carry;
						if(w<0){
							tp.num[i]=w+10;
							carry=-1;
						} else {
							tp.num[i]=w;
							carry=0;
						}
					}
				}
				while(tp.num[ma]==0 && ma>0) --ma;
				if(ma==-1) tp.len=1;
				else tp.len=ma+1;
			}
		} else {
			/*MInt tp2;
			if(fl){
				tp=-n;
				tp2=*this;
				tp=tp2+tp;
			} else {
				tp=-*this;
				tp2=n;
				tp=-(tp+tp2);
			}*/
			if(fl) tp=*this + (-n);
			else tp=-((-*this) + n);
		}
		return tp;
	}
	MInt operator-(const int n)const{return *this - MInt(n);}
	MInt operator*(const MInt & n)const{
		MInt tp;
		tp.fl=(fl==n.fl);
		tp.len=len+n.len;
		memset(tp.num,0,sizeof(tp.num));
		for(int i=0;i<len;++i){
			for(int j=0;j<n.len;++j){
				tp.num[i+j] += num[i]*n.num[j];
			}
		}
		int carry=0,w;
		for(int i=0;i<tp.len;++i){
			w = tp.num[i] + carry;
			if(w>=10){
				carry = w/10;
				tp.num[i] = w - carry*10;
			} else {
				carry = 0;
				tp.num[i] = w;
			}
		}
		while(tp.num[tp.len-1]==0) --tp.len;
		return tp;
	}
	MInt operator*(int n)const{
		if(n==0){return MInt();}
		else if(n==10){return *this<<1;}
		else if(n==100){return *this<<2;}
		else if(n==1000){return *this<<3;}
		else if(n==10000){return *this<<4;}
		else if(n==-10){return -(*this<<1);}
		else if(n==-100){return -(*this<<2);}
		else if(n==-1000){return -(*this<<3);}
		else if(n==-10000){return -(*this<<4);}
		else{return *this * MInt(n);}
	}
	MInt operator<<(int n)const{
		if(n<=0) return *this;
		MInt tp;
		tp.fl=fl;
		tp.len=len+n;
		for(int i=0;i<n;++i) tp.num[i]=0;
		for(int j=0;j<len;++j) tp.num[j+n]=num[j];
		return tp;
	}
	MInt operator>>(int n)const{
		if(n<=0) return *this;
		MInt tp;
		for(int i=0;i<len-n;++i) tp.num[i]=num[i+n];
		return tp;
	}
	MInt & operator<<=(int n){
		if(n<=0) return *this;
		for(int j=len-1;j>=0;++j)num[j+n]=num[j];
		for(int i=0;i<n;++i)num[i]=0;
		len+=n;
		return *this;
	}
	MInt & operator>>=(int n){
		if(n<=0) return *this;
		for(int i=0;i<len-n;++i) num[i]=num[i+n];
		for(int i=0;i<n;++i)num[i+len]=0;
		len-=n;
		return *this;
	}
	bool getflag()const{return fl;}
	void setflag(bool f){fl=f;}
	int length()const{return len;}
	int size()const{return len;}
	int at(int n)const{
		if(n<0 || n>=len) return -1;
		else return num[n];
	}
	int operator[](int n)const{
		if(n<0 || n>=MAXL) return -1;
		else return num[n];
	}
	MInt & operator-=(const MInt & n){
		*this = *this - n;
		return *this;
	}
	MInt & operator+=(const MInt & n){
		*this = *this + n;
		return *this;
	}
	MInt & operator*=(const MInt & n){
		*this = *this * n;
		return *this;
	}
	int abscmp(const MInt & n)const{
		if(len<n.len) return -1;
		else if(len>n.len) return 1;
		else {
			for(int i=len-1;i>=0;--i){
				if(num[i]<n.num[i]) return -1;
				else if(num[i]>n.num[i]) return 1;
			}
			return 0;
		}
	}
	MInt operator/(const MInt & n)const{
		int absCmp(const MInt, const MInt);
		MInt tp1=*this,tp2=n,res;
		tp1.fl=true;tp2.fl=true;
		if(n.isZero()) throw -1;
		int p=abscmp(n);
		if(p==1){
			for(int i=len-n.len;i>=0;--i){
				tp2=n<<i;
				p=absCmp(tp1, tp2);
				if(p==-1){res.num[i]=0;}
				else if(p==0){res.num[i]=1;break;}
				else{
					tp1-=tp2;
					p=absCmp(tp1, tp2);
					if(p==-1){res.num[i]=1;continue;}else
					if(p==0){res.num[i]=2;break;}
					tp1-=tp2;
					p=absCmp(tp1, tp2);
					if(p==-1){res.num[i]=2;continue;}
					else if(p==0){res.num[i]=3;break;}
					tp1-=tp2;
					p=absCmp(tp1, tp2);
					if(p==-1){res.num[i]=3;continue;}
					else if(p==0){res.num[i]=4;break;}
					tp1-=tp2;
					p=absCmp(tp1, tp2);
					if(p==-1){res.num[i]=4;continue;}
					else if(p==0){res.num[i]=5;break;}
					tp1-=tp2;
					p=absCmp(tp1, tp2);
					if(p==-1){res.num[i]=5;continue;}
					else if(p==0){res.num[i]=6;break;}
					tp1-=tp2;
					p=absCmp(tp1, tp2);
					if(p==-1){res.num[i]=6;continue;}
					else if(p==0){res.num[i]=7;break;}
					tp1-=tp2;
					p=absCmp(tp1, tp2);
					if(p==-1){res.num[i]=7;continue;}
					else if(p==0){res.num[i]=8;break;}
					tp1-=tp2;
					p=absCmp(tp1, tp2);
					if(p==-1){res.num[i]=8;continue;}
					else if(p==0){res.num[i]=9;break;}
					tp1-=tp2;res.num[i]=9;
				}
			}
			for(int i=len-n.len;i>=0;--i) if(res.num[i]!=0){res.len=i+1;break;}
		}
		else if(p==0){res.num[0]=1;}
		return res;
	}
	MInt operator/(const int n)const{return *this / MInt(n);}
	MInt operator%(const MInt & n)const{
		if(!n.fl || n.isZero()) return MInt(-1);
		else if(n==1) return MInt();
		else if(!fl) {
			MInt tp=((-*this) % n);
			if(tp.isZero()) return MInt();
			else return n - tp;
		}
		
		int absCmp(const MInt, const MInt);
		MInt tp1=*this,tp2=n;
		int p=abscmp(n);
		if(p==1){
			for(int i=len-n.len;i>=0;--i){
				tp2=n<<i;
				p=absCmp(tp1, tp2);
				if(p==-1){}
				else if(p==0){return MInt();}
				else{
					tp1-=tp2;
					p=absCmp(tp1, tp2);
					if(p==-1){continue;}else
					if(p==0){return MInt();}
					tp1-=tp2;
					p=absCmp(tp1, tp2);
					if(p==-1){continue;}
					else if(p==0){return MInt();}
					tp1-=tp2;
					p=absCmp(tp1, tp2);
					if(p==-1){continue;}
					else if(p==0){return MInt();}
					tp1-=tp2;
					p=absCmp(tp1, tp2);
					if(p==-1){continue;}
					else if(p==0){return MInt();}
					tp1-=tp2;
					p=absCmp(tp1, tp2);
					if(p==-1){continue;}
					else if(p==0){return MInt();}
					tp1-=tp2;
					p=absCmp(tp1, tp2);
					if(p==-1){continue;}
					else if(p==0){return MInt();}
					tp1-=tp2;
					p=absCmp(tp1, tp2);
					if(p==-1){continue;}
					else if(p==0){return MInt();}
					tp1-=tp2;
					p=absCmp(tp1, tp2);
					if(p==-1){continue;}
					else if(p==0){return MInt();}
					tp1-=tp2;
				}
			}
			for(int i=n.len;i>=0;--i) if(tp1.num[i]!=0){tp1.len=i+1;break;}
			return tp1;
		}
		else if(p==0){return MInt();}
		else return *this;
	}
	MInt operator%(const int n)const{return *this%MInt(n);}
	MInt & operator/=(const MInt & n){
		*this = *this / n;
		return *this;
	}
	MInt & operator++(){
		int i=0;
		if(fl){
			while(i<len){
				if(num[i]<9){
					++num[i];break;
				} else {
					num[i++]=0;
				}
			}
			if(i==len) {
				num[len++]=1;
			}
		} else {
			if(len==1 && num[0]==1){
				fl=true;num[0]=0;
			} else {
				while(i<len){
					if(num[i]>0){
						--num[i];break;
					} else {num[i++]=9;}
				}
				if(i==len-1){
					while(len>1 && num[len-1]==0) --len;
				}
			}
		}
		return *this;
	}
	MInt operator++(int){
		MInt tp(*this);
		int i=0;
		if(fl){
			while(i<len){
				if(num[i]<9){
					++num[i];break;
				} else {
					num[i++]=0;
				}
			}
			if(i==len) {
				num[len++]=1;
			}
		} else {
			if(len==1 && num[0]==1){
				fl=true;num[0]=0;
			} else {
				while(i<len){
					if(num[i]>0){
						--num[i];break;
					} else {num[i++]=9;}
				}
				if(i==len-1){
					while(len>1 && num[len-1]==0) --len;
				}
			}
		}
		return tp;
	}
	MInt & operator--(){
		int i=0;
		if(!fl){
			while(i<len){
				if(num[i]<9){
					++num[i];break;
				} else {
					num[i++]=0;
				}
			}
			if(i==len) {
				num[len++]=1;
			}
		} else {
			if(len==1 && num[0]==0){
				fl=false;num[0]=1;
			} else {
				while(i<len){
					if(num[i]>0){
						--num[i];break;
					} else {num[i++]=9;}
				}
				if(i==len-1){
					while(len>1 && num[len-1]==0) --len;
				}
			}
		}
		return *this;
	}
	MInt operator--(int){
		MInt tp(*this);
		int i=0;
		if(!fl){
			while(i<len){
				if(num[i]<9){
					++num[i];break;
				} else {
					num[i++]=0;
				}
			}
			if(i==len) {
				num[len++]=1;
			}
		} else {
			if(len==1 && num[0]==0){
				fl=false;num[0]=1;
			} else {
				while(i<len){
					if(num[i]>0){
						--num[i];break;
					} else {num[i++]=9;}
				}
			}
		}
		return tp;
	}
	operator int()const{
		int ans=0;
		if(len<10 || (len==10 && MInt("-2147483648")<=*this && *this <= MInt("2147483647"))) {
			for(int i=len-1;i>=0;--i){
				ans = ans*10-num[i];
			}
			if(fl) return -ans; else return ans;
		} else {
			return ((*this)%MInt("4294967296"))-MInt("2147483648");
		}
	}
	operator long long int()const{
		long long int ans=0;
		if(len<19 || (len==19 && MInt("-9223372036854775808")<=*this && *this <= MInt("9223372036854775807"))) {
			for(int i=len-1;i>=0;--i){
				ans = ans*10-num[i];
			}
			if(fl) return -ans; else return ans;
		} else {
			return ((*this)%MInt("18446744073709551616"))-MInt("9223372036854775808");
		}
	}
};
ostream & operator<<(ostream & o, const MInt & n){
	if(!n.fl) o<<"-";
	for(int i=n.len-1;i>=0;--i) o<<n.num[i];
	return o;
}
istream & operator>>(istream & i, MInt & n){
	string s;
	i >> s;
	n=MInt(s);
	return i;
}

int absCmp(const MInt a, const MInt b){
	if(a.len<b.len) return -1;
	else if(a.len>b.len) return 1;
	else {
		for(int i=a.len-1;i>=0;--i){
			if(a.num[i]<b.num[i]) return -1;
			else if(a.num[i]>b.num[i]) return 1;
		}
		return 0;
	}
}

// 阶乘函数
MInt factorial(MInt n){
	if(n.fl==false) return MInt(-1);
	else if(n.len==1){
		if(n.num[0]==0) return MInt(1);
		else if(n.num[0]==1) return MInt(1);
		else if(n.num[0]==2) return MInt(2);
		else if(n.num[0]==3) return MInt(6);
		else if(n.num[0]==4) return MInt(24);
		else if(n.num[0]==5) return MInt(120);
		else if(n.num[0]==6) return MInt(720);
		else if(n.num[0]==7) return MInt(5040);
		else if(n.num[0]==8) return MInt(40320);
		else return MInt(362880);
	} else {
		// return n * factorial_(n-1);
		MInt tp=1;
		while(n>1){ tp = tp*n; n = n-MInt(1);}
		return tp;
	}
}

下面是一些测试

void test1(){
	int n=567;
	string s=" 123 ";
	char ch[]="456";
	MInt m1(n),m2(s),m3(ch,sizeof(ch)),m4;
	//cout << m1.len << endl << m2.len << endl << m3.len << endl << m4.len << endl;
	cout << m1 << endl << m2 << endl << m3 << endl << m4 << endl;
	
	MInt t1=10,t2=12,t3=-5,t4=-6;
	cout << t1 << " " << t2 << " " << t3 << " " << t4 << endl;
	MInt t5,t6,t7,t8;
	t5=t1+t2;t6=t1-t2;t7=t1+t3;t8=t1-t3;
	cout << t5 << " " << t6 << " " << t7 << " " << t8 << endl;
	t5=t2-t1;t6=t3-t1;t7=t3-t4;t8=t3+t4;
	cout << t5 << " " << t6 << " " << t7 << " " << t8 << endl;
	t5="16";t6=string("16");t7=0;t8=t7-t1;
	cout << t5 << " " << t6 << " " << t7 << " " << t8 << endl;
	
}

void t2(){
	MInt t1="999999999999999999",t2("99999999999999999");
	cout << t1 << "*" << t2 << "=";
	MInt t3=t1*t2,t4=t1*100,t5;
	cout << t3 << endl;
	t3=26;t4=(t3<<2);
	cout << t4 << endl;
	t3=10000;t4=26;
	t5=t3/t4;
	cout << t5 << endl;
	t1=-2;t2=++t1;t3=++t2;t4=++t3;t5=t4++;
	cout << t1<<" "<<t2<<" "<<t3<<" "<<t4<<" "<<t5<<endl;
	t1=2;t2=--t1;t3=--t2;t4=--t3;t5=t4--;
	cout << t1<<" "<<t2<<" "<<t3<<" "<<t4<<" "<<t5<<endl;
	t1=100;t2=--t1;t3=++t2;t4=-100;t5=++t4;
	cout << t1<<" "<<t2<<" "<<t3<<" "<<t4<<" "<<t5<<endl;
}

void t3(){
	cout << "100!=" << factorial(100) << endl << endl;
	cout << "150!="<< factorial(150) << endl << endl;
	cout << "200!=" << factorial(200) << endl << endl;
	cout << "250!=" << factorial(250) << endl << endl;
	cout << "300!=" << factorial(300) << endl << endl;
	cout << "350!=" << factorial(350) << endl << endl;
	cout << "400!=" << factorial(400) << endl << endl;
	cout << "450!=" << factorial(450) << endl << endl; //1001位数
	cout << "500!=" << factorial(500) << endl << endl; //1135位数
	cout << "800!=" << factorial(800) << endl << endl; //1977位数
	cout << "2000!=" << factorial(2000) << endl << endl; //5736位数
}

void t4(){
	MInt ti,t2=2;
	cin >> ti;
	cout << ti << " " << ti%t2 << endl;
	cout << ti%3 << " " << ti%5 << endl;
	cout << ti%7 << " " << ti%10 << endl;
	ti="12345678901234567890";
	int i=(int)ti;
	long long int l=(long long int)ti;
	cout << ti << " " << i << " " << l << endl;
}
int main(){
	test1();
	t2();
	//t3();
	t4();
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今夕何夕2112

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值