Вы находитесь на странице: 1из 5

COMPSCI 220 Fall 2017

Assignment 4: Operator Overloading and Custom Classes

Out: 3 October, 2017


Due: 10 October, 2017

General Instructions For All Assignments

Assignments in COMPSCI 220 are to be programmed in C++11, using the virtual machine image provided
for the class. All assignments must follow the instructions on the handouts exactly, including instructions
on input and output formatting, use of language features, libraries, file names, and function declarations.
Failure to do so will automatically result in docked points. All assignments are individual: you must not
discuss assignment contents, solutions, or techniques with fellow students. Please review the course policies
on the course website, https://people.cs.umass.edu/~joydeepb/220R.

Instructions For This Assignment

In this assignment, you will be implementing mathematical operations for integer numbers of arbitrary
length. Your code must be written in the file assignment4.cpp, and it must be accompanied by a header
file assignment4.h. You are not allowed to use any library other than the Google Testing library, vector,
string, and iostream.

1 Arbitrary Precision Arithmetic

Fundamental numeric data types in C++ have a finite range of values they can store due to the finite number
of bits allocated to store them. However, we often find the need to use numbers of larger magnitudes. Values
that exceed the range of the data type undergo truncation and can cause incorrect results. To avoid this,
custom data types can be defined that allow numbers of arbitrarily large size. These are often constructed
by combining operations on smaller, more manageable numbers.

2 Code Implementation

You must implement a class IntUnlimited that can store and perform basic mathematical operations (ad-
dition, subtraction, multiplication and division) on integers of arbitrary length. You must overload the
operators as described in the Lecture 6 notes for the following 8 operations:

1
2.1 Initialization

IntUnlimited variables may be assigned values in the following ways:

2.1.1 From Strings

A string containing the digits of a number in the order of their magnitude can be used. This may be using
a constructor that accepts a single std::string as a parameter or by using the assignment (=) operator
where the left operand is an IntUnlimited and the right operand is an std::string.

IntUnlimited a("12345678901");
IntUnlimited b = "12345678901";
IntUnlimited c = "123";
// c holds the value corresponding to One Hundred and Twenty Three.

2.1.2 From int

The assignment operator must be able to assign the integer value to an IntUnlimited using an int as the
right operand.

int b = -32767;
IntUnlimited a = b;
IntUnlimited c = 5;

2.1.3 From other IntUnlimited

You must implement an assignment operator that accepts an IntUnlimited as the right operand and assigns
the same value to the IntUnlimited on the left. You must also implement a copy constructor that accepts
a single IntUnlimited as a parameter.

IntUnlimited a = "12345678901";
IntUnlimited b = a;
IntUnlimited c(a);

2
2.2 Mathematical Operator Overloading

You must also overload the following operators for IntUnlimited:

2.2.1 Addition

The + operator must perform arithmetic addition of two operands. At least one operand will be of the type
IntUnlimited while the other may be an IntUnlimited or an int. This operator must return a copy of the
resultant IntUnlimited.

IntUnlimited a = "12345678901";
IntUnlimited b = "10987654321";
IntUnlimited c = a + b;
int x = 5;
IntUnlimited d = c + x;
IntUnlimited e = x + c;

2.2.2 Subtraction

The - operator must perform arithmetic subtraction of two operands. At least one operand will be of the
type IntUnlimited while the other may be an IntUnlimited or an int. This operator must return a copy
of the resultant IntUnlimited.

IntUnlimited a = "12345678901";
IntUnlimited b = "10987654321";
IntUnlimited c = a - b;
int x = 5;
IntUnlimited d = c - x;
IntUnlimited e = x - c;

2.2.3 Multiplication

Similar to the above, the * operator must return a copy of the IntUnlimited value of the result of multi-
plication of two operands. Either operand may be an IntUnlimited while the other may be an int or an
IntUnlimited

3
2.2.4 Division

Similar to the above, the / operator must return a copy of the IntUnlimited value of the result of integer
division of two operands. You must return the integer quotient and discard the remainder. Either operand
may be an IntUnlimited while the other may be wither an int or an IntUnlimited. In case of the divisor
is 0, the string equivalent (see Section 2.3) of the result must be a string having the value NaN.

2.3 String Conversion

IntUnlimited must allow for conversion back to std::string. It is critical that you perform this task. We
will use this to validate the results of the operations during evaluation.

IntUnlimited a = "12345678901";
std::string b = a;

3 Code Robustness and Versatility

You will have to think about how to design your program to store and operate large numbers while main-
taining the significance of the digits. In particular, your code should be able to handle an IntUnlimited of
any length.

4 Example Output

To assist with the development of this assignment, we have provided you with the file testing_main.cpp, to
compile with your code and header file. To compile your code with testing_main.cpp and create a program
called run_tests, run:

1 clang++ -std=c++11 assignment4.cpp testing_main.cpp -lpthread -lgtest -o run_tests

Important: Note that the order of the parameters matters to the compiler because it resolves dependencies
from left to right. testing_main.cpp is provided to give you an idea of the type of main file we will use for
compilation and grading of your submission. The unit tests are not sufficient to test for correctness: it is
up to you to ensure that you write adequate additional test cases to verify correctness. However, a correct
submission must pass the provided test cases.

4
5 What To Turn In

Using the provided submission script submit.sh, the files assignment4.h and assignment4.cpp will be
checked to see if they compile, and if successful, will be added to an archive, assignment4.tar.gz. You
must upload this archive to Moodle.

Вам также может понравиться