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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
struct Movie
{
int timesCalled;
int year;
string name;
string director;
};
Movie *readMovieInfo(int &size);
void printDirectors(Movie *array, int size);
void selectionSortAscending(Movie *array, int size);
int binarySearch(Movie *array, int size, string value);
void printResults(int result, int size, Movie *array);
void writeMovies(Movie *array, int size);
int main()
{
int size = 0, result = 0;
string value;
Movie *movieList;
movieList = readMovieInfo(size);
printDirectors(movieList, size);
selectionSortAscending(movieList, size);
cout << endl << endl;
for (int i = 1; i <= size; i++)
{
cout << left << setw(20) << movieList[i - 1].director << "\t";
if (i % 3 == 0)
cout << endl;
}
/*
while (value != "QUIT" || value != "quit") // Loop until user wants to quit
{
cout << endl << endl << "Enter a director's name (or enter QUIT to quit): ";
getline(cin, value);
if (value == "QUIT" || value == "quit")
break;
result = binarySearch(movieList, size, value);
printResults(result, size, movieList);
}
writeMovies(movieList, size);
delete [] movieList;
*/
return 0;
}
//********************************************************************************
// Definition of function *readMovieInfo.
// This function uses an input file (chosen by user) to read in data for a
// dynamically allocated array of structures's members.
//********************************************************************************
Movie *readMovieInfo(int &size)
{
ifstream inFile;
string file;
Movie *movieList;
cout << "What is the name of the file you would like to open? ";
getline(cin, file);
file.append(".txt");
inFile.open(file.c_str());
if(!inFile) // File validation
{
cout << "Can't open the input file!" << endl;
exit(111);
}
inFile >> size;
movieList = new Movie[size];
for (int i = 0; i < size; i++)
{
inFile >> movieList[i].year;
getline(inFile, movieList[i].name, '\"');
getline(inFile, movieList[i].name, '\"');
getline(inFile, movieList[i].director, '\"');
getline(inFile, movieList[i].director, '\"');
movieList[i].timesCalled = 0;
}
inFile.close();
return movieList;
}
//***********************************************************************
// Definition of function printDirectors. *
// This function simply prints the available directors that are found *
// in the array of structures *array in a three director per row format.*
//***********************************************************************
void printDirectors(Movie *array, int size)
{
cout << "Here are the available directors:" << endl << endl;
for (int i = 1; i <= size; i++)
{
cout << left << setw(20) << array[i - 1].director << "\t";
if (i % 3 == 0)
cout << endl;
}
}
//***********************************************************************
// Definition of function selectionSortAscending *
// This function performs a selection sort on the array of structures
// *array by director name.
//***********************************************************************
void selectionSortAscending(Movie *array, int size)
{
string minValue;
int startScan, minIndex;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = array[startScan].director;
for(int index = startScan + 1; index < size; index++)
{
if (array[index].director < minValue)
{
minValue = array[index].director;
minIndex = index;
}
}
array[minIndex].director = array[startScan].director;
array[startScan].director = minValue;
}
}
|