Question

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 */

Homework Answers

Answer #1

Please find the overloaded operators.

Since this looks like part of the assignment, I am not sure about the
constructor you are using in the BigInt class. and hence I have not provided the output.

I have used the below constructor and the overloaded operators corresponds to the same.

// constructor used in the program.
// initialize value array using integer input.
BigInt::BigInt(int x){

int i = 0;

// declare a vector digits.
std::vector<int> digits;

// store the input as a vector in the digits.
while (x)
{
digits.push_back(x % 10);
x /= 10;
i++;
}

// initialize size variable so that it contains number of digits in the input.
size = i;

// reverse the vector contents to get the correct value.
std::reverse(digits.begin(), digits.end());

// copy the contents to the value array.
std::copy(digits.begin(), digits.end(), value);
}

// Overloaded operator >>
std::istream &operator>>( istream &input, const BigInt &bi ) {
  
int i= 0;
// read till we get the ; operator.
while(input != ';'){
// since what we read from file will be string, converting the same to
// integer and storing in the array.
bi.value[i] = std::stoi(input);
i++;
}
return input;
}

// overloaded operator +
BigInt BigInt::operator+(const BigInt & rhs)const{

// carry variable.
int carry = 0;

// check the size of the current class.
int lsize = this->size;

// check the size of the rhs class to be added.
int rsize = rhs.size;

// longSum contains the final sum in string form.
std::string longSum;

int temp;

// Till either the size of lsize OR rsize is ZERO, continue.
while (lsize !=0 || rsize !=0){
int a = 0;
int b = 0;
// if still we have the elements to be added then fetch them in
// current class and the rhs class.
if (lsize != 0)
a = this->value[lsize];
if(rsize != 0)
b = rhs.value[rsize];

// add the values with carry.
temp = a + b + carry;

// if the sunm is of 2+ digits, then find the carry and store
// the current result in the longSum.
int t2 = temp % 10;
carry = temp/10;

// convert the sum to string before storing.
std::string ts = std::to_string(t2);

// append to the longSum
longSum.append(ts);

// decrement the lsize and rsize.
lsize--;
rsize--;
}

// at the end create a result BigInt class and return the same.
Big res((longSum));

return res;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Please Use C++ I tried to calculate complex number by using *= and operator /= but...
Please Use C++ I tried to calculate complex number by using *= and operator /= but I got an incorrect result compared with the result of complex number calculator For example, When I calculate ( (c5 *= c4) *= c4) by using my operator function, the result was 1.08288e+06+1.11262e+07i on output, However, when using a complex calculator, the result was = −253987.448 − 355181.112i, so I got the wrong answer There is my code below. It compiles well, but my...
Something is either messed up in my operator overload <<, covertopostfix function, or my main output....
Something is either messed up in my operator overload <<, covertopostfix function, or my main output. Cannot figure it out. please help. Please comment your changes too. Program below is supposed to be outputting like this: InFix is:   A+B-C Post fix is:   A B + C - InFix is:   A+C Post fix is:   A C + InFix is:   x*(y+z)-(w+t) Post fix is:   x y z + * w t + - InFix is:   A+B*(C+D)-E/F+G+H Post fix is:   A B C...
How do I fix my error of binding on line 74? #include <iostream> #include <fstream> #include...
How do I fix my error of binding on line 74? #include <iostream> #include <fstream> #include <cctype> #include <cstring> #include <iomanip> using namespace std; #include "AvlTree.h" class WordCount { public:     char *word;     int *lines;     int count;     int size;     bool operator<(const WordCount &rhs) const {return strcmp(word, rhs.word) < 0;}     bool operator!= (const WordCount &rhs) const {return strcmp(word, rhs.word) != 0;}     WordCount():lines(NULL), count(0), size(0) {word = new char[1]; word[0] = '\0';}     friend ostream& operator<<...
C++ Class involving difference. The goal is to overload the function: void Bag::operator-=(const Bag& a_bag) //...
C++ Class involving difference. The goal is to overload the function: void Bag::operator-=(const Bag& a_bag) // The Set Difference between two sets A and B is the set that consists of the elements of A which are not elements of B. Bag bag1 = (1,2,3) and Bag bag2 = (2,4,5) then bag1-=bag2 should return 1,3,4,5. //parameter a_bag to be subtracted from this (the calling) bag //post removes all data from items_ that is also found in a_bag //Since type is...
can someone edit my c++ code where it will output to a file. I am currently...
can someone edit my c++ code where it will output to a file. I am currently using xcode. #include <iostream> #include <cctype> #include <cstring> #include <fstream> using namespace std; bool inputNum(int [],int&,istream&); void multiply(int[],int,int[],int,int[],int&); void print(int[],int,int,int); int main() {ifstream input; int num1[35],num2[35],len1,len2,num3[60],len3=10,i; input.open("multiplyV2.txt"); //open file if(input.fail()) //is it ok? { cout<<"file did not open please check it\n"; system("pause"); return 1; }    while(inputNum(num1,len1,input)) {inputNum(num2,len2,input); multiply(num1,len1,num2,len2,num3,len3); print(num1,len1,len3,1); print(num2,len2,len3,2); for(i=0;i<len3;i++) cout<<"-"; cout<<endl; print(num3,len3,len3,1); //cout<<len1<<" "<<len2<<" "<<len3<<endl; cout<<endl;    } system("pause"); } void...
The program shown below reads in (N) values of time (hours, minutes, and seconds). If the...
The program shown below reads in (N) values of time (hours, minutes, and seconds). If the values for hours, minutes and seconds are a legal military time (i.e. 00 00 00 to 23 59 59) the program should display the formatted results (i.e. 12 34 56 should be displayed as 12:34:56). If there's an error for any of the entered values, an exception should be thrown and an error message should be displayed. Note, there are three exception conditions, one...
(C++) I'm getting this message: "Programmer: Brooke Morris Course: ComSci 200 - 5047 libc++abi.dylib: terminating zsh:...
(C++) I'm getting this message: "Programmer: Brooke Morris Course: ComSci 200 - 5047 libc++abi.dylib: terminating zsh: abort ./a.out" when I run my program. What is the problem in my code? #include<iostream> #include<vector> #include<cstring> using namespace std; //leg class declaration class Leg { const char* const startCity; const char* const endCity; const double distance; friend class Routes; public: Leg(const char* const, const char* const, const double); Leg& operator= (const Leg&); double getDistance() const; void output(ostream&) const; }; //routes class declaration class...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an executable with gcc -Wall -DUNIT_TESTS=1 array-utils5A.c The definitions for is_reverse_sorted and all_different are both defective. Rewrite the definitions so that they are correct. The definition for is_alternating is missing. Write a correct definition for that function, and add unit tests for it, using the unit tests for is_reverse_sorted and all_different as models. Please explain the logic errors present in in the definition of is_reverse_sorted...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any type dynamic arrays (replace string by the template in all instances below). • The class should have: – A private member variable called dynamicArray that references a dynamic array of type string. – A private member variable called size that holds the number of entries in the array. – A default constructor that sets the dynamic array to NULL and sets size to 0....
C++ visual studios this function has to run twice. and the "count" variable needs to be...
C++ visual studios this function has to run twice. and the "count" variable needs to be "2" on the second run. How can i do that? The parameters can not change as well void printArray(const int *array, int n) {    int count = 1;    char check;    if (check == 'k')    {        count++;    }    cout << "Value " << count << " array contents." << endl;    cout << "------------------------" << endl;   ...