利用顺序表实现大整数的加法和乘法
类似竞赛的高精度加法算法
第一份代码比较简洁,第二份代码套用了课本模板
#include <iostream>
using namespace std;
class Array{
private:
static const int Size = 100000;
int len;
int arr[Size+5];//实际上可以用short甚至char类型减小空间复杂度
public:
Array(){ len = 0;}
Array(int n, int a[]) {
if(n > Size) throw "Too big!";
len = n;
for(int i=0; i<n; ++i)
arr[i] = a[i];
}
~Array(){}
void plus(int n, int b[]) {
/*
cout << "Testing plus" << endl;
for(int i=n-1; i>=0; --i)
cout << b[i];
cout << endl;
*/
if(n > Size) throw "Too big!";
if(n > len) len = n;
for(int j, i=0; i<len; ++i) {
j = min(i, n);
arr[i] += b[j];
if(arr[i] > 9) {
arr[i] -= 10;
++arr[i+1];
}
}
if(arr[len]>0) ++len;
/*
cout << "Testing plus" << endl;
for(int i=len-1; i>=0; --i)
cout << arr[i];
cout << endl;
*/
return ;
}
void times(int n, int b[]) {
int m = len*n*10;//防止运算结果太大爆数组
if(m >= Size) throw "Too big!";
int *c = new int [m];//预制一个大数组存结果
for(int i=0; i<m; ++i)
c[i] = 0;
for(int i=0; i<n; ++i)
for(int j=0; j<len; ++j) {
c[i+j] += b[i] * arr[j];
if(c[i+j] > 9) {
c[i+j+1] += c[i+j]/10;
c[i+j] %= 10;
}
}
while(c[m-1] == 0) --m;//防止做的太大
while(c[m]>0) {
if(c[m] > 9) {
c[m+1] += c[m]/10;
c[m] %= 10;
}
++m;
}
len = m;
for(int i=0; i<m; ++i)
arr[i] = c[i];
delete []c;
return ;
}
void print() {
cout << "Testing:" << endl;
cout << "len: " << len << endl;
for(int i=len-1; i>=0; --i)
cout << arr[i];
cout << endl;
return ;
}
};
int main(void) {
ios::sync_with_stdio(false);
int n, m;
int a[100] = {0}, b[100] = {0};
cin >> n;
for(int i=n-1; i>=0; --i)
cin >> a[i];
Array test1(n, a), test2(n, a);
//大整数加法
cin >> m;
for(int i=m-1; i>=0; --i)
cin >> b[i];
test1.plus(m, b);
test1.print();
//大整数乘法
cin >> m;
for(int i=m-1; i>=0; --i)
cin >> b[i];
test2.times(m, b);
test2.print();
return 0;
}
#include <iostream>
#define MaxSize 1000
using namespace std;
template <typename T>
class SeqList
{
public:
SeqList();
SeqList(T a[], int n);
~SeqList();
void PrintList();
int GetLength();
void GetData(T b[]);
void Add(T b[], int n);
void Mult(T b[], int n);
private:
T data[MaxSize];
int length;
};
template <typename T>
SeqList<T>::SeqList()
{
length = 0;
}
template <typename T>
SeqList<T>::SeqList(T a[], int n)
{
if(n>MaxSize)
throw "Parameters of illegal.";
for(int i=0; i<n; i++)
data[i] = a[i];
length = n;
}
template <typename T>
SeqList<T>::~SeqList()
{
//empty
}
template <typename T>
void SeqList<T>::PrintList()
{
cout << "This list is " << length << " digit." << endl;
cout << "List element:";
for(int i=length-1; i>=0; i--)
{
cout<<data[i]<<" ";
}
cout<<endl;
}
template <typename T>
int SeqList<T>::GetLength()
{
return length;
}
template <typename T>
void SeqList<T>::GetData(T b[])
{
for(int i=0; i<length; i++)
b[i]=data[i];
}
/*
template <typename DataType>
void SeqList<DataType>::Add(SeqList<DataType> A, SeqList<DataType> B)
{
DataType a3[MaxSize], a4[MaxSize];
A.GetData(a3), B.GetData(a4);
int length3 = A.GetLength(), length4 = B.GetLength();
int Max = max(length3, length4);
if(Max > length3)
{
int temp = Max - length3;
for(int i = 0; i < Max; i++)
{
a3[i+temp] = a3[i];
}
for(int i = 0; i < temp; i++)
{
a3[i] = 0;
}
}
if(Max > length4)
{
int temp = Max - length4;
for(int i = 0; i < Max; i++)
{
a4[i+temp] = a4[i];
}
for(int i = 0; i < temp; i++)
{
a4[i] = 0;
}
}
for(int i = 0; i < Max; i++)
{
data[i] = a3[i] + a4[i];
if(data[0] > 10)
{
length = ++Max;
}
if(data[i] > 9)
{
++data[i - 1];
data[i] -= 10;
}
}
}
template <typename DataType>
void SeqList<DataType>::Mult(SeqList<DataType> A, SeqList<DataType> B)
{
}
*/
template <typename T>
void SeqList<T>::Add(T b[], int n)
{
if(n > MaxSize) throw "Too big!";
if(n > length) length = n;
for(int j, i=0; i<length; ++i) {
j = min(i, n);
data[i] += b[j];
if(data[i] > 9) {
data[i] -= 10;
++data[i+1];
}
}
if(data[length]>0) ++length;
cout << "Add A and B:" << endl;
cout << "This list is " << length << " digit." << endl;
cout << "List element:";
for(int i=length-1; i>=0; i--)
cout << data[i] << " ";
cout << endl;
}
template <typename T>
void SeqList<T>::Mult(T b[], int n)
{
cout << "Testing" << endl;
int m = n*length*10;
if(m >= MaxSize) throw "Too big!";
T *c = new T [m];
for(int i=0; i<m; i++)
c[i]=0;
for(int i=0; i<n; ++i)
for(int j=0; j<length ; j++)
{
c[i+j] += data[j] * b[i];
if(c[i+j]>9)
{
c[i+j+1] += c[i+j]/10;
c[i+j] %= 10;
}
}
while(c[m-1] == 0) --m;//防止做的太大
while(c[m]>0) {
if(c[m]>9) {
c[m+1] += c[m]/10;
c[m] %= 10;
}
++m;
}
length = m;
for(int i=0; i<m; ++i)
data[i] = c[i];
delete []c;
cout << "Mult A and B:" << endl;
cout << "This list is " << length << " digit." << endl;
cout << "List element:";
for(int i=m-1; i>=0; i--)
cout << data[i] << " ";
cout << endl;
}
int main()
{
int n1,n2,a1[MaxSize],a2[MaxSize];
cout << "Please input length of list A:";
cin >> n1;
cout << "Please input element of list A:";
for(int i=n1-1; i>=0; i--)
cin >> a1[i];
SeqList<int> A(a1,n1);
cout << "Please input length of list B:";
cin >> n2;
cout << "Please input element of list B:";
for(int i=n2-1; i>=0 ; i--)
cin >> a2[i];
SeqList<int> B(a2,n2);
A.PrintList();
B.PrintList();
A.Add(a2, n2);
B.Mult(a1, n1);
/*
SeqList<int> C,D;
cout << "Add A and B:" << endl;
C.Add(A,B);
C.PrintList();
cout << "Mult A and B:" << endl;
D.Mult(A,B);
D.PrintList();
*/
return 0;
}