BigInt Project

Pages: 12
I need help with my project for my c++ class. I am a little confused on how to get started. I am making a class called "bigint" and i have to have a global constant "size". How do I make that global "size" the size of bigint? If "size" is an array and "bigint" is an object how does that work? Heres the instructions for the program..

Requirements:
-You must use the class construct to implement your ADT.
-The ADT bigint need only work for positive numbers.
-Use a global constant value for the maximum size of the bigint that is, a constant sized array.

Implementation:
-The size of the bigint must be specified by a global constant
-A method (constructor) to initialize a bigint to zero.
-A method (constructor) to initialize a bigint to an int value you provide [0, maxint]. Example: bigint(128).
-A method (constructor) to initialize a bigint to a char[] you provide. You can assume what is provided is a valid bigint. Example: bigint("299793").
-A method to write a bigint that prints at most 60 digits per line.
-A method to compare if two bigints are equal. It should return a bool - true if equal and false otherwise.
1
2
3
4
5
const int SIZE = 10; //sum num

class BigInt
{
};
Right, but now how is BigInt in anyway related to the size?
The global constant just means that it needs to be defined in the .h file for this, and outside of a function. The person who answered before me has it laid out for you. The size is almost an arbitrary number in this case. In lab we picked 500, so that is just what I am sticking with for the time being.

1
2
3
4
5
6
7
8
9
10
11
12
13
// bigint.h
#ifndef bigint_h
#def bigint_h

const int size = 500;

class bigint{
public:
private:

}
#endif
Last edited on
So then in my class would I have something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef BIGINT_H
#define BIGINT_H

const int MAXINT = 500;

class bigint{
	public:
		bigint();
		bigint(int);
		bigint(char);
		void output(bigint);
		bool compare(bigint, bigint);

	private:
		int size[MAXINT];
};

#endif 
Everything but the output looks good. Remember that with the print you are not passing the bigint, you are passing ostream by ref and giving it a new short name.

void output (std :: ostream& out)

That's the example he gave in lecture.
Might as well write the classic std::ostream& operator<<(std::ostream&, const YourClass&) overload if you're going to pass a reference to std::ostream for output.
Oh yeah I remember that now. My mistake. Thank you for the help.
One other question. For the method constructor

bigint(int);

We have the user input a value (int) for bigint. But which element of the array do we put it in? I was confused at the point of making bigint an array if we really only need it to hold one value.
I am guessing that the array holds individual digits of the number, not as a whole.
So if the number was lets say "423". size[0] would hold "4" size [1] "2" and size [2] "3"?
Last edited on
Sounds like what I would do(have done). Though I don't want to mislead you if your assignment actually calls for something else. You should ask your instructor for clarification (or madmuse since he/she seems to be in your class).
Daleth has it. The array will hold the individual digits, which is how initializing it as a char works at all.
I thought we were initializing it is an int value?
You have to do both. Initializing it with both a char and int allows you to enter all the numbers that exist. You have the line of code for initializing it as a char up there already. You just need to write a method that converts it from chars to int values.
Actually it wants a char array not a char.

Basically it wants something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef BIGINT.H
#define BIGINT.H

const int MAZ_SIZE = 500; //Can be anything that would be a 500 digit..

class BigInt
{
    public:
        BigInt( void ); //default that sets it equal to 0
        BigInt( const int value ); //I like to name in constructor so you know whats in it
        BigInt( const char *value ); //takes the character array in
        friend std::ostream operator<<( std::ostream &stm , const BigInt &bigi ); //output
        friend bool operator==( const BigInt &rhs , const BigInt &lhs ); //compare
    private:
        int value[ MAX_SIZE ];
};



*on a side note..

You can do the .output() and compare() functions if you want

But they should look like
1
2
3
public:
    void output( void ); // output();
    bool compare( const BigInt &rhs ); //*this will be your left hand side. 


So for your compare it could be something like
1
2
3
4
5
bool BigInt::compare( const BigInt &rhs )
{
    return( value == rhs.value ); //this will be different based on how you store your value. If you do the array method which you are supposed to
     //you should use a loop
}


It would be silly to do something like

1
2
3
4
5
int main()
{
    BigInt bi1 , bi2;
    bi1.compare( bi1 , bi2 );
}


When you can do

1
2
3
4
5
int main()
{
    BigInt bi1 , bi2;
    bi1.compare( bi2 );
}
Last edited on
I cant seem to figure out the char function. How do we make it an array of characters if its an int array? I got my int constructor to work but I cant figure out this char one.. Can someone get me started?
All chars are ints of a specific value. This is determined by the ASCII:
http://www.asciitable.com/

So, to convert '1' to 1, you need to take away a certain value. Let's take away the ASCII value of '0', so that '0' - '0' == 0.
'1' - '0'
~ 49 - 48
~ 1

And now you have the int equivalent.

Some people like to write '1' - 48, but I think writing '1' - '0' makes it clear that you are converting a char to what it should be in int.
So if I had a user input '128' for example. How could I access the first char to be able to change it to an integer? I need to somehow convert '128' in char to 128 in int basically. For integers it's easier because I can do things like
1
2
3
4
5
6
7
	int numdigits = log10((double)digit) + 1;

	for (int i=numdigits-1; i >= 0; i--)
	{
		value[i] = digit%10;
		digit /= 10;
	}

But for characters that doesnt work obviously.
"128" is a string, not a char. Go through each character in the string and convert each one to an int. Hint: for loop
Pages: 12