overloading insertion operator

R

ryan.fairchild

I have a problem I am trying to create a MyInt class to hanlde very
large ints. Its for a class, therefore I can only do what the teach
tells me. I want to be able to overload the insertion operator so that
I can read in one digit at a time from the buffer and if the digit is 0
- 9 then put it into the dynamic array which stores each digit of this
large int.

#include <iostream>
#include <string>
#include "myint.h"

using namespace std;

int C2I(char c)
// converts character into integer (returns -1 for error)
{
if (c < '0' || c > '9') return -1; // error
return (c - '0'); // success
}

char I2C(int x)
// converts single digit integer into character (returns '\0' for
error)
{
if (x < 0 || x > 9) return '\0'; // error
return (static_cast<char>(x) + '0'); // success
}

MyInt::MyInt(int n)
// default constructor for the MyInt class which takes an int as its
param
{
int i;
int temp = n; //temp var used because n should not be changed yet
int div;

curSize = 0;

while(temp/=10) curSize++; //calculate the length of n
curSize++;
maxSize = curSize + 5;
bigInt = new int[maxSize];

for(i = 0; i < maxSize; i++)
//initialize the array to all 0's
bigInt = -1;

if(n < 0)
Reset();
else
{
for(i = curSize - 1; i >= 0; i--)
//places each digit into a slot in the array
{
if(curSize >= maxSize)
Resize();

div = 10;
bigInt = (n % div);
n/=10;
}
}
}

MyInt::MyInt(char* s)
// constructor for the MyInt class which takes a literal
{
int i;

curSize = 0;
maxSize = curSize + 5;
bigInt = new int[maxSize];

for(i = 0; i < maxSize; i++)
//initialize the array to all 0's
bigInt = -1;

if(s == NULL)
Reset();
else
{
for(i = 0; i < strlen(s); i++)
//converts each char to a digit then palces it into the array
{
if(curSize >= maxSize)
Resize();

if(C2I(s) != -1)
{
bigInt = C2I(s);
curSize++;
}
else
{
Reset();
break;
}
}
}
}

MyInt::~MyInt()
// deltes all dynamically allocated memory to prevent memory leaks
{
delete [] bigInt;
}

MyInt::MyInt(const MyInt & m)
// copy constructor used to copy all member data
{
curSize = m.curSize;
maxSize = m.maxSize;

bigInt = new int[m.maxSize];

for(int i = 0; i < curSize; i++)
bigInt = m.bigInt;
}

MyInt& MyInt::eek:perator= (const MyInt & m)
// assignment operator
{
if(this != &m)
{
delete [] bigInt;

curSize = m.curSize;
maxSize = m.maxSize;

bigInt = new int[m.maxSize];

for(int i = 0; i < curSize; i++)
bigInt = m.bigInt;
}

return *this;
}

void MyInt::Reset()
// resets the array to 0
{
curSize = 1;
maxSize = 1;

delete [] bigInt;

bigInt = new int[1];

bigInt[0] = 0;
}

void MyInt::Resize()
{
cout << "resizing arr...";

int * temp = new int[maxSize+=5];

for(int i = 0; i < curSize; i++)
temp = bigInt;

delete [] bigInt;

bigInt = temp;
}

MyInt operator+ (const MyInt& x, const MyInt& y)
{

//not done

return 0;

}

MyInt operator* (const MyInt& x, const MyInt& y)
{
//not done
return 0;
}

bool operator< (const MyInt& x, const MyInt& y)
{
if(x.curSize < y.curSize)
return true;
else if(x.curSize > y.curSize)
return false;
else
{
for(int i = 0; i < x.curSize; i++)
{
if(x.bigInt > y.bigInt)
return false;
else if(x.bigInt < y.bigInt)
return true;
}
}

return false;
//returns false because if it reaches this part then all the numbers
are the same
//therefore x is not less than y they are equal
}
bool operator> (const MyInt& x, const MyInt& y)
{
if(x < y)
return false;
else if(x.curSize > y.curSize)
return true;
else
{
for(int i = 0; i < x.curSize; i++)
{
if(x.bigInt > y.bigInt)
return true;
else if(x.bigInt < y.bigInt)
return false;
}
}

return true;
}

bool operator>= (const MyInt& x, const MyInt& y)
{
if(x > y || x == y)
return true;

return false;
}
bool operator<= (const MyInt& x, const MyInt& y)
{
if(x < y || x ==y)
return true;

return false;
}
bool operator!= (const MyInt& x, const MyInt& y)
{
if(x == y)
return false;

return true;
}
bool operator== (const MyInt& x, const MyInt& y)
{
if(x > y)
return false;
else if(x < y)
return false;
else
return true;
}

istream& operator >> (istream &s, MyInt& y)
{
int c;



s.ignore();

return s;

}

ostream& operator << (ostream &s, const MyInt& y){
for(int i = 0; i < y.curSize; i++)
s << y.bigInt;

return s;
}
 
K

Karl Heinz Buchegger

I have a problem I am trying to create a MyInt class to hanlde very
large ints. Its for a class, therefore I can only do what the teach
tells me. I want to be able to overload the insertion operator so that
I can read in one digit at a time from the buffer and if the digit is 0
- 9 then put it into the dynamic array which stores each digit of this
large int.

OK. And what is your particular problem?
Your strategy sounds good to me:
read from the stream as long as the next character equals
a digit (you can use function isdigit() to figure that one out)
collect those characters in a string.
When the next read character is not a digit any more, create
a MyInt object from that and assign it to the passed MyInt
object.

Seems like you have all parts available to do that job. With some
additional functions inside the MyInt object you could simplify
the task, but it is definitly doable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top