Count the top 10 movies in the box office and save them to another file.

Source: Internet
Author: User

Today, I saw a pen question: Given a file (M. dat), which stores box office statistics for various movies. The format is as follows:

2012 Sony $769.7
"Harry Potter and the death Saint (I)" Warner Brothers $952.0
Star Wars 20th century forks $775.4
Trigger/DreamWorks for monster Shrek 4 $750.0
Avatar 20th Century Fox $2,782.2
Warner Brothers for Harry Potter and the flame cup $895.9
"Harry Potter and the Half-Blood Prince" Warner Brothers $934.0
New Line of Lord of the Rings 2: double tower qibing $925.3
Batman prequel 2: Dark Knight Warner Brothers $1,001.9
Harry Potter and the magic stone Warner Brothers $974.7
Bottom story Disney $867.9
Kung fu pandatv/DreamWorks $631.7
Pirates of the Caribbean 3: The end of the world Disney $961.0
Warner Brothers, the prisoner of Harry Potter and Azkaban, $795.6
E. T. Global $792.9
Raiders 4: The Kingdom of the crystal skull palaman $786.6
"Lord of the Rings 3: Return of the King" New Line $1,119.1
Monstrok 2 DreamWorks $919.8
Toys Story 3 Disney $1,063.2
Hacker Empire 2: reloaded into battle Warner Brothers $742.1

.......


Write a program to count the top 10 movies at the box office and save the statistical results to another file. Try using C ++ and share the following code: (GCC compilation in Linux)

For GCC compilation, two command line parameters must be input during execution, such as./A. Out.M. dat Li. dat(M. dat is the source box office file, and Li. dat is the file that stores the first 10)

# Include <iostream> # include <fstream> # include <vector> # include <algorithm> using namespace STD; Class movie {public: // reload the input operation friend istream & operator> (istream & is, Movie & movie) {return is> movie. m_title> movie. m_comp> movie. m_gross;} // reload the output operation friend ostream & operator <(ostream & OS, const Movie & movie) {return OS <movie. m_title <<'' <movie. m_comp <''<movie. m_gross;} // reload the less than sign for list sorting bool Operator <(const Movie & movie) const {return gross ()> movie. gross ();} PRIVATE: // convert the string read from the file to double. Double gross (void) const {string STR (m_gross); size_t Pos = 0; while (Pos = Str. find_first_of ("$,", POS ))! = // Remove "$" and "," string: NPOs) Str. erase (Pos, 1); Return atof (Str. c_str ();} string m_title; // movie name string m_comp; // produced company name string m_gross; // box office}; // read the file, store the read results to the vector <movie> & vmbool read (const char * file, vector <movie> & VM) {ifstream ifs (File); If (! IFS) {perror ("failed to open the box office file"); Return false;} movie; while (IFS> movie) // call the overloaded> operator VM. push_back (movie); ifs. close (); Return true;} // write the data in the vector <movie> & VM into the file bool write (const char * file, const vector <movie> & VM) {ofstream ofs (File); If (! OFS) {perror ("failed to open the ranking file"); Return false ;}for (vector <movie >:: const_iterator it = VM. Begin (); it! = VM. end (); ++ it) ofs <* It <Endl; // call the overloaded <operator ofs. close (); Return true;} int main (INT argc, char * argv []) {// determine whether the number of command line parameters is legal if (argc <3) {cerr <"Usage:" <argv [0] <"<box office File> <ranking File>" <Endl; Return-1 ;} vector <movie> VM; If (! Read (argv [1], Vm) Return-1; sort (VM. begin (), VM. end (); // sort the elements in the VM if (VM. size ()> 10) VM. resize (10); // retrieve the first 10 if (! Write (argv [2], Vm) Return-1; return 0 ;}




Count the top 10 movies in the box office and save them to another file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.