Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

largenum.h #include #include using namespace std; class LargeNum { // output number with a comma after ever 3 digits, // e.g. 1234567890 -> 1,234,567,890 friend

image text in transcribed

largenum.h

#include

#include

using namespace std;

class LargeNum {

// output number with a comma after ever 3 digits,

// e.g. 1234567890 -> 1,234,567,890

friend ostream &operator

private:

// Define private data members and methods here

public:

// default constructor from string

explicit LargeNum(const string &str = "0");

// constructor from int

explicit LargeNum(int anInteger);

// use the default copy constructor

LargeNum(const LargeNum &other) = default;

// use the default copy assignment operator

LargeNum &operator=(const LargeNum &other) = default;

// use the default destructor

~LargeNum() = default;

// returns true if the number is zero

bool isZero() const;

// negate the number, positive becomes negative, negative becomes positive

// Zero is always positive

LargeNum &negate();

// add two numbers

LargeNum operator+(const LargeNum &rhs) const;

// subtract two numbers

LargeNum operator-(const LargeNum &rhs) const;

// multiply two numbers

LargeNum operator*(const LargeNum &rhs) const;

// divide two numbers. rhs is the divisor

// similar to integer division, ignore remainder

LargeNum operator/(const LargeNum &rhs) const;

// return true if the numbers are equal

bool operator==(const LargeNum &rhs) const;

// return true if the numbers are not equal

bool operator!=(const LargeNum &rhs) const;

// return true if the left-hand-side number is less than the

// right-hand-side number

bool operator

// return true if the left-hand-side number is greater than the

// right-hand-side number

bool operator>(const LargeNum &rhs) const;

// return true if the left-hand-side number is less than or equal to the

// right-hand-side number

bool operator

// return true if the left-hand-side number is greater than or equal to the

// right-hand-side number

bool operator>=(const LargeNum &rhs) const;

// prefix increment

LargeNum &operator++();

// postfix increment

LargeNum operator++(int);

// prefix decrement

LargeNum &operator--();

// postfix decrement

LargeNum operator--(int);

};

main.cpp

#include "largenum.h"

#include

#include

#include

using namespace std;

// check printing and addition

void test1() {

// stringstream strs;

// LargeNum num0("1234567890123456789");

// strs

// assert(strs.str() == "1,234,567,890,123,456,789");

// strs.str("");

// LargeNum num1(12345);

// strs

// assert(strs.str() == "12,345");

// strs.str("");

// LargeNum num2(11115);

// LargeNum num3 = num1 + num2;

// strs

// assert(strs.str() == "23,460");

// strs.str("");

// LargeNum num4(99);

// LargeNum num5 = num1 + num4;

// strs

// assert(strs.str() == "12,444");

// strs.str("");

// LargeNum num6(99000);

// LargeNum num7 = num1 + num6;

// strs

// assert(strs.str() == "111,345");

// cout

}

// check comparator operators

void test2() {

// assert(LargeNum(99) == LargeNum(99) && LargeNum(99) != LargeNum(100));

// assert(LargeNum(99) > LargeNum(7) && LargeNum(7)

// assert(LargeNum(105) >= LargeNum(100) && LargeNum(100)

// assert(LargeNum(99) > LargeNum(-7) && LargeNum(-7)

// assert(LargeNum(-5) > LargeNum(-10) && LargeNum(-10)

// assert(!(LargeNum(5) > LargeNum(5)));

// cout

}

// check negative numbers

void test3() {

// stringstream strs;

// LargeNum num0("-1234567890123456789");

// strs

// assert(strs.str() == "-1,234,567,890,123,456,789");

// strs.str("");

// LargeNum num1(-12345);

// strs

// assert(strs.str() == "-12,345");

// strs.str("");

// LargeNum numZero(-0000);

// strs

// assert(strs.str() == "0");

// assert(LargeNum(0).isZero() && LargeNum(-0).isZero());

// assert(LargeNum(0) == LargeNum(-0));

// assert(LargeNum(0).negate() == LargeNum(-0));

// assert(LargeNum(100) - LargeNum(100) == LargeNum(0));

// assert(LargeNum(100) + LargeNum(-100) == LargeNum(0));

// assert(LargeNum(100) - LargeNum(-100) == LargeNum(200));

// assert(LargeNum(-100) - LargeNum(-100) == LargeNum(0));

// assert(LargeNum(-100) + LargeNum(100) == LargeNum(0));

// assert(LargeNum(100) - LargeNum(7) == LargeNum(93));

// assert(LargeNum(7) - LargeNum(100) == LargeNum(-93));

// assert(LargeNum(-7) + LargeNum(-100) == LargeNum(-107));

// cout

}

