Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C++, the largest unsigned int value is 123645678998. So, an integer value larger than this cannot be stored and processed as an integer. Similarly,

In C++, the largest unsigned int value is 123645678998. So, an integer value larger than this cannot be stored and processed as an integer. Similarly, if the sum or product of two unsigned integers is greater than 123645678998, the result will be incorrect. One way to store and manipulate large integers is to store each individual digit of the number in an Array container. Write a program that inputs two unsigned integers of at most 20 digits and outputs the sum of the numbers.

Program requirements:

Extend the Array class (Array.h) using protected inheritance to create a new class, BigInteger, which can store a range of values from [0..99'999'999'999'999'999'99910]. The derived class should have the following operations:

A parameterized constructor that accepts a std::string containing only digit characters as input and converts it into a BigInteger value. This function makes two assertions:

1) that the number of digits in the parameter string does not exceed capacity,

2) only digit characters exist in the parameter string.

For example:

const BigInteger ONE("1"); // stores 1 BigInteger value("42"); // stores 42 BigInteger oops("abc"); // No! assertion fails std::string buffer; cin >> buffer; BigInteger number(buffer);

A parameterized constructor that accepts an unsigned long long and converts it into a BigInteger value.

// for example BigInteger value(98912121582662265113ULL);

A void member function add() that adds another BigInteger value to the current object. The current object will contain the sum of its original value plus the value of the other BigInteger, as though we had used compound addition assignment, e.g., number += other.

// for example const BigInteger ONE("1"); // one = 1 BigInteger sum; // sum == 0 sum.add(ONE); // sum == 1 sum.add(ONE); // sum == 2 sum.add(BigInteger("38")); // sum == 40

A value-returning member function, to_string(), that returns a std::string representation of the BigInteger value with the digits in the correct order.

// For example BigInteger value("38"); cout << value.to_string(); // prints "38"

My Array.h

class Array { public: // type aliases using value_type = int; using size_type = std::size_t; using reference = value_type&; using const_reference = const value_type&; static const size_type CAPACITY = 20; // constructors Array(); Array(const value_type source[], size_type count); void fill(value_type value); // element access reference at(size_type pos); const_reference at(size_type pos) const; reference front(); const_reference front() const; reference back(); const_reference back() const; private: value_type data[CAPACITY]; }; // Array bool equal(const Array& lhs, const Array& rhs); void print(const Array& array, Array::size_type first = 0, Array::size_type last = Array::CAPACITY);

My Biginteger:

class BigInteger : protected Array { public: /// Default constructor BigInteger() : Array() {} /// Constructs a BigInteger value from string BigInteger(const std::string& digits); /// Constructs from an unsigned long long value. BigInteger(unsigned long long value); /// Add another BigInteger value to this one void add(const BigInteger& other); /// Converts a BigInteger value to a std::string. std::string to_string() const;

Please anyone can help me write Biginteger.cpp. I'm stuck and want to see how do you guys do it. Thanks

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions