高精度加法和乘法的c++实现

上一篇用c写了高精度加法的计算,总的来说,从底层写起的话,对于不是很繁琐的代码,面向过程还是比较方便的。但是当我打算计算高精度乘法的时候,发现计算过程要比加法麻烦的多,而且会反复用到一些函数的调用,而且这里面还会用到高精度加法的计算,于是果断放弃的c,采用c++。


这里,我写了一个类,名为megaData,就是指一个很大的数(不过这个单词好像是指大数据,完全不懂大数据是什么,反正就这么命名了)。


类视图如下:

高精度加法和乘法的c++实现_第1张图片


.h文件:

#pragma once
#include
#include

using namespace std;

//数据倒序储存(输出的时候反序)
class megaData
{
private:
	int * pmegaData;//指向数据的指针

	int length;//数据长度

	megaData operator*(int a);//"*"的重载(a是个位数)
	megaData extend(int Len, int move, megaData &a);
public:
	megaData();
	megaData(int length);
	megaData(megaData&a);//拷贝构造

	megaData operator=(megaData&a);//"="的重载

	megaData operator+(megaData&a);//"+"的重载

	megaData operator*(megaData&a);//"*"的重载

	void print();//打印数据

	void printLenth();//打印数据长度

	~megaData();
};


.cpp文件:

#include "megaData.h"

megaData::megaData()
{
	srand((unsigned)time(NULL));

	pmegaData = new int[100];

	for (int i = 0; i < 100; ++i) {
		pmegaData[i] = rand() % 10;
	}

	this->length = 100;
}

megaData::megaData(int length)
{

	pmegaData = new int[length];

	for (int i = 0; i < length; ++i) {
		pmegaData[i] = rand() % 10;
	}

	this->length = length;
}

megaData::megaData(megaData & a)
{
	pmegaData = new int[a.length];

	length = a.length;

	memcpy(pmegaData, a.pmegaData, sizeof(int)*length);
}

//"="的重载
megaData megaData::operator=(megaData & a)
{
	delete(this->pmegaData);

	this->pmegaData = new int[a.length];

	this->length = a.length;

	for (int i = 0; i < a.length; ++i) {

		this->pmegaData[i] = a.pmegaData[i];
	}

	return *this;
}


//"+"的重载
megaData megaData::operator+(megaData &a)
{
	int lenth_result;//结果的长度(开头可能会是0)

	if (this->length > a.length) lenth_result = this->length + 1;
	else lenth_result = a.length + 1;//通过一个判断来确定结果的长度


	int * a2 = new int[lenth_result];//a2***********

	int * b2 = new int[lenth_result];//b2***********

	memcpy(a2, a.pmegaData, sizeof(int)*a.length);

	memset(a2 + a.length, 0, sizeof(int)*(lenth_result - a.length));

	memcpy(b2, this->pmegaData, sizeof(int)*this->length);

	memset(b2 + this->length, 0, sizeof(int)*(lenth_result - this->length));


	int * result = new int[lenth_result];//result*********


	result[0] = a2[0] + b2[0];

	for (int i = 1; i < lenth_result - 1; ++i) {

		result[i] = a2[i] + b2[i] + result[i - 1] / 10;

		result[i - 1] = result[i - 1] % 10;
	}

	result[lenth_result - 1] = result[lenth_result - 2] / 10;

	result[lenth_result - 2] = result[lenth_result - 2] % 10;

	delete[] a2;
	delete[] b2;

	megaData result2(lenth_result);

	if (result[lenth_result - 1] == 0) {//如果结果的最大位是0(说明多了一位)

		delete[] result2.pmegaData;

		result2.pmegaData = new int[lenth_result - 1];

		memcpy(result2.pmegaData, result, sizeof(int)*(lenth_result - 1));

		result2.length = lenth_result - 1;
	}
	else {
		memcpy(result2.pmegaData, result, sizeof(int)*(lenth_result));
	}
	return result2;
}

//"*"的重载
megaData megaData::operator*(megaData & a){

	int Len = this->length + a.length;
	
	megaData result = *this * a.pmegaData[a.length-1];

	result = extend(Len, a.length - 1, result);

	for (int i = a.length - 2; i >= 0; --i) {

		megaData tempresult = *this * a.pmegaData[i];

		tempresult = extend(Len, i, tempresult);

		result = tempresult + result;
	}

	if (result.pmegaData[result.length] == 0) {//如果结果的最大位是0(说明多了一位)
		result.length -= 1;
	}

	return result;
}


//"*"的重载(a是个位数)
megaData megaData::operator*(int a)
{
	megaData result(this->length + 1);

	for (int j = 0; j < length + 1; ++j) {

		if (j == 0) {//第一位

			result.pmegaData[j] = a*this->pmegaData[j] % 10;

		}
		else if (j == length ) {//最后一位

			result.pmegaData[j] = a*this->pmegaData[j - 1] / 10 + result.pmegaData[j - 1] / 10;//把这一位在正常的计算结果基础上加result[j - 1] / 10

			result.pmegaData[j - 1] = result.pmegaData[j - 1] % 10;//把前一位变成之前的前一位除10取余
		}
		else {//中间的某一位

			result.pmegaData[j] = a*this->pmegaData[j - 1] / 10 + a*this->pmegaData[j] % 10 + result.pmegaData[j - 1] / 10;//把这一位在正常的计算结果基础上加result[j - 1] / 10

			result.pmegaData[j - 1] = result.pmegaData[j - 1] % 10;//把前一位变成之前的前一位取余数
		}
	}

	if (result.pmegaData[length] == 0) {//如果结果的最大位是0(说明多了一位)
		result.length -= 1;
	}

	return result;
}

//拓展并移动数据
megaData megaData::extend(int Len, int n, megaData & a)
{
	megaData result(Len);

	memset(result.pmegaData, 0, sizeof(int)*Len);

	memcpy(result.pmegaData + n, a.pmegaData, sizeof(int)*a.length);

	return result;
}

//打印出数据
void megaData::print()
{
	for (int i = length-1; i >=0; --i)cout << pmegaData[i];
	cout << endl;
}

//打印数据长度
void megaData::printLenth()
{
	cout << length << endl;
}


megaData::~megaData()
{
	delete[] pmegaData;
}

测试main函数:

//author			summer_awn
//date				2017/6/20


#include"megaData.h"

void main() {

	srand((unsigned)time(NULL));

	clock_t start, end;

	start = clock();

	megaData a(10000);
	cout << "a = ";
	//a.print();

	megaData b(3000);
	cout << "b = ";
	//b.print();

	megaData c = a + b;
	cout << "a + b = ";
	//c.print();

	megaData d = a * b;
	cout << "a * b =";
	//d.print();

	end = clock();

	float time = (float)(end - start) / 1000;

	cout << "time is:" << time << "s" << endl;

	system("pause");
}

运行结果:

高精度加法和乘法的c++实现_第2张图片


说实话,我今天是第一次写拷贝构造函数以及运算符的重载,,,,,,

很想知道我这一年都干啥了,,,,,

em。。。本篇文章非教程(因为我啥也没讲,代码也挺乱的),就当是记录我的学习过程了吧,


明天考大物,祈求不挂。。。


你可能感兴趣的:(高精度算法)