// check multiplication

void test4() {

// assert(LargeNum(25) * LargeNum(0) == LargeNum(0));

// assert(LargeNum(25) * LargeNum(5) == LargeNum(125));

// assert(LargeNum(-25) * LargeNum(-5) == LargeNum(125));

// assert(LargeNum(-25) * LargeNum(5) == LargeNum(-125));

// assert(LargeNum(25) * LargeNum(-5) == LargeNum(-125));

// assert(LargeNum("123456789123456789") * LargeNum("123456789123456789") ==

// LargeNum("15241578780673678515622620750190521"));

// cout

}

// check division

// use small numbers to avoid excessive computation

void test5() {

// assert(LargeNum(0) / LargeNum(5) == LargeNum(0));

// assert(LargeNum(25) / LargeNum(5) == LargeNum(5));

// assert(LargeNum(-25) / LargeNum(-5) == LargeNum(5));

// assert(LargeNum(-25) / LargeNum(5) == LargeNum(-5));

// assert(LargeNum(25) / LargeNum(-5) == LargeNum(-5));

// assert(LargeNum("25") / LargeNum("7") == LargeNum("3"));

// assert(LargeNum("7") / LargeNum("25") == LargeNum("0"));

// cout

}

// check prefix and postfix operators

void test6() {

// LargeNum num0(10);

// assert(num0++ == LargeNum(10));

// assert(num0 == LargeNum(11));

// assert(--num0 == LargeNum(10));

// assert(num0-- == LargeNum(10));

// assert(num0 == LargeNum(9));

// assert(++num0 == LargeNum(10));

// assert(num0-- == LargeNum(10));

// assert(num0 == LargeNum(9));

// num0.negate();

// assert(++num0 == LargeNum(-8));

// assert(--num0 == LargeNum(-9));

// LargeNum num1(1);

// assert(--num1 == LargeNum(0));

// assert(--num1 == LargeNum(-1));

// assert(++num1 == LargeNum(0));

// cout

}

// run all tests

int main() {

test1();

test2();

test3();

test4();

test5();

test6();

cout

}

largenum.cpp

#include "largenum.h"

#include

#include

#include

using namespace std;

// Your code goes here

Design and implement LargeNum class which can be used to represent arbitrarily large numbers and operations on them. Accept the assignment and create your own private GitHub repository with the started code by clicking on this link .. Do not change the repository name or make it public. file is provided for you. You must implement all the public functions that have been defined. file includes a series of tests to help guide your design and development. Design Choices You have several design choices. Most important choice is how will you store the digits internally. One possibility is using class. Strings can be as long as needed, provide easy access to each index, have and functions to expand and shrink as needed. You may also want to store the digits in reverse order internally so index-0 of two large numbers correspond to the ones unit. For example, storing 12345 and 789 internally as "54321" and "987" means one's digit is at index-0 corresponding to 5 and 9 respectively which will make it easier when adding them. How do we differentiate between positive and negative numbers? I suggest having a boolean variable, that is set to by default. If the number is negated, the variable can be flipped. The number zero is a special case. We will define zero to be a positive number and make sure it is always treated as a positive number. I suggest starting the assignment by commenting almost everything and completing the minimum functions necessary for passing test1. Once your program successfully completes test1, you can then proceed to satisfy additional tests. This is similar to test-driven development, but in this case the tests are provided for you

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

Graph Databases New Opportunities For Connected Data

Authors: Ian Robinson, Jim Webber, Emil Eifrem

2nd Edition

1491930896, 978-1491930892

More Books

Students also viewed these Databases questions

Question

15. Identify the rescue from without in The Empire Strikes Back.

Answered: 1 week ago