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; }

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 6PE
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
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning
New Perspectives on HTML5, CSS3, and JavaScript
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:
9781305503922
Author:
Patrick M. Carey
Publisher:
Cengage Learning