#include #include #include using namespace std; class BalancedTernary { protected: // Store the value as a reversed string of +, 0 and - characters string value; // Helper function to change a balanced ternary character to an integer int charToInt(char c) const { if (c == '0') return 0; return 44 - c; } // Helper function to negate a string of ternary characters string negate(string s) const { for (int i = 0; i < s.length(); ++i) { if (s[i] == '+') s[i] = '-'; else if (s[i] == '-') s[i] = '+'; } return s; } public: // Default constructor BalancedTernary() { value = "0"; } // Construct from a string BalancedTernary(string s) { value = string(s.rbegin(), s.rend()); } // Construct from an integer BalancedTernary(long long n) { if (n == 0) { value = "0"; return; } bool neg = n < 0; if (neg) n = -n; value = ""; while (n != 0) { int r = n % 3; if (r == 0) value += "0"; else if (r == 1) value += "+"; else { value += "-"; ++n; } n /= 3; } if (neg) value = negate(value); } // Copy constructor BalancedTernary(const BalancedTernary &n) { value = n.value; } // Addition operators BalancedTernary operator+(BalancedTernary n) const { n += *this; return n; } BalancedTernary& operator+=(const BalancedTernary &n) { static char *add = "0+-0+-0"; static char *carry = "--000++"; int lastNonZero = 0; char c = '0'; for (int i = 0; i < value.length() || i < n.value.length(); ++i) { char a = i < value.length() ? value[i] : '0'; char b = i < n.value.length() ? n.value[i] : '0'; int sum = charToInt(a) + charToInt(b) + charToInt(c) + 3; c = carry[sum]; if (i < value.length()) value[i] = add[sum]; else value += add[sum]; if (add[sum] != '0') lastNonZero = i; } if (c != '0') value += c; else value = value.substr(0, lastNonZero + 1); // Chop off leading zeroes return *this; } // Negation operator BalancedTernary operator-() const { BalancedTernary result; result.value = negate(value); return result; } // Subtraction operators BalancedTernary operator-(const BalancedTernary &n) const { return operator+(-n); } BalancedTernary& operator-=(const BalancedTernary &n) { return operator+=(-n); } // Multiplication operators BalancedTernary operator*(BalancedTernary n) const { n *= *this; return n; } BalancedTernary& operator*=(const BalancedTernary &n) { BalancedTernary pos = *this; BalancedTernary neg = -pos; // Storing an extra copy to avoid negating repeatedly value = "0"; for (int i = 0; i < n.value.length(); ++i) { if (n.value[i] == '+') operator+=(pos); else if (n.value[i] == '-') operator+=(neg); pos.value = '0' + pos.value; neg.value = '0' + neg.value; } return *this; } // Stream output operator friend ostream& operator<<(ostream &out, const BalancedTernary &n) { out << n.toString(); return out; } // Convert to string string toString() const { return string(value.rbegin(), value.rend()); } // Convert to integer long long toInt() const { long long result = 0; for (long long i = 0, pow = 1; i < value.length(); ++i, pow *= 3) result += pow * charToInt(value[i]); return result; } // Convert to integer if possible bool tryInt(long long &out) const { long long result = 0; bool ok = true; for (long long i = 0, pow = 1; i < value.length() && ok; ++i, pow *= 3) { if (value[i] == '+') { ok &= LLONG_MAX - pow >= result; // Clear ok if the result overflows result += pow; } else if (value[i] == '-') { ok &= LLONG_MIN + pow <= result; // Clear ok if the result overflows result -= pow; } } if (ok) out = result; return ok; } }; int main() { int num; cout<<"\nEnter number:"; cin>>num; BalancedTernary b(num); cout << "b = " << b << " = " << b.toInt() << endl; return 0; }   Help me to find an output with integers in it Having issues with the output. Please look into it

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

#include <iostream>
#include <string>
#include <climits>
using namespace std;

class BalancedTernary {
protected:
// Store the value as a reversed string of +, 0 and - characters
string value;

// Helper function to change a balanced ternary character to an integer
int charToInt(char c) const {
if (c == '0')
return 0;
return 44 - c;
}

// Helper function to negate a string of ternary characters
string negate(string s) const {
for (int i = 0; i < s.length(); ++i) {
if (s[i] == '+')
s[i] = '-';
else if (s[i] == '-')
s[i] = '+';
}
return s;
}

public:
// Default constructor
BalancedTernary() {
value = "0";
}

// Construct from a string
BalancedTernary(string s) {
value = string(s.rbegin(), s.rend());
}

// Construct from an integer
BalancedTernary(long long n) {
if (n == 0) {
value = "0";
return;
}

bool neg = n < 0;
if (neg)
n = -n;

value = "";
while (n != 0) {
int r = n % 3;
if (r == 0)
value += "0";
else if (r == 1)
value += "+";
else {
value += "-";
++n;
}

n /= 3;
}

if (neg)
value = negate(value);
}

// Copy constructor
BalancedTernary(const BalancedTernary &n) {
value = n.value;
}

// Addition operators
BalancedTernary operator+(BalancedTernary n) const {
n += *this;
return n;
}

BalancedTernary& operator+=(const BalancedTernary &n) {
static char *add = "0+-0+-0";
static char *carry = "--000++";

int lastNonZero = 0;
char c = '0';
for (int i = 0; i < value.length() || i < n.value.length(); ++i) {
char a = i < value.length() ? value[i] : '0';
char b = i < n.value.length() ? n.value[i] : '0';

int sum = charToInt(a) + charToInt(b) + charToInt(c) + 3;
c = carry[sum];

if (i < value.length())
value[i] = add[sum];
else
value += add[sum];

if (add[sum] != '0')
lastNonZero = i;
}

if (c != '0')
value += c;
else
value = value.substr(0, lastNonZero + 1); // Chop off leading zeroes

return *this;
}

// Negation operator
BalancedTernary operator-() const {
BalancedTernary result;
result.value = negate(value);
return result;
}

// Subtraction operators
BalancedTernary operator-(const BalancedTernary &n) const {
return operator+(-n);
}

BalancedTernary& operator-=(const BalancedTernary &n) {
return operator+=(-n);
}

// Multiplication operators
BalancedTernary operator*(BalancedTernary n) const {
n *= *this;
return n;
}

BalancedTernary& operator*=(const BalancedTernary &n) {
BalancedTernary pos = *this;
BalancedTernary neg = -pos; // Storing an extra copy to avoid negating repeatedly
value = "0";

for (int i = 0; i < n.value.length(); ++i) {
if (n.value[i] == '+')
operator+=(pos);
else if (n.value[i] == '-')
operator+=(neg);
pos.value = '0' + pos.value;
neg.value = '0' + neg.value;
}

return *this;
}

// Stream output operator
friend ostream& operator<<(ostream &out, const BalancedTernary &n) {
out << n.toString();
return out;
}

// Convert to string
string toString() const {
return string(value.rbegin(), value.rend());
}

// Convert to integer
long long toInt() const {
long long result = 0;
for (long long i = 0, pow = 1; i < value.length(); ++i, pow *= 3)
result += pow * charToInt(value[i]);
return result;
}

// Convert to integer if possible
bool tryInt(long long &out) const {
long long result = 0;
bool ok = true;

for (long long i = 0, pow = 1; i < value.length() && ok; ++i, pow *= 3) {
if (value[i] == '+') {
ok &= LLONG_MAX - pow >= result; // Clear ok if the result overflows
result += pow;
} else if (value[i] == '-') {
ok &= LLONG_MIN + pow <= result; // Clear ok if the result overflows
result -= pow;
}
}

if (ok)
out = result;
return ok;
}
};

int main() {

int num;
cout<<"\nEnter number:";
cin>>num;
BalancedTernary b(num);

cout << "b = " << b << " = " << b.toInt() << endl;

return 0;
}

 

Help me to find an output with integers in it

Having issues with the output. Please look into it

+
->
A onlinegdb.com/online_c++_compiler
p
H Apps
Gmail
YouTube
Мaps
Translate
News
OISSS Virtual Advisi...
O Employment & Pra..
9 View Holds
Other bookmarks
E Reading list
Run
O Debug
I Stop
C Share
A Save
{} Beautify
Language C++
OnlineGDB beta
main.cpp
online compiler and debugger for c/c++
1
#include <iostream>
2 #include <string>
code. compile. run. debug. share.
3
#include <climits>
4 using namespace std;
IDE
class BalancedTernary {
7 protected:
8 // Store the value as a reversed string of +, 0 and - characters
9 string value;
My Projects
6
Classroom new
Learn Programming
10
Programming Questions
11 // Helper function to change a balanced ternary character to an integer
int charToInt(char c) const {
if (c == '0')
return 0;
return 44 - c;
Sign Up
12
13
Login
14
15
f
46.8K
16 }
17
18 // Helper function to negate a string of ternary characters
19 - string negate(string s) const {
20 - for (int i = 0; i < s.length(); ++i) {
:c Le :1
input
Enter number:6
b = +-0 = 6
About • FAQ • Blog • Terms of Use • Contact
Us • GDB Tutorial • Credits • Privacy
© 2016 - 2021 GDB Online
...Program finished with exit code 0
Press ENTER to exit console
13:23
O Type here to search
74°F
ENG
16-09-2021
...
Transcribed Image Text:+ -> A onlinegdb.com/online_c++_compiler p H Apps Gmail YouTube Мaps Translate News OISSS Virtual Advisi... O Employment & Pra.. 9 View Holds Other bookmarks E Reading list Run O Debug I Stop C Share A Save {} Beautify Language C++ OnlineGDB beta main.cpp online compiler and debugger for c/c++ 1 #include <iostream> 2 #include <string> code. compile. run. debug. share. 3 #include <climits> 4 using namespace std; IDE class BalancedTernary { 7 protected: 8 // Store the value as a reversed string of +, 0 and - characters 9 string value; My Projects 6 Classroom new Learn Programming 10 Programming Questions 11 // Helper function to change a balanced ternary character to an integer int charToInt(char c) const { if (c == '0') return 0; return 44 - c; Sign Up 12 13 Login 14 15 f 46.8K 16 } 17 18 // Helper function to negate a string of ternary characters 19 - string negate(string s) const { 20 - for (int i = 0; i < s.length(); ++i) { :c Le :1 input Enter number:6 b = +-0 = 6 About • FAQ • Blog • Terms of Use • Contact Us • GDB Tutorial • Credits • Privacy © 2016 - 2021 GDB Online ...Program finished with exit code 0 Press ENTER to exit console 13:23 O Type here to search 74°F ENG 16-09-2021 ...
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY