Question #24247

Programming Exercise 11 in Chapter 9 explains how to add large integers using

arrays. However, in that exercise, the program could add only integers of, at

most, 20 digits. This chapter explains how to work with dynamic integers.

Design a class named largeIntegers such that an object of this class can

store an integer of any number of digits. Add operations to add, subtract,

multiply, and compare integers stored in two objects. Also add constructors

to properly initialize objects and functions to set, retrieve, and print the values

of objects.

arrays. However, in that exercise, the program could add only integers of, at

most, 20 digits. This chapter explains how to work with dynamic integers.

Design a class named largeIntegers such that an object of this class can

store an integer of any number of digits. Add operations to add, subtract,

multiply, and compare integers stored in two objects. Also add constructors

to properly initialize objects and functions to set, retrieve, and print the values

of objects.

Expert's answer

#include <conio.h>

#include <string>

#include <iostream>

using namespace std;

class LargeInteger

{

private:

string number;

int char2digit(char ch)

{

return ch - 48;

}

char digit2char(int digit)

{

return digit + 48;

}

void fillNumber(string& number, int width)

{

int delta = width - number.size();

for (int c = 0; c < delta; c++)

number.insert(0, "0");

}

void normalizeNumber(string& number)

{

while ((*number.begin() == '0') && (number.size() > 1))

number.erase(number.begin());

}

public:

LargeInteger(string number)

{

this->number = number;

}

LargeInteger operator + (LargeInteger& integer)

{

string a = this->number, b = integer.getNumber();

int width = (a.size() > b.size()) ? a.size() : b.size();

width++;

fillNumber(a, width);

fillNumber(b, width);

string result(width, '0');

int rest = 0;

int tmp;

for (int d = width - 1; d >= 0; d--)

{

tmp = char2digit(a[d]) + char2digit(b[d]) + rest;

result[d] = digit2char(tmp % 10);

rest = (tmp >= 10) ? 1 : 0;

}

normalizeNumber(result);

return LargeInteger(result);

}

LargeInteger operator * (LargeInteger& integer);

LargeInteger operator - (LargeInteger& integer);

string getNumber()

{

return this->number;

}

void print()

{

cout << number << endl;

}

};

int main()

{

LargeInteger a("5890897398729866897045987689796745608754");

LargeInteger b("2346579086098739646987349632476596898635");

cout << "a = ";

a.print();

cout << "b = ";

b.print();

cout << "a + b = ";

(a + b).print();

cout << "a - b = ";

(a - b).print();

cout << "a * b = ";

(a * b).print();

_getch();

return 0;

}

#include <string>

#include <iostream>

using namespace std;

class LargeInteger

{

private:

string number;

int char2digit(char ch)

{

return ch - 48;

}

char digit2char(int digit)

{

return digit + 48;

}

void fillNumber(string& number, int width)

{

int delta = width - number.size();

for (int c = 0; c < delta; c++)

number.insert(0, "0");

}

void normalizeNumber(string& number)

{

while ((*number.begin() == '0') && (number.size() > 1))

number.erase(number.begin());

}

public:

LargeInteger(string number)

{

this->number = number;

}

LargeInteger operator + (LargeInteger& integer)

{

string a = this->number, b = integer.getNumber();

int width = (a.size() > b.size()) ? a.size() : b.size();

width++;

fillNumber(a, width);

fillNumber(b, width);

string result(width, '0');

int rest = 0;

int tmp;

for (int d = width - 1; d >= 0; d--)

{

tmp = char2digit(a[d]) + char2digit(b[d]) + rest;

result[d] = digit2char(tmp % 10);

rest = (tmp >= 10) ? 1 : 0;

}

normalizeNumber(result);

return LargeInteger(result);

}

LargeInteger operator * (LargeInteger& integer);

LargeInteger operator - (LargeInteger& integer);

string getNumber()

{

return this->number;

}

void print()

{

cout << number << endl;

}

};

int main()

{

LargeInteger a("5890897398729866897045987689796745608754");

LargeInteger b("2346579086098739646987349632476596898635");

cout << "a = ";

a.print();

cout << "b = ";

b.print();

cout << "a + b = ";

(a + b).print();

cout << "a - b = ";

(a - b).print();

cout << "a * b = ";

(a * b).print();

_getch();

return 0;

}

## Comments

## Leave a comment