Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Program. I have included SamplePie.cpp, ChartDriver.cpp and ChartData.txt under the instructions. Thank you so much for your help! --------------------------Instructions---------------------------- Charts come in many forms.

C++ Program. I have included SamplePie.cpp, ChartDriver.cpp and ChartData.txt under the instructions. Thank you so much for your help!

--------------------------Instructions----------------------------

Charts come in many forms. Two common types are bar charts and pie charts. You are to create a collection of classes that store and display data for charts. To do this, you must create three classes:

Chart Class (base class)

This class must contain an array of Categories (Categories are structs that have two fields, a string to hold the title of the category and a double to hold the categorys value) and an int that stores the number of categories currently in the Chart. Use 10 as the maximum number of Categories in a chart. Make sure you use protected instead of private for your data members.

The Chart class must contain:

Constructors (default and copy).

An accessor for the number of categories in the chart (not for the array itself).

Overloaded operators: [], and >>. The overloaded input operator (>>) must assume that the chart is stored using this format: first an integer with the number of categories in the chart, then each category with the title on one line and the value on the next. Use getline() to read the titles, they may have more than one word. The subscript operator ([]), returns the category in the Charts array at the arguments index.

A function to add a category onto the end of the current list of categories. Dont exceed maximum.

A displayChart function that accepts an ostream as a call-by-reference parameter that displays the categories as a simple list.

PieChart Class (derived from Chart class)

This class must be publicly-derived from the chart class. Add any data members that you might need, but remember that you have access to the public and protected members of the Chart class.

The PieChart class must contain:

Constructors (default and copy).

A displayChart function that accepts an ostream as a call-by-reference parameter that displays the categories in a pie chart. Code for printing a pie chart is available in the file SamplePie.cpp which is available on Moodle. The displayChart function must have an identical header as the one in the Chart class. Also, have this function print a legend.

BarChart Class (derived from Chart class)

This class must be publicly-derived from the chart class. Add any data members that you might need, but remember that you have access to the public and protected members of the Chart class.

The BarChart class must contain:

Constructors (default and copy).

A displayChart function that accepts an ostream as a call-by-reference parameter that displays the categories in a vertical bar chart. You must scale the chart so that the largest Category is 20 lines tall and the others are in correct proportion. The displayChart function must have an identical header as the one in the Chart class. This function must also print a legend.

Driver details:

You are to complete a small driver to test your Chart classes. Most of the driver is already done in the file ChartDriver.cpp available on Moodle. Fill in the missing pieces. The program needs a data file, called ChartData.txt.

-------------------------------SamplePie.cpp-------------------------------------------

