Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Advances In Spatial And Temporal Databases 11th International Symposium Sstd 2009 Aalborg Denmark July 8 10 2009 Proceedings Lncs 5644

Authors: Nikos Mamoulis ,Thomas Seidl ,Kristian Torp ,Ira Assent

2009th Edition

3642029817, 978-3642029813

More Books

Students also viewed these Databases questions