Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement and test the class named Ternary according to the following class defi

ID: 3587747 • Letter: I

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-=( 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&);

In this assignment you will define, implement, and test a C++ class called Ternary to represent and use whole numbers (positive or negative) in base-three number system. In the decimal system or base-ten number system we have 10 symbols or numerals 0,1,2,... ,9 representing values zero, one, two, ..., nine, respectively. In the ternary number system there are only three symbols 0, 1, 2 representing values zero, one, two, respectively. For a multi-digit whole number e.g., 122, its value in decimal systern is computed as (122) 11022 1012 10 one hundred twenty two. In the ternary number system its value Is (122),-1*32 +2 * 31 + 2 * 30-1710-

Explanation / Answer

//Terinary.h

#include<iostream>

#include<fstream>

using namespace std;

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;

//declare below as friend to work as expected , CheggEA

friend ostream& operator<<(ostream &out, const Ternary &obj);

friend istream& operator>>(istream&, Ternary&);

friend const Ternary& operator+(const Ternary&, const Ternary&);

friend const Ternary& operator-(const Ternary&, const Ternary&);

friend const Ternary& operator*(const Ternary&, const Ternary&);

private:

bool isTernary(int) const;

int toDecimal(int) const;

int toTernary(int) const;

int decimal_equivalent;

};

-----------------------------------

//Terinary.cpp

#include"Terinary.h"

#include<string.h>

Ternary::Ternary()

{

decimal_equivalent = 0;

}

Ternary::Ternary(int n)

{

if (isTernary(n))

{

decimal_equivalent=toDecimal(n);

}

}

bool Ternary::isTernary(int n) const

{

int rem;

do

{

rem = n % 10;

if (rem > 2)

return false;

n = n / 10;

} while (n > 1);

return true;

}

istream&Ternary::extract(istream&in)

{

in >> decimal_equivalent;

return in;

}

ostream&Ternary::insert(ostream&out) const

{

out << decimal_equivalent;

return out;

}

ostream& operator<<(ostream &out, const Ternary &obj)

{

out<<obj.decimal_equivalent<<endl;

return out;

}

istream& operator>>(istream&in, Ternary&obj)

{

in >> obj.decimal_equivalent;

return in;

}

const Ternary& operator+(const Ternary&obj1, const Ternary&obj2)

{

int sum = obj1.decimal_equivalent + obj2.decimal_equivalent;

sum = obj1.toTernary(sum);

return sum;

}

const Ternary& operator-(const Ternary&obj1, const Ternary&obj2)

{

int diff = obj1.decimal_equivalent - obj2.decimal_equivalent;

diff = obj1.toTernary(diff);

return diff;

}

const Ternary& operator*(const Ternary&obj1, const Ternary&obj2)

{

int prod = obj1.decimal_equivalent * obj2.decimal_equivalent;

prod = obj1.toTernary(prod);

return prod;

}

int Ternary::toDecimal(int n) const

{

int rem, count = 0,dec = 0;

do

{

rem = n % 10;

dec += rem*pow(3, count++);

n = n / 10;

} while (n >0);

return dec;

}

int Ternary::toTernary(int n) const

{

char s[20];

int rem,i=0;

do

{

rem = n % 3;

s[i++] = rem + 48;

n = n / 3;

} while (n > 0);

s[i] = '';

_strrev(s);

return atoi(s);

}

Ternary& Ternary::operator += (const Ternary&obj)

{

this->decimal_equivalent += obj.decimal_equivalent;

decimal_equivalent = toTernary(decimal_equivalent);

return *this;

}

Ternary& Ternary::operator -= (const Ternary&obj)

{

this->decimal_equivalent -= obj.decimal_equivalent;

toTernary(decimal_equivalent);

return *this;

}

Ternary& Ternary::operator *= (const Ternary&obj)

{

this->decimal_equivalent *= obj.decimal_equivalent;

toTernary(decimal_equivalent);

return *this;

}

Ternary& Ternary::operator -()

{

this->decimal_equivalent *= -1;

return *this;

}

bool Ternary::operator == (const Ternary&obj) const

{

if (decimal_equivalent == obj.decimal_equivalent)

return true;

else

false;

}

bool Ternary::operator != (const Ternary&obj) const

{

if (decimal_equivalent != obj.decimal_equivalent)

return true;

else

return false;

}

bool Ternary::operator<(const Ternary&obj) const

{

if (decimal_equivalent<obj.decimal_equivalent)

return true;

else

return false;

}

bool Ternary::operator>(const Ternary&obj) const

{

if (decimal_equivalent > obj.decimal_equivalent)

return true;

else

return false;

}

bool Ternary::operator <= (const Ternary&obj) const

{

if (decimal_equivalent <= obj.decimal_equivalent)

return true;

else

return false;

}

bool Ternary::operator >= (const Ternary&obj) const

{

if (decimal_equivalent >= obj.decimal_equivalent)

return true;

else

return false;

}

--------------------------------------------

//Main.cpp

#include"Terinary.h"

using namespace std;

int main()

{

Ternary num1(2), num2(12),result;

cout << num1 + num2;

num1 += num2;

cout << num1;

}

---------------------

//output for addition

7
21

//as time was less for doing this program I could only test addition,,

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote