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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cctype> //used for toupper
using namespace std;
const int MAX_CITIES = 10;
const int NOT_FOUND = -1;
const int MAXLEN = 20;
const int MAXCOMMANDLEN = 9;
typedef char CityNameType[MAXLEN + 1];
enum CommandType { ADD, DELETE, UPDATE, OUTPUT, CLEAR, SUMMARIZE,
TOTAL, SORT, QUIT, UNKNOWN };
// This class handles the commands which find and store data for each
// city in the list.
class DestinationList
{
private:
int num; // number of objects in the list
Destination list[MAX_CITIES]; // array of destinations
//This function searches the list for a city name, and
//determines if the name is already in the list or not.
//params: in
int Search(CityNameType city)
{
for(int i = 0; i < num; i++)
{
if (list[i].DestinationHasName( city ))
return i;
}
return NOT_FOUND;
}
void Swap( Destination & Dest1, Destination & Dest2)
{
Destination temp = Dest1;
Dest1 = Dest2;
Dest2 = temp;
}
public:
//This constructor initializes num to 0.
DestinationList()
{
num = 0;
}
// This function adds a city name to the list, only if the list
// is not full, and the city does not already appear in the list.
//params: in, out
void Add ( )
{
CityNameType city;
cin >> city;
ToUpper();
Destination dest( city );
int index = Search(city);
if (num < (MAX_CITIES) && index == NOT_FOUND)
{
list[num] = dest;
num++;
cout << city << " added to the list.";
}
else if ( num >= (MAX_CITIES))
cout << city << " not added. List is full.";
else
cout << city << " is already in the list.";
cout << endl;
}
// This function updates a city's number of packages and weight,
// adding to the value totals.
//params: in, out
void Update ( )
{
CityNameType city;
cin >> city;
int numpackages;
float weight;
cin >> numpackages >> weight;
int index = Search(city);
if (index == NOT_FOUND)
cout << "Cannot update. " << city << " is not in the list."
<< endl;
else
{
list[index].RecordShipment( numpackages, weight );
cout << "Destination " << city << " updated with "
<< numpackages << " packages weighing "
<< fixed << showpoint << setprecision(2) << weight
<< " lbs." << endl;
}
}
// This function displays the Average weight of the packages
// in a certain city, or an error message.
//params: none
void Output ( )
{
CityNameType city;
cin >> city;
int index = Search(city);
if (index == NOT_FOUND)
cout << "Cannot output. " << city << " is not in the list."
<< endl;
else
cout << "Average weight of all packages to " << city << ": "
<< fixed << showpoint << setprecision(2)
<< list[index].AverageWeight() << endl;
}
//This function ends the program.
//params: none
void Quit ( )
{
cout << "Normal termination.";
}
//This function clears the number of packages and weight for
//the specified city.
//params: in, out
void Clear ( )
{
CityNameType city;
cin >> city;
int index = Search(city);
if (index == NOT_FOUND)
cout << "Cannot clear. " << city << " is not in the list."
<< endl;
else
list[index].ClearShipment();
}
//Get the command from the user input
CommandType GetCommand()
{
char command [MAXCOMMANDLEN + 1];
cin >> command;
if (strcmp(command, "Add") == 0)
return ADD;
else if (strcmp(command, "Delete") == 0)
return DELETE;
else if (strcmp(command, "Update") == 0)
return UPDATE;
else if (strcmp(command, "Output") == 0)
return OUTPUT;
else if (strcmp(command, "Clear") == 0)
return CLEAR;
else if (strcmp(command, "Summarize") == 0)
return SUMMARIZE;
else if (strcmp(command, "Total") == 0)
return TOTAL;
else if (strcmp(command, "Sort") == 0)
return SORT;
else if (strcmp(command, "Quit") == 0)
return QUIT;
else
return UNKNOWN;
}
//Converts city to all uppercase
void ToUpper()
{
CityNameType city;
for ( int i = 0; i < strlen(city); i++ )
city[i] = toupper(city[i]);
}
int main ()
{
DestinationList list;
CommandType command;
GetCommand();
switch(command)
{
case ADD:
Add().list;
break;
case 'DELETE':
Delete();
break;
case 'UPDATE':
Update();
break;
case 'OUTPUT':
case 'CLEAR':
case 'SUMMARIZE':
case 'TOTAL':
case 'SORT':
case 'QUIT':
case 'UNKNOWN':
}
list.Quit();
}
|