How to use C to implement a simple movie rating system?
The movie rating system is a system that allows users to rate and comment on the movies they watch. In this system, users can select and rate movies, as well as view ratings and reviews from other users. In this article, we will introduce how to implement a simple movie rating system using C programming language.
First, we need to define a Movie class to represent movies. The Movie class should contain attributes such as the movie's name, director, release date, and ratings. You can use C classes to implement the Movie class, and implement related functions in the member functions of the class. The following is a sample code:
#include <iostream> #include <string> class Movie { private: std::string name; std::string director; std::string releaseDate; float rating; public: // 构造函数 Movie(std::string _name, std::string _director, std::string _releaseDate) { name = _name; director = _director; releaseDate = _releaseDate; rating = 0; // 初始评分为0 } // 获取电影信息 void getInfo() { std::cout << "电影名称: " << name << std::endl; std::cout << "导演: " << director << std::endl; std::cout << "上映日期: " << releaseDate << std::endl; std::cout << "评分: " << rating << std::endl; } // 评分 void rate(float _rating) { rating = _rating; } }; int main() { // 创建一个电影对象 Movie movie("肖申克的救赎", "弗兰克·德拉邦特", "1994年"); // 输出电影初始信息 movie.getInfo(); // 评分 movie.rate(9.5); // 输出电影最新评分 movie.getInfo(); return 0; }
In the above code, we define a class called Movie, which contains attributes such as the name, director, release date, and rating of the movie. We use the constructor to initialize these properties and provide member functions to obtain movie information and perform rating operations.
In the main function, we first create a movie object movie, and then output its initial information. Next, we called the rate function to rate the movie and output the latest rating information.
In addition to implementing the movie class, we also need to create a movie rating system to manage the ratings and comments of movies and users. You can use vectors to store movie objects and user ratings. The following is a sample code:
#include <iostream> #include <string> #include <vector> class Movie { // ... }; class MovieRatingSystem { private: std::vector<Movie> movies; std::vector<float> ratings; public: // 添加电影 void addMovie(Movie movie) { movies.push_back(movie); ratings.push_back(0); // 初始评分为0 } // 获取电影数量 int getMovieCount() { return movies.size(); } // 获取电影信息 void getMovieInfo(int index) { if (index < getMovieCount()) { movies[index].getInfo(); } else { std::cout << "电影不存在!" << std::endl; } } // 评分 void rateMovie(int index, float rating) { if (index < getMovieCount()) { ratings[index] = rating; } else { std::cout << "电影不存在!" << std::endl; } } }; int main() { MovieRatingSystem system; // 添加电影 Movie movie1("肖申克的救赎", "弗兰克·德拉邦特", "1994年"); Movie movie2("阿甘正传", "罗伯特·泽米吉斯", "1994年"); system.addMovie(movie1); system.addMovie(movie2); // 输出电影数量 std::cout << "电影数量: " << system.getMovieCount() << std::endl; // 输出电影信息 system.getMovieInfo(0); system.getMovieInfo(1); // 评分 system.rateMovie(0, 9.5); system.rateMovie(1, 9.0); // 输出电影信息 system.getMovieInfo(0); system.getMovieInfo(1); return 0; }
In the above code, we have created a class called MovieRatingSystem to manage the ratings of movies and users. We can operate the movie rating system through the member functions of adding movies, getting the number of movies, getting movie information, and rating.
In the main function, we first created a MovieRatingSystem object system, and then added two movie objects. Next, we output the number of movies and related information, perform scoring operations on the two movies, and finally output the movie information again.
Through such a simple implementation, we can see how to use C language to create a simple movie rating system. Of course, in practical applications, the system can be further expanded and provide more functions and interactivity. I hope the content of this article is helpful to you!
The above is the detailed content of How to implement a simple movie rating system using C++?. For more information, please follow other related articles on the PHP Chinese website!