Question & Answer: My professor gave us this assignemnt. The input file is provided by him an…..

My professor gave us this assignemnt. The input file is provided by him and cannot be changed. I will attach it.

Needed in C++

There is a file of a few movies that contains the following data:

Date of release
Movie name
Production cost
Gross profit

Your assignment is: for each movie,

1. Without changing the data, read in the Date of release,
the Movie name, the Production cost and the Gross profit
for all of the movies and put the information into an
array of structs (or classes).
2. Sort the array of movies by Movie Name alphabetically
3. Print the array of movies with a heading.
The fields should be printed in the following order:
Movie Name, Release Date, Production cost and Gross profit
4. Sort the array of movies by Percent Profit (Gross profit/Production Cost)
3. Print the array of movies with a heading.
The fields should be printed in the following order:
Movie Name, Production cost, Release Date and Percent Profit

                                                                                                   11111
         11111111112222222222333333333344444444445555555555666666666677777777778888888888999999999900000
12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234
Release Date         Movie                                   Production Budget        Worldwide Box Office
Dec 15, 1939         Gone with the Wind                         $3,900,000                  $390,525,192
Jun 4, 1982          Poltergeist                               $10,700,000                  $121,706,019
Feb 9, 2001          Hannibal                                  $87,000,000                  $350,100,000
Mar 23, 2001         Heartbreakers                             $38,000,000                   $57,753,825
Aug 3, 2001          Original Sin                              $26,000,000                   $16,521,410
Jun 14, 2002         Windtalkers                              $115,000,000                   $77,628,265
Mar 14, 2003         Agent Cody Banks                          $25,000,000                   $58,240,458
Apr 15, 2005         The Amityville Horror                     $18,500,000                  $108,800,304
Mar 16, 2007         Premonition                               $20,000,000                   $81,461,343
Feb 22, 2008         Charlie Bartlett                          $12,000,000                    $5,295,909
Dec 25, 2008         Valkyrie                                  $90,000,000                  $203,902,107
Jun 12, 2009         The Taking of Pelham 123                 $110,000,000                  $152,364,370
Jul 8, 2011          Zookeeper                                 $80,000,000                  $170,805,525
Dec 20, 2011         The Girl with the Dragon Tattoo           $90,000,000                  $239,373,970
Mar 16, 2012         21 Jump Street                            $42,000,000                  $202,812,429
Aug 8, 2012          Hope Springs                              $30,000,000                  $115,849,781
Nov 8, 2012          Skyfall                                  $200,000,000                $1,110,526,981
Dec 14, 2012         The Hobbit: An Unexpected Journey        $250,000,000                $1,017,003,568
Jan 25, 2013         Hansel & Gretel: Witch Hunters            $50,000,000                  $214,949,716
Mar 27, 2013         G.I. Joe: Retaliation                    $140,000,000                  $371,923,060
Oct 18, 2013         Carrie                                    $30,000,000                   $82,409,520
Jun 13, 2014         22 Jump Street                            $50,000,000                  $331,333,876
Dec 17, 2014         The Hobbit: The Battle of the 5 Armies   $250,000,000                  $955,119,788
Feb 20, 2015         Hot Tub Time Machine 2                    $14,000,000                   $12,452,601
May 8, 2015          Hot Pursuit                               $35,000,000                   $45,680,201

I can’t edit the source file. For the people that keep saying that. I wish I could but I can’t, it’s part of the assigment.

Expert Answer

 

c++ code:

#include <iostream>

#include <fstream>

#include <iomanip>

#include <sstream>

#include <algorithm>

using namespace std;

//defining structure with having arrays

typedef struct Movie {

//Define Arrays

string name, date;

float budget, sale;

} Movie;

class punct_facet: public std::numpunct<char>

{

char do_decimal_point() const { return ‘.’; }

char do_thousands_sep() const { return ‘,’; }

std::string do_grouping() const { return “3”; }

};

string formatCurrency(int amount) {

ostringstream stream;

stream.imbue(locale(cout.getloc(), new punct_facet));

stream << amount;

return “$” + stream.str();

}

void writetofin(Movie *movies, int count) {

for (int i = 0; i < count; i++) {

cout << setw(20) << movies[i].name;

cout << setw(15) << movies[i].date;

cout << setw(15) << formatCurrency(movies[i].budget);

cout << setw(15) << formatCurrency(movies[i].sale) << endl;

}

}

void sortByName(Movie *movies, int count){

Movie ct;

for (int i = 0; i < count; i++) {

for(int j=i+1; j < count; j++)

{

if(movies[i].name.compare(movies[j].name) > 0)

{

ct=movies[i];

movies[i]=movies[j];

movies[j]=ct;

}

}

}

}

double getProfit(Movie m) {

return (m.sale – m.budget)/m.budget;

}

void sortByProfit(Movie *movies, int count){

Movie ct;

for (int i = 0; i < count; i++) {

for(int j=i+1; j < count; j++)

{

if(getProfit(movies[i]) > getProfit(movies[j]))

{

ct=movies[i];

movies[i]=movies[j];

movies[j]=ct;

}

}

}

}

int main() {

string inputFile;

cout << “Enter the input file name: “;

cin >> inputFile;

int count = 0;

Movie movies[100];

 

ifstream file(inputFile.c_str());

if(!file) {

cerr << “Unable to find input file.” << endl;

return 1;

}

string line;

while(getline(file, line))

{

stringstream linestream(line);

string date, name, prod_cost, sale;

getline(linestream, movies[count].date, ‘t’); // read up-to the first tab (discard tab).

getline(linestream, movies[count].name, ‘t’);

getline(linestream, prod_cost, ‘t’);

prod_cost.erase(remove(prod_cost.begin(), prod_cost.end(), ‘,’), prod_cost.end());

prod_cost.erase(remove(prod_cost.begin(), prod_cost.end(), ‘$’), prod_cost.end());

movies[count].budget = atof(prod_cost.c_str());

getline(linestream, sale, ‘t’);

sale.erase(remove(sale.begin(), sale.end(), ‘,’), sale.end());

sale.erase(remove(sale.begin(), sale.end(), ‘$’), sale.end());

movies[count].sale = atoi(sale.c_str());

count++;

}

writetofin(movies, count);

cout << endl << “Sorted by profit: ” << endl;

sortByProfit(movies, count);

writetofin(movies, count);

cout << endl << “Sorted by name: ” << endl;

sortByName(movies, count);

writetofin(movies, count);

return 0;

}

output:

Still stressed from student homework?
Get quality assistance from academic writers!