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

Source: Internet
Author: User

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

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
 
  
# Include
  
   
# Include
   
    
# Include 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 minor signs for List sorting bool operator <(const Movie & movie) const {return gross ()> movie. gro Ss ();} 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, read the result and store it in Vector
    
     
& Vmbool read (const char * file, vector
     
      
& Vm) {ifstream ifs (file); if (! Ifs) {perror ("failed to open the box office file"); return false;} Movie movie; while (ifs> movie) // call the overloaded> operator vm. push_back (movie); ifs. close (); return true;} // write the file to put the vector
      
        & Write bool write (const char * file, const vector
       
         & Vm) {ofstream ofs (file); if (! Ofs) {perror ("failed to open the ranking file"); return false ;}for (vector
        
          : Const_iterator it = vm. begin (); it! = Vm. end (); ++ it) ofs <* it <endl; // call the overloaded <
         <操作符ofs.close ();return true;}int main (int argc, char* argv[]) { 判断命令行参数个数是否合法if (argc < 3) {cerr << "用法:" argv[0]<< " <票房文件> 
          <排行文件>
            "<Endl; return-1;} vector
           
             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 ;}
           
          
         
        
       
      
     
    
   
  
 




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.