Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Assignment: 3
- //Version: 1
- //Name: Jeremy Pequeno
- //Instructor: Patricia Carando
- #include <algorithm>
- #include <cstdlib>
- #include <iostream>
- #include <fstream>
- #include <vector>
- using namespace std;
- const int FILM_RATING_G = 0;
- const int FILM_RATING_PG = 1;
- const int FILM_RATING_PG_13 = 2;
- const int FILM_RATING_R = 3;
- const int FILM_RATING_NC_17 = 4;
- const int FILM_RATING_UNRATED = 5;
- const int SIZE = 10;
- struct Film {
- int id; // the unique id of the film
- string name; // the film's name
- string description; // the film's description
- int runningTime; // the film's running time in minutes <= 180
- int rating; // the film's rating, from the const decls
- };
- string filmRatingToString(int rating);
- vector <Film> addFilms (vector <Film> &films);
- vector <Film> findFilmsByRating ( int rating, vector <Film> &films);
- vector <Film> findFilmsByRatingAndRunningTime ( int rating, int runningTime, vector <Film> &films);
- vector <Film> selectionSort (vector <Film> &films);
- void printFilm(Film films);
- void printFilms(vector<Film> films);
- int main(){
- vector <Film> films (SIZE);
- cout << "***** Films added ***** \n" << endl;
- addFilms(films);
- printFilms(films);
- cout << "\n***** Films in sorted order by their run time ***** \n" << endl;
- printFilms(selectionSort(films));
- cout << "\n*****Films rated PG-13 or less***** \n" << endl;
- findFilmsByRating(FILM_RATING_PG_13, films);
- cout << "\n*****Films rated PG-13 or less and less than 90 minutes long***** \n" << endl;
- findFilmsByRatingAndRunningTime(FILM_RATING_PG_13, 90, films);
- return 0;
- }
- string filmRatingToString(int rating){
- string filmRating;
- if(rating == 0){
- filmRating = "G";
- }
- if(rating == 1){
- filmRating = "PG";
- }
- if(rating == 2){
- filmRating = "PG-13";
- }
- if(rating == 3){
- filmRating = "R";
- }
- if(rating == 4){
- filmRating = "NC-17";
- }
- if(rating == 5){
- filmRating = "Unrated";
- }
- return filmRating;
- }
- //innitialize values in the vector films
- vector <Film> addFilms (vector <Film> &films)
- {
- films[0].id = 0;
- films[0].name = "Willy Wonka & the Chocolate Factory";
- films[0].description = "Children take a tour of a candy factory from the most famous of all candy producers.";
- films[0].runningTime = 100;
- films[0].rating = FILM_RATING_G;
- films[1].id = 1;
- films[1].name = "It (2017)";
- films[1].description = "In the summer of 1989, a group of bullied kids band together to destroy a shapeshifting monster, which disguises itself as a clown and preys on the children of Derry, their small Maine town.";
- films[1].runningTime = 135;
- films[1].rating = FILM_RATING_R;
- films[2].id = 2;
- films[2].name = "Toy Story";
- films[2].description = "A cowboy doll is profoundly threatened and jealous when a new spaceman figure supplants him as top toy.";
- films[2].runningTime = 81;
- films[2].rating = FILM_RATING_G;
- films[3].id = 3;
- films[3].name = "Silent Hill";
- films[3].description = "A woman, Rose, goes in search for her adopted daughter within the confines of a strange, desolate town called Silent Hill.";
- films[3].runningTime = 132;
- films[3].rating = FILM_RATING_R;
- films[4].id = 4;
- films[4].name = "Avatar";
- films[4].description = "A paraplegic marine dispatched to the moon Pandora on a unique mission becomes torn between following his orders and protecting the world he feels is his home.";
- films[4].runningTime = 162;
- films[4].rating = FILM_RATING_PG_13;
- films[5].id = 5;
- films[5].name = "The Incredibles";
- films[5].description = "A family of undercover superheroes, while trying to live the quiet suburban life, are forced into action to save the world.";
- films[5].runningTime = 116;
- films[5].rating = FILM_RATING_PG;
- films[6].id = 6;
- films[6].name = "Up";
- films[6].description = "Seventy-eight year old Carl Fredricksen travels to Paradise Falls in his home equipped with balloons, inadvertently taking a young stowaway.";
- films[6].runningTime = 96;
- films[6].rating = FILM_RATING_PG;
- films[7].id = 7;
- films[7].name = "Forrest Gump";
- films[7].description = "The presidencies of Kennedy and Johnson, Vietnam, Watergate, and other history unfold through the perspective of an Alabama man with an IQ of 75.";
- films[7].runningTime = 142;
- films[7].rating = FILM_RATING_PG_13;
- films[8].id = 8;
- films[8].name = "Jurassic Park";
- films[8].description = "During a preview tour, a theme park suffers a major power breakdown that allows its cloned dinosaur exhibits to run amok.";
- films[8].runningTime = 127;
- films[8].rating = FILM_RATING_PG_13;
- films[9].id = 9;
- films[9].name = "The Lion King";
- films[9].description = "Lion cub and future king Simba searches for his identity. His eagerness to please others and penchant for testing his boundaries sometimes gets him into trouble.";
- films[9].runningTime = 88;
- films[9].rating = FILM_RATING_G;
- return films;
- }
- //Sorts movies based on their run time
- vector <Film> selectionSort (vector <Film> &films){
- int minIndex, minValue;
- for(int i = 0; i < SIZE-1; i++){
- minIndex = i;
- minValue = films[i].runningTime;
- for(int j = i +1; j < SIZE; j++){
- if (films[j].runningTime < films[i].runningTime){
- minValue = films[j].runningTime;
- minIndex = j;
- }
- }
- swap(films[minIndex], films[i]);
- }
- return films;
- }
- //returns the set of films that have a rating of the specified rating or less
- vector <Film> findFilmsByRating (int rating, vector <Film> &films){
- int index = 0; //subscript to search vector
- while(index < SIZE){
- if (films[index].rating <= rating){
- printFilm(films[index]);
- }
- index++;
- }
- return films;
- }
- //returns the set of films that have a rating of the specified rating or less and the specified running time or less
- vector <Film> findFilmsByRatingAndRunningTime (int rating, int runningTime, vector <Film> &films){
- int index = 0; //subscript to search vector
- while(index < SIZE){
- if (films[index].rating <= rating && films[index].runningTime <= runningTime){
- printFilm(films[index]);
- }
- index++;
- }
- return films;
- }
- //prints the struct Film in a readable forma
- void printFilm(Film film){
- cout << "Film title : " << film.name << endl;
- cout << "Film description : " << film.description << endl;
- cout << "Film running time : " << film.runningTime <<" minutes" << endl;
- cout << "Film rating : " << filmRatingToString(film.rating) << "\n"<< endl;
- }
- //prints a vector of structs in a readable format
- void printFilms(vector<Film> films){
- for (int i = 0; i < SIZE; i++){
- printFilm(films[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement