Answered step by step
Verified Expert Solution
Question
1 Approved Answer
my operator*= it is not working properly when I call the other operator+= it is not doing giving me a sum, but it's copying the
my operator*= it is not working properly
when I call the other operator+= it is not doing giving me a sum, but it's copying the two string. Ex. mult.nb+=nb;(5*3) it is giving me 1011010
How can I fix it?
This is my code:
//my class Uint.hpp
class Uint { private: string nb; //variable public: // Constructors Uint(); // Default constructeur Uint(size_t n); // Constructeur avec size_t
Uint& operator*=(const Uint& other);
friend Uint operator*(Uint lhs, const Uint & rhs);
//my file Uint.cpp
Uint::Uint() = default; Uint::Uint(size_t n){ string u; while (n > 0) { if (n % 2 == 0) u.insert(u.begin(), '0'); else u.insert(u.begin(), '1'); n /= 2; } this->nb = u; }
Uint& Uint::operator+=(const Uint& autre) { Uint sum; Uint tmp = autre; if (nb.size() > tmp.nb.size()) { while (tmp.nb.size() != nb.size()) { tmp.nb = '0' + tmp.nb; } } else if (tmp.nb.size() > nb.size()) { while (nb.size() != tmp.nb.size()) { nb = '0' + nb; } } int retenue = 0; sum.nb.resize(nb.length()); for (int i = nb.size()-1; i>=0 ; i--) { int n1=nb[i]-'0'; int n2=tmp.nb[i]-'0'; int result = n1 + n2 + retenue; retenue = result / 2; result = result % 2; sum.nb[i] = result + '0'; } if (retenue > 0 ){ sum.nb = '1' + sum.nb; } return *this=sum; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started