#ifndef MOVIETREE_H_INCLUDED
#define MOVIETREE_H_INCLUDED
#include <iostream>
#include <cstdlib>
#include <vector>
#include <math.h>
using namespace std;
class BinarySearchTree
{
private:
struct tree_node
{
tree_node* left;
tree_node* right;
int ranking;
string title;
int year;
int quantity;
};
tree_node* root;
public:
BinarySearchTree()
{
root = NULL;
}
bool isEmpty() const { return root==NULL; }
void insert(int ranking, string title, int year, int quantity);
void remove(string title);
void inorder(tree_node* p);
double orderrating(string title);
void print_inorder();
void search(string d);
void rent(string l);
// void split(const string& s, char c,vector<string>& v);
};
double BinarySearchTree::orderrating(string title)
{
// string letters[52]={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","o","p","q","r","s","t","u","v","w","x","y","z"};
char letters[54]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',
'P','Q','R','S','T','U','V','W','X','Y','Z',':','a','b','c','d','e','f','g','h','i','j',
'k','l','m','o','p','q','r','s','t','u','v','w','x','y','z',' '};
double rating = 0;
for(int i = 0; i<title.length();i++)
{
for(int j = 0;j<52;j++)
{
if(letters[j]==title.at(i))
{
rating = rating+pow(10,-i)*((j)%26);
}
}
}
//cout<<rating<<endl;
return rating;
}
void split(const string& s, char c, vector<string>& v)
{
string::size_type i = 0;
string::size_type j = s.find(c);
while (j != string::npos) {
v.push_back(s.substr(i, j-i));
i = ++j;
j = s.find(c, j);
if (j == string::npos)
v.push_back(s.substr(i, s.length()));
}
}
void BinarySearchTree::insert(int ranking, string title, int year, int quantity)
{
tree_node* t = new tree_node;
tree_node* parent;
t->quantity = quantity;
t->ranking = ranking;
t->title = title;
t->year = year;
t->left = NULL;
t->right = NULL;
parent = NULL;
// is this a new tree?
if(isEmpty()) root = t;
else
{
//Note: ALL insertions are as leaf nodes
tree_node* curr;
curr = root;
// Find the Node's parent
while(curr)
{
parent = curr;
if(orderrating(t->title) > orderrating(curr->title)) curr = curr->right;
else curr = curr->left;
}
if(orderrating(t->title) <= orderrating(parent->title))
parent->left = t;
else
parent->right = t;
}
}
void BinarySearchTree::search(string l)
{
//Locate the element
bool found = false;
double d = orderrating(l);
if(isEmpty())
{
cout<<" This Tree is empty! "<<endl;
return;
}
tree_node* curr;
tree_node* parent;
curr = root;
while(curr != NULL)
{
if(curr->title == l)
{
found = true;
cout << "Movie Info:" << endl;
cout << "===========" << endl;
cout << "Ranking:" <<curr->ranking<<endl;
cout << "Title:"<<curr->title<<endl;
cout << "Year:" <<curr->year<<endl;
cout << "Quantity:"<<curr->quantity<<endl;
break;
}
else
{
parent = curr;
if(d>orderrating(curr->title)) curr = curr->right;
else curr = curr->left;
}
}
if(!found)
{
cout<<" Movie not found."<<endl;
return;
}
}
void BinarySearchTree::rent(string l)
{
//Locate the element
bool found = false;
double d = orderrating(l);
if(isEmpty())
{
cout<<"This Tree is empty!"<<endl;
return;
}
tree_node* curr;
tree_node* parent;
curr = root;
while(curr != NULL)
{
if(curr->title == l)
{
found = true;
if(curr->quantity!=0)
{curr->quantity = curr->quantity-1;
cout << "Movie has been rented." << endl;
cout << "Movie Info:" << endl;
cout << "===========" << endl;
cout << "Ranking:" <<curr->ranking<<endl;
cout << "Title:" <<curr->title<<endl;
cout << "Year:" <<curr->year<<endl;
cout << "Quantity:" << curr->quantity<<endl;
}
else{//If movie is in stock
cout << "Movie out of stock." << endl;
}
break;
}
else
{
parent = curr;
if(d>orderrating(curr->title)) curr = curr->right;
else curr = curr->left;
}
}
if(!found)
{
cout<<"Movie not found."<<endl;
return;
}
}
void BinarySearchTree::print_inorder()
{
inorder(root);
}
int counter =0;
void BinarySearchTree::inorder(tree_node* p)
{
if(p != NULL)
{
if(p->left) inorder(p->left);
cout<<"Movie: "<<p->title<<endl;
//cout<<" "<<p->quantity<<endl;//cout<<counter<<endl;
if(p->right) inorder(p->right);
}
else return;
}
#endif // MOVIETREE_H_INCLUDED
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <stdlib.h>
#include "MovieTree.h"
using namespace std;
struct MovieInfo{
int ranking;
string title;
int year;
int quantity;
};
int main(int argc, char * argv[])
{
//creates struct to store movie info
MovieInfo MovieInfo1[51];
int counter1 = 0;
std::string word1;
//"Assignment5Movies.txt"
ifstream myfile1("Assignment5Movies.txt");
if (myfile1.is_open())
{
std::string line;
while ( getline (myfile1,line) )
{
//need delimeter for file.
vector<string> v;
string s = line;
split(s, ',', v);
for (unsigned int i = 0; i < v.size(); ++i)
//add individual atributes to moviearray
{//cout<<v[i]<<endl;
if(i%4==1){MovieInfo1[counter1].title = v[i];}
if(i%4==2){ int value =atoi(v[i].c_str()); MovieInfo1[counter1].year = value;}
if(i%4==3){ int value =atoi(v[i].c_str()); MovieInfo1[counter1].quantity = value;}
if(i%4==0){ int value =atoi(v[i].c_str()); MovieInfo1[counter1].ranking = value;}
}//cout<<counter1<<endl;
counter1++;
}
myfile1.close();
}
// construct Binary tree
BinarySearchTree b;
for(int i = 0; i<counter1;i++)
{
b.insert(MovieInfo1[i].ranking,MovieInfo1[i].title,MovieInfo1[i].year, MovieInfo1[i].quantity);
}
int option;
//do while loop for user interface
do{
cout << "======Main Menu=====" << endl;
cout << "1. Find a movie" << endl;
cout << "2. Rent a movie" << endl;
cout << "3. Print the inventory" << endl;
cout << "4. Quit" << endl;
cin>>option;
cin.ignore(10000,'\n');
if (option==1)//find movie
{
cout << "Enter title:" << endl;
string temp;
cin >> ws;
getline(cin, temp);
b.search(temp);
}
if (option==2)//rent movie
{
cout << "Enter title:" << endl;
string temp;
cin >> ws;
getline(cin, temp);
b.rent(temp);
}
if (option==3)//print inventory
{
b.print_inorder();
}
}while(option!=4);
cout << "Goodbye!" << endl;
}
这是我的主文件和我的标题,基本上我的输出关闭了两个电影,我不知道为什么。任何输入将不胜感激。是的,当我打印出一个txt文件时,我也应该使用string.compare(string)
12 Angry Men
Back to the Future
Casablanaca
... the rest are in order except
Lord of the Rings: the two towers
Lord of the Rings: the fellowship of the ring
Lord of the Rings: the return of the king
在正确的位置,但订购不正确。
答案 0 :(得分:0)
这三部电影的orderrating()
值相同。因此,它们将以您将它们插入树中的相反顺序打印。
只有双倍的精确度。