Question: hey i need solution for this ques and i have posted ist assignment (last pic) also because both are related Please refer to the description



Please refer to the description of balanced ternary mumbers in Assignment 1. In Assignment 2 you are asked to complete the definition, implementation and testing of class BTernary As you already know, a ternary number (as specified in the assignment) is input/output as a C++ string while stored internally as an integer. As such, the class BTernary needs two conversion tasks, namely, 1. ternary Todecimal 2. decimalToternary Consider the ternary number (-11011).. Its value in decimal system is computed by multiplying each ternary digit (henceforth trit) by its positional value and then summing them. (-1) 34 + 1 x 3 + 0 x 3 + 1 x 3' +1 x 3 = (-50). On the other hand, given a decimal number, its equivalent ternary number can be found by algorithm DECIMAL2TERNARY. The algorithm is given in pseudocode where, the assignment operation is indicated by symbol any text appearing after symbol D up to the end of line is considered comment. DECIMAL TOTERNARY 1 Let d be a decimal number to be converted to balanced ternary 2 Lets be a string variable, trit and temp be integer variables, and negative be a Boolean variable 3 temp abs(d) Get the absolute value. 4 negative - False 5 if d 0 8 do 9 trit temp modulo 3 10 if trit is 0 11 then st"0"+D append character 0 to the front of strings 12 temp -- temp/3 13 else if trit is 1 14 then if negative 15 then +41"+D append the string =1" to the front of strings 16 else 17 $+"1" +D append character 1 to the front of strings 18 temp -- temp/3 19 else if trit is 2 20 then if negative 21 then st "1" + 22 else 23 "-1" + 24 25 temp (temp+1)/3 2 Complete Specification for class BTernary Given below is the complete specification for class BTernary. Code encataples are used to illustrate the essential properties of the class objects. 1. Implement two different constructors to create BTernary class objects in a consistent state. string si - "10-1-1": string 52 - "41-10": BTernary a, b(si), c(02); // create objects of class type Meaning. Object a is created and the data member decimal equivalent is initialized with value 0. Object b is a valid BTernary number whose decimal value is 23; the data member decinal equivalent is initialized with value 23. Object is an invalid BTernary number and the constructor should give an error move and the data member decimal.equivalent be initialized with value 0. 2. Other member functions you need to implement in class BTernary are an illustrated below BTernary d(s1); b.plus(d); a = Meaning. BTernary class object a contains the sum of BTernary class objects b and d. (b) a = b.minus(d); Meaning. BTernary class object a contains the result of BTernary class object d subtracted from BTernary class object b. (c) a = b.times(d); Meaning. BTernary class object a contains the product of BTernary class objects b and d. a.insert(cout); Meaning. BTernary class object a is displayed on the terminal (standard output). c.extract(cin); Meaning. BTernary class object c is read from the keyboard (standard input) (1) Implement a member function called ternaryTodecimal that takes a C++ string (e.g.-11011") as parameter and interprets it as a balanced ternary number and returns its decimal value. (read discussion in Section 1) (8) Implement a member function called decimalToternary that takes a decimal num- beras parameter and returns its balanced ternary number representation as a C++ string using algorithm DECIMAL TOTERNARY. (h) Implement a member function called isTernary that takes a C + + string as pa- rameter and returns true if the passed argument is a valid ternary number. The function returns-false if the passed argument is not a valid ternary number. 3 What to submit 1. Class definition for BTernary in a text file named BTornary.h that meets the specification in Section 2. Give brief but convincing arguments, as comments, for your choice of class member access restriction (i.c., public/private). 2. In a file named Bernary.cc, implement the member functions of class BTernary 3. Write a test program in a file named test.BTernary.ce to test the correctness of imple mented member functions (nee, for example, Rational clatest program) Code snippets and their intended meaning given below are used to illustrate the member functions that you need to include in class BTernary. 1. Define two different constructors to create BTernary class objects in a consistent state. string si - "10-1-1"; string s2 - "41-10"; BTernary a, b(sa), c(s2); // create objects of class type Meaning. Object a is created in the "empty" state: its data member decimal equivalent is initialized to 0. Object bis created from parameter si: its data member decimal equivalent is initialized with the value 23. Object c is an invalid BTernary number and the con- structor should give an error message, however c does get created but in the empty" state. 2. Other member functions you need to include in class BTernary are indicated in boldface (a) plus BTernary d(81); a = b.plus (a); Meaning. BTernary class object a contains the sum of BTernary class objects b and d. (b) minus a = b.minus (d); Meaning. BTernary class object a contains the result of BTernary class object d subtracted from BTernary class object b. (e) times a = b.times (d): Meaning. BTernary class object a contains the product of BTernary class objects b and a (d) insert a insert (cout); Meaning. BTernary class object a is displayed on the terminal (standard output). (e) extract c.extract (cin); Meaning. BTernary class object c is read from the keyboard (standard input)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
