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&);
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,,
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.