Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views3 pages

Satyam 3

The document describes a C++ program that allows users to create and display a hierarchical structure of books, chapters, and sections. It includes a class for managing the tree structure and methods for creating and displaying the book hierarchy. The program runs in a loop, allowing users to create new book entries or display existing ones until they choose to quit.

Uploaded by

engineersatyam2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Satyam 3

The document describes a C++ program that allows users to create and display a hierarchical structure of books, chapters, and sections. It includes a class for managing the tree structure and methods for creating and displaying the book hierarchy. The program runs in a loop, allowing users to create new book entries or display existing ones until they choose to quit.

Uploaded by

engineersatyam2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PracticalNo.

//Name:SATYAM.
//Roll no:52. DIV:B
//EXPERIMENT-3
#incude<iostream>
#include <string>

using namespacestd;

structnode//NodeDeclaration
{
string label;
intch_count;
structnode*child[10];
}*root;

classGT//ClassDeclaration
{
public:
void create_tree();
voiddisplay(node*r1);

GT()
{
root=NULL;
}
};

voidGT::create_tree()
{
inttbooks,tchapters,i,j,k;

root=newnode;
cout<<"Enternameofbook:"; cin.ignore();
getline(cin,root->label);

cout<<"Enternumberofchaptersinbook:"; cin
>> tchapters;
root->ch_count=tchapters;

for(i=0;i<tchapters;i++)
{
root->child[i]=newnode;
cout<<"EnterthenameofChapter"<<i+1<<":"; cin.ignore();
getline(cin,root->child[i]->label);

cout<<"EnternumberofsectionsinChapter"<<i+1<<":"; cin >>


root->child[i]->ch_count;
for(j=0;j<root->child[i]->ch_count;j++)
{
root->child[i]->child[j]=newnode;
cout<<"EnterNameofSection"<<j+1<<":"; cin.ignore();
getline(cin,root->child[i]->child[j]->label);
}
}
}

voidGT::display(node*r1)
{
inti,j;
if(r1!=NULL)
{
cout << "\n----Book Hierarchy---";
cout<<"\nBooktitle:"<<r1->label; int
tchapters = r1->ch_count;

for(i=0;i<tchapters;i++)
{
cout<<"\nChapter"<<i+1<<":"<<r1->child[i]->label; cout <<
"\nSections: ";
for(j=0;j<r1->child[i]->ch_count;j++)
{
cout<<"\n"<<r1->child[i]->child[j]->label;
}
}
}
cout<<endl;
}

intmain()
{
intchoice;
GT gt;

while(1)
{
cout<<"\nBookTreeCreation"<<endl; cout <<
"1. Create" << endl;
cout << "2. Display" << endl;
cout << "3. Quit" << endl;
cout<<"Enteryourchoice:"; cin
>> choice;

switch(choice)
{
case1:
gt.create_tree();
break;
case2:
gt.display(root);
break;
case3:
cout<<"Thanksforusingthisprogram!!!"<<endl; exit(0);
default:
cout<<"Wrongchoice!!!"<<endl;
}
}
return0;
}
//output:-
BookTreeCreation
1. Create
2. Display
3. Quit
Enteryourchoice:1
Enternameofbook:Maths
Enternumberofchaptersinbook:2
EnterthenameofChapter1:chapter1
EnternumberofsectionsinChapter1:1
EnterNameofSection1:ch1_section1
EnterthenameofChapter2:chapter2
EnternumberofsectionsinChapter2:1
EnterNameofSection1:ch2_section2
BookTreeCreation
1. Create
2. Display
3. Quit
Enteryourchoice:2
----BookHierarchy---
Booktitle:Maths
Chapter1:chapter1
Sections:
ch1_section1
Chapter2:chapter2
Sections:
ch2_section2
BookTreeCreation
1. Create
2. Display
3. Quit
Enteryourchoice:3
Thanksforusingthisprogram!!!

You might also like