10. Modify tmdb.cpp so that the program outputs two columns: run time, followed by title. The rows should be sorted by run time in decreasing order. #include #include #include #include #include #include // data source: https://www.kaggle.com/tmdb/tmdb-movie-metadata struct Movie { std::string Title; std::string ReleaseDate; // YYYY-MM-DD, so we can compare as a string unsigned int RuntimeInMinutes; uint64_t BudgetInDollars; uint64_t RevenueInDollars; void print() { //std::cout.precision(17); std::cout << BudgetInDollars << ";" << ReleaseDate << ";" << RevenueInDollars << ";" << RuntimeInMinutes << ";" << Title << ";" << std::endl; } }; // convert line in csv to Movie struct // columns: budget, release date, revenue, runtime, title Movie parse(const std::string line) { Movie movie; int idxBudget = line.find(';'); std::string strBudget = line.substr(0, idxBudget); movie.BudgetInDollars = std::stoll(strBudget); int idxDate = line.find(';', idxBudget + 1); movie.ReleaseDate = line.substr(idxBudget + 1, idxDate - idxBudget - 1); int idxRevenue = line.find(';', idxDate + 1); movie.RevenueInDollars = std::stoll(line.substr(idxDate + 1, idxRevenue - idxDate - 1)); int idxRuntime = line.find(';', idxRevenue + 1); movie.RuntimeInMinutes = std::stoi(line.substr(idxRevenue + 1, idxRuntime - idxRevenue - 1)); std::string title = line.substr(idxRuntime + 1, line.length() - idxRuntime - 2); movie.Title = title; return movie; } struct MovieComparer { // no member variables // one member function... custom "less than" bool operator()(Movie m1, Movie m2) { return m1.ReleaseDate < m2.ReleaseDate; // string comparison } }; int main() { // open a file std::string path = "tmdb.csv"; std::ifstream file(path); // think of ifstream as cin // did it open? if (!file.is_open()) { std::cout << "ERROR: the file " << path << " did not open" << std::endl; return -1; } // populate vector std::vector movies; std::string line; while (std::getline(file, line)) { Movie movie = parse(line); // convert std::string to Movie movies.push_back(movie); } // sort vector std::sort(movies.begin(), movies.end(), MovieComparer()); // echo movies for (int idx = 0; idx < movies.size(); idx++) { movies[idx].print(); } return 0; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

10. Modify tmdb.cpp so that the program outputs two columns: run time, followed by title.
The rows should be sorted by run time in decreasing order.

 

#include <algorithm>
#include <string>
#include <vector>
#include <cstdint>
#include <fstream>
#include <iostream>
// data source: https://www.kaggle.com/tmdb/tmdb-movie-metadata
struct Movie
{
std::string Title;
std::string ReleaseDate; // YYYY-MM-DD, so we can compare as a string
unsigned int RuntimeInMinutes;
uint64_t BudgetInDollars;
uint64_t RevenueInDollars;
void print()
{
//std::cout.precision(17);
std::cout
<< BudgetInDollars << ";"
<< ReleaseDate << ";"
<< RevenueInDollars << ";"
<< RuntimeInMinutes << ";"
<< Title << ";"
<< std::endl;
}
};
// convert line in csv to Movie struct
// columns: budget, release date, revenue, runtime, title
Movie parse(const std::string line)
{
Movie movie;
int idxBudget = line.find(';');
std::string strBudget = line.substr(0, idxBudget);
movie.BudgetInDollars = std::stoll(strBudget);
int idxDate = line.find(';', idxBudget + 1);
movie.ReleaseDate = line.substr(idxBudget + 1, idxDate - idxBudget - 1);
int idxRevenue = line.find(';', idxDate + 1);
movie.RevenueInDollars = std::stoll(line.substr(idxDate + 1, idxRevenue - idxDate
- 1));
int idxRuntime = line.find(';', idxRevenue + 1);
movie.RuntimeInMinutes = std::stoi(line.substr(idxRevenue + 1, idxRuntime -
idxRevenue - 1));
std::string title = line.substr(idxRuntime + 1, line.length() - idxRuntime - 2);
movie.Title = title;
return movie;
}
struct MovieComparer
{
 
 
 
 
 
 
 
// no member variables
// one member function... custom "less than"
bool operator()(Movie m1, Movie m2)
{
return m1.ReleaseDate < m2.ReleaseDate; // string comparison
}
};
int main()
{
// open a file
std::string path = "tmdb.csv";
std::ifstream file(path); // think of ifstream as cin
// did it open?
if (!file.is_open())
{
std::cout << "ERROR: the file " << path << " did not open" << std::endl;
return -1;
}
// populate vector
std::vector<Movie> movies;
std::string line;
while (std::getline(file, line))
{
Movie movie = parse(line); // convert std::string to Movie
movies.push_back(movie);
}
// sort vector
std::sort(movies.begin(), movies.end(), MovieComparer());
// echo movies
for (int idx = 0; idx < movies.size(); idx++)
{
movies[idx].print();
}
return 0;
}
Expert Solution
steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Knowledge Booster
File Input and Output Operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education