This is for practice only using file I/O and STL deques so please don\'t manipul
ID: 3714004 • Letter: T
Question
This is for practice only using file I/O and STL deques so please don't manipulate anything except where it says insert code here! Here is the details. Write a C++ program that reads lines from an input file, redux.dat. Each line is terminated with an equal sign. Each line contains two or more binary numbers that are to be added together and the resulting sum is to be output to standard output (stdout) You may assume only well-formed (valid) binary numbers will be entered.
And here is the text file redux.dat you read from:
0 0 =
0 1 =
1 1 =
101 1100 =
100 1 1101 0 =
1 1 01 0 10000000 111 0100000 =
1 10 100 1000 10000 100000 1000000 10000000 =
1000000000 111111110 0 1 =
1 1111111110 =
111111111111111111111111 0 =
111111111111111111111111 1 =
101010101010101010101010 1 111111110110111 =
Finally here is the code thus far:
#include <iostream>
#include <fstream>
#include <cstring>
#include <deque>
using namespace std;
typedef deque<char> bindeque;
void Badd(const bindeque augend, const bindeque addend, bindeque& sum);
// IN IN OUT
// Return a C++ string as a char vector (bindeque)
bindeque conv(const string& s)
{
return bindeque(s.begin(), s.end());
}
int main( int argc, char* argv[] )
// IN IN
{
ifstream input("redux.dat"); // Input file
bindeque term; // Binary term (number vector)
bindeque partialSum; // Partial sum of the binary numbers
bindeque sum; // Sum of the binary numbers
string binaryNumber; // Binary term (number string)
input >> binaryNumber;
while (input)
{
cout << binaryNumber;
sum = conv(binaryNumber);
input >> binaryNumber;
while (input && binaryNumber[0] != '=')
{
cout << " + " << binaryNumber;
term = conv(binaryNumber);
partialSum = sum;
// Add together the two binary numbers
Badd( partialSum, term, sum );
input >> binaryNumber;
}
// Print answer on standard output.
cout << " = ";
for (char digit: sum)
cout << digit;
cout << endl;
input >> binaryNumber;
}
input.close();
return 0;
} // end main
void Badd(const bindeque augend, const bindeque addend, bindeque& sum)
// IN IN OUT
// Pre: augend and addend are char deques representing valid binary numbers.
// Post: sum is a char deque representing the sum of augend + addend.
{
sum.clear(); // Empty (clear) sum
// ... INSERT CODE HERE! ...
return;
} // end Badd
Thank you to anyone that helps!
Explanation / Answer
Hi,
Below is the complete program with Badd function in bold. The logic is add each binary digit one by one from input queues and form one result string. After all digits are added, convert the result string into deque and return.Output of the program is also given below. Please run the program and verify the output:
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.