Creating bigint class

Hello,

I'm working on a class project and we're creating a bigint class from scratch, and i'm having trouble with the last two functions. Instructions for each method are below. Any help would be appreciated!

Functions i'm having trouble with:

1) A method, called output(std::ostream&), that takes a stream as input and writes a bigint to that stream. It should print at most 50 digits per line. I am not sure how to 1) eliminate the leading zeros, and 2) only print 50 digits per line.

2) A method to compare if two bigints (objects) are equal. It should return a bool - true if equal and false otherwise.


Here is my header file:

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

#include <iostream>

const int SIZE = 10;

class bigint{
 public:
  bigint();
  bigint(int);
  bigint(char *);
  void output(std::ostream&);
  bool compare(bigint);
 private:
  int number[SIZE];
};

#endif


Here are my function definitions:
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

#include "bigint.h"

bigint::bigint(){
    for (int i = 0; i < SIZE; ++i)
        number[i] = 0;
}

bigint::bigint(int value){
    for (int i = 0; i < SIZE; ++i)
        number[i] = 0;
    int count = 0;
    while (value > 0){
        number[count] = value % 10;
        value = value / 10;
        ++count;
    }
}

bigint::bigint(char* mychar){
    for (int i = 0; i < SIZE; ++i)
        number[i] = 0;
    int count = 0;
    while (mychar[count] != '\0'){
        ++count;
    }
    int i = 0;
    while (count >= 0){
        number[i] = mychar[count] - '0';
        ++i;
        --count;
    }
}

void bigint::output(std::ostream& out){
    for(int i = 0; i < SIZE; ++i){
        out << number[i];
    }
}

bool compare(bigint a, bigint b){
    
}

> eliminate the leading zeros
ignore them

> only print 50 digits per line.
1
2
3
4
if( digits_printed_in_this_line == 50 ){
   out << '\n';
   digits_printed_in_this_line = 0;
}
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
// I/O operator overloading
#include<iostream>
#include<string>
//using namespace std;

class TT{
public:
	std::string num;
	TT (std::string s) : num(s) {}
	friend std::ostream& operator<<(std::ostream &out, TT &arg);
};

std::ostream& operator<<(std::ostream &out, TT &arg){
	for(int i=0; i<arg.num.size(); i++){
		out << arg.num[i];
		if((i+1)%5 == 0) 
			out << "\n";  // 5 digits per line
	}
	return out;
}

int main(){
	
	TT a("123456789012345678");

	std::cout << a << std::endl;

return 0;	
}
From the constructors, it looks like number[0] is the least significant digit. So to print it out:
- start at number[SIZE-1] and go backwards through the digits until you find a non-zero one. Be sure to stop if you get to the beginning too.
- Now go from that digit to the beginning, printing digits.
- As you print them, see if you've printed 50 and insert a newline as per ne555's suggestion.

I have a few questions that you may want to ask yourself:
- How do you represent negative numbers? Is this "BigInt" or "BigUnsigned"?
- Each number represents only a single digit. That's pretty inefficient. Is that what you're supposed to do? You could represent 9 digits in a single 32 bit int.
dhayden - We don't have to deal with negative numbers in this assignment. Yes, each number is to represent a single digit per the instructions. This is just teaching us how to start to build a class for large numbers. I'm still very much stuck on the last two functions.
The compare() function should be easy. Just compare the number[] arrays to see if they are the same.

I'll get you started with the output function:
1
2
3
4
5
6
7
8
9
10
11
12
13
void bigint::output(std::ostream&os)
{
    int digit;
    for (digit = SIZE-1; digit >= 0; --digit) {
        if (number[digit]) break;
   }
   // digit is now the index of the most significant non-zero digit, or -1
   // If digit is -1 then print '0' and return. This is a special case for 0.
   // Otherwise execute a loop. At each step, print numbers[digit]
   // and decrement digit.
   // You will also want to keep track of how many digits you've printed
   // on this line. If it's 50 then print a new line and reset the counter.
}

const int SIZE = 10;
it doesn't look like big digit at all.

while, by unsigned long long int variables you can operate upto 19 digits.

and big digit variables should not have size limit, as they can be hundreds digits long (i.e. big factorials) . so, use std::string or std::vector to store the digits.
dhayden - So, after digit iterates backwards through the array, the if statement will be the flag checking for 50 digits? Something like this?

1
2
3
4
if (number[digit] == 50) {
out << number[digit];
}
break;


Regarding the compare function, this is what i've come up with but it doesn't feel right. It doesn't let me compare if (a == b).

1
2
3
4
5
6
7
8
9
10
11
bool bigint::compare(bigint a, bigint b){
    for (int i=0; i<SIZE; ++i){
        for (int j = 0; j < SIZE; ++j){
            a = number[i];
            b = number[j];
            if (number[i] == number[j])
                return true;
        }
    }
    return false;
}
Last edited on
Topic archived. No new replies allowed.