Bigint Class Problem

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

private:
int digits[CAPACITY];


};


#endif /* BIGINT_H */

##############################################################################

Implication File:


//////////////////////////////CONSTRUCTORS/////////////////////////////////////
bigint::bigint()
{
for (int i = 0; i < CAPACITY; ++i)
{
digits[i] = 0;
}

}

//------------------------------------------------------------------------------

bigint::bigint(int initial_num)
{
// initialize 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;
}

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;

}

##############################################################################

Main File:

#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;
}






Last edited on
s that all the code? What you\ve posted so far looks ok, but there must be more.
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.


Header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#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
    bigint(char initial_digits[]);
    //Postcondition: Initializes the digits array to a char value
    bool operator==(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);

    int operator[](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 */ 


######################################################################

Implication File:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#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])
            return false;
    }

    return true;
}

//------------------------------------------------------------------------

 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])
            return false;

    // The remaining digits should be 0
    for (  ; idx < CAPACITY; ++idx)
        if (digits[idx] != 0)
            return false;

    // Correspoinding digits all the same
    return true;
}

//-----------------------------------------------------------------------------

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];
}



#####################################################################

Main File:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

#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.


Last edited on
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.

Why is display() different from operator<< ?
Topic archived. No new replies allowed.