General Instructions Write a program which will add digit strings. The input str
ID: 3792522 • Letter: G
Question
General Instructions
Write a program which will add digit strings. The input strings, in the given format, will be read as Byte objects. The program will evaluate the sum of each Byte pair. The program will write each input pair as well as the results to the screen as well as an output file according to the specified format. The results will include meaningful messages about errors that might occur during the addition. The program shall continue to read and evaluate strings until the end of the input file is reached. You will need to define Byte as a class. The driver program is given to you and you are NOT allowed to change it. Hence, the obvious overloaded operators will be that of addition i.e. +, input >> and output <<.
Specific Instructions
This assignment should be written as a modular C++ program. You will develop a class (module) called Byte, with header (.h) and implementation (.cpp) files. The main program should be in its own module, and is given to you. You are to handle additions of two binary strings with the following assumptions: both strings are stored using bias notation. This notation is commonly used to store floating point values. These binary strings should be first converted to their decimal counterparts according to the specified rules. Next the decimal values should be added and finally the decimal sum should be converted to a binary string in bias notation. In case of error, specific messages should be output as specified below. Remember all this is to be handled within the overloaded + operator (this can in turn call other helper functions like todecimal( )).
Step 1: Find the value of the binary strings and subtract 128.
E.g. 00001111 has an unsigned value of 15. Subtracting 128 gives us the decimal value of -113. Similarly, 10000111 evaluates to 135 – 128 = +7.
Step 2: Add the two decimal values.
Continuing the above example, (-113) + (+7) = -106
Step 3: Convert the decimal sum back to biased notation by first subtracting adding 128 and then converting into a binary string. Remember if the value after subtracting before adding 128 is less than -128 then you have a case of underflow and if it is more than +127 then you have a case of overflow.
In our example, the decimal sum is -106. Adding 128, we get 22 which when converted to binary gives 00010110
Input File Data: Sample Output Screen:
Driver File Code
// FileName: MyByteTest.cpp
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
#include "MyByte.h"
int main()
{
cout << "Program Starting ... ";
cout << "Opening Files ... ";
ifstream in;
in.open("input.txt");
ofstream out;
out.open("output.txt");
cout << "Files Opened Successfully ... ";
MyByte b1, b2, sum;
while (in>>b1 && in>>b2)
{
// Number crunching part
sum = b1 + b2;
// Output to screen
cout << " " << b1 << " " << b1.getnotation()
<< " " << std::right << std::setw(8) << b1.toDecimal() << endl;
cout << "+" << b2 << " " << b2.getnotation()
<< " " << std::right << std::setw(8) << b2.toDecimal() << endl;
cout << "----------- "
<< std::right << std::setw(8) << "----" << endl;
cout << " " << sum << " " << sum.getnotation()
<<" "<< std::right << std::setw(8)
<< b1.toDecimal() + b2.toDecimal() << endl << endl;
// Output to file
out << " " << b1 << " " << b1.getnotation()
<< " " << std::right << std::setw(8) << b1.toDecimal() << endl;
out << "+" << b2 << " " << b2.getnotation()
<< " " << std::right << std::setw(8) << b2.toDecimal() << endl;
out << "----------- "
<< std::right << std::setw(8) << "----" << endl;
out << " " << sum <<" " << sum.getnotation()
<<" "<< std::right << std::setw(8)
<< b1.toDecimal() + b2.toDecimal() << endl << endl;
}
return 0;
}
// FileName: MyByteTest.cpp
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
#include "MyByte.h"
int main()
{
cout << "Program Starting ... ";
cout << "Opening Files ... ";
ifstream in;
in.open("input.txt");
ofstream out;
out.open("output.txt");
cout << "Files Opened Successfully ... ";
MyByte b1, b2, sum;
while (in>>b1 && in>>b2)
{
// Number crunching part
sum = b1 + b2;
// Output to screen
cout << " " << b1 << " " << b1.getnotation()
<< " " << std::right << std::setw(8) << b1.toDecimal() << endl;
cout << "+" << b2 << " " << b2.getnotation()
<< " " << std::right << std::setw(8) << b2.toDecimal() << endl;
cout << "----------- "
<< std::right << std::setw(8) << "----" << endl;
cout << " " << sum << " " << sum.getnotation()
<<" "<< std::right << std::setw(8)
<< b1.toDecimal() + b2.toDecimal() << endl << endl;
// Output to file
out << " " << b1 << " " << b1.getnotation()
<< " " << std::right << std::setw(8) << b1.toDecimal() << endl;
out << "+" << b2 << " " << b2.getnotation()
<< " " << std::right << std::setw(8) << b2.toDecimal() << endl;
out << "----------- "
<< std::right << std::setw(8) << "----" << endl;
out << " " << sum <<" " << sum.getnotation()
<<" "<< std::right << std::setw(8)
<< b1.toDecimal() + b2.toDecimal() << endl << endl;
}
return 0;
}
4-1 input! utl-… File Edit Format View Help 00001111 10000111 10000000 11111111 11000011 11110000 10000001 10000000 00010110 00000011 01eeeeee 11000000 01111111 00000000 00111111 01111111Explanation / Answer
A)
#include <iostream>
#include <iomanip>
#include <fstream>
#include "MyByte.h"
using std::cout;
using std::endl;
using std::setw;
int main()
{
cout << "Add Starting ";
cout << "Opnening files ";
ifstream in;
in.open("input.txt");
Mybite b1, b2, sum;
while (in>>b1 && in>>b2)
{
sum i= b1 + b2;
out << " " << b1 << " " << b1.getnotation()
<< " " << std::right << std::setw(2) <<i<< endl;
out << "+" << b2 << " " << b2.getnotation()
<< " " << std::right << std::setw(2) << i << endl;
out << "----------- "
<<" "<< std::right << std::setw(2)
<< b1.toDecimal() + b2.toDecimal() ;
}
cout << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.