Related List Search, Various Data Types

I was commissioned to create a program that searches for more than 400 films (linked to each other using a linked list) by name, genre, year, rating, lead actor, etc.

There is a catch, but we only allow the ONE search function to pre-search through the linked list. In addition, within this search function, we are allowed only one thing, and the loop - I assume that in my case it will be something like ...

  while (moviePtr != NULL)

Obviously, there will be many different copies of them, if this is a search for an actor or a search for a genre. In the case of an actor, genre, rating, year, sub-genre and supporting actor, he must output each individual copy. (for example, if Kevin Bacon was in x-men and a laptop, he should output not only one of them (the output file is not a screen)).

I was completely immersed in these restrictions that we were given. How does my search function handle different types of data? (year and rating must be declared as integers). How does he know what exactly I'm looking for? If I am looking for an actor, I do not want him to look for the name either.

Any suggestions on how to get started and get started are greatly appreciated.

EDIT: Hi everyone, I thought I was updating you guys what I did. I have 3 different search functions. One for numerical values ​​(year and rating), 1 for genre and actors, and finally one for name.

Here is the code for all three. First of all, the title.

void TitleSearched(MovieNode*head,
                string titleSearched,
                ofstream& outFile)
{
MovieNode* moviePtr;
bool found;

moviePtr = head;
found = false;

while (moviePtr !=NULL & !found)
{
    if (moviePtr-> title == titleSearched)
    {
        found = true;
    }
    else
    {
        moviePtr = moviePtr -> next;
    }

}
if (found)
{
    cout << endl << titleSearched << " has been found!\n";
    TitleOutput (moviePtr,outFile);
}
else
{
    cout << endl << titleSearched << " was not found.\n";
}
}

now search by year / rating.

int NumSearched(MovieNode* head, int numSearched)
{

int instances;
MovieNode* moviePtr;

ofstream outFile;


    moviePtr = head;
    instances = 0;
    while (moviePtr !=NULL)
    {
        if (moviePtr-> year == numSearched)
        {

            instances = instances +1;
            NumOutList(moviePtr,outFile,"year",numSearched,instances);
            moviePtr = moviePtr -> next;
        }
        else if (moviePtr->rating == numSearched)
        {

            instances = instances +1;
            NumOutList(moviePtr,outFile,"rating",numSearched,instances);
            moviePtr = moviePtr -> next;
        }
        else
        {
            moviePtr = moviePtr ->next;
        }


    }
return instances;
}

Finally, search for the genre / actors.

int ItemSearch (MovieNode* head,string itemSearched, ofstream& outFile)
{
int instances;
MovieNode* moviePtr;


moviePtr = head;
instances = 0;






    while (moviePtr !=NULL)
    {
        if (moviePtr-> genre == itemSearched || moviePtr ->subGenre == itemSearched)
        {

            instances = instances +1;
            OutList(moviePtr,outFile,"Genre",itemSearched,instances);
            moviePtr = moviePtr -> next;
        }
        else if (moviePtr->leadActor == itemSearched || moviePtr->supportActor == itemSearched)
        {

            instances = instances +1;
            OutList(moviePtr,outFile,"Actor",itemSearched,instances);
            moviePtr = moviePtr -> next;
        }
        else
        {
            moviePtr = moviePtr ->next;
        }


    }



    return instances;
}

I wanted to remind you what my task is. 1. Combine these three search functions into one 2. there is only ONE, and the loop in the search 3. only one return in any given function (however, id assumes that this will be the void function when combined)

My main problem I beileve is my ints and strings. I am not allowed to declare a rating or year as strings. And only the code format when combing all three as a whole causes me a headache.

+3
source share
3 answers

, . - "" ( ", ", " ", - .)

++ , ( ) :

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cctype>

bool begins_with_s(std::string s)
{
    return  s.length() > 0                  && 
            std::toupper( s.at(0) ) == 'S';
}

bool contains_a_number(std::string s)
{
    return std::find_if(s.begin(), s.end(), std::isdigit) != s.end(); 
}

int main()
{
    std::string movies_array[] =  
    { 
        "King Kong", 
        "Singin in the Rain", 
        "Die Hard 2", 
        "Superman", 
        "Star Wars", 
        "Jaws 3"
    };
    std::vector<std::string> movies( std::begin(movies_array), 
                                     std::end(movies_array) );

    // Use predicate - count if the movie name begins with "S"
    std::cout << "Movies beginning with S: " 
        << std::count_if(movies.begin(), movies.end(), begins_with_s) 
        << std::endl;

    // Use predicate - count if the movie name contains a number
    std::cout << "Movies containing a number: " 
        << std::count_if(movies.begin(), movies.end(), contains_a_number) 
        << std::endl;
}

++ , , ,

template< typename PredicateType >
void my_function(PredicateType predicate)
{
    Movie my_movie;
    predicate(my_movie);
}

- ( " " ).

+2

"match" , , . .

- :

template <typename MatchFunction>
void search_movies(movie* moviePtr, MatchFunction match)
{
    while (moviePtr != NULL)
    {
        if (match(*moviePtr))
        {
            // output movie
        }
        moviePtr = moviePtr->next;
    }
}

:

bool matches_actor(movie& m, const std::string& actor)
{
    return m.actor == actor;
}

, :

search_movies(moviePtr, std::bind(matches_actor, _1, "Morgan Freeman"));

(std::bind - ++ 11 <functional>, boost::bind std::bind2nd)

, C-, - :

void search_movies(movie* moviePtr, bool (*match)(movie*, void*), void* match_arg)
{
    while (moviePtr != NULL)
    {
        if (match(moviePtr, match_arg))
        {
            // output movie
        }
        moviePtr = moviePtr->next;
    }
}
...
bool matches_actor(movie* m, void* actor)
{
    return m.actor == *((std::string*)actor);
}
...
std::string actor = "Morgan Freeman";
search_movies(moviePtr, &matches_actor, (void*)(&actor));
+2

, . ( boost::optional . :

void print_matching( node* list, int * year, std::string * actor... ) {
   // iterate over the list:
   while (...) {
      if (  ( !year  || ptr->year == *year )
         && ( !actor || ptr->actor == *actor ) 
         && ...
         )
      {
          // Film matches, print it
      }
   }
}

, search_pattern, , ( : bools ):

struct pattern {
   bool check_actor;
   std::string actor;
   bool check_year;
   int year;
};

void print_matching( node* list, pattern const & p ) {
   // iterate over the list:
   while (...) {
      if (  ( !p.check_year  || ptr->year == p.year )
         && ( !p.check_actor || ptr->actor == p.actor ) 
         && ...
         )
      {
          // Film matches, print it
      }
   }
}

pattern :

bool pattern:: matches (movie cosnt & m) const {  return (! check_year || m.year == year)       & & (! check_actor || m.actor == actor); }   void print_matching (node * list, pattern const p) {      // :       (...) {         if (p.matches(list- > data))         {             // ,         }      }   }

+2

All Articles