Question

In: Computer Science

Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...

Using the following code perform ALL of the tasks below in C++:

-------------------------------------------------------------------------------------------------------------------------------------------

Implementation:

Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid.

Overload the operator+ so that it adds two bigint together.

Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first digit is the one's place (10^0) and the second digit is the ten's place (10^1).

Testing:

Build unit test for add. There is some testing but it is very incomplete. You will need to develop better tests cases here.

Build unit tests for subscript. There is a file for this but it has no tests.

Make sure your input operator works. This requires you to manually inspect the output. Test with different values and ranges.

You will need to update the Makefile - instructions are in the Makefile.

The command make tests will build and run the unit tests.

Create a main body, name the file add.cpp. See main.cpp (in svn/shared) as a starting point. The main reads from the file data1-1.txt and must do the following:

Test for success of opening the file in your program.

Read in two numbers into bigints and write both out separated by a blank line.

Add these together and write the result.

Then read in two more big numbers, adding them and writing the result until end of file.

All output must be labeled and neat.

The command make add will build and run this program.

Code:

------------------------------------------------------------------------------------------------------------------------------------------------------

Main.cpp

-----------------------------------------------------------------------------------------------------------------------------------------------------

#include

#include

#include

#include "Header.h"

int main() {

//open files

std::ifstream inFile;

inFile.open("dataFiles/mixed_input.txt");

  

//check if file even opened

if(!inFile.is_open()){

std::cout << "File did not open.\n";

return -1;

}

//stores some input data

char i;

inFile >> i;

std::cout << i << "\n";

  

while(!inFile.eof()){

std::cout << i << "\n";

inFile >> i;

}

  

BigInt bi(7925);

std::cout << bi << std::endl;

//bi.debug_print(std::cout);

  

BigInt bi1 ("7925");

std::cout << bi1 << std::endl;

//bi1.debug_print(std::cout);

  

//comparing bi and bi1.

// 1 - indicates EQUAL

// 0 - indicates UNEQUAL.

  

bool areEqual = (bi == bi1);

printf("\n Are bi and bi1 equal? %d\n",areEqual);

  

BigInt bi2 (1234);

std::cout << bi2 << std::endl;

//bi2.debug_print(std::cout);

  

//comparing the value after it has changed.

areEqual = (bi == bi2);

printf("\n Are bi and bi2 equal? %d\n",areEqual);

  

}

size_t strlen_(const char *s) {

return strlen(s);

}

//helps debug the print

void BigInt::debug_print(std::ostream& out) const {

  

out << " | ";

for(int i = MAX_DIGITS - 1; i >= 0; --i)

out << digit[i] << " | ";

}

//default

BigInt::BigInt():digit(){

  

}

//takes integer

BigInt::BigInt(int value){

  

//initialize all array elements to 0

int x = 0;

while (x < MAX_DIGITS) {

digit[x] = 0;

x++;

}

  

int i = 0;

while (value > 0){

i = i * 10 + (value % 10);

value /= 10;

}

  

//store the input into array

for (int x = 0; x <= MAX_DIGITS; ++x) {

digit[x] = i % 10;

i /= 10;

}

}

//takes char value

BigInt::BigInt(const char *value){

  

//initialize all array elements to 0

int x = 0;

while (x < MAX_DIGITS) {

digit[x] = 0;

x++;

}

  

//initialize size variable so it has number of digits in input.

size = strlen_(value);

  

//convert the character array to integer array and store the same value to it.

for(int i = 0; i < size; ++i){

this->digit[i] = *value - '0';

++value;

}

}

//overload operator== to compare

bool operator==(const BigInt &rhs , const BigInt &lhs){

  

//check to see if size of both inputs are equal, if not, it returns false.

if(lhs.size != rhs.size)

return false;

  

//compare every value.

for(int i = 0; i < lhs.size; i++){

if (lhs.digit[i] != rhs.digit[i])

return false;

}

return true;

}

//overload operator<<

std::ostream &operator<<(std::ostream & out, const BigInt & bi){

  

//return each index value to the out stream

for(int i = 0; i < bi.size; ++i )

out << bi.digit[i];

  

return(out);

}

//overload operator>>

std::istream &operator>>(std::istream & in, const BigInt & bi){

}

//overload operator+

BigInt operator+(const BigInt & rhs)const{

return lhs + rhs;

}

//overload operator[]

int BigInt::operator[](const BigInt &bi){

//10^0 represents the ones place in the number and [0] in the array and so on

return 0;

}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

Header.h

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

#ifndef Header_h

#define Header_h

const int MAX_DIGITS = 500;

