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

Provide a brief and logical commeting for each/most of the expressions/functions

ID: 3871151 • Letter: P

Question

Provide a brief and logical commeting for each/most of the expressions/functions within the code below. For example; what is the purpose of #include <sstream>, #include <algorithm>, or int sign = arg > 0 ? 1 : -1;? And so on...

Here is the ternary.cc:

#include "ternary.h"
#include <iostream>
#include <sstream>
#include <algorithm>
void Ternary::validateAndInitializeValue(int arg){

int carg = arg;
int s = 0;
int q = 1;
int sign = arg > 0 ? 1 : -1;
arg *= sign;
while(arg){
int l = arg%10;
arg /= 10;
if(l > 2){
cout << " Input " << carg << " is invalid ternary input" << endl;
decimalEquivalent = 0;
return;
}

s += l*q;
q *= 3;
}
decimalEquivalent = sign*s;
}

Ternary::Ternary():decimalEquivalent(0){

}

Ternary::Ternary(int a){

validateAndInitializeValue(a);

}

Ternary Ternary::plus(Ternary b){

Ternary result;
result.decimalEquivalent = decimalEquivalent + b.decimalEquivalent;

return result;
}

Ternary Ternary::minus(Ternary b){

Ternary result;
result.decimalEquivalent = decimalEquivalent - b.decimalEquivalent;
return result;
}

return result;
}

Ternary Ternary::times(Ternary b){

Ternary result;
result.decimalEquivalent = decimalEquivalent*b.decimalEquivalent;
return result;
}

void Ternary::insert(ostream& sout){

if(decimalEquivalent == 0){

sout << "0";
return;
}
int x = decimalEquivalent;
stringstream res;
if(x < 0){
x = -x;
}
while(x){
int c = x%3;

res << c;
x /= 3;
}
string r = res.str();
reverse(r.begin(), r.end());
if(decimalEquivalent < 0){
sout << "-";
}
sout << r;
}

void Ternary::extract(istream& sin){
int q;
sin >> q;
validateAndInitializeValue(q);
}

Explanation / Answer

Hi, i have added comments what each line does in the above code,

#include "ternary.h"
#include <iostream>
#include <sstream>
#include <algorithm>
void Ternary::validateAndInitializeValue(int arg){ //defining the methodm, this method places the decimal value of given arg in the array

int carg = arg;
int s = 0;
int q = 1;
int sign = arg > 0 ? 1 : -1; //this will set sign to 1 if arg >0 or -1 otherwise
arg *= sign; //arg=arg*sign
while(arg){ //looping till arg becomes 0
int l = arg%10; //getting the first digit from left
arg /= 10; //removing the last digit
if(l > 2){
cout << " Input " << carg << " is invalid ternary input" << endl;
decimalEquivalent = 0;
return;
}

s += l*q; //converting to decimal by multiplying with power of 10
q *= 3;
}
decimalEquivalent = sign*s; //multiplying sign to print decimal equivalent
}

Ternary::Ternary():decimalEquivalent(0){

}

Ternary::Ternary(int a){

validateAndInitializeValue(a);

}

Ternary Ternary::plus(Ternary b){ // here we are defining plus operation, which is by adding decimal equivalents of both numbers

Ternary result;
result.decimalEquivalent = decimalEquivalent + b.decimalEquivalent;

return result;
}

Ternary Ternary::minus(Ternary b){ // here we are defining minusoperation, which is by subtracting decimal equivalents of both numbers

Ternary result;
result.decimalEquivalent = decimalEquivalent - b.decimalEquivalent;
return result;
}

return result;
}

Ternary Ternary::times(Ternary b){ // here we are defining multiply operation, which is by multiplying b times the decimal equivalents of second number

Ternary result;
result.decimalEquivalent = decimalEquivalent*b.decimalEquivalent;
return result;
}

void Ternary::insert(ostream& sout){ // this is used to put the contents to output stream

if(decimalEquivalent == 0){

sout << "0";
return;
}
int x = decimalEquivalent;
stringstream res;
if(x < 0){
x = -x;
}
while(x){ //while looping through array
int c = x%3;

res << c; // this is left shift operator which shifts bits to left by c positions i.e equivalent to res * 2c
x /= 3;
}
string r = res.str();
reverse(r.begin(), r.end()); // reversing because to print from units to tens order
if(decimalEquivalent < 0){
sout << "-"; //printing sign
}
sout << r; //printing number
}

void Ternary::extract(istream& sin){ //this is used to extract input from stream
int q;
sin >> q;
validateAndInitializeValue(q); //initialization
}
Thumbs up if this was helpful, otherwise let me know in comments

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