Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
1.80/5 (4 votes)
See more:
C++
bool Mint::operator<(const Mint &rhs)
{
	if (sign != rhs.sign && sign == 0)
	{
		return false;
	}
	if (sign != rhs.sign && sign == 1)
	{
			return true;
	}
	if (num.size() != rhs.num.size())
	{
		if (num.size() < rhs.num.size())
		{
			if (sign == 0)
			return true;
		}
	}
	if (num.size() == rhs.num.size())
	{
		if (sign == 0)
		{
			int i = num.size() - 1;
			while (i>=0)
			{
				if ( num[i] < rhs.num[i])
				{
					return true;
					i = 0;
				}
				i--;
			}
		}
	}
	return false;
}


hello guys
so far the code above is my idea of the < operator for bigint class.
Please take a look at it and tell me if i missed something or if there any improvements i can make.
thank you
Posted
Updated 1-Jan-16 22:47pm
v2
Comments
Richard MacCutchan 1-Jan-16 11:36am    
You keep posting these but without any real questions. You should test your code under all different conditions to see whether it works or not.
Arthur V. Ratz 1-Jan-16 18:28pm    
Everything is o'key, but are the sign and num variable are the Mint class members ? If so, the code should nicely work.
Philippe Mori 1-Jan-16 20:23pm    
That code can easily be improved... There are some redundant tests... and there are cases that are ignored. For example, it does not seems to compare negative numbers of the same length. And by the way, usually, you want such operator to be a free (maybe friend) function for symmetry (for example, if there is implicit conversion from integer and you want to compare both in either with the integer or the big integer first and without specialization).
Aescleal 2-Jan-16 5:15am    
If you want it to be a member make the function const.
[no name] 2-Jan-16 6:51am    
All getting a bit pointless. Create some test cases and learn to use your debugger. At what point are you proposing to stand on your own two legs.

1 solution

As already noted by others, this can be improved, function should be const and not all cases are handled:
// Operator should be const
//bool Mint::operator<(const Mint &rhs)
bool Mint::operator<(const Mint &rhs) const
{
    // This can be shortened:
//  if (sign != rhs.sign && sign == 0)
//  {
//      return false;
//  }
//  if (sign != rhs.sign && sign == 1)
//  {
//      return true;
//  }
    // Not same sign: Smaller when this is negative.
    if (sign != rhs.sign)
        return sign == 1;

    // Again this can be shortened.
    // This will always return false when this size is larger (sign not handled).
//  if (num.size() != rhs.num.size())
//  {
//      if (num.size() < rhs.num.size())
//	{
//	    if (sign == 0)
//	        return true;
//      }
//  }
    // Suggested:
    //  This size is smaller: Less if positive.
    if (num.size() < rhs.num.size())
        return sign == 0;
    //  This size is larger: Less if negative.
    if (num.size() > rhs.num.size())
        return sign == 1;

    // At this point sign and size are identical.
    // So there is no need to check the size again.
    // Also this code handles only positive numbers 
    //  (returns always false when sign == 1).
//  if (num.size() == rhs.num.size())
//  {
//      if (sign == 0)
//      {
//          int i = num.size() - 1;
//          while (i>=0)
//          {
//              if ( num[i] < rhs.num[i])
//              {
//                  return true;
//                  i = 0;
//              }
//              i--;
//          }
//      }
//  }
//  return false;
    // Suggested:
    for (int i = num.size() - 1; i>=0; i--)
    {
        if ( num[i] < rhs.num[i])
        {
            // This num is smaller: Less if positive.
            return sign == 0;
        }
        if ( num[i] > rhs.num[i])
        {
            // This num is larger: Less if negative.
            return sign == 1;
        }
    }
    // Identical.
    return false;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900