////////////////////////////////////////////////////////////////////////////////// // This program requests four values from the user, representing survey results // // from a poll which was taken to determine people's favorite Three Stooges // // character: Moe, Larry, Curly, or Shemp. Percentages are computed and the // // results are displayed in a pie chart composed entirely of characters. // ////////////////////////////////////////////////////////////////////////////////// #include  #include  #include  using namespace std; void queryUser(int total[], int &stgTot); void calculate(int tot, int stgTot, double &pct, double &ang); void outputPie(double angle[]); void outputLegend(double percent[]); const int MAXRADIUS = 6; // Pie chart radius (in characters) const double PI = 3.141592653589793; // Extremely precise approximation const char MOECHAR = ':', // Pie chart character for Moe LARRYCHAR = '#', // " " " " Larry CURLYCHAR = 'O', // " " " " Curly SHEMPCHAR = '\\'; // " " " " Shemp void main() { int surveyTotal[4]; // The survey results for the four stooges, indicating // how many respondents selected each character as // their favorite stooge; #0 is Moe, #1 is Larry, // #2 is Curly, #3 is Shemp. int stoogeTotal; // The total number of survey respondents (i.e., the // sum of the four surveyTotal values). double surveyPercent[4]; // The percentage of the survey results allocated to // each stooge; #0 is Moe, #1 is Larry, #2 is Curly, // #3 is Shemp. double surveyAngle[4]; // The angle (in radians) encompassed by each section // of the pie chart. Thus, Moe's section goes from 0 // to surveyAngle[0], Larry's from surveyAngle[0] to // surveyAngle[1], etc. int i; // Loop iteration variable. queryUser(surveyTotal, stoogeTotal); for (i = 0; i <= 3; i++) calculate(surveyTotal[i], stoogeTotal, surveyPercent[i], surveyAngle[i]); outputPie(surveyAngle); outputLegend(surveyPercent); return; } //////////////////////////////////////////////////////////////////////////// // Query the user for the survey results, compute percentages and angles. // //////////////////////////////////////////////////////////////////////////// void queryUser(int total[], int &stgTot) { int i; // Loop iteration variable. cout << "How many people surveyed chose Moe as their favorite Stooge? "; cin >> total[0]; cout << "How many people surveyed chose Larry as their favorite Stooge? "; cin >> total[1]; cout << "How many people surveyed chose Curly as their favorite Stooge? "; cin >> total[2]; cout << "How many people surveyed chose Shemp as their favorite Stooge? "; cin >> total[3]; stgTot = 0; for (i = 0; i <= 3; i++) stgTot += total[i]; return; } ////////////////////////////////////////////////////////////////////////// // Calculate the popularity percentage and maximum pie-chart angle for // // the designated stooge, using a running tally of the pie-chart angle // // that has already been traversed by previous stooge calculations. // ////////////////////////////////////////////////////////////////////////// void calculate(int tot, int stgTot, double &pct, double &ang) { static double angleSoFar = 0.0; // This static variable tallies the // total of all of the angles for // the previously computed stooges. pct = double(tot) / stgTot; ang = angleSoFar + 2 * PI * pct; angleSoFar = ang; return; } ////////////////////////////////////////////////////////////////////////// // Cycle point by point through the character positions. For any char- // // acter within the pie chart's boundaries, determine which stooge's // // pie wedge it lies within, and insert the appropriate character. // ////////////////////////////////////////////////////////////////////////// void outputPie(double angle[]) { int x, // The horizontal and vertical coordinates y; // of the pie chart point being considered. double distanceFromCenter; // The distance (in characters) from the // pie chart's center to the point which // is currently being considered. double currentAngle; // The angle (in radians) formed by the three // points: the point at the top of the pie // chart, the center of the pie chart, and the // point which is currently being considered. cout << endl; for (y = MAXRADIUS; y >= -MAXRADIUS; y--) { cout << '\t'; for (x = int(-1.5*MAXRADIUS); x <= int(1.5*MAXRADIUS); x++) { distanceFromCenter = sqrt((2.0*x / 3.0) * (2.0*x / 3.0) + y * y); if (distanceFromCenter > MAXRADIUS) cout << ' '; else { currentAngle = atan2(2.0*x / 3.0, y); // atan2 returns arctan of 2x/3y if (currentAngle < 0) // (adjusting if y=0); 2*PI may be currentAngle += 2 * PI; // added to yield angles in [0,2*PI]. if (currentAngle <= angle[0]) cout << MOECHAR; else if (currentAngle <= angle[1]) cout << LARRYCHAR; else if (currentAngle <= angle[2]) cout << CURLYCHAR; else cout << SHEMPCHAR; } } cout << endl; } return; } ////////////////////////////////////////////////////////////// // A small legend is added below the pie chart to indicate // // which character is associated with which of the stooges. // ////////////////////////////////////////////////////////////// void outputLegend(double percent[]) { cout.setf(ios::fixed); cout << setprecision(3); cout << "\t\t\t\t" << MOECHAR << " - Moe: " << (percent[0] * 100) << "% "; cout << "\t\t\t\t" << LARRYCHAR << " - Larry: " << (percent[1] * 100) << "% "; cout << "\t\t\t\t" << CURLYCHAR << " - Curly: " << (percent[2] * 100) << "% "; cout << "\t\t\t\t" << SHEMPCHAR << " - Shemp: " << (percent[3] * 100) << "% "; cout << endl; return; } 

-------------------------------ChartDriver.cpp-------------------------------------------

#include  #include  #include  #include  #include "Chart.h" #include "PieChart.h" #include "BarChart.h" using namespace std; typedef Chart* chartPtr; void fillArray(chartPtr list[], int &size); void modChart(chartPtr list[], int size); int getIndex(int size); char getChoice(int size); void main() { chartPtr chartList[10]; int size; char choice; cout << " \t\tWelcome to the charting program! "; fillArray(chartList, size); do { choice = getChoice(size); switch (choice) { case '1': (*chartList[getIndex(size)]).displayChart(cout); break; case '2': modChart(chartList, size); break; case 'Q': cout << " \t\t\tGoodbye "; for (int i = 0; i> index; while ((index < 1) || (index > size)) { cout << "That is not a valid chart number. Try again. "; cout << "Which chart would you like? (1.." << size << "): "; cin >> index; } index--; return index; } char getChoice(int size) { char choice; cout << "There are " << size << " charts. " << "What would you like to do? " << endl << "1) View a chart " << "2) Add a category to a chart " << "Q) Quit " << "?: "; cin >> choice; choice = toupper(choice); while ((choice != 'Q') && (choice != '1') && (choice != '2')) { cout << " *** '" << choice << "' is not a listed choice. Try again. *** "; cout << "There are " << size << " charts. " << "What would you like to do? (Choose 1.." << size << endl << "1) View a chart " << "2) Add a category to a chart " << "Q) Quit " << "?: "; cin >> choice; choice = toupper(choice); } return choice; } void fillArray(chartPtr list[], int &size) { } 

-------------------------------ChartData.txt-------------------------------------------

B 5 Widgets 21314.32 Gadgets 25150.89 Thingamajiggs 32451.21 Watzits 18034.87 Themthings 20414.21 P 5 Gump 23 Falcon 25 Sam 20 Lambert 30 Topic 15 C 2 Friendly 45.38 UnFriendly 36.32 P 3 Yes 8 No 5 Undecided 87 B 6 A 295 B 302 C 189 D 248 E 312 F 274 C 6 Larry 20 Moe 25 Curly 35 Shemp 20 Joe 8 Curly Joe 2 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Visual Basic 6 Databases

Authors: Charles Williams

1st Edition

1861002025, 978-1861002020

More Books

Students also viewed these Databases questions