Reputation: 43
I'm doing a project for school and where I need to create a bigint class and it has 4 requirements thus far.
1.) Write a method to write a bigint that prints at most 80 digits per line.
2.) Write a method to compare if two bigints are equal. It should return a bool.
3.) Write a method to initialize a bigint to an int value you provide [0, maxint].
4.) Write a method to initialize a bigint to a char[].
I think I have 2 and 3 correct, but I'm having trouble with comparing two bigints and I was hoping that someone could lead me in the right direction on how to limit print to 80 digits per line.
Here's my code so far:
.h file
class bigint
{
public:
bigint(); //default constructor
bool operator==(const bigint& num1);
bigint( int n);
bigint(char new_digits[]);
private:
int digit[MAX];
int digitb[MAX];
};
here is the implementation file:
#include "bigint.h"
#include<cassert>
#include<iostream>
//default constructor
bigint::bigint()
{
for (int i = 0; i < MAX; i++)
{
digit[i] = 0;
}
}
bigint::bigint( int n )
{
int i = 0;
while(n > 0)
{
digit[i] = n % 10;
n = n /10;
++i;
break;
}
for(i; i< MAX; ++i)
digit[i] = 0;
}
bool bigint::operator==(const bigint& num1)
{
for(int i = 0; i < MAX; i++)
{
if (num1.digit == num1.digit)
return true;
}
return false;
}
bigint::bigint(char new_digit[])
{
int i = 0;
//Reads the characters of numbers until it is ended by the null symbol
while(new_digit[i] != '\0')
++i;
--i;
//Converts the characters into int values and puts them in the digit array
while( i >= 0)
{
digit[i] = new_digit[i] - '0';
--i;
}
}
}
int main()
{
#include<iostream>
using namespace std;
using PROJECT_1::bigint;
bigint a(0);
assert(a == 0);
}
BTW, I'm not trying to get the answers for my homework that I just started an hour ago. I've been working on this all day and I finally gave in and asked for help.
Upvotes: 1
Views: 798
Reputation: 153929
There are a number of problems with your code. The most immediate one
has been pointed out: the expression on both sides of the ==
operator
is identical, so naturally the function returns true
. The most
idiomatic way of writing this function in C++ would be:
return std::equals(
std::begin( digit ), std::end( digit ), std::begin( num1.digit ) );
In professional code, I would consider anything else poor programming. In a sudent assignment, it's less clear, since one of the goals may be to learn how to write such things yourself, and you may not be allowed to use the standard algorithms in such cases. I'd still go for the same basic approach, however, using “iterators”, rather than indexes:
int const* current = std::begin( digit );
int const* other = std::begin( num1.digit );
int const* end = std::end( digit );
while ( current != end && *current == *other ) {
++ current;
++ other;
}
return current == end;
As for the rest of the code:
operator==
should definitely be const
; otherwise, even something as simple as myBigInt == 0
won't work.
In fact, operator==
should probably be a non-member. I like having
a member function isEqual
, and having operator==
(and
operator!=
) call it, but making it a friend
is a perfectly valid
option as well.
Not sure what digitb
is supposed to do.
You're constructor using int
isn't compatible with the one using
char[]
. You need to decide whether your internal representation is
big-endian or little-endian. Little-endian is probably easier for
the arithmetic operations, but it means you'll have to process the
digits in the reverse order in BigInt( char[] )
. In fact, you start
out as if you're going to process the characters in reverse order, but
you end up going backwards over both arrays, never initializing the end
of digit
, and not changing the order. (You also need to check that
each char
really is a digit—using the standard isdigit
function.)
In general, you should be using standard functions whenever they do the
job (and your assignment allows it). In BigInt::BigInt( char[] )
, for
example, new_digit + strlen( new_digit )
will give you an
“iterator” to the '\0'
. (Using an iterator to go
backwards is a bit trickier than going forwards, because you're not
allowed to decrement it beyond the front of the array. Something like:
const const* source = new_digits + strlen( new_digits );
while ( source != new_digits ) {
-- source;
// ...
}
works well, however.
Upvotes: 1
Reputation: 4676
Well I think your implementation of comparing is not correct (i maybe wrong because you haven't specified what is the condition of comparison, so it is my assumption) because:
You are comparing the two numbers in the same passed bigint, see this:
bool bigint::operator==(const bigint& num1)
{
for(int i = 0; i < MAX; i++)
{
if (num1.digit == num1.digit) // it'll be always true
return true;
}
return false;
}
So since this is homework, i am not giving exact solution, but for method, compare digit by digit and see this on operator overloading, it should help. Read, try and learn.
For the second question, you need to print 80 digits per line, so run a loop, from start to finish for all characters, and when loop counter reaches 80 (or 79 is you are starting from 0), output a newline character. This is one solution.
And mention requirements clearer next time.
Upvotes: 0
Reputation: 409196
The contents of the loop inside operator==
is not working. What you are doing is comparing the digit
array (which technically is a pointer) of num1
with the digit
array pointer of num1
. This will always be true. You should be comparing each index in this->digit
to the corresponding index in num1.digit
. something like this:
bool bigint::operator==(const bigint& num1)
{
for(int i = 0; i < MAX; i++)
{
if (digit[i] != num1.digit[i])
return false;
}
return true;
}
As you can see, I also changed the comparison from equal to not-equal. This is because otherwise if only the first digit is the same in both bigint
objects then using equal would have the function return true
after only checking the first digit.
Upvotes: 1