Question
The class largeIntegers is designed to process large integers of at most 100 digits. Using dynamic arrays, redesign this class so that integers of any
The class largeIntegers is designed to process large integers of at most 100 digits. Using dynamic arrays, redesign this class so that integers of any number of digits can be added or subtracted. Also overload the multiplication operator to multiply large integers.
largeIntegers.h
#pragma once
#include
using namespace std;
class largeIntegers
{
public:
largeIntegers();
largeIntegers(const string& snumbers);
largeIntegers add(const largeIntegers& addend);
largeIntegers sub(const largeIntegers& subtrahend);
void print() const;
private:
static const size_t size = 16;
int numbers[size + 1];
};
largeIntegers.cpp
#include
#include "largeIntegers.h"
using namespace std;
largeIntegers::largeIntegers()
{
for (size_t idx = 0; idx < size; ++idx)
numbers[idx] = 0;
}
largeIntegers::largeIntegers(const string& snumbers)
{
//size - 1 = sidx + nidx
for (size_t sidx = 0; sidx < snumbers.size(); sidx++)
{
size_t nidx = snumbers.size() - 1 - sidx;
numbers[nidx] = snumbers[sidx] - '0';
}
for (size_t nidx = snumbers.length(); nidx < size; ++nidx)
numbers[nidx] = 0;
}
largeIntegers largeIntegers::add(const largeIntegers& addend)
{
largeIntegers sum;
for (size_t idx = 0; idx < size; ++idx)
{
int littlesum = numbers[idx] + addend.numbers[idx];
sum.numbers[idx] = sum.numbers[idx] + littlesum % 10;
sum.numbers[idx + 1] = sum.numbers[idx + 1] + littlesum /
10;
}
return sum;
}
largeIntegers largeIntegers::sub(const largeIntegers& subtrahend)
{
largeIntegers difference;//sub works because difference is filled
with zeroes
for (size_t idx = 0; idx < size; ++idx)
{
difference.numbers[idx] = difference.numbers[idx] +
numbers[idx] - subtrahend.numbers[idx];
if (difference.numbers[idx] < 0)
{
difference.numbers[idx] =
difference.numbers[idx] + 10;
difference.numbers[idx + 1] = - 1;
}
}
return difference;
}
void largeIntegers::print() const
{
for(int idx = size - 1; idx >= 0; --idx)
cout << numbers[idx];
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started