BigInteger Class

This is my code so far. I was wondering if anyone would offer any advice on what I have or what I should do. I'm trying to get two numbers longer than long long int and add them together and multiply. I'm not sure how to convert the numbers to a string.

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
#ifndef BIG_INTEGER_H
#define BIG_INTEGER_H

#include "BigIntegerException.h"

class BigInteger {
private:
	int biginteger[500];
	int bigintegertwo[500];

public:
	// default constructor
	BigInteger(){
		string biginteger = "0";
	}

	// constructor with one string argument
	BigInteger(string value) throw (NumberFormatException);

	// copy constructor
	BigInteger(const BigInteger& bigInt){
		for (int i = 0; i < sizeof(bigInt); i++){
			bigintegertwo[i] = bigInt.biginteger[i];
		}
	}

	// assignment operator
	BigInteger& operator=(const BigInteger& bigInt){
		for (int i = 0; i < sizeof(bigInt); i++){
			bigintegertwo[i] = bigInt.biginteger[i];
		}
		return *this;
	}

	// destructor
	~BigInteger();

	// print the number without leading zero
	string toString();

	// addition operator
	friend BigInteger operator+(const BigInteger& leftNum, const BigInteger& rightNum);

	// multiplication operator
	friend BigInteger operator*(const BigInteger& leftNum, const BigInteger& rightNum);
};

#endif
1
2
3
BigInteger(){
	string biginteger = "0";//Does nothing
}


BigInteger(string value) throw (NumberFormatException); — missing body and why do you have a constructor which only throws?

copy and assigment: thy are you copying to second array from first? What If I made copy of copy?

How are numbers actually stored?
OP,
Here are some suggestions as to storing the number.
* Store a decimal digit in one char variable.
This is the easiest way , totally inefficient but if you want to do very few calculations and don't want to use a library , go for this way.
Arithematics is much easier this way , just add , subtract the corresponding digits with carry.
Multiplication can be done just like the way we are taught in schools.

  42
x 21
-----
  42
 81X
-----
 852

Converting to a string is straightforward , for each digit , add char('0'+digit) to the string.

* Store ⌊log10(28*sizeof(std::size_t))⌋ digits in one std::size_t variable.
In this format , you store the maximum possible number of digits in a word.
e.g. if sizeof(size_t) is 4 , you store ⌊log10(28*4)⌋ = 9 digits per word.
Operations similar to above but we have a word at a time than digit.
When you convert to string , add each digit from a word by taking char('0'+ word (mod 10) ) , then divide by 10 in each iteration. But , remember to add padding zeroes.

* Store in binary format.
This is the way libraries like GMP et al. store numbers internally.Not Recommended.
@a k n first of all 8 in your formula is technically wrong. Use CHAR_BIT instead.
* Store in binary format.
This is the way libraries like GMP et al. store numbers internally.Not Recommended.
That is funny, because it is the most efficient and easy to implement method.
And you suggest method which has all problems of binary with none of the bonuses.


Alternative to a char-driven approach is to store one digit per 4 bits: twice as compact as straightforward char approach and just slightly slower.
MiiNiPaa wrote:
first of all 8 in your formula is technically wrong. Use CHAR_BIT instead.

Well, Thank you , I didn't knew that macro existed.

MiiNiPaa wrote:

That is funny, because it is the most efficient and easy to implement method.
And you suggest method which has all problems of binary with none of the bonuses.

Yes , you are right but I thought for a beginner handling decimal would be easier ( and more intuitive ) than binary.
Last edited on
Topic archived. No new replies allowed.