首页 > 学院 > 开发设计 > 正文

大数相加 大数相乘 C++

2019-11-14 12:40:49
字体:
来源:转载
供稿:网友
最简单的思路是直接用int型数组存储大数的每一位,程序比较容易实现,但是效率稍低,直接上代码。
#include<iostream>#include<cstring>using namespace std;class bign{	int num[1000];	int pos_high;//最高位所在位置public:	bign() { pos_high = -1; }	bign(const bign&t)	{		memcpy(num, t.num, sizeof(num));		pos_high = t.pos_high;	}	bign Operator=(const bign&t)	{		memcpy(num, t.num, sizeof(num));		pos_high = t.pos_high;		return t;	}	bign operator=(const char*t)	{		int len = strlen(t);		for (int i = 0;i < len;i++)			num[i] = t[len - i - 1]-'0';		pos_high = len - 1;		return *this;	}	bign operator+(const bign&t)const	{		bign ans;		int i,r = 0,s;		for ( i = 0;i <= pos_high || i <= t.pos_high;i++)		{			if (i > t.pos_high)			{				s = num[i] + r;				ans.num[i] = s%10;				r = s / 10;			}			else if (i > pos_high)			{				s = t.num[i] + r;				ans.num[i] = s % 10;				r = s / 10;			}			else			{				s = num[i] + t.num[i]+r;				ans.num[i]=s % 10;				r = s / 10;			}		}		if (r > 0)			ans.num[i] = 1, ans.pos_high = i;		else			ans.pos_high = i - 1;		return ans;	}	bign operator*(const bign&t)const	{		bign te,ans;		int i, j,r,s;		char str[1000];		for (i = 0;i <= t.pos_high;i++)		{			r = 0;			te.pos_high = 0;			for (int k = 0;k < i;k++)				str[k] = '0';			str[i] = '/0';			te = str;			for (j = 0;j <= pos_high;j++)			{				s = t.num[i]*num[j] + r;				te.num[j+i] =s% 10;				r = s / 10;			}			if (r > 0)				te.num[j+i] = r,te.pos_high=j+i;			else				 te.pos_high = j-1+i;			ans = ans + te;		}		return ans;	}	bign operator++()	{		int i,r=1,s;		for (i = 0;i <= pos_high&&r>0;i++)		{			s = num[i] + r;			num[i] = s % 10;			r = s / 10;		}		if (r > 0)			num[i] = 1,pos_high++;		return *this;	}	bool operator<(const bign&t)	{		int i;		if (t.pos_high != pos_high)			return pos_high < t.pos_high;		else		{			for ( i = pos_high;i >= 0 && num[i] == t.num[i];i--);			return num[i] < t.num[i];		}	}	friend ostream& operator<<(ostream&out,const bign&x)	{		for (int i = x.pos_high;i >= 0;i--)			out << x.num[i];		return out;	}	int mod(int MOD)	{		int i, res = 0;		for (i = pos_high;i>=0;i--)			res = (num[i] + res * 10)%MOD;		return res;	}};int main(){	bign a,b,c,d;         a = "9999999999999999999999999999999999999999999999999999999999999";	 b = "9999999999999999999999999999999999999999999999999999999999999999999";	c = a + b;	d = a*b;	cout << c << endl;	cout << d << endl;		return 0;	cout << n;}

int型可以表示所有9位数,仅表示一位不仅浪费空间,而且增加了9倍的运算次数,故可以用int表示9位数进行优化,如123456789这个数占1个,1234567890占两个。乘法采用模拟手动算法的方法,各位依次相乘,最后相加。直接看代码吧。

#include<iostream>#include<cstring>using namespace std;const int MOD = 1000000000;class bign{	int num[10000];//10^9进制	int len;      //大数长度public:	bign() { len = 0; }	bign(char*t)	{		int length = strlen(t);		int a, b, sum,c=0;		a = length / 9;		b = length % 9;		for (int i = 0;i < a;i++)		{			sum = 0;			for (int j = 0;j<9 ;j++)				sum = sum * 10 +( t[b+j + (a-i-1) * 9]-'0');			num[i] = sum;		}		if (b > 0){			sum = 0;			for (int i = 0;i <b;i++)				sum = sum * 10 + t[i]-'0';			num[a] = sum;			len = a + 1;		}		else			len = a;	}	bign(const bign&t)	{		memcpy(num, t.num, sizeof(int)*t.len);		len = t.len;	}	bign operator=(bign const&t)	{		memcpy(num, t.num, sizeof(int)*t.len);		len = t.len;		return t;	}	bign operator++()	{		int r = 1, s;		for (int i = 0;i < len&&r == 1;i++)		{			s = num[i] + r;			num[i] = s % MOD;			r = s / MOD;		}		if (r > 0)			num[len] = 1, len = len + 1;		return*this;	}	bign operator+(const bign&t)const	{		bign ans;		int  r = 0, s, i;		for (i = 0;i < len || i < t.len;i++)		{			if (i >= len)			{				s = t.num[i] + r;				ans.num[i] = s % MOD;				r = s / MOD;			}			else if (i >= t.len)			{				s = num[i] + r;				ans.num[i] = s % MOD;				r = s / MOD;			}			else			{				s = num[i] + t.num[i] + r;				ans.num[i] = s % MOD;				r = s / MOD;			}		}		if (r > 0)			ans.num[i] = r, ans.len = i + 1;		else			ans.len = i;		return ans;	}	bign operator+(const int&t)	{		bign ans;		int r =t, s,i;		for (i = 0;i < len&&r>0;i++)		{			s = num[i] + r;			ans.num[i] = s % MOD;			r = s / MOD;		}		if (r > 0)			ans.num[len] = 1, ans.len = len + 1;		else		{			for (;i < len;i++)				ans.num[i] = num[i];			ans.len = len;		}		return ans;	}	bign operator*(const bign&t)const	{		bign ans, te;		int   i,j;		long long r = 0, s;		for (i = 0;i < len;i++ )		{			if (num[i] == 0) continue;			for (int k = 0;k < i;k++)				te.num[k] = 0;			for (j = 0;j < t.len;j++)			{				s =(long long)num[i] * (long long)t.num[j]+r;				r = s /MOD;				te.num[i + j] =(int)( s % MOD);			}			if (r > 0)				te.num[i + j] = (int)r, te.len = i + j + 1,r=0;			else				te.len=i + j;			ans = ans + te;		}		return ans;	}	bign operator*(const int&t)	{		bign ans;		long long r = 0, s;		int i;		for (i = 0;i < len;i++)		{			s = (long long)num[i] * (long long)t + r;			r = s / MOD;			ans.num[i] = (int)(s % MOD);		}		if (r > 0)			ans.num[i] = (int)r, ans.len = i + 1, r = 0;		else			ans.len = i ;		return ans;	}	friend ostream& operator<<(ostream&out, const bign&t)	{		int i = t.len - 1;		if (i >= 0) {			out << t.num[i];//最高位直接输出			for (i--;i >= 0;i--)			{				out.width(9);//设置位宽				out.fill('0');//补充前置0,如123输出000000123				out << t.num[i];			}		}		else out << 0;//大数占0位直接输出0		return out;	}	bool operator<(const bign&t)	{		if (t.len != len)			return len < t.len;		else		{			int i;			for (i = len - 1;i >= 0 && t.num[i] == num[i];i--);			return num[i] < t.num[i];		}	}};int main(){	bign a = "9999999999999999999999999999999999999999999999999999999999999",		b = "9999999999999999999999999999999999999999999999999999999999999999999", c,d;	c = a + b;	d = a*b;	cout << c << endl;	cout << d << endl;	return 0;}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表