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
|
//----------------------------------------------------------------------
// Purpose: Maintain list of clubs by processing commands. Valid
// commands are A (Add), D (Delete), P (Print), and S (Sort,
// ascending order). Print an error if the command is invalid
// and ignore the rest of the command. Add at the end of the
// list. Delete by replacing the club to be deleted with the
// last club in the list. Repeat this process until
// end-of-file: Use Ctrl-Z in Visual Studio for end-of-file.
//
// Input: List of commands, some with arguments, terminated by
// end-of-file.
//
// Output: A message is printed when a club is added to the list,
// deleted from the list, and sorted. If the command is not
// valid, a message will be printed. If the P command is typed,
// the list is sorted in ascending order using
// the Selection Sort
// algorithm from the textbook and then printed.
//-----------------------------------------------------------------------
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const int MAX_CLUBS = 6;
const int CLUB_SPACING = 20;
int Find(const string clubs[], int num, string club);
//DO_8: Write the prototype for function Add here
void Delete(string clubs[], int & num);
void Sort(string clubs[], int num);
void PrintClub(const string clubs[], int num);
void GetClubName(string & name);
int main()
{
//DO_2: declare an arrays of strings named "clubs" of size MAX_CLUBS
string clubs[MAX_CLUBS];
int numClubs = 0;
char command;
cout << fixed << showpoint << setprecision(2);
cin >> command;
while (cin)
{
//DO_3: write code to call the function to process a command,
// calling the proper function. Valid commands are
// A, D, P, and S.
if (command == 'A')
{
void Add();
}
if (command == 'D')
{
void Delete();
}
if (command == 'P')
{
void PrintClub();
}
if (command == 'S')
{
void Sort();
}
else
{
cout << "Bad Command!" << endl;
cin.ignore(200, '\n');
}
cin >> command;
}
cout << "Normal Termination of Lab 9." << endl;
return 0;
}
//-----------------------------------------------------------------------
// This function will search for the name in the array club[]. It will
// return a -1 if it is not found, and the index of the array name if it
// is found.
// params: (in, in)
//-----------------------------------------------------------------------
int Find(const string clubs[], int num, string club)
{
// DO_4: write the body of the Find function. Refer to the comments.
for (int i = 0; i < MAX_CLUBS; ++i)
{
if (clubs[i] == club)
return i;
}
return -1;
}
//-----------------------------------------------------------------------
// This function will add a club to the end of the list if the
// club is not already in the list, and if the list is not full.
// params: (out, out)
//-----------------------------------------------------------------------
//DO_5: Complete the function header for the Add function.
void Add(int index, int num)
{
string club;
GetClubName(club);
//DO_6: Call the Find function, saving the result in index.
int Find();
if (index >= 0)
{
cout << club << " is already in the list." << endl;
}
else if (num >= MAX_CLUBS)
{
cout << club + " wasn't added. List is full." << endl;
}
else
{
//DO_7: add the club to the end of the array and increment the
// number.
cout << club + " was added to the list." << endl;
}
}
//------------------------------------------------------------------------
// This function will delete a club from the array list if the club is
// in the array. It will replace the club with the last member in the
// array.
// params: (out,out)
//------------------------------------------------------------------------
void Delete(string clubs[], int & num)
{
string club;
GetClubName(club);
//DO_9: Call the Find function, saving the result in index.
if (index < 0)
{
cout << club + " was not removed because it is not in the list."
<< endl;
}
else
{
//DO_10: remove the team by replacing it with the last member, and
// decrement the number of members.
cout << club + " was removed from the list." << endl;
}
}
//------------------------------------------------------------------------
// This function prints the list of clubs.
// params: (in, in)
//------------------------------------------------------------------------
void PrintClub(const string clubs[], int num)
{
cout << "The list of clubs is:" << endl;
for (int i = 0; i < num; i++)
cout << " " << left << setw(CLUB_SPACING) << clubs[i] << endl;
}
//------------------------------------------------------------------------
// This function will sort the list of club in ascending order.
// params: (out, in)
//------------------------------------------------------------------------
void Sort(string clubs[], int num)
{
for (int i = 0; i < num - 1; i++)
{
//DO_11: declare mindex and define it
for (int j = i + 1; j < num; j++)
{
//DO_12: Check to see if the current slot is smaller than mindex
// and take appropriate action.
}
//DO_13: Swap the array cells at i and mindex.
}
cout << "List sorted by club." << endl;
}
void GetClubName(string & name)
{
char dummy;
// Skip over spaces
while (cin.peek() == ' ')
cin.get(dummy);
// Read characters into name, including spaces, till end of line
getline(cin, name);
}
|