So I have been working on this old program trying to comprehend some stuff for a final coming up soon. I have gotten this far and now I must give up because I have other finals to study for not to mention the rest of the information for this specific final as well. My current code is as follows(and i feel like I'm close):
/*
* element.h
* project 2
*
*
*/
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
template <class T>
class element
{
public:
element()
{
chapter = false;
section = false;
subsection = false;
paragraph = false;
};
element(string newTitle)
{
title = newTitle;
};
string getTitle()
{
return title;
};
void setTitle(string newTitle)
{
title = newTitle;
}
friend istream & operator >>(istream &in, element &right)
{
string kind;
int num;
string word;
char tmp;
in >> kind;
if(in.fail())
return in;
else
{
if(kind == "chap")
{
chapter = true;
in >> tmp;
while(tmp != "\"")
in >> tmp;
in.getline(word, 1000, tmp)
title = word;
in >> num;
}
else if(kind == "parg")
{
paragraph = true;
in >> tmp;
while(tmp != "\"")
in >> tmp;
in.getline(word, 1000, tmp)
title = word;
}
else if(kind == "sect")
{
section = true;
in >> tmp;
while(tmp != "\"")
in >> tmp;
in.getline(word, 1000, tmp)
title = word;
}
else if(kind == "subs")
{
subsection = true;
in >> tmp;
while(tmp != "\"")
in >> tmp;
in.getline(word, 1000, tmp)
title = word;
}
right = element(letters);
return in;
}
}
//<<
friend ostream & operator << (ostream &out,
const element &right)
{
out << right.title;
return out;
}
friend bool operator==(const element &left,
const element &right)
{
return left.title==right.title;
}
private:
string title;
int numpages, place;
bool chapter, section, subsection, paragraph;
T nextLevel;
};
If someone could help me finish this code so that the program works (at least at a basic level) and explain what I did wrong I would be very appreciative. Posting the program description and sample input below:
Pretty Printing a Book's Table of Content
Motivation
You are charged with printing the "Table of Content" for a book, where Chapters, sections, and subsections
are printed with their page numbers.
However, this needs to be dynamically implemented, in that you must allow for the author to provide for
additional material, that will be included in the book. Each chapter has zero or more sections, each section
has zero or more subsections, and each subsection will have zero or more paragraph. Only paragraphs will
actually take up space on the printed page, and each paragraph takes between 1 and 5 pages. Your program
needs to be able to add paragraphs to subsections, subsections to sections, sections to chapters, and chapters
to the book. For instance, the command
chap "former and nephew to the present" 2
This instruction means that a new chapter is to be created.
The title of the new chapter is former and nephew to the present
The new chapter will be immediately after the current chapter 2. And thus it will be chapter 3. If
there was already a chapter 3, (and 4, and 5, . . . ), then these will now be chapter 4, (and 5, and 6,
. . . ).
The title of a chapter does not take enough space on a page.
sect "king polonius lord chamberlain horatio friend" 5 7
This instruction means that a new section is to be created.
The title of the new section is king polonius lord chamberlain horatio friend
The new section will be immediately after the current section 7 in chapter 5. And thus it will be
section 8 in chapter 5. If there was already a section 8, (and 9, and 10, . . . ) in chapter 5, then this
will now be section 9, (and 10, and 11, . . . ).
The title of a chapter does not take enough space on a page.
subs "to hamlet laertes son to polonius" 3 5 8
This instruction means that a new subsection is to be created.
The title of the new subsection is to hamlet laertes son to polonius
The new subsection will be immediately after the current subsection 8 of section 5 in chapter 3. And
thus it will be subsection 9 of section 5 in chapter 3. If there was already a subsection 9, (and 10, and
11, . . . ), then this will now be subsection 10, (and 11, and 12, . . . ).
The title of a chapter does not take enough space on a page.
parg 2 4 6 8 10
This instruction means that a new paragraph is to be created.
Paragraphs do not have titles, but paragraphs take up a number of pages. This particular paragraph
takes 2 pages when printed.
The new paragraph will be in chapter 4, section 6, subsection 8, and will be immediately after the
current paragraph 10. And thus it will be paragraph 11. If there was already a paragraph 10, then
this will now be subsection paragraph 11.
prtb
This instruction means that you need to print the current table of content: all chapters, all sections,
all subsections, correctly indented, and with correct page numbers.
prch 8 3
This instruction means that you need to print the current table of content, but limit yourself to printing
the chapter titles for all chapters (with page numbers), no deeper. Except when you get to chapter 8,
you need to print also all the section titles (with page numbers). Except when you get to section 3,
you also need to print all the subsection titles with page numbers.
Some notes:
1. You must implement these with the binary representation of a general tree, you must
use linked pointers, you must use recursion, and you must use C++.
2. You might be asked to insert a chapter, section, subsection, or paragraph `immediately after 0'. How-
ever, there are no chapters, sections, subsections, or paragraphs that have number 0. So this is a
mechanism to populate the particular sub-structure with its rst chapter, section, subsection, or para-
graph.
3. The title are chosen from the text of Hamlet. Each title is four, ve or six words taken from the text,
and are enclosed in double quotes. To strip the extra quotes, you may want to use filein.get();
(or use another method, such as regular expressions from the STL for 5 points extra credit).
4. Some of you have started the project already, and have populated already parts of the ToC, which is
perfectly ne. The new chapters, sections, and subsections can still be inserted
Sample input(this input is really giving me grief):
chap ` the tragedy of` 0
chap `hamlet prince of denmark by william` 0
chap `shakespeare dramatis personae claudius king` 0
chap `of denmark marcellus officer hamlet son` 1
sec `to the former and nephew` 1 0
chap `to the present king` 1
sub `polonius lord chamberlain horatio friend` 1 1 0
sec `to hamlet laertes son to polonius` 4 0
par 1 1 1 1 0
sec `voltemand courtier cornelius courtier rosencrantz` 2 0
sec `courtier guildenstern courtier osric courtier a` 4 1
sec `gentleman courtier a priest` 5 0
par 4 1 1 1 1
sub `marcellus officer bernardo officer francisco a` 1 1 1
chap `soldier reynaldo servant to polonius` 2
prtb
sub `players two clowns gravediggers` 5 2 0
sec `fortinbras prince of norway` 2 0
par 3 1 1 2 0
sub `a norwegian captain english` 2 1 0
sec `ambassadors getrude queen of` 3 0
par 3 5 2 1 0
par 2 2 1 1 0
par 2 1 1 2 1
sub `denmark mother to hamlet` 2 1 0
sub `ophelia daughter to polonius` 2 2 0
sec `ghost of hamlet father lords ladies` 6 0
sub `officers soldiers sailors messengers` 2 2 0
par 1 2 2 2 0
par 1 1 1 2 1
sub `attendants scene elsinore act` 3 1 0
sub `i scene i elsinore` 5 1 0
sub `a platform before the castle enter` 5 1 0
sub `two sentinelsfirst francisco who paces` 3 1 0
sec `up and down at` 1 0
sec `his post then bernardo` 1 2
sub `who approaches him ber` 3 1 1
sub `who there fran nay` 5 2 1
sub `answer me stand and` 5 2 1
sec `unfold yourself ber long` 1 0
sec `live the king fran` 1 3
par 2 5 1 1 0
par 3 5 1 2 0
sub `bernardo ber he fran you` 1 4 0