myint.h
#include <iostream> // so that we can overload << and >>
using namespace std;
class MyInt
{
// these overload starters are declared as friend functions
friend MyInt operator+(const MyInt& f1, const MyInt& f2);
//friend MyInt operator*(const MyInt& f1, const MyInt& f2);
/*friend MyInt operator++(MyInt& f1, int); // postfix ++
friend MyInt operator++(MyInt& f1); // ++ prefix
friend MyInt operator--(MyInt& f1); // -- prefix
friend MyInt operator--(MyInt& f1, int); // postfix --*/
friend bool operator<(const MyInt& f1, const MyInt& f2);
friend bool operator>(const MyInt& f1, const MyInt& f2);
friend bool operator>=(const MyInt& f1, const MyInt& f2);
friend bool operator<=(const MyInt& f1, const MyInt& f2);
friend bool operator==(const MyInt& f1, const MyInt& f2);
friend bool operator!=(const MyInt& f1, const MyInt& f2);
friend istream& operator>>(istream& in, MyInt& f);
friend ostream& operator<<(ostream& out, MyInt& f);
// declare overloads for input and output (MUST be non-member functions)
// you may make them friends of the class
public:
MyInt(int n = 0); // first constructor
MyInt(char *num); // second constructor
~ MyInt(); //destructor
MyInt(const MyInt &); // copy constructor
MyInt& operator= (const MyInt &); // assignment operator
// be sure to add in the second constructor, and the user-defined
// versions of destructor, copy constructor, and assignment operator
private:
int maxSize, sizeNow;
char *NumberList;
int FindCharSize (char *);
void Grow();
void Reverse (char list[], const int SIZE);
// member data (suggested: use a dynamic array to store the digits)
};
myint.cpp
#include <iostream>
#include <cstring>
#include <cctype>
#include "myintprac.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
}
istream& operator >> (istream& in, MyInt& f)
{
char *TempSpace;
for(int i = 0; i < f.maxSize; i++)
{
f.NumberList[i] = in.peek();
if (f.NumberList[i] = '\0')
in.get();
}
/*char num;
int count = 0;
while ('0' <= num <= '9')
{
in >> num;
m.integer[count] = num;
count++;
return in;
}
m.integer2 = new char[strlen(num)+1];
strcpy(m.integer2,num);
bool result;
int count = 0;
while ( result != false)
{
char get(m.integer2[count];
if ('0' >= m.integer[count] >= '9')
result = false;
else
m.integer[count] = m.integer2[count];
count++;
}*/
return in;
}
ostream& operator<<(ostream& out, MyInt& f)
{
for(int i = 0; i < f.maxSize; i++)
out << f.NumberList[i];
return out;
}
MyInt::MyInt(int n)
{
int newNumber;
maxSize = 5;
sizeNow = 0;
NumberList = new char[maxSize];
int numb2 = n; // temporary variable
do // Loop will calculate the legnth of n
{
newNumber = numb2 % 10;
numb2 = numb2 / 10;
sizeNow++;
}
while (numb2 != 0);
for (int i = 0; i < maxSize; i++)
// places each digit into a slot in the array
{
if (sizeNow == maxSize) // If the integer is full, grow it.
Grow();
NumberList[i] = I2C(n % 10);
n = n / 10;
}
Reverse (NumberList, maxSize);
}
MyInt::MyInt(char *num)
{
num = NumberList;
NumberList = new char[maxSize];
sizeNow = FindCharSize(num);
if (sizeNow == maxSize) // If the directory is full, grow it.
Grow();
}
MyInt::~MyInt()
{
delete [] NumberList;
}
MyInt::MyInt(const MyInt & I)
// copy constructor. Initialize obzect as a copy of I
{
maxSize = I.maxSize;
sizeNow = I.sizeNow;
// allocate new array of Entry objects
NumberList = new char[maxSize];
// copy the list over from I
for (int i = 0; i < sizeNow; i++)
NumberList[i] = I.NumberList[i];
}
MyInt& MyInt::operator =(const MyInt & I)
{
if (this != &I) // only make the copy if the original is
{ // not this same object
// first, delete the existing array
delete [] NumberList;
// now do the copy. Same way we did copy constructor
maxSize = I.maxSize;
sizeNow = I.sizeNow;
NumberList = new char[maxSize];
for (int i = 0; i < sizeNow; i++)
NumberList[i] = I.NumberList[i];
}
return *this; // return this object
}
int MyInt:: FindCharSize (char *num)
{
int count = 0;
for (int i = 0; num[i] != '\0'; i++)
{
count++;
}
return count;
}
void MyInt::Grow()
{
maxSize = sizeNow + 5; // Determine a new size.
char *newSize = new char[maxSize]; // Allocate a new array.
for (int i = 0; i < sizeNow; i++) // Copy each book into
newSize[i] = NumberList[i]; // the new array.
delete [] NumberList; // Remove the old array
NumberList = newSize; // Point old name to new array.
}
void MyInt::Reverse (char list[], const int SIZE)
{
int list2 [15], j, i;
for (i = SIZE - 1, j = 0; (i >= 0) && (j < SIZE); --i, ++j)
list2[i] = list[j];
for (j = 0; j < SIZE; ++j)
list[j] = list2[j];
}
MyInt operator+(const MyInt& f1, const MyInt& f2)
{
MyInt z;
int carry = 0;
for (int i = 0; i < z.maxSize; i++)
{
z.NumberList[i] = f1.NumberList[i] + f2.NumberList[i] + carry;
if(z.NumberList[i] > 9)
{
carry = z.NumberList[i] / 10;
z.NumberList[i] %= 10;
return z;
}
}
}
bool operator< (const MyInt& f1, const MyInt& f2)
{
if(f1.maxSize < f2.maxSize)
return true;
else if(f1.maxSize < f2.maxSize)
return false;
else
{
for(int i = 0; i < f1.maxSize; i++)
{
if(f1.maxSize[i] < f2.maxSize[i])
return true;
else if(f1.maxSize[i] < f2.maxSize[i])
return false;
}
}
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& f1, const MyInt& f2)
{
if (f1.maxSize > f2.maxSize)
return false;
else if (f1.maxSize > f2.maxSize)
return true;
else
{
for (int i = 0; i < f1.maxSize; i++)
{
if(f1.maxSize[i] > f2.maxSize[i])
return true;
else if(f1.maxSize[i] > f2.maxSize[i])
return false;
}
}
return true;
}
bool operator>=(const MyInt& f1, const MyInt& f2)
{
if(f1.maxSize >= f2.maxSize)
return true;
return false;
}
bool operator<=(const MyInt& f1, const MyInt& f2)
{
if(f1.maxSize <= f2.maxSize)
return true;
return false;
}
bool operator!=(const MyInt& f1, const MyInt& f2)
{
if(f1.maxSize != f2.maxSize)
return true;
return false;
}
bool operator==(const MyInt& f1, const MyInt& f2)
{
if(f1.maxSize == f2.maxSize)
return true;
else
return false;
}
I need help writing the overloading insertion operator, if I cant understand that then it will be difficult to do the rest. Please if there is any help you can do, please help
Thus far I keep getting these errors:
1>c:\users\gina\documents\visual studio 2008\projects\myintprac\myintprac\myintprac.cpp(23) : warning C4101: 'TempSpace' : unreferenced local variable
1>c:\users\gina\documents\visual studio 2008\projects\myintprac\myintprac\myintprac.cpp(198) : error C2109: subscript requires array or pointer type
1>c:\users\gina\documents\visual studio 2008\projects\myintprac\myintprac\myintprac.cpp(198) : error C2109: subscript requires array or pointer type
1>c:\users\gina\documents\visual studio 2008\projects\myintprac\myintprac\myintprac.cpp(200) : error C2109: subscript requires array or pointer type
1>c:\users\gina\documents\visual studio 2008\projects\myintprac\myintprac\myintprac.cpp(200) : error C2109: subscript requires array or pointer type
1>c:\users\gina\documents\visual studio 2008\projects\myintprac\myintprac\myintprac.cpp(219) : error C2109: subscript requires array or pointer type
1>c:\users\gina\documents\visual studio 2008\projects\myintprac\myintprac\myintprac.cpp(219) : error C2109: subscript requires array or pointer type
1>c:\users\gina\documents\visual studio 2008\projects\myintprac\myintprac\myintprac.cpp(221) : error C2109: subscript requires array or pointer type
1>c:\users\gina\documents\visual studio 2008\projects\myintprac\myintprac\myintprac.cpp(221) : error C2109: subscript requires array or pointer type
1>Build log was saved at "file://c:\Users\Gina\Documents\Visual Studio 2008\Projects\myintprac\myintprac\Debug\BuildLog.htm"
1>myintprac - 8 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========