C++ Help! Any help would be appreciated. For this project, you will create a fun
ID: 3773960 • Letter: C
Question
C++ Help! Any help would be appreciated.
For this project, you will create a function that will allow a poet to write the forward poem and output the entire reverse poem: the original forward poem followed by its mirror image. For example, if this poem is entered:
REVENGE
seeks destruction,
like a dragon
breathes
fire
The output should be:
REVENGE
seeks destruction,
like a dragon
breathes
fire
fire
breathes
like a dragon
seeks destruction,
REVENGE
Requirements
To accomplish this objective, create two objects of a template queue class and put each line of the forward poem into each object. Then, create a template function to reverse one of the queues and push each line onto the other queue so that it contains the entire palindrome poem. You can use a loop or you can use recursion to accomplish this. You cannot use an array or vector for this assignment for any reason.
This function needs to be a template function because the poet may want to create a palindrome poem using characters rather than entire strings.
For this project, you must use the following prototype (do not change the prototype at all):
template<class Item>
void reversePoem(queue &initialQ, queue &finalQ);
/*
Precondition: Two objects, initialQ and finalQ, of a queue class have been filled with data.
Postcondition: The queue object initialQ has been loaded in reverse order onto the end of the queue object finalQ. In other words, finalQ contains its original data plus the reversed data from initialQ. The initialQ is empty.
*/
Program must contain the following:
A template queue class of your choosing. It can be the template queue class demonstrated in lecture, a template queue class from a textbook or the Internet (please give attribution) or the STL queue class.
A function using the prototype listed above. This function must be a template function. If you choose to use recursion, make sure this is also a recursive function (optional).
A main function that loads two queue objects and calls the reversePoem function and pass the two queue objects to that function, then the main function should pop all of the contents of the finalQ for display. This means that your main function will be the only function with cout statements.
Explanation / Answer
SOURCE CODE:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <queue>
using namespace std;
/*
template<class Item>
void reversePoem(queue &initialQ, queue &finalQ);
*/
template<class Item>
void reversePoem(queue<Item> &initialQ, queue<Item> &finalQ);
int main()
{
queue<string> initialQ, finalQ;
string sLine = "";
ifstream infile;
infile.open("poem.txt"); //We are taking the input from a file ins
while (!infile.eof())
{
getline(infile, sLine);
initialQ.push(sLine);
finalQ.push(sLine);
sLine="";
}
infile.close();
reversePoem(initialQ,finalQ);
while(!finalQ.empty())
{
cout<<finalQ.front()<<" ";
finalQ.pop();
}
return 1;
}
template<class Item>
void reversePoem(queue<Item> &initialQ, queue<Item> &finalQ)
{
queue<Item> temp;
Item t,i;
while(!initialQ.empty())
{
t = initialQ.front();
temp.push(t);
while(!finalQ.empty())
{
i = finalQ.front();
temp.push(i);
finalQ.pop();
}
while(!temp.empty())
{
i = temp.front();
finalQ.push(i);
temp.pop();
}
initialQ.pop();
}
}
poem.txt
REVENGE
seeks destruction,
like a dragon
breathes
fire
OUTPUT:
REVENGE
seeks destruction,
like a dragon
breathes
fire
fire
breathes
like a dragon
seeks destruction,
REVENGE
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.