Operator* Overloading

Would it be best to loop and multiply each time using the carry and remainder or to do something like this
1
2
3
4
5
6
7
8
9
Integer& Integer::operator*=( const Integer &rhs )
{
    const Integer temp( *this );
    for( Integer i( "0" ); i < rhs - Integer( "1" ); ++i )
    {
       *this += temp;
    }
    return( *this );
}


*edit
Fixed line 5 accidentally put value += value;
Instead of *this += *this;

**edit wasn't thinking when i said *this += *this.
I need a temp value for the orig and then add that each time I fixed but is this a viable method for multiplication or would it be better to do it like we do on paper?

***edit
whoops forgot about the initial so it would be < rhs-1 or just use temp variable to modify like this

1
2
3
4
5
6
7
8
9
10
11
Integer& Integer::operator*=( const Integer &rhs )
{
    Integer temp( "0" );
    for( Integer i( "0" ); i < rhs; ++i )
    {
        std::cout << *this << " + " << temp << " = " << *this + temp << std::endl;
        temp += *this;
    }
    *this = temp;
    return( *this );
}
Last edited on
I used a similar method, but to reduce number of the iterations, I increased the operand along with the final value.

Edit:
Missing word.
Last edited on
Oh alright and one more question can you help me better my < operator
It seems to not work all the time.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool operator<( const Integer &lhs , const Integer &rhs )
{
    if( lhs == rhs ) return( false ); //lhs is not less than rhs since they are equal
    if( lhs.value.size() != rhs.value.size() ) return( lhs.value.size() < rhs.value.size() ); //if lhs length is greater than rhs lhs then it is not less than. ( 100 > 99 ) since 100 has 3 digits and 99 has 2 digits.

    auto lhs_it( lhs.value.cbegin() ) , rhs_it( rhs.value.cbegin() ); //const iterators
    //the two integers are the same number of digits
    while( lhs_it != lhs.value.cend() ) //loop tlll end of  lhs integer 
    {
        if( *lhs_it < *rhs_it ) return( true ); //compare from left to right if the right digit is greater than return true since that means the left is smaller
//if they are equal or the lhs is greater return nothing since we must check all of them from left to right.
        ++lhs_it;
        ++rhs_it;
    }
    return( false ); //they are equal so return false since if they are equal lhs is not less than rhs
}

1
2
3
4
//main.cpp
std::cout << "85 < 17 = " << std::boolalpha << ( Integer( "85" ) < Integer( "17" ) ) << std::endl;
85 < 17 = true

Process returned 0 (0x0)   execution time : 0.024 s
Press any key to continue.


*edit the reason I know there is a problems is because umm..
it thinks that 85 is less than 17...
Last edited on
You don't really need your check on line 3. Take a look at your comment on line 15.

I don't really know what is wrong with your while loop, because it would depend on whether or not you are storing the integer's digits/sections in reverse order or not.

Also, std::string (based on your original post) already has overloaded comparison operators you can use instead of doing it manually (Of course, this would not be true if you actually meant to iterate from back to front).
Sorry line 15 is if they are equal orlhs is greater than rhs and I had no idea that std::string overloaded the comparison operators o.O.

Thanks for letting me know that =]
I'll just use their operators inside of mine lol.
http://www.cplusplus.com/reference/string/string/operators/

1
2
3
4
bool operator<( const Integer &lhs , const Integer &rhs )
{
    return( lhs.value < rhs.value );
}


Much easier thanks again.
Last edited on
You still need to check if the sizes are equal. The overloaded comparison operators for std::string are meant for alphabetizing.
 
std::string("2999") < "90" == true
Yeah I just noticed that =p thanks again.

1
2
3
4
5
bool operator<( const Integer &lhs , const Integer &rhs )
{
    if( lhs.value.size() != rhs.value.size() ) return( lhs.value.size() < rhs.value.size() );
    return( lhs.value < rhs.value );
}
Last edited on
Topic archived. No new replies allowed.