473,396 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

overloading insertion operator

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[i] = -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[i] = (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[i] = -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[i]) != -1)
{
bigInt[i] = C2I(s[i]);
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[i] = m.bigInt[i];
}

MyInt& MyInt::operator= (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[i] = m.bigInt[i];
}

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[i] = bigInt[i];

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[i] > y.bigInt[i])
return false;
else if(x.bigInt[i] < y.bigInt[i])
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[i] > y.bigInt[i])
return true;
else if(x.bigInt[i] < y.bigInt[i])
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[i];

return s;
}

Jul 23 '05 #1
2 3804
"ry************@gmail.com" wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #2
i finally got it.

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ceo | last post by:
Hi there, I'm trying to overload insertion (<<) and extraction (>>) operators and the program yields output I didn't expect. Could someone please clarify what's wrong with my program. Thanks,...
8
by: jois.de.vivre | last post by:
Hi, I'm having some trouble overloading the << operator. I have the following, very simple code: #include <iostream> using namespace std; class test { private: int val;
20
by: Patrick Guio | last post by:
Dear all, I have some problem with insertion operator together with namespace. I have a header file foo.h containing declaration of classes, typedefs and insertion operators for the typedefs in...
1
by: acheron05 | last post by:
Hi, I've been writing a program for another school assignment but I am having trouble working out how to overload the < and << operators. The program is designed to read data from a file, create...
5
by: raan | last post by:
What I am trying to achieve here is depicted in the small program below. // Wrapit.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <map> #include...
16
by: EM.Bateman | last post by:
Working on Visual Studio .Net I've implemented a class: #ifndef CONTRIBUTOR_H #define CONTRIBUTOR_H enum Gender {male=1, female, unk}; #include <iostream> #include <iomanip> #include...
3
by: kvnsmnsn | last post by:
I've been asked to overload the insertion operator. What exactly is the insertion operator in C++, and how would one overload it? I think that in C I could overload the "+" operator like so: ...
2
by: davoxol | last post by:
Hi, I have a problem using the insertion overloading. I defined it this way: friend ostream& operator<< (ostream& os, Base & base); I have to get specific output depending on the type of...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.