Ok so I have to do the classic bigint class for school. I have quite a few problems but I will focus on parts of the code that I'm most concerned about. The way he have to implement the class is using arrays, and are not allowed to use vectors(if anyone should suggest this). So I have my class setup in in three files: header, implication, and main. My main problem is that my int constructor doesn't work the way it should. It will accept and display any integer that goes in it if its less than 10 digits, but anything more it gives me a random number which defeats the purpose of the class. Any tips, tricks w/e would be helpful
Header File:
#ifndef BIGINT_H
#define BIGINT_H
const int CAPACITY = 200;
class bigint
{
public:
bigint();
//Postcondition: Sets the value of the digits array to 0
bigint(int initial_num);
//Postcondition: Initializes the digits array to an int value
friend std::ostream& operator <<(std::ostream& out, const bigint& a);
//Postcondition: Displays the contents of bigint to an output device
//Makes sure that the remaining elements of the array is set to 0
for( ; i< CAPACITY; ++i)
digits[i] = 0;
}
std::ostream& operator <<(std::ostream& out, const bigint& a)
{
//Postcondition: Displays the contents of bigint to an output device
//Friend of: bigint class
int i;
for(i = CAPACITY - 1; i >= 0 && a.digits[i] == 0; i--)
{
//Empty body
}
Ya I do but i figured this would suffice but if not here is my entire code:
Also note that this is a work in progress so i have declared some functions/operators in here but have not defined them yet.
#ifndef BIGINT_H
#define BIGINT_H
constint CAPACITY = 200;
class bigint
{
public:
bigint();
//Postcondition: Sets the value of the digits array to 0
bigint(int initial_num);
//Postcondition: Initializes the digits array to an int value
bigint(char initial_digits[]);
//Postcondition: Initializes the digits array to a char value
booloperator==(const bigint& numbers) const;
//Postcondition: Compares two bigint values to see if they are equal
bigint operator *(const bigint& a) const;
//Postcondition: Multiplys two bigint numbers together
bigint operator +(bigint a);
//Postcondition: Adds two bigint numbers together
void Times_10(int power);
int& operator[](int i);
intoperator[](int i) const;
friend std::istream& operator >>(std::istream& in, bigint& a);
//Postcondition: Reads the contents of input into bigint
friend std::ostream& operator <<(std::ostream& out, const bigint& a);
//Postcondition: Displays the contents of bigint to an output device
void display();
bool utest(int given_digits[], int size);
private:
int digits[CAPACITY];
};
#endif /* BIGINT_H */
#include <iostream>
#include <cassert>
#include "bigint_test.h"
//////////////////////////////CONSTRUCTORS/////////////////////////////////////
bigint::bigint()
{
for (int i = 0; i < CAPACITY; ++i)
{
digits[i] = 0;
}
}
//------------------------------------------------------------------------------
bigint::bigint(int initial_num)
{
// intialize i to 0
int i = 0;
//Inputs the individual numbers given to bigint into the digits array's elements
while(initial_num > 0)
{
digits[i] = initial_num%10;
initial_num = initial_num/10;
++i;
}
//Makes sure that the remaining elements of the array is set to 0
for( ; i< CAPACITY; ++i)
digits[i] = 0;
}
//-----------------------------------------------------------------------------
bigint::bigint(char initial_digits[])
{
int i = 0;
//Reads the characters of numbers until it is ended by the null symbol
while(initial_digits[i] != '\0')
++i;
--i;
//Converts the characters into int values and puts them in the digit array
while( i >= 0)
{
digits[i] = initial_digits[i] - '0';
--i;
}
}
///////////////////////////OVERLOADED OPERATORS/////////////////////////////////
bool bigint::operator ==( const bigint& numbers) const
{
//Returns true if the two ojects equal each other
for(int i = 0; i < CAPACITY; ++i)
{
if (digits[i] != digits[i])
returnfalse;
}
returntrue;
}
//------------------------------------------------------------------------
int& bigint::operator[](int i)
{
assert(0 <= i && i < CAPACITY);
return digits[i];
}
//-------------------------------------------------------------------------
int bigint::operator[](int i) const
{
assert(0 <= i && i < CAPACITY);
return digits[i];
}
//---------------------------------------------------------------------
bigint bigint::operator +(bigint a)
{
bigint temp;
int num = 0;
int carry = 0;
for(int i = 0; i < CAPACITY; i++)
{
num = digits[i] + a.digits[i] + carry;
if(num >= 10)
{
num = num - 10;
carry = 1;
}
temp.digits[i] = num;
}
return temp;
}
//------------------------------------------------------------------------
std::ostream& operator <<(std::ostream& out, const bigint& a)
{
//Postcondition: Displays the contents of bigint to an output device
//Friend of: bigint class
int i;
for(i = CAPACITY - 1; i >= 0 && a.digits[i] == 0; i--)
{
//Empty body
}
for( ; i >= 0; i--)
out << a.digits[i];
return out;
}
//----------------------------------------------------------------------------
std::istream& operator >>(std::istream& in, bigint& a)
{
//Postcondition: Reads char until whitespace
//Friend of: bigint class
for(int i = 0; i < CAPACITY; ++i)
{
in >> a.digits[i];
break;
}
return in;
}
////////////////////////MEMBER FUNCTIONS///////////////////////////////////////
bool bigint::utest(int given_digits[], int size)
{
// For both loops, second picks up where first left off
int idx = 0;
// Compare corresponding digits with given
for ( ; idx < size; ++idx)
if (digits[idx] != given_digits[idx])
returnfalse;
// The remaining digits should be 0
for ( ; idx < CAPACITY; ++idx)
if (digits[idx] != 0)
returnfalse;
// Correspoinding digits all the same
returntrue;
}
//-----------------------------------------------------------------------------
void bigint::display()
{
int i = 0;
for(i = CAPACITY - 1; i >= 0 && digits[i] == 0; i--)
{
//Do nothing
}
for( ; i >= 0; i--)
{
std::cout << digits[i];
if(i % 80 == 0 && i!= 0)
std::cout << "\n";
}
}
//----------------------------------------------------------------------------
void bigint::Times_10(int power)
{
if(!power)
return;
for(int i = CAPACITY - 1; i >= power; --i)
digits[i]=digits[i-power];
}
#include <iostream>
#include <cassert>
#include <cstdlib>
#include <fstream>
#include "bigint_test.h"
using std::cout;
using std::cin;
int main()
{
bigint object1(457), object2(474357878545);
cout << "Object1 equals " << object1 << std::endl; //Works fine
cout << "Object2 equals " << object2 << std::endl;
//Get random numbers
}
return 0;
}
While we are here I guess I could just list some more problems that I have with my program and if anyone is willing to pick and choose which ones they would like to help me with that would be great:
1:For my overloading of my "+" operator, whenever I add two numbers together and run it, it gives me all leading 1's until it hits my number which is correct. I know it's due to the carry variable but I don't understand where it goes wrong.
2:We have to overload the * operator for multiplication. We have to multiply two bigints together and also multiply for a single digit number. I am totally lost on even how to start it. This isn't a problem with code obviously but if anyone could give me a general pseudocode for solving it, that would be great.
Take another look at the equality operator, it doesn't check the input parameter.
operator+ doesn't reset the carry.
You're bigint class is processing a BCD (binary coded decimal), so each element is a digit. You could implement them as chars, but you've gone for int. But you need to remember that when you do i/o. So double check your stream in and out methods.
Double check your Times_10 method, you've missed a spot.