#include <iostream>
#include <string>
#include <fstream>
#include <limits>
#include <iomanip>
using namespace std;
struct node
{
string data;
node* next;
node* bel;
node(string data)
: data(data), next(NULL), bel(NULL)
{
}
~node()
{
node* next;
for(; bel != NULL; bel = next)
{
next = bel->next;
delete bel;
}
}
node* insert_bel(string data)
{
node** b;
for (b = &bel; *b != NULL && (*b)->data != data; b = &((*b)->next));
if (*b == NULL)
*b = new node(data);
return *b;
}
};
class tree
{
node* head;
tree(const tree &) {}
tree& operator=(const tree &) { return *this; }
public:
tree() : head(NULL) {}
~tree()
{
clear();
}
void clear()
{
node* next;
for (; head != NULL; head = next)
{
next = head->next;
delete head;
}
}
node* find_mov(string mov)
{
node* m;
for (m = head; m != NULL; m = m->next)
{
if (m->data == mov)
break;
}
return m;
}
node* insert_mov(string mov)
{
node** m;
for (m = &head; *m != NULL && (*m)->data != mov; m = &((*m)->next));
if (*m == NULL)
*m = new node(mov);
return *m;
}
void insert_actor(string mov, string act)
{
node* m = insert_mov(mov);
m->insert_bel(act);
}
void load(string filename)
{
ifstream input(filename);
load(input);
}
void load(istream &input)
{
clear();
string mov, act;
/*
while (getline(input, mov))
{
while (getline(input, act) && act != "#")
insert_actor(mov, act);
}
*/
while (getline(input, mov))
{
node* m = insert_mov(mov);
while (getline(input, act) && act != "#")
m->insert_bel(act);
}
}
void print_mov()
{
if (head == NULL)
{
cout << "No Movie\n";
}
else
{
for(node *m = head; m != NULL; m = m->next)
cout << m->data << "\n";
}
}
void print_actors(string mov)
{
node *m = find_mov(mov);
if (m == NULL)
{
cout << "No Movie\n";
}
else
{
for (node* a = m->bel; a != NULL; a = a->next)
cout << a->data << "\n";
}
}
};
int main()
{
tree t;
t.load("mov.txt");
bool keepRunning = true;
do
{
cout << "\n\nMenu\n";
cout << "1. Show the list of movies\n";
cout << "2. Search\n";
cout << "3. Exit\n";
cout << "Enter Your Choice \n";
int ch;
if (!(cin >> ch))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Bad Input";
continue;
}
if (ch < 1 || ch > 3)
{
cout << "Bad Input";
continue;
}
switch (ch)
{
case 1:
{
system("CLS");
cout << "\nList of Movies\n";
t.print_mov();
break;
}
case 2:
{
system("CLS");
cout << "\n\nEnter the name of Movie\n";
string mov;
if (getline(cin >> ws, mov))
t.print_actors(mov);
break;
}
case 3:
{
keepRunning = false;
break;
}
}
}
while (keepRunning);
}
2 Secondes (1998)
John Walsh (III)
Michael Scherer
Ralph Bellamy
#
Accomplice (1946)
Richard Arlen
Walter Matthau
Shannon Tweed
#
Aliens in the Wild, Wild West (2000)
Carly Pope
Markus Parilo
Michael J. Fox
Steven Spielberg
#