class BigInt {

  

private:

//array contains the BigInt.

int digit[MAX_DIGITS];

int size;

  

public:

//default constructor

BigInt();

  

//constructor that takes integer.

BigInt(int value);

  

//constructor that takes character array.

BigInt(const char *value);

  

//debug code

void debug_print(std::ostream& out) const;

  

// overloaded <<, ==, >>, +, []friend functions.

friend std::ostream &operator<<(std::ostream &out, const BigInt &bi);

friend std::istream &operator>>(std::istream &in, const BigInt &bi);

friend bool operator==(const BigInt &rhs, const BigInt &lhs);

int operator[](const BigInt &bi);

BigInt operator+(BigInt & rhs)const;

};

#endif /* Header_h */

Solutions

Expert Solution

#include

#include

#include

#include "Header.h"

int main() {

//open files

std::ifstream inFile;

inFile.open("dataFiles/mixed_input.txt");

  

//check if file even opened

if(!inFile.is_open()){

std::cout << "File did not open.\n";

return -1;

}

//stores some input data

char i;

inFile >> i;

std::cout << i << "\n";

  

while(!inFile.eof()){

std::cout << i << "\n";

inFile >> i;

}

  

BigInt bi(7925);

std::cout << bi << std::endl;

//bi.debug_print(std::cout);

  

BigInt bi1 ("7925");

std::cout << bi1 << std::endl;

//bi1.debug_print(std::cout);

  

//comparing bi and bi1.

// 1 - indicates EQUAL

// 0 - indicates UNEQUAL.

  

bool areEqual = (bi == bi1);

printf("\n Are bi and bi1 equal? %d\n",areEqual);

  

BigInt bi2 (1234);

std::cout << bi2 << std::endl;

//bi2.debug_print(std::cout);

  

//comparing the value after it has changed.

areEqual = (bi == bi2);

printf("\n Are bi and bi2 equal? %d\n",areEqual);

  

}

size_t strlen_(const char *s) {

return strlen(s);

}

//helps debug the print

void BigInt::debug_print(std::ostream& out) const {

  

out << " | ";

for(int i = MAX_DIGITS - 1; i >= 0; --i)

out << digit[i] << " | ";

}

//default

BigInt::BigInt():digit(){

  

}

//takes integer

BigInt::BigInt(int value){

  

//initialize all array elements to 0

int x = 0;

while (x < MAX_DIGITS) {

digit[x] = 0;

x++;

}

  

int i = 0;

while (value > 0){

i = i * 10 + (value % 10);

value /= 10;

}

  

//store the input into array

for (int x = 0; x <= MAX_DIGITS; ++x) {

digit[x] = i % 10;

i /= 10;

}

}

//takes char value

BigInt::BigInt(const char *value){

  

//initialize all array elements to 0

int x = 0;

while (x < MAX_DIGITS) {

digit[x] = 0;

x++;

}

  

//initialize size variable so it has number of digits in input.

size = strlen_(value);

  

//convert the character array to integer array and store the same value to it.

for(int i = 0; i < size; ++i){

this->digit[i] = *value - '0';

++value;

}

}

//overload operator== to compare

bool operator==(const BigInt &rhs , const BigInt &lhs){

  

//check to see if size of both inputs are equal, if not, it returns false.

if(lhs.size != rhs.size)

return false;

  

//compare every value.

for(int i = 0; i < lhs.size; i++){

if (lhs.digit[i] != rhs.digit[i])

return false;

}

return true;

}

//overload operator<<

std::ostream &operator<<(std::ostream & out, const BigInt & bi){

  

//return each index value to the out stream

for(int i = 0; i < bi.size; ++i )

out << bi.digit[i];

  

return(out);

}

//overload operator>>

std::istream &operator>>(std::istream & in, const BigInt & bi){

}

//overload operator+

BigInt operator+(const BigInt & rhs)const{

return lhs + rhs;

}

//overload operator[]

int BigInt::operator[](const BigInt &bi){

//10^0 represents the ones place in the number and [0] in the array and so on

return 0;

}


Related Solutions

