Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

1 //tvDatabase.

cpp
2 //stores the data from a file containing tv shows as a binarySearchTree
3 //and displays the data in a useful way
4
5 #include<iostream>
6 #include"list.h"
7 #include"binarySearchTree.h"
8 #include <fstream>
9 #include <cstring>
10 using namespace std;
11
12 binarySearchTree readTVFile();
13 void getSeriesName( char line[], char seriesName[] );
14 void getYears( char line[], int& yStart, int& yEnd );
15 int indexOf( char s[], char c );
16 void removeBlanksFromString( char s[] );
17 void getSubString( char s[], int start, int end, char res[] );
18 void printSeries( series ); //used mostly for testing
19
20 int main(){
21
22 binarySearchTree i = readTVFile();
23 int input = 1;
24 while ( input != 0 ) {
25 cout << "What to do with the data?" << endl;
26 cout << " 0) Exit" << endl;
27 cout << " 1) Print all data" << endl;
28 cout << " 2) Print data from a specific show" << endl;
29 cout << " 3) Print all shows of a specific actor" << endl;
30 cout << " 4) Print all shows released within a range" << endl;
31 cin >> input;
32 if ( input == 1 ){
33 cout << "-------------------" << endl;
34 i.printTreeInOrder();
35 cout << "-------------------" << endl;
36 } else if ( input == 2 ){
37 string showName;
38 cout << "Please Input show name: " << endl;
39 cin.ignore();//clear input stream
40 getline( cin, showName ); //get line of input
41 // cout << showName << endl;
42 i.printShow( showName );
43 } else if ( input == 3 ) {
44 string actorName;
45 cout << "Please input actor name: " << endl;
46 cin.ignore();//clear input stream
47 getline( cin, actorName ); //get line of input
48 // cout << showName << endl;
49 cout << "-------------------" << endl;
50 i.printShowsWithActor( actorName );
51 cout << "-------------------" << endl;
52 } else if ( input == 4 ) {
53 int start, end;
54 cout << "Please input the starting year of the range: " << endl;
55 cin >> start;
56 cout << "Please input the ending year of the range: " << endl;
57 cin >> end;
58 cout << "-------------------" << endl;
59 i.printShowsInRange( start, end );
60 cout << "-------------------" << endl;
61 } else if ( input == 0){
62 cout << "Terminating Program" << endl;
63 } else {
64 cout << "Invalid Input" << endl;
65 }
66 }
67
68 }
69
70 //reads data from the tv series file
71 binarySearchTree readTVFile() {
72
73 //get file name
74 string fileName = "tvDB.txt";
75 // cout << "Please input file name: ";
76 // cin >> fileName;
77 // cout << endl;
78
79 //open file
80 ifstream fIn( fileName, ios::in );
81 //check if file exists, if not, terminate program
82 if( !fIn ) {
83 cout << "Unable to open " << fileName << " data file" << endl;
84 exit( -1 );
85 }
86 //declare variables:
87 binarySearchTree seriesTree; //tree that holds the data, will be returned
88 const int MAX_LINE = 128; //maximum line size
89 char line[MAX_LINE]; //current line in the file
90
91 //loop retrieves data, feeds it into the tree:
92 while( fIn.getline( line, MAX_LINE ) ) {
93 char seriesName[MAX_LINE]; //curent series name
94 char seriesCategory[MAX_LINE/2]; //category of the current series
95 char seriesURL[MAX_LINE]; //url of current series
96 char actorName[MAX_LINE/2]; //current actor in the current series
97 int yStart, yEnd; //start year, end year of the current series
98 series currentSeries; //hold data of current series, used to add data to the tree
99 getSeriesName( line, seriesName );
100 if ( strlen( line ) > 0 ){ //checks if series name is blank
101 getYears( line, yStart, yEnd );
102 fIn.getline( seriesCategory, MAX_LINE/2 );
103 fIn.getline( seriesURL, MAX_LINE );
104 // cout << "series name: |" << seriesName << "|" << endl;
105 currentSeries.title = seriesName;
106 // cout << " year start: " << yStart << endl;
107 currentSeries.yearStart = yStart;
108 // cout << " year end: " << yEnd << endl;
109 currentSeries.yearEnd = yEnd;
110 // cout << " Category: |" << seriesCategory << "|" << endl;
111 currentSeries.category = seriesCategory;
112 // cout << " URL: |" << seriesURL << "|" << endl;
113 currentSeries.url = seriesURL;
114 fIn.getline( line, MAX_LINE/2 );
115 while( strlen( line ) > 0 ){
116 strcpy( actorName, line );
117 // cout << " actor name: |" << actorName << "|" << endl;
118 currentSeries.actors.push( actorName );
119 fIn.getline( line, MAX_LINE/2 );
120 }
121 // printSeries ( currentSeries );
122 seriesTree.addNode( currentSeries );
123 // currentSeries.actors.clear(); //clears actors list for next show
124 }
125 }
126 fIn.close(); //closes file
127 // return nSeries;
128 return seriesTree; //return the tree
129 }
130
131 //seperates the series name from the years it was made
132 void getSeriesName( char line[], char seriesName[] ){
133 int yearStart;
134 yearStart = indexOf( line, '(' );
135 getSubString( line, 0, yearStart - 1, seriesName );
136 }
137
138 //seperates the start and end years from the series name on the same line
139 void getYears( char line[], int & yStart, int & yEnd ) {
140 char tmpString[8];
141 char yearString[16];
142 int yearStart, yearEnd;
143 // cout << "Entering GetYears()" << endl;
144 yearStart = indexOf( line, '(' );
145 yearEnd = indexOf( line, ')' );
146 // cout << " yearStart: " << yearStart << endl;
147 // cout << " yearEnd: " << yearEnd << endl;
148 getSubString( line, yearStart + 1, yearEnd - yearStart -1, yearString );
149 // getSubString( line, yearStart+1, 11, yearString );
150 // cout << " yearString: " << yearString << endl;
151 getSubString( yearString, 0, 4, tmpString );
152 // cout << " tmpString: " << tmpString << endl;
153 yStart = atoi( tmpString );
154 getSubString( yearString, 5, 7, tmpString ); // 7? not ’-’!
155 // cout << " tmpString: " << tmpString << endl;
156 yEnd = atoi( tmpString );
157 // cout << " year start: " << yStart << endl;
158 // cout << " year end: " << yEnd << endl;
159 // cout << "Leaving GetYears()" << endl;
160 }
161
162 //find the index of a given character in a string (char array)
163 int indexOf( char s[], char c ){
164 int i = 0;
165 while( s[i] != '\0' && s[i] != c )
166 i++;
167 return i;
168 }
169
170 //removes blank space, etc from a string
171 //used in the getSubString function
172 void removeBlanksFromString( char s[] ) {
173 int sLen = strlen( s );
174 for( int i = sLen ; i >= 0 ; i-- ){
175 if( isalpha(s[i]) ){
176 break;
177 }
178 if( s[i] == ' ' ){
179 s[i] = '\0';
180 }
181 }
182 }
183
184 //returns a substring of the given string, starting and ending at the given values
185 //used to seperate title, name, and years from the text given by the file
186 void getSubString( char s[], int start, int end, char res[] ){
187 int i;
188 int iRes = 0; // index of result string
189 for( i = start ; i < start + end ; i++ ){
190 res[iRes++] = s[i];
191 }
192 res[iRes] = '\0';
193 removeBlanksFromString( res );
194 }
195
196 //print a series struct,
197 //used primarily for testing
198 void printSeries( series s ){
199 cout << "-------------------" << endl;
200 cout << "Title: " << s.title << endl;
201 cout << "Category: " << s.category << endl;
202 cout << "URL: " << s.url << endl;
203 cout << "Start Year: " << s.yearStart << endl;
204 cout << "End Year: " << s.yearEnd << endl;
205 cout << "Actors: " << endl;
206 s.actors.printList();
207 cout << "-------------------" << endl;
208 }
209
210
211
212

You might also like