Answered step by step
Verified Expert Solution
Link Copied!

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Real Time Database Systems Architecture And Techniques

Authors: Kam-Yiu Lam ,Tei-Wei Kuo

1st Edition

1475784023, 978-1475784022

More Books

Students also viewed these Databases questions

Question

3. Comment on how diversity and equality should be managed.

Answered: 1 week ago

Question

describe the legislation that addresses workplace equality

Answered: 1 week ago