Write C++ programs to perform the following tasks. In the program descriptions below, example input and...
Write C++ programs to perform the following tasks. In the program descriptions below, example input and output is provided. NOTE: You don’t need arrays to solve any of these problems. You should NOT use arrays to solve any of these problems. • stat.cpp: Let the user input a one or more integers, space separated, on a single line (as seen below), then work out and display the sum, average, sum of squares and population variance of the numbers. Remember, you...
In C++ and input code using the template below Get a copy of the caseswitch.cpp. The...
In C++ and input code using the template below Get a copy of the caseswitch.cpp. The purpose of the program is to demonstrate how to use Switch statement. Complete the for loop so that the program will ask the user for 6 input grades. Compile and run this C++ program. Use input value A, B, C, D, E, and F. Notice there is no output for B and E. Add cases for them. B is "Good Work." Add a case...
C++ coding question From the text file given to you- “worldpop.txt”, perform the following tasks using...
C++ coding question From the text file given to you- “worldpop.txt”, perform the following tasks using Boolean function. PS-*Write separate codes for each task* Task 1. Display the names of the countries with: 1. Population >=1000,000,000 2. Population <= 1000,000 Task 2. Display the names of the first 10 countries Task 3. Display the names of the last 10 countries contents of worldpop.txt: Afghanistan 32738376 Akrotiri 15700 Albania 3619778 Algeria 33769669 Andorra 72413 Angola 12531357 Anguilla 14108 Argentina 40677348 Armenia...
3. Using the information below, perform the following tasks: Account Receivables Not Past Due 0-31 days...
3. Using the information below, perform the following tasks: Account Receivables Not Past Due 0-31 days Past Due 31-60 days Past Due 61-90 days Past Due + 90 days Past Due 350,000 175,000 83,000 38,000 12,000 Percentage of not collecting 2% 8% 22% 58% 78% Calculate the estimated amount of receivables that might not be collected Perform the adjustment(debit/credit) using the allowance method.
Using C++, identify suitable coding modules for the following (a) Overload the * operator so that...
Using C++, identify suitable coding modules for the following (a) Overload the * operator so that two instances of Quat can be multiplied using the * operator. Given that q1 and q2 are Quaternions. Let q1 = (a1, b1i, c1j, d1k) and q2 = (a2, b2i, c2j, d2k) The product (q1 * q2) is ( a1a2 – b1b2 – c1c2 – d1d2, (a1b2 + b1a2 + c1d2 – d1c2)i, (a1c2 + c1a2 + d1b2 – b1d2)j, (a1d2 + d1a2 +...
Perform the following tasks with NumPy arrays. All of them can be done (elegantly) in 1...
Perform the following tasks with NumPy arrays. All of them can be done (elegantly) in 1 to 3 lines. (a) Create an 8 × 8 array with ones on all the edges and zeros everywhere else. (b) Create an 8 × 8 array of integers with a checkerboard pattern of ones and zeros. (c) Given the array c = np.arange(2, 50, 5), make all the numbers not divisible by 3 negative. (d) Find the size, shape, mean, and standard deviation...
Based on the tables below, write SQL command to perform the following tasks for MySql: Create...
Based on the tables below, write SQL command to perform the following tasks for MySql: Create SALESREP and CUSTOMER tables Create primary and foreign keys as appropriate. The custNo should use a surrogate key as the primary key with auto-increment increase the balance of the Gonzales account by $100 to a total of $450? Find an average customer balance Display the name of the sales representative and the name of the customer for each customer that has a balance greater...
C++ Directions: Follow the insttructions of implementation and then fill in the following code for QueueArray...
C++ Directions: Follow the insttructions of implementation and then fill in the following code for QueueArray class Both given below: 5.1 Implement QueueArray<DataType>::QueueArray(int maxNumber) 5.2 Implement QueueArray<DataType>::QueueArray(const QueueArray& other) 5.3 Implement QueueArray<DataType>::~QueueArray() 5.4 Implement void QueueArray<DataType>::enqueue(const DataType& newDataItem) throw (logic_error) 5.5 Implement DataType QueueArray<DataType>::dequeue() throw (logic_error) 5.6 Implement void void QueueArray<DataType>::clear() throw (logic_error) 5.7 Implement bool QueueArray<DataType>::isEmpty() const 5.8 Implement bool QueueArray<DataType>::isFull() const 5.9 Implement void QueueArray<DataType>::putFront(const DataType& newDataItem) throw (logic_error) 5.10 Implement DataType QueueArray<DataType>::getRear() throw (logic_error) 5.11 Implement int...
C++ programming Complete the code to input and store all the information in the structure Book:...
C++ programming Complete the code to input and store all the information in the structure Book: title, author, year and pages. Complete the function print() with arguments and code to print a Book variable in the following format: Book title: <title> Book author: <name> Published in <year> Number of pages: <pages> #include <iostream> using namespace std; struct Book { string title; string author; string year; int pages; }; void print(/*arguments*/) { //Complete function } int main() { //Declare structure variable...
Using the pseudocode found below, write only the actual (C++) code for this program. Include all...
Using the pseudocode found below, write only the actual (C++) code for this program. Include all libraries. Specification: Write a program that will repeatedly ask the user for quiz grades in the range from 0 to 20. Any negative value will act as a sentinel. When the sentinel is entered compute the average of the grades entered. Design: Constants None Variables float grade float total = 0 int count = 0 float average ------- Inital Algorithm Repeatedly ask user for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT