1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
#include<iostream>
#include<iomanip>
#include<string>
#include<cctype>
using namespace std;
const string CARPOOL = "Carpool",
FAMILY = "Family",
SPORT = "Sport";
// TODO (Objective 1): Replace named constants for capacity groupings with
// the required CapacityGroup enumeration
enum CapacityGroup{CG_ERR, CG_1_2, CG_3_4, CG_5_6, CG_7_8, CG_9_12};
// Named constants for seating capacity groupings
// TODO (Objective 2): Define the required Vehicle structure with two fields
struct Vehicle
{
string category;
int occupants;
};
// category: type string
// occupants: type int
//------------------------------------------------------------------------------
// function prototype declarations
void DisplayResults(string category, int occupants);
// TODO (Objective 3a): Replace string and int paramters with 1 Vehicle structure type
string GetRecommendation(Vehicle motorVehicle);
// OPTIONAL (Idea 1a): Change return type to CapacityGroup
int GetCapacityGroup(int occupants);
// OPTIONAL (Idea 2a): Change parameter type to CapacityGroup
string GetCarpoolRecommendation(int group);
string GetFamilyRecommendation(int group);
string GetSportRecommendation(int group);
//------------------------------------------------------------------------------
int main()
{
/*
DO NOT MODIFY the main() function block!
*/
cout << endl
<< "------------------------------------------------------" << endl
<< "This program recommends a vehicle based on its purpose" << endl
<< "and the seating capacity for expected occupants." << endl
<< "------------------------------------------------------" << endl
<< endl;
// Test Carpool category.
cout << CARPOOL << ":" << endl;
for(int occupants = 13; occupants >= 0; occupants--)
DisplayResults(CARPOOL, occupants);
cout << endl;
// Test Family category.
cout << FAMILY << ":" << endl;
for(int occupants = 13; occupants >= 0; occupants--)
DisplayResults(FAMILY, occupants);
cout << endl;
// Test Sport category.
cout << SPORT << ":" << endl;
for(int occupants = 13; occupants >= 0; occupants--)
DisplayResults(SPORT, occupants);
cout << endl;
// Test unknown and not specified category
cout << "Other:" << endl;
DisplayResults("Vacation", 5); // category unknown
DisplayResults("", -1); // category not specified
cout << endl << "- End Program - " << endl;
}
//------------------------------------------------------------------------------
void DisplayResults(string category, int occupants)
{
// TODO (Objective 3c): Declare Vehicle structure variable
Vehicle motorVehicle;
// TODO (Objective 3c): Initialize Vehicle fields with function parameter values
category = motorVehicle.category;
occupants = motorVehicle.occupants;
// TODO (Objective 3c): Replace function call arguments with 1 Vehicle structure variable
string recommendation = GetRecommendation(motorVehicle);
cout << setw(12) << occupants << " occupants - "
<< recommendation << endl;
}
//------------------------------------------------------------------------------
// OPTIONAL (Idea 1a): Change return type to CapacityGroup
int GetCapacityGroup(int occupants)
{
// OPTIONAL (Idea 1b): Change variable type to match function return type
int group;
if( occupants < 1 || occupants > 12 ) // invalid
group = CG_ERR;
else if( occupants >= 9 ) // 9-12
group = CG_9_12;
else if( occupants >= 7 ) // 7-8
group = CG_7_8;
else if( occupants >= 5 ) // 5-6
group = CG_5_6;
else if( occupants >= 3 ) // 3-4
group = CG_3_4;
else // 1-2
group = CG_1_2;
return group;
}
//------------------------------------------------------------------------------
// TODO (Objective 3a): Replace string and int parameters with 1 Vehicle parameter
string GetRecommendation(Vehicle motorVehicle)
{
// TODO (Objective 3b): Use Vehicle fields where necessary in place of
// string and int parameters.
// NOTE: Should require 4 modifications
// OPTIONAL (Ideas 1c/2b): Change variable type to match function return type
// Get the capacity group for the occupants parameter.
int group = GetCapacityGroup(motorVehicle.occupants);
string recommendation;
// Compare the category parameter to string constants.
if( motorVehicle.category == CARPOOL )
{
recommendation = GetCarpoolRecommendation(group);
}
else if( motorVehicle.category == FAMILY )
{
recommendation = GetFamilyRecommendation(group);
}
else if( motorVehicle.category == SPORT )
{
recommendation = GetSportRecommendation(group);
}
else
{
recommendation = "Purpose unknown or not specified.";
}
return recommendation;
}
//------------------------------------------------------------------------------
|