2. You have been given the header file and implementation file for very_long_int
ID: 3628215 • Letter: 2
Question
2. You have been given the header file and implementation file for very_long_int. You have also been given an application file (main driver file). You are to do the following:Add the implementation for the overloaded < operator to the implementation file.
Add the implementation for the overloaded == operator to the implementation file.
Execute the code and turn in the output.
// file verylong.h
#ifndef VERYLONG
#define VERYLONG
#include <iostream>
#include <vector>
using namespace std;
class very_long_int
{
// Postcondition: The value for very_long has been read in from stream.
friend istream& operator>> (istream& stream, very_long_int& very_long);
// Postcondition: The value of very_long has been output to stream.
friend ostream& operator<< (ostream& stream, very_long_int& very_long);
protected:
vector<char> digits;
// Postcondition: If i >= digits.size(), 0 has been returned; else
// the ith least significant digit in digits has
// been returned. The least significant digit is
// the 0th least significant digit.
char least (unsigned i) const;
public:
// Postcondition: The very_long_int has been initialized to an
// empty very_long_int.
very_long_int();
// Postcondition: The value of the very_long_int is the
// unsigned int n.
very_long_int (unsigned n);
// Postcondition: The value of the very_long_int is the
// unsigned int n.
void initialize (unsigned n);
// Postcondition: The very_long_int returned is the sum of the
// calling very_long_int and other_very_long.
very_long_int operator+ (const very_long_int &other_very_long);
// Postcondition: true has been returned if the
// value of the calling object is less than the value
// of other_very_long. Otherwise, false
// has been returned.
bool operator< (const very_long_int& other_very_long) const;
// Postcondition: true has been returned if the value of the
// calling object is greater than the value
// of other_very_long. Otherwise, false
// has been returned.
bool operator> (const very_long_int& other_very_long) const;
// Postcondition: true has been returned if the value of the
// calling object is equal to the value
// of other_very_long. Otherwise, false
// has been returned.
bool operator== (const very_long_int& other_very_long) const;
}; // very_long_int
#endif
#include "verylong.h" // declares very_long_int class
#include <algorithm> // declares reverse
using namespace std;
very_long_int::very_long_int()
{
} // default constructor
void very_long_int::initialize (unsigned n)
{
const short BASE = 10;
digits.erase (digits.begin(), digits.end());
do
{
digits.push_back (char (n % BASE));
n = n / BASE;
}
while (n > 0);
// digits is now in reverse order, so we reverse:
reverse (digits.begin(), digits.end());
} // initialize
very_long_int::very_long_int (unsigned n)
{
initialize (n);
} // constructor
istream& operator>> (istream& stream, very_long_int& very_long)
{
const char LOWEST_DIGIT_CHAR = '0';
const char HIGHEST_DIGIT_CHAR = '9';
const char SENTINEL = 'X';
char digit_char;
very_long.digits.erase (very_long.digits.begin(), very_long.digits.end());
do
{
stream >> digit_char;
if ((LOWEST_DIGIT_CHAR <= digit_char) &&
(digit_char <= HIGHEST_DIGIT_CHAR))
very_long.digits.push_back (char (digit_char - LOWEST_DIGIT_CHAR));
} // while
while (digit_char != SENTINEL);
return stream;
} // overloading >>
ostream& operator<< (ostream& stream, very_long_int& very_long)
{
for (int i = 0; i < very_long.digits.size(); i++)
stream << (int)very_long.digits [i];
return stream;
} // overloading <<
char very_long_int::least (unsigned i) const
{
if (i >= digits.size())
return 0;
else
return digits [digits.size() - i - 1] ;
} // least
very_long_int very_long_int::operator+ (const very_long_int& other_very_long)
{
int carry = 0,
larger_size,
i,
partial_sum;
very_long_int sum;
if (digits.size() > other_very_long.digits.size())
larger_size = digits.size();
else
larger_size = other_very_long.digits.size();
for (i = 0; i < larger_size; i++)
{
partial_sum = least (i) + other_very_long.least (i) + carry;
carry = partial_sum / 10;
sum.digits.push_back (char (partial_sum % 10));
} // for
if (carry == 1)
sum.digits.push_back (char (carry));
reverse (sum.digits.begin(), sum.digits.end());
return sum;
} // overloading +
// Postcondition: true has been returned if the
// value of the calling object is less than the value
// of other_very_long. Otherwise, false
// has been returned.
bool very_long_int::operator< (const very_long_int& other_very_long) const
{
return true;
}
// Postcondition: true has been returned if the value of the
// calling object is greater than the value
// of other_very_long. Otherwise, false
// has been returned.
bool very_long_int::operator> (const very_long_int& other_very_long) const
{
return true;
}
// Postcondition: true has been returned if the value of the
// calling object is equal to the value
// of other_very_long. Otherwise, false
// has been returned.
bool very_long_int::operator== (const very_long_int& other_very_long) const
{
return true;
}
#include <iostream>
#include <fstream>
#include <string>
#include <limits>
#include "msg.h"
#include "veryLong.h"
using namespace std;
// Postcondition: Returns string equilavent of method number
string printMethod (const int& method);
void printMenu();
int main()
{
typedef int OpCode; // Make OpCode another name for int
typedef string T; // Make T another name for string
ifstream inFile;
ofstream outFile;
very_long_int very_long;
//new comment
const string INFILE_PROMPT =
"Please enter the path for the input file: ";
const string OUTFILE_PROMPT =
"Please enter the path for the output file: ";
const string IN_FAILURE =
"*** Error opening input file: ";
const string CLOSE_WINDOW_PROMPT =
"Please press the Enter key to close this output window.";
const string METHOD_MESSAGE = "The method is ";
const string PRINT_HEADING = "The current very_long_int is: ";
const int INITIALIZE = 1;
const int READ = 2;
const int PRINT = 3;
const int ADD = 4;
const int MULTIPLY = 5;
const int LESS = 6;
const int GREATER = 7;
const int EQUALS = 8;
const int ASSIGN = 9;
const int EXIT = 0;
const string INT_MESSAGE = "The integer is ";
const string VERY_LONG_MESSAGE =
"The current very_long's value is ";
const string NEW_VERY_LONG_MESSAGE =
"The new very long integer is ";
const string FIRST_VERY_LONG_MESSAGE =
"The first very long integer is ";
const string SECOND_VERY_LONG_MESSAGE =
"The second very long integer is ";
const string EQUAL_MESSAGE = " equal to ";
const string LESS_MESSAGE = " less than ";
const string GREATER_MESSAGE = " greater than ";
const string FALSE_MESSAGE = " is false.";
const string TRUE_MESSAGE = " is true.";
const string COPY_MESSAGE = "The copy's value is ";
const string NOT_A_METHOD_MESSAGE =
" is not a method.";
const string PRE_NEW_MESSAGE =
"Here is the new very long integer before copying: ";
const string NEW_MESSAGE =
"Here is the new very long integer after copying: ";
const string ALTERED_NEW_MESSAGE =
"Here is the new very long integer after alteration: ";
string inFileName, outFileName;
// cout << endl << INFILE_PROMPT;
// cin >> inFileName;
// inFile.open (inFileName.c_str(), ios::in);
/* while (inFile.fail ())
{
cout << endl << IN_FAILURE << inFileName << endl;
cout << endl << INFILE_PROMPT;
cin >> inFileName;
inFile.open (inFileName.c_str(), ios::in);
} // if input file incorrect
*/
cout << endl << OUTFILE_PROMPT;
cin >> outFileName;
outFile.open (outFileName.c_str(), ios::out);
// end SET UP FILES
// begin READANDPROCESS
int method;
bool cont = true;
printMenu();
cout << "Enter choice" << endl;
cin >> method;
while (cont)
{
outFile << METHOD_MESSAGE << printMethod(method) << endl;
cout << METHOD_MESSAGE << printMethod(method) << endl;
if (method != 0)
{
very_long_int very_long_1, very_long_2;
unsigned n;
switch (method)
{
case INITIALIZE :
cout << "Enter integer value ";
cin >> n;
very_long.initialize (n);
outFile << endl << INT_MESSAGE << very_long << endl;
cout << endl << INT_MESSAGE << very_long << endl;
break;
// initialize
case READ :
cout << "Enter very long integer ";
cin >> very_long;
break;
case PRINT :
outFile << VERY_LONG_MESSAGE << very_long << endl;
cout << VERY_LONG_MESSAGE << very_long << endl;
break;
case ADD :
cout << "Enter first very long value ";
cin >> very_long_1;
outFile << endl << FIRST_VERY_LONG_MESSAGE << very_long_1 << endl;
cout << endl << FIRST_VERY_LONG_MESSAGE << very_long_1 << endl;
cout << "Enter second very long value ";
cin >> very_long_2;
outFile << endl << SECOND_VERY_LONG_MESSAGE << very_long_2 << endl;
cout << endl << SECOND_VERY_LONG_MESSAGE << very_long_2 << endl;
very_long = very_long_1 + very_long_2;
break;
// add
case LESS :
cout << "Enter first very long value ";
cin >> very_long_1;
cout << "Enter second very long value ";
cin >> very_long_2;
if (very_long_1 < very_long_2)
{
cout << very_long_1 << " < " << very_long_2;
outFile << very_long_1 << " < " << very_long_2;
}
else
{
cout << very_long_1 << " >= " << very_long_2;
outFile << very_long_1 << " >= " << very_long_2;
}
break;
// less
case GREATER :
cout << "Enter first very long value ";
cin >> very_long_1;
cout << "Enter second very long value ";
cin >> very_long_2;
if (very_long_1 > very_long_2)
{
cout << very_long_1 << " > " << very_long_2;
outFile << very_long_1 << " > " << very_long_2;
}
else
{
cout << very_long_1 << " <= " << very_long_2;
outFile << very_long_1 << " <= " << very_long_2;
}
break;
// greater
case EQUALS :
cout << "Enter first very long value ";
cin >> very_long_1;
cout << "Enter second very long value ";
cin >> very_long_2;
if (very_long_1 == very_long_2)
{
cout << very_long_1 << " == " << very_long_2;
outFile << very_long_1 << " == " << very_long_2;
}
else
{
cout << very_long_1 << " != " << very_long_2;
outFile << very_long_1 << " != " << very_long_2;
}
break;
// equals
case EXIT :
break;
default :
cout << printMethod(method) << NOT_A_METHOD_MESSAGE << endl;
}
outFile << endl << PRINT_HEADING;
outFile << endl << very_long << endl << endl << endl;
printMenu();
cin >> method;
}
else
cont = false;
} // while
outFile.close();
cout << endl << endl << CLOSE_WINDOW_PROMPT;
cin.get();
return 0;
} // main
void printMenu()
{
cout << "1 for Initialize" << endl;
cout << "2 for Read" << endl;
cout << "3 for Print" << endl;
cout << "4 for Add" << endl;
cout << "6 for Less Than" << endl;
cout << "7 for Greater Than" << endl;
cout << "8 for Equals" << endl;
cout << "0 for Exit" << endl;
}
string printMethod (const int& method)
{
const int INITIALIZE = 1;
const int READ = 2;
const int PRINT = 3;
const int ADD = 4;
const int MULTIPLY = 5;
const int LESS = 6;
const int GREATER = 7;
const int EQUALS = 8;
const int ASSIGN = 9;
const int EXIT = 0;
switch (method)
{
case INITIALIZE : return "initialize";
break; // initialize
case READ : return ">>";
break;
case PRINT : return "<<";
break;
case ADD : return "add";
break; // add
case LESS : return "<";
break; // less
case GREATER : return ">";
break; // greater
case EQUALS : return "==";
break; // equals
case ASSIGN : return "=";
break; // operator=
case MULTIPLY : return "*";
break; // multiply
case EXIT : return "exit";
break;
default :
return "not a valid choice";
}
} // printMethod
Explanation / Answer
Consider this implementation of operatorRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.