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

In this assignment you will define, mplement, and test a C++ class called Ternar

ID: 3891028 • Letter: I

Question

In this assignment you will define, mplement, 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 system is computed as (122)1- 1102210210one undred twenty two. In the ternary number system s value ls (122h = 1+32 +2 * 31 + 2+30-1710. Your task is to design, mplement, and test a class named Ternary according to the following specification. A ternary number is represented by its equivalent decimal value in the class. Thus, for example, ternary umber 122)3 is actually stored as the decimal integer 17. Therefore, the only data member in class Ternary is an int object named decimal equivalent Code snippets and their intended meaning given below are used to ustrate the member functions that you need to include in class Ternary 1. Define two different constructors to create Ternary class objects in a consistent state. Ternary a b(122), c(241); // create objects of class type Meaning. Object a is created and the data member decimal equivalent is initial ized with value 0. Object b is a valid ternary number whose decimal value is 17; the data member decimal equivalent is initialized with value 17. Object c is an invalid ternary number and the constructor must give an error message and the data member decimal equivalent is initialized with value 0 2. Other member functions you need to have in class Ternary Ternary d (201) // d is created b-plus (d) ; Ternary class object a contains the sum of Ternary class objects b and b.minus (d) ; a Meaning. a Meaning. Ternary class object a contains the result of Ternary class object d subtracted from Ternary class object b a = b , times (d) ; Meaning. and d. Ternary class object a contains the product of Ternary class objects b a. insert (cout); Meaning. Ternary class object a is displayed on the terminal (standard output) c.extract(cin); Meaning. Ternary class object c is read from the keyboard (standard input)

Explanation / Answer

#ifindef ternary_h

#define ternary_h

#include<iostream>

class Ternary

{

private:

int decimal_equivalent;

void validate_and_intialize_value(int arg);

public:

Ternary();

Ternary(int arg);

Ternary plus(Ternary b);

Ternary minus(Ternary b);

Ternary times(Ternary b);

void insert(std::ostream&);

void extract(std::istream&);

};

#endif/*ternary_h*/

ternary.cc

#include"ternary.h"

#include<sstream>

#include<algorithm>

void Ternary::validate_and_intialize_value(int arg)

{

int carg = arg;

int s = 0;

int q = 1;

int sign = arg>0 ? 1:-1;

arg *=sign;

while(arg)

{

int! = arg%10;

arg /= 10;

if(l>2)

{

std::cout<<"input"<<carg<<"is invalid ternary input/n";

decimal_equivalent = 0;

return;

}

s +=l*q;

q *=3;

}

decimal_equivalent=sign*s;

}

Ternary::Ternary():decimal_equivalent(0)

}

Ternary::Ternary(int a)

{

vaidate_and_intailize_value(a);

}

Ternary Ternary::plus(Ternary b)

{

Ternary result;

result.decimal_equivalent=decimal_equivalent+ b.decimal_equivalent;

return resut;

}

Ternary Ternary::minus(Ternary b)

{

Ternary result;

result.decimal_equivalent = decimal_equivalent-b.decimal_equivalent;

return result;

}

Ternary Ternary::times(Ternary b)

{

Ternary result;

result.decimal_equivalent =decimal_equivalent*b.decimal_equivalent;

return result;

}

void Ternary::insert(std::ostream&OS)

{

if(decimal_equivalent==0)

{

os<<"0";

return;

}

intX=decimal_equivalent;

std::stringstream res;

if(x<0)

{

x = -x;

}

while(x)

{

int c =x%3;

res <<c;

x/=3;

}

std::string r = res.str();

std::reverse(r.begin(),r.end());

if(decimal_equivalent<0)

{

os <<"-";

}

os <<r;

}

void Ternary::extract(std::istream&is)

{

intq;

is>>q;

validate_and_intialize_value(q);

}

test ternary.cc

#include "ternary.h"

#include<iostream>

int main()

{

ternary a;

a.insert(std::count);

std::cout<<std::endl;

Ternary b(122);

b.insert(std::cout);

std::cout<<std::endl;

ternary c(241);

c.insert(std::cout);

std::cout<<std::endl;

Ternary d(201);

d.insert(std::cout);

std::cout<<std::endl;

a = b.plus(d);

a.insert(std::cout);

std::cout<<std::endl;

a=b.minus(d);

a.insert(std::cout);

std::cout<<std::endl;

a = b.times(d);

a.insert(std::cout);

std::cout<<std::endl;

a.extract(std::cin);

a.insert(std::cout);

std::cout<<std::endl;

return 0;

}