In this project you are to build and exercise a class called luint (ie long unsigned int). Luint should have the ability of storing very long unsigned numbers (any size up to the size of memory) such as 12435436546543234497840905438695045967888993331000. It should also have the ability to initialize them, add these numbers together and print them out using the overloaded stream operator<<. I have the required luint.h file given below. Note that it is using a vector< unsigned int> to hold these long numbers in reverse order. In other-words, if vector< unsigned int> A is stored in the luint then A[0] is the least significant digit. Your program should be able to work with a list or vector. You must use common access techniques such as iterators and methods like push_back() etc so they both will work properly when swapped.
#pragma once
#include < vector>
#include < list>
#include < string>
#include < iomanip>
using namespace std;
class luint
{
// When you get this working change vector to list
// and see if it still works
vector< unsigned int> num;
// here num[0] is the least significant digit
// You should be able to change dpn to be any value from 1 thru 9
unsigned int dpn=2;// node size Number of digits/node
public:
luint();
~luint();
// This is a parameterized constructor
luint(unsigned int v);
// The following overloaded + adds two luint's
friend luint operator+(const luint&, const luint&);
// This is the overloaded * operator(extra credit)
// extra credit will be given only if everything else works.
friend luint operator*(const luint&, const luint&);
// This is the overloaded << output operator
friend ostream & operator<<(ostream & os, const luint & n) {
// must print it backwards using reverse iterators
}
};
In order to be able to change the vector to list in the above project you will need to follow a few procedures. First you are not allowed to subscript the vector since the class list is not also subscriptable. The only way you can access and traverse the vector or list is using either a forward iterator or a reverse iterator. Here are some simple examples for your review.
vector< int> A{1,5,3,4,7,6};// inits the vector.
vector< int> B;
for(int i=0;i<10;i++)
B.push_back(i); //inits this vector using push_back
// print out A forward
for(vector< int>::iterator it=A.begin();it!=A.end();it++)
cout<<*it << endl;
// print out B backward
for(vector< int>::iterator it=B.rbegin();it!=B.rend();it++)
cout<<*it << endl;
Main Program to use:
#include < iostream>
#include < vector>
#include "luint.h"
using namespace std;
int main(){
int a, b;
while (cin >> a >> b) {
luint p(2);
luint x(a);
luint y(b);
luint z = x + y;// NOTE: this was a+b. I fixed it.
cout << x << " + " << y << " = " << z << endl;
// calculates 2^a
for (int i = 1; i < a; i++)
p = p + p;
cout << "2^" << a << " is " << p << endl;
}
return 0;
}