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.
#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){
}
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.
}
constint SIZE = 10;
it doesn't look like big digit at all.
while, by unsignedlonglongint 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.