98 lines
2.9 KiB
C++
98 lines
2.9 KiB
C++
//
|
|
// Created by caleb on 3/25/24.
|
|
//
|
|
#include <cstring>
|
|
#include <iostream>
|
|
#include "structures.h"
|
|
|
|
Employee::Employee(char * pName, double salary) {
|
|
this->pName = pName;
|
|
this->salary = salary;
|
|
memset(this->nameOfSpouse, 0, 50);
|
|
//this->pAddress = "Not defined!";
|
|
//strcpy(this->nameOfSpouse, "Not defined!");
|
|
}
|
|
|
|
Employee::Employee(char * pName, char * pAddress, char nameOfSpouse[50], double salary) {
|
|
this->pName = new char[strlen(pName) + 1];
|
|
this->pAddress = new char[strlen(pAddress) + 1];
|
|
strcpy(this->nameOfSpouse, nameOfSpouse); //unsafe!
|
|
this->salary = salary;
|
|
}
|
|
Employee::~Employee() {
|
|
if (this->pName != nullptr) {
|
|
delete[] this->pName;
|
|
}
|
|
if (this->pAddress != nullptr) {
|
|
delete[] this->pAddress;
|
|
}
|
|
}
|
|
std::string Employee::toString() {
|
|
const char *NOT_DEFINED = "Not defined!";
|
|
// Grab variables
|
|
char *pAddress;
|
|
char nameOfSpouse[50];
|
|
if (this->pAddress == nullptr) {
|
|
pAddress = const_cast<char*>(NOT_DEFINED);
|
|
} else {
|
|
pAddress = this->pAddress;
|
|
}
|
|
if (std::strlen(this->nameOfSpouse) == 0) {
|
|
strcpy(nameOfSpouse, NOT_DEFINED); //unsafe!
|
|
} else {
|
|
strcpy(nameOfSpouse, this->nameOfSpouse); //unsafe!
|
|
}
|
|
|
|
std::string outputString;
|
|
outputString.append("pName: ").append(this->pName).append("\n");
|
|
outputString.append("pAddress: ").append(pAddress).append("\n");
|
|
outputString.append("nameOfSpouse: ").append(nameOfSpouse).append("\n");
|
|
outputString.append("salary: ").append(std::to_string(this->salary)).append("\n");
|
|
return outputString;
|
|
}
|
|
|
|
/** Populates a dynamic 2D array of employees by reading the data from the console.
|
|
*
|
|
* @param pp existing 2D array to be populated.
|
|
* @param rows number of rows
|
|
* @param col number of columns
|
|
*/
|
|
void populate(Employee **pp, int rows, int col ) {
|
|
std::string name;
|
|
double salary;
|
|
printf("Populating a %i x %i 2D array... (Total entries required: %i)\n", rows, col, (rows * col));
|
|
for (int i = 0; i < rows; ++i) {
|
|
for (int j = 0; j < col; ++j) {
|
|
printf("Enter a name for the employee at (%i x %i): ", i, j);
|
|
std::getline(std::cin, name);
|
|
printf("Enter salary for %s: ", name.c_str());
|
|
std::cin >> salary;
|
|
*(*(pp + i) + j) = *new Employee(const_cast<char*>(name.c_str()), salary);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/** Prints the 2D dynamic array.
|
|
*
|
|
* @param pp the dynamic array
|
|
* @param rows number of rows
|
|
* @param col number of columns
|
|
*/
|
|
void print(Employee **pp, int rows, int col );
|
|
|
|
/** Frees all memory occupied by the array.
|
|
*
|
|
* @param pp the dynamic array
|
|
* @param rows number of rows
|
|
* @param col number of columns
|
|
*/
|
|
void free(Employee **pp, int rows, int col );
|
|
|
|
/**Sorts the dynamic array in ascending order by name.
|
|
*
|
|
* @param pp the dynamic array
|
|
* @param rows number of rows
|
|
* @param col number of columns
|
|
*/
|
|
void sort(Employee **pp, int rows, int col ); |