Question
Implement and test the class named Ternary according to the following class definition: class Ternary { public: Ternary(); Ternary(int); Ternary& operator+=( const Ternary&); Ternary& operator-=(
Implement and test the class named Ternary according to the following class definition:
class Ternary {
public:
Ternary();
Ternary(int);
Ternary& operator+=( const Ternary&);
Ternary& operator-=( const Ternary&);
Ternary& operator*=(const Ternary&);
Ternary& operator-();
bool operator==(const Ternary&) const;
bool operator!=(const Ternary&) const;
bool operator<(const Ternary&) const;
bool operator>(const Ternary&) const;
bool operator<=(const Ternary&) const;
bool operator>=(const Ternary&) const;
istream& extract(istream&);
ostream& insert(ostream&) const;
private:
bool isTernary(int) const;
int toDecimal(int) const;
int toTernary(int) const;
int decimal_equivalent;
};
ostream& operator<<(ostream&, const Ternary&);
istream& operator>>(istream&, Ternary&);
const Ternary& operator+(const Ternary&, const Ternary&);
const Ternary& operator-(const Ternary&, const Ternary&);
const Ternary& operator*(const Ternary&, const Ternary&);
The specification for each function is given below. 1. Ternary() Default Constructor. Precondition: None Postcondition: A ternary number is created by initializing its data member to 0 2. Ternary(int) Constructor. Precondition: None Postcondition: The int actual parameter is checked to see if any of the digits in it is greater than 2 and if so an error message is output on to the terminal and its data member is initialized to zero; otherwise the int actual parameter is converted to the corresponding decimal value and the data member decimal equivalent is initialized with the decimal value. 3. Ternary& operator+=( const Ternary&) Overloaded add-assignment operator. Precondition: None Postcondition: The actual parameter is added to this Ternary number and this Ternary number is returned.
4. Ternary& operator-=( const Ternary&) Overloaded subtract-assignment operator
Precondition: None Postcondition: The actual parameter is subtracted from this Ternary number and this Ternary number is returned. 5. Ternary& operator*=( const Ternary&) Overloaded multiply-assignment operator. Precondition: None Postcondition: The actual parameter is multiplied with this Ternary number and this Ternary number is returned. 6. Ternary& operator-( const Ternary&) Overloaded unary minus. Precondition: None Postcondition: this Ternary number is negated and returned. 7. bool operator==( const Ternary&) const Overloaded Boolean equality operator. Precondition: None Postcondition: If the actual parameter is equal to this Ternary number the Boolean constant true is returned; otherwise Boolean constant false is returned. 8. bool operator!=( const Ternary&) const Overloaded Boolean not equal operator. Precondition: None Postcondition: If the actual parameter is not equal to this Ternary number the Boolean constant true is returned; otherwise Boolean constant false is returned. 9. bool operator<( const Ternary&) const Overloaded Boolean less-than operator. Precondition: None Postcondition: If the actual parameter is less than this Ternary number the Boolean constant true is returned; otherwise Boolean constant false is returned. 10. bool operator<=( const Ternary&) const Overloaded Boolean less-than-or-equal operator. Precondition: None Postcondition: If the actual parameter is less than or equal to this Ternary num- ber the Boolean constant true is returned; otherwise Boolean constant false is returned. 11. bool operator>( const Ternary&) const Overloaded Boolean greater-than operator. Precondition: None Postcondition: If the actual parameter is greater than this Ternary number the Boolean constant true is returned; otherwise Boolean constant false is returned. 12. bool operator>=( const Ternary&) const Overloaded Boolean greater-than-or-equal operator.
Precondition: None Postcondition: If the actual parameter is greater than or equal to this Ternary number the Boolean constant true is returned; otherwise Boolean constant false is returned. 13. ostream & insert( ostream&) const Stream insertion. Precondition: None Postcondition: this Ternary number is output to the output stream in the actual pa- rameter. Converts the data member decimal equivalent to the equivalent Ternary and outputs it to the output stream in the actual parameter. 14. istream & extract( istream&) Stream extraction. Precondition: None Postcondition: Extracts the next int from the input stream. The number extracted is checked to see if any of the digits in it is greater than 2 and if so an error message is output on the terminal and this Ternary number is initialized to zero; otherwise the int number extracted is converted to the corresponding decimal value and the decimal value is assigned to the data member decimal equivalent of this Ternary number. 15. const Ternary& operator+( const Ternary&, const Ternary&) Overloaded add op- erator; uses overloaded add-assign operator. Precondition: None Postcondition: The right actual parameter is added to the left actual parameter and the result is returned. 16. const Ternary& operator-( const Ternary&, const Ternary&) Overloaded subtrac- tion operator; uses overloaded subtract-assign operator. Precondition: None Postcondition: The right actual parameter is subtracted from the left actual param- eter and the result is returned. 17. const Ternary& operator*( const Ternary&, const Ternary&) Overloaded multipli- cation operator; uses overloaded multiply-assign operator. Precondition: None Postcondition: The right actual parameter is multiplied with the left actual param- eter and the result is returned. 18. ostream & operator<<( Ternary& ostream, const Ternary&) Overloaded stream in- sertion operator; uses the member function insert. Precondition: None Postcondition: The Ternary actual parameter is inserted in the ostream actual pa- rameter.
19. ostream & operator<<( Ternary& ostream, Ternary&) Overloaded stream extraction operator; uses the member function extract. Precondition: None Postcondition: The Ternary actual parameter is assigned the number extracted from istream actual parameter. 20. bool isTernary(int n) const Check if the int parameter is a ternary number. Precondition: none Postcondition: Returns Boolean constant true if the int parameter consists of digits 0, 1, or 2 only; if any digit is larger than two then the functions returns Boolean constant false. 21. int toDecimal(int n) const Converts the Ternary number passed as actual param- eter to its decimal equivalent. For example, if the number passed is 112, then it returns its decimal equivalent 14. Precondition: The number passed must be a valid Ternary number i.e., it does not contain a digit larger than 2. Postcondition: Returns the decimal number corresponding to the Ternary number passed to the function. 22. int toDecimal(int) const Converts the decimal number passed as actual parameter to its Ternary equivalent. For example, if the number passed is 15, then the function returns its Ternary equivalent 120. Precondition: The actual parameter is a valid integer. Postcondition: Returns the decimal number corresponding to the Ternary number passed to the function.
*To make things slightly easier, just modify the three files below with what required above:
File #1: ternary.h
#ifndef TERNARY_H #define TERNARY_H #include using namespace std;
const int MAX_BITS = 32; const int BASE3 = 3; const int BASE10 = 10;
class Ternary { public:
Ternary();
Ternary(int);
void insert(ostream&) const;
void extract(istream&);
Ternary plus(const Ternary&) const;
Ternary minus(const Ternary&) const;
Ternary times(const Ternary&) const;
private: int decimal_equivalent; }; #endif
File #2: ternary.cc
#include #include #include #include "ternary.h" using namespace std;
Ternary::Ternary() { decimal_equivalent = 0; }
Ternary::Ternary(int val) { decimal_equivalent = 0; int cntr = 0; int digit; while(val != 0) { digit = val % BASE10; if(abs(digit) > 2)
{ cout << "Illegal ternary number. This ternary is set to 0" <\ < endl; decimal_equivalent = 0; break; } decimal_equivalent += digit * pow(static_cast(BASE3),static_cast(cntr)); cntr++; val = val/BASE10;
} }
Ternary Ternary::plus(const Ternary& t) const { Ternary temp; int sum = decimal_equivalent + t.decimal_equivalent; temp.decimal_equivalent = sum; return temp; }
Ternary Ternary::minus(const Ternary& t) const { Ternary temp; // Create a temporary Ternary object int diff = decimal_equivalent - t.decimal_equivalent; temp.decimal_equivalent = diff; return temp; }
Ternary Ternary::times(const Ternary& t) const { Ternary temp; int product = decimal_equivalent * t.decimal_equivalent; temp.decimal_equivalent = product; return temp; }
void Ternary::insert(ostream& outS) const { int val; bool negative = decimal_equivalent < 0; // this Ternary negative? val = abs(decimal_equivalent); int digitArray[MAX_BITS] = {0}; int bit = 0; while (val > 0) { digitArray[bit] = val % BASE3; val = val / BASE3; if(val > 0) bit++;
} if(negative) outS << '-'; for(int i=bit; i>=0; i--) outS << digitArray[i]; }
void Ternary::extract(istream& iStr) { int value; iStr >> value; Ternary temp(value); decimal_equivalent = temp.decimal_equivalent; };
File #3: ternary_test.cc
#include #include "ternary.h"
using namespace std;
int main() { // Declare some Ternaries cout << "Now declaring Ternary t1 ..." << endl; Ternary t1; cout << "Now attempting to declare t2(-8) ..." << endl; Ternary t2(-8); // This declaration should produce error message cout << "t1 is: "; t1.insert(cout); cout << endl; cout << "t2 is: "; t2.insert(cout); cout << endl;
Ternary t3(-12); Ternary t4(2);
cout << "t3 is "; t3.insert(cout); cout << endl; cout << "t4 is "; t4.insert(cout); cout << endl;
Ternary t5 = t3.plus(t4); Ternary t6 = t3.minus(t4); Ternary t7 = t3.times(t4); // + t3.insert(cout); cout << " + ("; t4.insert(cout); cout << ") = "; t5.insert(cout); cout << endl;
// - t3.insert(cout); cout << " - ("; t4.insert(cout); cout << ") = "; t6.insert(cout); cout << endl;
// * t3.insert(cout); cout << " * ("; t4.insert(cout); cout << ") = "; t7.insert(cout); cout << endl;
Ternary t8(12212); cout << "t8 is "; t8.insert(cout);
cout << endl; cout << "Enter a new value for t8: "; t8.extract(cin); cout << "t8 after t8.extract(cin) is: "; t8.insert(cout); cout << endl;
return 0; }
Note: What would I suggest first, is to run the given files above to know what to expect and see final outcome, then modify them with required specifications.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started