-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbook.cpp
More file actions
70 lines (61 loc) · 1.69 KB
/
book.cpp
File metadata and controls
70 lines (61 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<iostream>
#include<vector>
using namespace std;
class tree{
public:
string name;
vector<tree*>children;
tree(string name){
this->name=name;
}
void addchild(tree*child){
children.push_back(child);
}
void display(int level=0){
for(int i=0;i<level;i++)
cout<<" ";
cout<<name<<endl;
for(tree*child :children){
child->display(level+1);
}
}
};
int main(){
tree* book = new tree("How Universe Works");
int chnum;
cout << "Enter the number of chapters: ";
cin >> chnum;
cin.ignore();
for (int i = 0; i < chnum; i++) {
string chname;
cout << "Enter the name of chapter " << (i + 1) << ": ";
getline(cin, chname);
tree* chapter = new tree(chname);
int secnum;
cout << "Enter the number of sections in chapter " << (i + 1) << ": ";
cin >> secnum;
cin.ignore();
for (int j = 0; j < secnum; j++) {
string secname;
cout << "Enter the name of section " << (j + 1) << ": ";
getline(cin, secname);
tree* section = new tree(secname);
int subsecnum;
cout << "Enter the number of subsections in section " << (j + 1) << ": ";
cin >> subsecnum;
cin.ignore();
for (int k = 0; k < subsecnum; k++) {
string subname;
cout << "Enter the name of subsection " << (k + 1) << ": ";
getline(cin, subname);
tree* subsection = new tree(subname);
section->addchild(subsection);
}
chapter->addchild(section);
}
book->addchild(chapter);
}
cout << " Displaying Book Structure:";
book->display();
return 0;
}