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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
struct File1
{
string firstName;
string lastName;
string employeeNumber;
int hours;
float payrate;
};
struct File2
{
string employeeNumber;
int hours;
};
int getData1(string, File1*);
//Precondition: The first parameter is the name of a file filled with employee data, the second is a pointer to a structure array used to hold the employee data
//Postcondition: Opens the user specified file and writes various employee data to a structure array
int getData2(string, File2*);
//Precondition: The first parameter is the name of a file containing the number of hours an employee worked, the second is a pointer to a structure array used to hold the hours from the file
//Postcondition: Opens the user specified file and writes the number of hours each employee worked to a structure array
void selectionSort1(File1*, int);
//Precondition: The first parameter is a pointer to a structure array used to hold the employee data, the second is an integer that holds the number of employees contained in the array
//Postcondition: Sorts employees in the structure array by last name
void selectionSort2(File2*, int);
//Precondition: The first parameter is a pointer to a structure array used to hold the employee hours, the second is an integer to hold the number of hours in the array
//Postcondition: Sorts employee hours in the second structure array by employee ID
int binarySearch(File2*, string, int);
//Precondition: The first parameter is a pointer to a structure array used to hold the employee hours, the second is a string that holds the employee ID member from the second struct,the third is an integer that holds the number of hours contained in the array
//Postcondition: Uses a binary search to look through the structure array for employee hours
void matchHoursWorked(File1*, int, File2*, int);
//Precondition: The first parameter is a pointer to a structure array to hold the employee data, the second is an integer to hold the number of employees in the array, the third is a pointer to a structure array to hold the employee hours, and the fourth is an integer to hold the number of hours in the array
//Postcondition: Matches the hours worked from the second struct to the employees in the first struct using the employee ID
void fileWrite(string, File1*, int);
//Precondition: The first parameter is a string that contains user specified name of the file to write to, the second is a pointer to a structure array to hold the employee data, the third is an integer to hold the number of employees in the array
//Postcondition: Writes the first structure array to a user named file
int main()
{
string fileName1;
string fileName2;
string fileName3;
int numberOfEmployees = 0;
int numberOfEmpHours = 0;
File1 *dataArray1 = nullptr;
dataArray1 = new File1[numberOfEmployees];
File2 *dataArray2 = nullptr;
dataArray2 = new File2[numberOfEmpHours];
cout << "Enter the name of the file containing the employee data"
"(first name, last name, employee number, payrate): " << endl;
cin >> fileName1;
numberOfEmployees = getData1(fileName1, dataArray1);
selectionSort1(dataArray1, numberOfEmployees);
cout << "Enter the name of the file containing the number of hours worked: " << endl;
cin >> fileName2;
numberOfEmpHours = getData2(fileName2, dataArray2);
selectionSort2(dataArray2, numberOfEmpHours);
matchHoursWorked(dataArray1, numberOfEmployees, dataArray2, numberOfEmpHours);
cout << "Enter the name of the file you would like to save the data to: " << endl;
cin >> fileName3;
fileWrite(fileName3, dataArray1, numberOfEmployees);
delete [] dataArray1;
delete [] dataArray2;
return 0;
}
int getData1(string fileName1, File1 *dataArray1)
{
ifstream inputFile;
int counter = 0;
float z;
string w, x, y;
inputFile.open(fileName1.c_str());
if (inputFile)
{
while (inputFile >> w >> x >> y >> z)
{
dataArray1[counter]->firstName = w;
dataArray1[counter]->lastName = x;
dataArray1[counter]->employeeNumber = y;
dataArray1[counter]->payrate = z;
dataArray1[counter]->hours = 0;
counter++;
}
inputFile.close();
}
else
{
cout << "There was a problem opening the file->" << endl;
}
return counter;
}
int getData2(string fileName2, File2 *dataArray2)
{
ifstream inputFile;
int counter = 0,
z;
string y;
inputFile.open(fileName2.c_str());
if (inputFile)
{
while (inputFile >> y >> z)
{
dataArray2[counter]->employeeNumber = y;
dataArray2[counter]->hours = z;
counter++;
}
inputFile.close();
}
else
{
cout << "There was a problem opening the file->" << endl;
}
return counter;
}
void selectionSort1(File1 *dataArray1, int numberOfEmployees)
{
int startScan, minIndex;
float minPay;
string minLast, minFirst, minID;
for (startScan = 0; startScan < (numberOfEmployees - 1); startScan++)
{
minIndex = startScan;
minLast = dataArray1[startScan]->lastName;
minFirst = dataArray1[startScan]->firstName;
minID = dataArray1[startScan]->employeeNumber;
minPay = dataArray1[startScan]->payrate;
for (int index = startScan + 1; index < numberOfEmployees; index++)
{
if (dataArray1[index]->lastName < minLast)
{
minLast = dataArray1[index]->lastName;
minFirst = dataArray1[index]->firstName;
minID = dataArray1[index]->employeeNumber;
minPay = dataArray1[index]->payrate;
minIndex = index;
}
}
dataArray1[minIndex]->lastName = dataArray1[startScan]->lastName;
dataArray1[startScan]->lastName = minLast;
dataArray1[minIndex]->firstName = dataArray1[startScan]->firstName;
dataArray1[startScan]->firstName = minFirst;
dataArray1[minIndex]->employeeNumber = dataArray1[startScan]->employeeNumber;
dataArray1[startScan]->employeeNumber = minID;
dataArray1[minIndex]->payrate = dataArray1[startScan]->payrate;
dataArray1[startScan]->payrate = minPay;
}
}
void selectionSort2(File2 *dataArray2, int numberOfEmpHours)
{
int startScan, minIndex, minHours;
string minID;
for (startScan = 0; startScan < (numberOfEmpHours - 1); startScan++)
{
minIndex = startScan;
minID = dataArray2[startScan]->employeeNumber;
minHours = dataArray2[startScan]->hours;
for (int index = startScan + 1; index < numberOfEmpHours; index++)
{
if (dataArray2[index]->employeeNumber < minID)
{
minID = dataArray2[index]->employeeNumber;
minHours = dataArray2[index]->hours;
minIndex = index;
}
}
dataArray2[minIndex]->employeeNumber = dataArray2[startScan]->employeeNumber;
dataArray2[startScan]->employeeNumber = minID;
dataArray2[minIndex]->hours = dataArray2[startScan]->hours;
dataArray2[startScan]->hours = minHours;
}
}
void matchHoursWorked(File1 *dataArray1, int numberOfEmployees,
File2 *dataArray2, int numberOfEmpHours)
{
for (int i = 0; i < numberOfEmployees; i++)
{
int j = binarySearch(dataArray2, dataArray1[i]->employeeNumber, numberOfEmpHours);
if (j >= 0)
{
dataArray1[i]->hours = dataArray2[j]->hours;
}
}
}
|