Ok, heres what I got so far. The problem I have is my loops for adding data are not adding what I'm expecting... Each node is like "00343068", etc. when it's suppose to be like 123
Also if anyone has any ideas on the sum and increment portion, it would be greatfull.
Header
class number
{
public:
number(); // constructor
~number(); // deconstructor
// member functions
void insert(int newValue);
void Increment();
void Copy(const number& Num);
void Sum(int Num);
void DisplayNumber();
bool isEmpty() const;
int getLength() const;
private:
struct digit
{
int Value; // a digit value, 0-9
struct digit* next;
};
typedef digit* ptrType;
int size;
digit *head;
digit *find(int index) const;
};
Class Declaration
#include <iostream>
#include <cstddef>
#include <new>
#include "number.h"
using namespace std;
number::number() : size(0), head(NULL)
{
}
number::~number()
{
while (!isEmpty())
remove(NULL);
}
bool number::isEmpty() const
{
return size == 0;
}
int number::getLength() const
{
return size;
}
void number::DisplayNumber()
{
digit *temp;
temp = head;
if (isEmpty() == 1){}
else
{
while (temp!=NULL)
{
cout << temp << endl;
temp = temp->next;
}
}
}
void number::insert(int newValue)
{
int newLength = getLength() + 1;
digit *newPtr = new digit;
if (newPtr==NULL)
{
cout << "Not enough memory!" << endl;
}
else
{
size = newLength;
newPtr->Value = newValue;
newPtr->next = head;
head = newPtr;
}
}
void number::Increment() // Increase a # by one. adding 1 to 1999, for example, requires changing all four digits to get 2000, and adding 1 to 9 requires making a new digit to get 10.
{
}
void number::Copy(const number& Num) // Copy one number to another
{
head = new digit;
head->Value = Num.head->Value;
digit *newPtr = head;
for (digit *origPtr = Num.head->next;
origPtr != NULL;
origPtr = origPtr->next)
{
newPtr = newPtr->next;
newPtr->Value = origPtr->Value;
}
newPtr->next = NULL;
}
void number::Sum(int Num) // A method that takes two numbers and sums them. Sample usage: N.Sum(M); (adds M to N). Don't forget about a carry when the sum of two digits is greater than 10.
{
}
and Main
#include <iostream>
#include <string>
#include <cmath>
#include "number.h"
using namespace std;
int main()
{
// Number M
number M;
for(int i=0; i<3; i++)
M.insert(i);
// Number N
number N;
for(int i=0; i<3; i++)
N.insert(i);
// End Number Creation
M.DisplayNumber();
N.DisplayNumber();
M.Copy(N);
M.DisplayNumber();
N.DisplayNumber();
return 0;
}