Problem: “Big” Integer Arithmetic This problem asks you to create a custom data
ID: 3829133 • Letter: P
Question
Problem: “Big” Integer Arithmetic
This problem asks you to create a custom data type to handle large integer arithmetic, by creating a custom C++ class called BigInt. C++ has a number of types to handle integer data, including ints, short ints and long ints, and their signed and unsigned variants. However, the maximum positive number that one can store in the largest of these, which is an unsigned long int, is 18,446,744,073,709,551,615. This number, as you can see, has 20 digits. In many scientific and engineering applications, this does not suffice to hold values required for complex computations. Anything about 1020 would be too big to hold in the unsigned long int type. So your task in this assignment is to create a C++ class called BigInt which will allow basic arithmetic operations on positive integer values of up to 300 digits, storing integers from 0 to 9999 ...9 (300 9s). The three operations you have to do are addition, subtraction and multiplication. Specific tasks are these:
1. Add two BigInt objects, and store the result in another BigInt, using the + operator.
2. Add an int to a BigInt object, and store the result in another BigInt, also using the +operator.
3. Subtract one BigInt object from another, and store the result in another BigInt, using the –operator. The second BigInt can always be smaller or equal to the first BigInt. In other words, the results shall never be negative.
4. Subtract an int from a BigInt object, and store the result in another BigInt, also using the –operator. Should be obvious in this case that the results shall never be negative.
5. Multiply two BigInt objects, and store the result in another BigInt, using the * operator. The resulting BigInt shall not exceed 300 digits.
6. Multiply a BigInt object with an int, and store the result in another BigInt, using the * operator. Again, the resulting BigInt shall not exceed 300 digits.
7. Overload the >> and << operators to input and output BigInt objects to streams, respectively. Of course, they have to be overloaded as friend functions.
To start, here’s a bare skeleton of the BigInt class. You are welcome to start with this, but do not have to. As a hint, you have to remember your basic school-level arithmetic, and implement this in a C++ class to solve this problem.
// Complete this below
class BigInt {
private:
int digits[300];
...
public:
BigInt();
BigInt( string value );
...
BigInt operator+(BigInt b);
BigInt operator-(BigInt b);
BigInt operator*(BigInt b);
friend ostream& operator<<(ostream &os, BigInt b);
...
};
The program will run as follows:
1. Ask the user to input two BigInts.
2. Output the results of addition, subtraction, and multiplication of these two BigInts
3. Then, ask the user to input a BigInt and an integers
4. Output the results of addition, subtraction, and multiplication of the BigInt and int
See the example output below.
Example (user input is bold):
Enter the first BigInt:
222222222222222222222222222222
Enter the second BigInt:
111111111111111111111111111111
Adding: 333333333333333333333333333333
Subtracting: 111111111111111111111111111111
Multiplying: 24691358024691358024691358024641975308641975308641975308642
------------
Enter a BigInt:
222222222222222222222222222222
Enter an int: 4
Adding: 222222222222222222222222222226
Subtracting: 222222222222222222222222222218
Multiplying: 888888888888888888888888888888
For this problem, you have to create three files: one HPP file containing the BigInt class declaration, a CPP file containing the BigInt class definition, and a driver file that implements the program.
Programming for Scientists and Engineen Spring 2017 Optional Extra Credit Assignment Due Date Friday, May 10 2017 at ER00vm. Instructions This is an individual assignment, that will count towards 5%of the final grade. There is problems, which have to solve by yourself, and submit the soluton as separate C source code, hrader and driver entirely optional assigamenLHowever, if you submit this your grade will improve somewhere from 0 to 5 points. you donOL there are eo penalties, either. You submit thecorvottile(s)through Moodle by the due deadline. Problem: "mig Integer Arithmetic This problem asks data type to handle large integer arithmetic by creating class called BigInt C has a numb of ypesto handle integr data including intu short ints and long ints and their sigued and unsigned variants HoweveL the navimam posiive number har can in the largest these, which is an unsigned long int is 18-446 744073,703.55L615. This number, as you can see, has digits In mary scientific and engineering applications, this does suffice to hold values nequired for comples computarians Aaything about i0 would be too big to hold inthe unsigned ong int type. So your unkin his assignment is to a C dass called Bight which will allow baic arihmetic operations pesitive intreer values of up to 300 digits storing inlegors 0 3-9 The three operations you addicion subtraction and multiplication. Specific tanks 1. Add two Biglini objects, and storethe resultin another Biglnu using the operator. 2. Add an int a Biglint store the also using the 3, Subtract one Biglm object from another and store the in another Biglm, using the always be smaller or equal to the first Biglnt In other vends operator. The second can the results shall never be engutive. Subtract int from a Bigint object, and tore the in another Eiglint, also awing the operator Should be obvious in this case that the results hull never benegative, 5. Multiply two Bigint and sore he revalt in another Bight, uing the operaot The resulting Bighnt shall not escred 300 digts 6. Multiply a Bight with an int, and the reult in aaother Biglr, using the operator. Again, the resulting Biglitshall not escend 300 digits. operators to input and output Biglint objects to streams, eespectively. of course they have to be as friend functions. To art, here's bare skeleton of the Biglint class You are welcome to start withthis, but do have to. Asa hint, you your basic rchool level arithmetic, and implement this dass Io solve this problem. string value The program will run as follows: 2. Ouwouthe results of addition suberaction, and muhiplication of these two 3 Then, ask the user to inguta Biglint and an integers 4. ouput the results of addition suboraction, and muhiplication of the Biglit and Ser the example output below. Example (uner input is undmised: For this problem, you have to oreale three files: one HPP file containing the Biglm class declaration. OPPfile containing the Bight class definition, and driver file that implements the program When you are done. ame the source code files Here you replace cuermame with your U of Memal address for example, if your email address is smital2Ba@amneda, your files should be namedExplanation / Answer
#include "conio.h" #include "iostream" #include "vector" using namespace std; const int CAPACITY = 200; class BigInt{ int digits[CAPACITY]; public: BigInt(); BigInt(char num[]); BigInt(int init_num); BigInt operator+=(BigInt a); }; BigInt::BigInt(){ for(int i=0; i 0){ digits[i] = init_num % 10; init_num = init_num/10; } for(;iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.