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
|
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class employee
{
/* Employee class to contain employee data
*/
private:
string surname;
double hourlyRate;
int empNumber;
public:
employee() {
hourlyRate = -1;
empNumber = -1;
surname = "";
}
employee(const employee &other) :
surname(other.surname),
hourlyRate(other.hourlyRate),
empNumber(other.empNumber)
{
// copy constructor
}
void setEmployee(const string &name, double rate, int num) {
surname = name;
hourlyRate = rate;
empNumber = num;
}
const string& getSurname() const {
return surname;
}
void printEmployee() const {
cout << fixed;
cout << setprecision(2) << fixed << left << setw(20) << surname << setw(4) << empNumber << " " << hourlyRate << "\n";
}
};
void swap(employee employees[], int a, int b);
void sortEmployees(employee employees[], int number);
void printEmployees(employee employees[], int number);
int main()
{
employee employees[5];
employees[0].setEmployee("Stone", 35.75, 053);
employees[1].setEmployee("Rubble", 12, 163);
employees[2].setEmployee("Flintstone", 15.75, 97);
employees[3].setEmployee("Pebble", 10.25, 104);
employees[4].setEmployee("Rockwall", 22.75, 15);
printEmployees(employees, 5);
sortEmployees(employees, 5);
printEmployees(employees, 5);
return 0;
}
void printEmployees(employee employees[], int number)
{
int i;
for (i=0; i<number; i++) {
employees[i].printEmployee();
}
cout << "\n";
}
void swap(employee employees[], int a, int b)
{
employee temp(employees[a]);
for (a=a; a<=b; a++)
{
if (employees[a].getSurname() > employees[b].getSurname())
{
employees[a] = employees[b];
employees[b] = temp;
}
}
}
void sortEmployees(employee employees[], int number)
{
/* use selection sort to order employees,
in employee name order
*/
int inner, outer, max;
for (outer=number-1; outer>0; outer--)
{
// run though array number of times
max = 0;
for (inner=1; inner<=outer; inner++)
{
// find alphabeticaly largest surname in section of array
if (employees[inner].getSurname() > employees[max].getSurname())
max = inner;
}
if (max != outer)
{
// swap largest with last element looked at in array
swap(employees, max, outer);
}
}
}
|