C++ 大数加、减、乘、除、乘方运算

#include 
#include 
#include 
#define DIGIT	4
#define DEPTH	10000
#define MAX     100
#define MAXSIZE 200000
using namespace std;
typedef int bignum_t[MAX + 1];

/*
*Created by HarvestWu on 2018.06.28.
*/

//大数类
class  BigInteger
{
public:
	int read(bignum_t a, istream& is );//输入数据
	void write(const bignum_t a, ostream& os );//输出数据
	int comp(const bignum_t a, const int c, const int d, const bignum_t b);
	void add(bignum_t a, const bignum_t b);//大数加法
	void sub(bignum_t a, const bignum_t b);//大数减法
	void sub(bignum_t a, const bignum_t b, const int c, const int d);
	void mul(bignum_t c, const bignum_t a, const bignum_t b);//大数乘法
	void div(bignum_t c, bignum_t a, const bignum_t b);//大数除法
	void pow(int b, int n);//大数乘方
private:

};


//输入数据
int BigInteger::read(bignum_t a, istream& is = cin)
{
	char buf[MAX*DIGIT + 1], ch;
	int i, j;
	memset((void*)a, 0, sizeof(bignum_t));
	if (!(is >> buf))	return 0;
	for (a[0] = strlen(buf), i = a[0] / 2 - 1; i >= 0; i--)
		ch = buf[i], buf[i] = buf[a[0] - 1 - i], buf[a[0] - 1 - i] = ch;
	for (a[0] = (a[0] + DIGIT - 1) / DIGIT, j = strlen(buf); j < a[0] * DIGIT; buf[j++] = '0');
	for (i = 1; i <= a[0]; i++)
	for (a[i] = 0, j = 0; j1; a[0]--);
	return 1;
}
//输出数据
void BigInteger::write(const bignum_t a, ostream& os = cout)
{
	int i, j;
	for (os << a[i = a[0]], i--; i; i--)
	for (j = DEPTH / 10; j; j /= 10)
		os << a[i] / j % 10;
}

int BigInteger::comp(const bignum_t a, const int c, const int d, const bignum_t b)
{
	int i, t = 0, O = -DEPTH * 2;
	if (b[0] - a[0]d; i--){
		t = t*DEPTH + a[i - d] * c - b[i];
		if (t > 0) return 1;
		if (t0) return 1;
		if (t0;
}

/*
*大数加法
*/
void BigInteger::add(bignum_t a, const bignum_t b)
{
	int i;
	for (i = 1; i <= b[0]; i++)
	if ((a[i] += b[i]) >= DEPTH)
		a[i] -= DEPTH, a[i + 1]++;
	if (b[0] >= a[0])
		a[0] = b[0];
	else
	for (; a[i] >= DEPTH&&i0);
}

/*
*大数减法
*/
void BigInteger::sub(bignum_t a, const bignum_t b)
{
	int i;
	for (i = 1; i <= b[0]; i++)
	if ((a[i] -= b[i]) < 0)
		a[i + 1]--, a[i] += DEPTH;
	for (; a[i]<0; a[i] += DEPTH, i++, a[i]--);
	for (; !a[a[0]] && a[0]>1; a[0]--);
}

void BigInteger::sub(bignum_t a, const bignum_t b, const int c, const int d)
{
	int i, O = b[0] + d;
	for (i = 1 + d; i <= O; i++)
	if ((a[i] -= b[i - d] * c) < 0)
		a[i + 1] += (a[i] - DEPTH + 1) / DEPTH, a[i] -= (a[i] - DEPTH + 1) / DEPTH*DEPTH;
	for (; a[i]<0; a[i + 1] += (a[i] - DEPTH + 1) / DEPTH, a[i] -= (a[i] - DEPTH + 1) / DEPTH*DEPTH, i++);
	for (; !a[a[0]] && a[0]>1; a[0]--);
}

/*
*大数乘法
*/
void BigInteger::mul(bignum_t c, const bignum_t a, const bignum_t b)
{
	int i, j;
	memset((void*)c, 0, sizeof(bignum_t));
	for (c[0] = a[0] + b[0] - 1, i = 1; i <= a[0]; i++)
	for (j = 1; j <= b[0]; j++)
	if ((c[i + j - 1] += a[i] * b[j]) >= DEPTH)
		c[i + j] += c[i + j - 1] / DEPTH, c[i + j - 1] %= DEPTH;
	for (c[0] += (c[c[0] + 1] > 0); !c[c[0]] && c[0] > 1; c[0]--);
}

/*
*大数除法
*/
void BigInteger::div(bignum_t c, bignum_t a, const bignum_t b)
{
	int h, l, m, i;
	memset((void*)c, 0, sizeof(bignum_t));
	c[0] = (b[0]> 1; h>l; m = (h + l + 1) >> 1)
	if (comp(b, m, i - 1, a)) h = m - 1;
	else l = m;
	for (; !c[c[0]] && c[0] > 1; c[0]--);
	c[0] = c[0] > 1 ? c[0] : 1;
}

/*
*大数乘方
*/
void BigInteger::pow(int b, int n)
{
	time_t start, end;//计时
	int *a, i, j;
	start = time(NULL);
	a = (int *)malloc(sizeof(int)* MAXSIZE);
	for (i = 0; i= 0; j--)
		if (a[j] >= 10)
		{
			a[j - 1] += a[j] / 10;
			a[j] %= 10;
		}
	}
	end = time(NULL);
	for (i = 0; a[i] == 0; i++);
	for (i; i> s;
		if (!s)break;
		select(s);
	}
	return 0;
}

 

你可能感兴趣的:(C/C++,数据结构(Data,Structure),算法)