Big integer summation C ++ implementation for ordered linear tables, linear summation
Sum of big integers in a sequential linear table
Big integer summation pseudocode
1. initialize the carry flag = 0;
2. Length of integers A and B:
Int aLength = a. GetLength ();
Int bLength = B. GetLength ();
3. Start to add the I-bit by bit until the calculation of A or B is completed:
3.1. Calculate the value of the I-bit: c. insert (I + 1, (. getElement (I + 1) + B. getElement (I + 1) + flag) % 10 );
3.2 calculate the carry of this bit: flag = (a. GetElement (I + 1) + B. GetElement (I + 1) + flag)/10;
4. Calculate the remaining part of A or B;
5. Carry of calculation results
Note:Use arrays to save and enlarge Integers
I. Sequence linear table header file: SeqList. h
// Header file of the ordered linear table
# Include <iostream>
Const int MaxSize = 100;
// Define the template class of the sequence table SeqList
Template <class DataType>
Class SeqList {
Public:
// Sequence table without parameter Constructor (create an empty sequence table)
SeqList () {length = 0 ;}
// Ordered table has a parameter Constructor (create an ordered table with a length of n)
SeqList (DataType array [], int n );
// Sequence table destructor
~ SeqList (){}
// Calculate the length of the sequence table
Int GetLength () {return length ;}
// The sequence table searches by bit and returns the elements at the I position.
DataType GetElement (int I );
// The sequence table is queried by value and returns the position of the element.
Int GetLocal (DataType x );
// Insert the specified element at the specified position in the sequence table
Void Insert (int I, DataType x );
// Delete an element from an ordered table. The deleted element is returned.
DataType Delete (int I );
// Output the elements in the sequence table
Void PrintSeqList ();
Private:
// One-dimensional array for storing data elements
DataType data [MaxSize];
// Sequence table length
Int length;
};
// Implement the sequence table with a parameter Constructor
Template <class DataType>
SeqList <DataType >:: SeqList (DataType array [], int n)
{
If (n> MaxSize)
{
Throw "the length of the input sequence table is too long ";
}
// Assign values to the array of elements stored in the sequence table
For (int I = 0; I <n; I ++)
{
Data [I] = array [I];
}
// Assign a value to the length of the sequence table
Length = n;
}
// Implement sequential table searching by bit
Template <class DataType>
DataType SeqList <DataType>: GetElement (int I)
{
// Determine whether the location is correct
If (I <1 | I> length)
{
Throw "incorrect position ";
}
Else
{
// Returns the element at the specified position.
Return data [I-1];
}
}
// Implement value-based search for the ordered table and return the position of the element
Template <class DataType>
Int SeqList <DataType >:: GetLocal (DataType x)
{
// Traverse the elements of the sequence table
For (int I = 0; I <length; I ++)
{
// Determine whether the specified element is in the sequence table
If (data [I] = x)
{
// Returns the position of the specified Element in the sequence table.
Return (I + 1 );
}
}
// If the specified element is not in the sequence table, the return position is 0.
Return 0;
}
// Insert elements into the sequence table
Template <class DataType>
Void SeqList <DataType>: Insert (int index, DataType x)
{
// Determine whether the insert position is reasonable
If (length> = MaxSize)
{
Throw "the sequence table has been fully stored ";
}
If (index <1 | index> length + 1)
{
Throw "An error occurred while inserting the element ";
}
// If the insert position is reasonable, the elements in the sequence table are moved backward from the last position to the specified insert position.
For (int j = length; j> = index; j --)
{
Data [j] = data [j-1];
}
// Insert the specified element to the inserted position
Data [index-1] = x;
Length ++;
}
// Deletes the elements at the specified position in the sequence table.
Template <class DataType>
DataType SeqList <DataType>: Delete (int index)
{
// Declare the elements to be retrieved
DataType x;
// Determine whether the location to be deleted is reasonable
If (index <1 | index> length)
{
Throw "The deletion location is incorrect ";
}
Else
{
// Retrieve the elements at the specified position
X = data [index-1];
// Move all elements at the specified position forward to a position
For (int I = index; I <length; I ++)
{
Data [I-1] = data [I];
}
// After the elements in the sequence table are deleted, the length of the elements is reduced by 1.
Length --;
}
Return x;
}
// Elements in the sequence output table
Template <class DataType>
Void SeqList <DataType>: PrintSeqList ()
{
If (length <1)
{
Throw "no element in the sequence table ";
}
Else
{
// Ordered output sequence table elements
For (int I = 0; I <length; I ++)
{
Cout <data [I] <"";
}
Cout <endl;
}
}
Ii. header file for big integer summation: BidIntegerAdd. h
// Sum of big integers in a sequential linear table
# Include <iostream>
// Introduce the header file of the ordered linear table
# Include "SeqList. h"
Using namespace std;
SeqList <int> Add (SeqList <int> a, SeqList <int> B)
{
// Define intermediate variables and sequence linear tables
SeqList <int> c = SeqList <int> ();
// Flag is the carry sign. I is a certain digit of a large integer.
Int flag = 0, I = 0;
// Calculate the number of digits of a and B.
Int aLength = a. GetLength ();
Int bLength = B. GetLength ();
// Addition by bit until a large integer is computed
While (I <aLength & I <bLength)
{
// Calculate the I-th Value
C. Insert (I + 1, (a. GetElement (I + 1) + B. GetElement (I + 1) + flag) % 10 );
// Calculate the carry of the I-bit
Flag = (a. GetElement (I + 1) + B. GetElement (I + 1) + flag)/10;
I ++;
}
// Calculate the remaining part of big integer
For (; I <aLength; I ++)
{
C. Insert (I + 1, (a. GetElement (I + 1) + flag) % 10 );
Flag = (a. GetElement (I + 1) + flag)/10;
}
// Calculate the remaining part of big integer B
For (; I <bLength; I ++)
{
C. Insert (I + 1, (B. GetElement (I + 1) + flag) % 10 );
Flag = (B. GetElement (I + 1) + flag)/10;
}
// If there is an increment at the end, one more result will be returned.
If (flag = 1)
{
C. Insert (c. GetLength () + 1, 1 );
}
Return c;
}
Iii. Test the sum of big integers in the ordered linear table: TestBigIntegerAdd. h
// Test the sum of big integers in the ordered linear table
# Include <iostream>
// Import the big integer sum header file of the sequence table
# Include "BigIntegerAdd. h"
Using namespace std;
Int main ()
{
// Define two ordered linear tables
Int array1 [] = {1, 2, 3, 4, 5, 6, 7, 8, 9 };
Int array2 [] = {1, 1, 1, 1, 1, 1, 1 };
SeqList <int> a = SeqList <int> (array1, 9 );
Cout <"the first big integer is:" <endl;
A. PrintSeqList ();
SeqList <int> B = SeqList <int> (array2, 8 );
Cout <"the second big integer is:" <endl;
B. PrintSeqList ();
SeqList <int> c = Add (a, B );
Cout <"their sum is:" <endl;
C. PrintSeqList ();
Return 0;
}