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...
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.
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...
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...
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...
A C program that will perform the following: Input the string: Abc_3_deF Expected Output: The string...
A C program that will perform the following: Input the string: Abc_3_deF Expected Output: The string you entered is: aBC_Three_DEf An output for just this specific input will be fine. If you want to provide a program for all outputs, it would help with my understanding of how the program works overall as well but it is not needed. Thanks!
Using R and the data in the table below, perform the regression of D on C...
Using R and the data in the table below, perform the regression of D on C (i.e., report the regression equation). C D 3 2 6 7 8 5 9 4 1 0 3 4 Hint: The code to enter the vectors C and D into R is: C <- c(3, 6, 8, 9, 1, 3) D <- c(2, 7, 5, 4, 0, 4) You must figure out how to obtain the regression equation from R. Enter the code below...
Using the Adjusted Trial Balance for merchandising company, perform the following tasks: Prepare an Income Statement...
Using the Adjusted Trial Balance for merchandising company, perform the following tasks: Prepare an Income Statement in multiple-step format with subtotals for net sales, gross profit, income before taxes, and net income. (20 points) Calculate Gross Profit Margin. (5 points) Tower Corporation Adjusted Trial Balance at 31 December 2019                                                               Debit               Credit Cash                                                       7,590             Accounts Receivable                              5,910 Merchandise Inventory                            4,870 Prepaid Insurance                                   2,550             Equipment            ...
Please find implementation of KNN algorithm (in C++) below Please explain how each code implements the...
Please find implementation of KNN algorithm (in C++) below Please explain how each code implements the following steps of the algorithm in question: 1. Determine parameter K = number of nearest neighbors 2. Calculate the distance between the query-instance and all the training samples 3. Sort the distance and determine nearest neighbors based on the K-th minimumdistance 4. Gather the category Y of the nearest neighbors 5. Use simple majority of the category of nearest neighbors as the prediction value...
Please code in C#-Visual Studio Tasks The program needs to contain the following A comment header...
Please code in C#-Visual Studio Tasks The program needs to contain the following A comment header containing your name and a brief description of the program Output prompting the user for the following inputs: Name as a string Length of a rectangle as a double Width of a rectangle as a double Length of a square as an int After accepting user input, the program outputs the following: User name Area of a rectangle with dimensions matching the inputs Area...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT