joecrouch_75
n00b
- Joined
- Jun 29, 2006
- Messages
- 13
So, here's my program I'm doing for my C++ class. It works perfectly so far, however I need to sort the elements in the array alphabetically. So, when the user utilizes the print (P) command, the "counters" will be listed in alphabetical order. However, for the life of me, I cannot get it to work and I've tried everything I thought possible. Any suggestions?
THE CODE:
SAMPLE INPUT:
A Tach1 0.35 M
A Tach2 0.44 M
O Tach1
U Tach2 4006 3
U Tach1 5043 2
U Tach2 20100 7
P
O Tach2
O Tach3
A Tach3 0.17 M
A Tach1 0.32 M
A Fuel1 0.97 G
A Fuel2 2.03 G
A Tach4 1.77 M
U Fuel1 48237 380
U Tach3 36009 5
P
O Fuel1
O Fuel1
U Fuel2 3828 50
U Fuel3 3828 50
U Fuel1 3828 50
U Tach2 10100 3
P
D Fuel1
D Fuel1
P
D Tach3
P
D Tach1
D Tach2
D Fuel2
P
Q
CORRESPONDING SAMPLE OUTPUT:
A Tach1 0.35 M
Tach1 added to the list.
A Tach2 0.44 M
Tach2 added to the list.
O Tach1
Tach1: 0 counts per second
U Tach2 4006 3
Counter Tach2 updated with 4006, 3
U Tach1 5043 2
Counter Tach1 updated with 5043, 2
U Tach2 20100 7
Counter Tach2 updated with 20100, 7
P
The counter values are:
Counter 0 - Tach1: 882.525 MPH
Counter 1 - Tach2: 1060.66 MPH
O Tach2
Tach2: 2410.6 counts per second
O Tach3
Cannot output. Tach3 is not in the list.
A Tach3 0.17 M
Tach3 added to the list.
A Tach1 0.32 M
Tach1 was updated with new information.
A Fuel1 0.97 G
Fuel1 added to the list.
A Fuel2 2.03 G
Fuel2 added to the list.
A Tach4 1.77 M
Tach4 not added. List is full.
U Fuel1 48237 380
Counter Fuel1 updated with 48237, 380
U Tach3 36009 5
Counter Tach3 updated with 36009, 5
P
The counter values are:
Counter 0 - Fuel1: 123.131 GPH
Counter 1 - Fuel2: 0 GPH
Counter 2 - Tach1: 0 MPH
Counter 3 - Tach2: 0 MPH
Counter 4 - Tach3: 1224.31 MPH
O Fuel1
Fuel1: 126.939 counts per second
O Fuel1
Fuel1: 0 counts per second
U Fuel2 3828 50
Counter Fuel2 updated with 3828, 50
U Fuel3 3828 50
Cannot update. Fuel3 is not in the list.
U Fuel1 3828 50
Counter Fuel1 updated with 3828, 50
U Tach2 10100 3
Counter Tach2 updated with 10100, 3
P
The counter values are:
Counter 0 - Fuel1: 74.2632 GPH
Counter 1 - Fuel2: 155.417 GPH
Counter 2 - Tach1: 0 MPH
Counter 3 - Tach2: 1481.33 MPH
Counter 4 - Tach3: 1224.31 MPH
D Fuel1
Fuel1 deleted from the list.
D Fuel1
No delete. Fuel1 not in the list.
P
The counter values are:
Counter 0 - Fuel2: 155.417 GPH
Counter 1 - Tach1: 0 MPH
Counter 2 - Tach2: 1481.33 MPH
Counter 3 - Tach3: 1224.31 MPH
D Tach3
Tach3 deleted from the list.
P
The counter values are:
Counter 0 - Fuel2: 155.417 GPH
Counter 1 - Tach1: 0 MPH
Counter 2 - Tach2: 1481.33 MPH
D Tach1
Tach1 deleted from the list.
D Tach2
Tach2 deleted from the list.
D Fuel2
Fuel2 deleted from the list.
P
The counter values are:
Q
Normal Termination of Counter Program.
THE CODE:
Code:
#include <iostream>
#include <string>
#include <string.h>
#include <stddef>
using namespace std;
const char ADD_COMMAND = 'A';
const char DELETE_COMMAND = 'D';
const char OUTPUT_COMMAND = 'O';
const char PRINT_COMMAND = 'P';
const char UPDATE_COMMAND = 'U';
const char QUIT_COMMAND = 'Q';
const char GALLONS_SYMBOL = 'G';
const char MILES_SYMBOL = 'M';
const char POUNDS_SYMBOL = 'P';
const int NO_TIME = 0;
const int NO_ARRAY = -1;
const int LIST_MAX = 5;
const int NAME_MAX = 20;
enum DisplayUnits
{
GALLONS_PER_HOUR,
MILES_PER_HOUR,
POUNDS_PER_HOUR
};
struct Counter
{
int totalCounts, totalTime;
char counterName[NAME_MAX];
DisplayUnits units; // display units
float scalingFactor; // multiplies counts per second
};
struct CounterList
{
int Number;
Counter Currents[LIST_MAX];
};
int CounterSearch(CounterList& Counters, string name);
bool DeleteCounter (CounterList& list, string name);
void AddOpperation(CounterList& Counters);
void DeleteOpperation(CounterList& Counters);
void OutputOpperation(CounterList& Counters);
void PrintOpperation(CounterList& Counters);
void UpdateOpperation(CounterList& Counters);
void QuitOpperation();
bool LessThan (const Counter& c1, const Counter& c2);
void DisplayCounter (const Counter& c);
void InitCounter (Counter& c, char name[], float scale, char units);
void UpdateCounter (Counter& c, int counts, int time);
bool CounterHasName (const Counter& c, const string name);
float CounterValue (Counter& c);
void main()
{
char command;
CounterList Counters;
Counters.Number = 0;
while (!cin.eof())
{
cin >> command;
switch (command)
{
case ADD_COMMAND: AddOpperation(Counters);
break;
case DELETE_COMMAND: DeleteOpperation(Counters);
break;
case OUTPUT_COMMAND: OutputOpperation(Counters);
break;
case PRINT_COMMAND: PrintOpperation(Counters);
break;
case UPDATE_COMMAND: UpdateOpperation(Counters);
break;
case QUIT_COMMAND: QuitOpperation();
break;
}
}
}
//-----------------------------------------------------------------------
// Searches if the counter is already in the list. Returns the index
// location of the counter, else returns -1 if there is no counter.
// params: (in, in)
//-----------------------------------------------------------------------
int CounterSearch(CounterList& Counters, string name)
{
bool CorrectCounter = false;
for (int i = 0; CorrectCounter == false && i < Counters.Number; i++)
{
CorrectCounter = CounterHasName(Counters.Currents[i], name);
if (CorrectCounter)
return i;
}
return NO_ARRAY;
}
//-----------------------------------------------------------------------
// If a counter with the given name is in the list, it is deleted and
//true is returned. Otherwise, false is returned.
// params: ()
//-----------------------------------------------------------------------
bool DeleteCounter (CounterList& list, string name)
{
int search;
search = CounterSearch(list, name);
if (search == NO_ARRAY)
return false;
else
{
for (int i = search; i < list.Number - 1; i++)
list.Currents[i] = list.Currents[i + 1];
list.Number--;
return true;
}
}
//-----------------------------------------------------------------------
// Recieves Counter name to Add.
// Determines if the list is full. Will not add if full.
// Determines if the coutner being added is already in the list. If it
// is in the list, it will not add. Else adds the counter.
// params: (inout)
//-----------------------------------------------------------------------
void AddOpperation(CounterList& Counters)
{
Counter newCounter;
char CounterName[NAME_MAX], units;
float scalingFactor;
int search;
cin >> CounterName >> scalingFactor >> units;
search = CounterSearch(Counters, CounterName);
if (search == NO_ARRAY)
{
if (Counters.Number == LIST_MAX)
cout << CounterName << " not added. List is full." << endl;
else
{
InitCounter(Counters.Currents[Counters.Number], CounterName, scalingFactor, units);
cout << CounterName << " added to the list." << endl;
Counters.Number++;
}
}
else
{
InitCounter(Counters.Currents[search], CounterName, scalingFactor,
units);
cout << CounterName << " was updated with new information."
<< endl;
}
}
//-----------------------------------------------------------------------
//
//-----------------------------------------------------------------------
void DeleteOpperation(CounterList& Counters)
{
string CounterName;
cin >> CounterName;
if (DeleteCounter(Counters, CounterName))
cout << CounterName << " deleted from the list." << endl;
else
{
cout << "No delete. " << CounterName << " not in the list."
<< endl;
}
}
//-----------------------------------------------------------------------
// Recieves Counter name to output.
// Determines if the counter being output is in the list.
// If the counter isn't the list it will not output, else the function
// displays the counter and the counts per second.
// params: (inout)
//-----------------------------------------------------------------------
void OutputOpperation(CounterList& Counters)
{
string CounterName;
float counts;
int search;
cin >> CounterName;
search = CounterSearch(Counters, CounterName);
if (search == NO_ARRAY)
{
cout << "Cannot output. " << CounterName << " is not in the list."
<< endl;
}
else
{
counts = CounterValue(Counters.Currents[search]);
cout << CounterName << ": " << counts << " counts per second"
<< endl;
}
}
//-----------------------------------------------------------------------
//
//-----------------------------------------------------------------------
void PrintOpperation(CounterList& Counters)
{
cout << "The counter values are:" << endl;
for (int i = 0; i < Counters.Number; i++)
{
cout << "Counter " << i << " - ";
DisplayCounter(Counters.Currents[i]);
cout << endl;
}
}
//-----------------------------------------------------------------------
// Recieves update information.
// Determines if the counter being updated is in the list.
// If the counter isn't in the list it will not update, else the function
// updates the counter through the UpdateCounter function.
// Displays that the counter was updated.
// params: (inout)
//-----------------------------------------------------------------------
void UpdateOpperation(CounterList& Counters)
{
string CounterName;
int counts, time, search;
cin >> CounterName >> counts >> time;
search = CounterSearch(Counters, CounterName);
if (search == NO_ARRAY)
{
cout << "Cannot update. " << CounterName << " is not in the list."
<< endl;
}
else
{
UpdateCounter(Counters.Currents[search], counts, time);
cout << "Counter " << CounterName << " updated with " << counts
<< ", " << time << endl;
}
}
//-----------------------------------------------------------------------
//
//-----------------------------------------------------------------------
void QuitOpperation()
{
cout << "Normal Termination of Counter Program.";
exit(0);
}
//-----------------------------------------------------------------------
//----------ONLY FUNCTIONS ALLOWED TO MANIPULATE COUNTER-----------------
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// Returns true if the name for c1 is "less than" the name for c2;
// returns false otherwise.
// You will need this to maintain the list in sorted order.
// params: ()
//-----------------------------------------------------------------------
bool LessThan (const Counter& c1, const Counter& c2)
{
for (int i = 0; c1.counterName[i] != '\0'; i++)
{
if (c1.counterName[i] < c2.counterName[i])
return true;
}
return false;
}
//-----------------------------------------------------------------------
// Prints the Counter display to the standard output (cout).
// See #2 above for how this is calculated.
// Use GPH for GALLONS_PER_HOUR, MPH for MILES_PER_HOUR, PPH for POUNDS_PER_HOUR
// It must only display the name, number and units, nothing else,
// not even a newline. The format is: name: value units
// For example: Tach1: 278.6 MPH
// Note that unlike CounterValue, calling this doesn't zero the counts and time.
// params: ()
//-----------------------------------------------------------------------
void DisplayCounter (const Counter& c)
{
float equation;
if (c.totalTime != NO_TIME)
equation = (float(c.totalCounts) / c.totalTime) * c.scalingFactor;
else
equation = 0;
cout << c.counterName << ": " << equation;
switch (c.units)
{
case GALLONS_PER_HOUR: cout << " GPH";
break;
case MILES_PER_HOUR: cout << " MPH";
break;
case POUNDS_PER_HOUR: cout << " PPH";
break;
}
}
//-----------------------------------------------------------------------
// Initializes the Counter's name to name.
// Initializes the Counter's counts and time to 0.
// Displays that the counter was added to the list.
// params: (out, in, in, in)
//-----------------------------------------------------------------------
void InitCounter (Counter& c, char name[], float scale, char units)
{
strcpy(c.counterName, name);
c.totalCounts = 0;
c.totalTime = 0;
c.scalingFactor = scale;
if (units == GALLONS_SYMBOL)
c.units = GALLONS_PER_HOUR;
else if (units == MILES_SYMBOL)
c.units = MILES_PER_HOUR;
else
c.units = POUNDS_PER_HOUR;
}
//-----------------------------------------------------------------------
// Adds the counts and time to the Counter's counts and time.
// params: (inout, in, in)
//-----------------------------------------------------------------------
void UpdateCounter (Counter& c, int counts, int time)
{
c.totalCounts += counts;
c.totalTime += time;
}
//-----------------------------------------------------------------------
// Returns true if the Counter's name is the same as name; false
// otherwise.
// params: (in, in)
//-----------------------------------------------------------------------
bool CounterHasName (const Counter& c, const string name)
{
if (c.counterName == name)
return true;
return false;
}
//-----------------------------------------------------------------------
// Returns counts-per-second for the Counter.
// Returns 0.0 if the Counter's time is 0.
// It also zeroes the Counter's counts & time when done calculating.
// params: (inout)
//-----------------------------------------------------------------------
float CounterValue (Counter& c)
{
float counts;
if (c.totalTime != NO_TIME)
counts = float(c.totalCounts) / c.totalTime;
else
counts = 0;
c.totalCounts = 0;
c.totalTime = 0;
return counts;
}
SAMPLE INPUT:
A Tach1 0.35 M
A Tach2 0.44 M
O Tach1
U Tach2 4006 3
U Tach1 5043 2
U Tach2 20100 7
P
O Tach2
O Tach3
A Tach3 0.17 M
A Tach1 0.32 M
A Fuel1 0.97 G
A Fuel2 2.03 G
A Tach4 1.77 M
U Fuel1 48237 380
U Tach3 36009 5
P
O Fuel1
O Fuel1
U Fuel2 3828 50
U Fuel3 3828 50
U Fuel1 3828 50
U Tach2 10100 3
P
D Fuel1
D Fuel1
P
D Tach3
P
D Tach1
D Tach2
D Fuel2
P
Q
CORRESPONDING SAMPLE OUTPUT:
A Tach1 0.35 M
Tach1 added to the list.
A Tach2 0.44 M
Tach2 added to the list.
O Tach1
Tach1: 0 counts per second
U Tach2 4006 3
Counter Tach2 updated with 4006, 3
U Tach1 5043 2
Counter Tach1 updated with 5043, 2
U Tach2 20100 7
Counter Tach2 updated with 20100, 7
P
The counter values are:
Counter 0 - Tach1: 882.525 MPH
Counter 1 - Tach2: 1060.66 MPH
O Tach2
Tach2: 2410.6 counts per second
O Tach3
Cannot output. Tach3 is not in the list.
A Tach3 0.17 M
Tach3 added to the list.
A Tach1 0.32 M
Tach1 was updated with new information.
A Fuel1 0.97 G
Fuel1 added to the list.
A Fuel2 2.03 G
Fuel2 added to the list.
A Tach4 1.77 M
Tach4 not added. List is full.
U Fuel1 48237 380
Counter Fuel1 updated with 48237, 380
U Tach3 36009 5
Counter Tach3 updated with 36009, 5
P
The counter values are:
Counter 0 - Fuel1: 123.131 GPH
Counter 1 - Fuel2: 0 GPH
Counter 2 - Tach1: 0 MPH
Counter 3 - Tach2: 0 MPH
Counter 4 - Tach3: 1224.31 MPH
O Fuel1
Fuel1: 126.939 counts per second
O Fuel1
Fuel1: 0 counts per second
U Fuel2 3828 50
Counter Fuel2 updated with 3828, 50
U Fuel3 3828 50
Cannot update. Fuel3 is not in the list.
U Fuel1 3828 50
Counter Fuel1 updated with 3828, 50
U Tach2 10100 3
Counter Tach2 updated with 10100, 3
P
The counter values are:
Counter 0 - Fuel1: 74.2632 GPH
Counter 1 - Fuel2: 155.417 GPH
Counter 2 - Tach1: 0 MPH
Counter 3 - Tach2: 1481.33 MPH
Counter 4 - Tach3: 1224.31 MPH
D Fuel1
Fuel1 deleted from the list.
D Fuel1
No delete. Fuel1 not in the list.
P
The counter values are:
Counter 0 - Fuel2: 155.417 GPH
Counter 1 - Tach1: 0 MPH
Counter 2 - Tach2: 1481.33 MPH
Counter 3 - Tach3: 1224.31 MPH
D Tach3
Tach3 deleted from the list.
P
The counter values are:
Counter 0 - Fuel2: 155.417 GPH
Counter 1 - Tach1: 0 MPH
Counter 2 - Tach2: 1481.33 MPH
D Tach1
Tach1 deleted from the list.
D Tach2
Tach2 deleted from the list.
D Fuel2
Fuel2 deleted from the list.
P
The counter values are:
Q
Normal Termination of Counter Program.