Code:
//Included Libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Symbolic Constants
#define ARGSZ 2
#define ONE 1
#define SALARYARRYSZ 2
#define ZEROD 0.0
#define ZEROI 0
//Memory Structures
struct employeeNode
{
char *firstName;
char *lastName;
int id;
char *employeeClass;
double salary[SALARYARRYSZ];
struct employeeNode *nextNode;
};
//Function Prototypes
void cmdLineTest(int, char *[]);
void dataProcess(char [], char *, char *, char *, char *, char [], char [], char [], int *);
void fileOpen(FILE **, char *[]);
void insertNode(FILE *, struct employeeNode **, char []);
void memAlloc(char [], char [], char [], int , struct employeeNode **);
void memStore(char [], char [], char [], int , struct employeeNode **);
void printList(struct employeeNode *);
void read(FILE *, char []);
void readInitSal(FILE *, struct employeeNode **, char []);
void readPromotions(FILE *, struct employeeNode **, char []);
void readTransfers(FILE *, struct employeeNode **, char[]);
Driver:
Code:
#include "hw3Constants.c"
int main(int argc, char *argv[])
{
char selection, readData[50];
struct employeeNode *startPtr = NULL;
FILE *inFile = NULL;
printf("\n%s %d %s %s %s %s %s %s%c\n\n", "This is line number", __LINE__,
"of the file", __FILE__,
"which was\n compiled on", __DATE__,
"at time", __TIME__, '.');
cmdLineTest(argc, argv);
fileOpen(&inFile, argv);
read(inFile, readData);
while (!feof(inFile))
{
if (readData[0] == '*')
{
selection = readData[2];
switch (selection)
{
case 'H': insertNode(inFile, &startPtr, readData);
break;
case 'I': readInitSal(inFile, &startPtr, readData);
break;
case 'P': readPromotions(inFile, &startPtr, readData);
break;
case 'T': readTransfers(inFile, &startPtr, readData);
break;
default: break;
}
}
}
printList(startPtr);
return;
}
void cmdLineTest(int argc, char *argv[])
{
if (argc == ARGSZ)
printf("The argument supplied is %s\n\n", argv[ARGSZ-ONE]);
else
{
if (argc >2)
{
printf("Too many arguments supplied. Exiting Program.\n");
exit(1);
}
else
{
printf("One argument expected. Exiting Program\n");
exit(1);
}
}
return;
}
void dataProcess(char data[], char *fName, char *lName, char *id, char *cl, char tempLName[], char tempFName[], char tempClass[], int *tempID)
{
lName = strtok(data, " ");
fName = strtok(NULL, " ");
id = strtok(NULL, " ");
cl = strtok(NULL, " ");
strcpy(tempLName, lName);
strcpy(tempFName, fName);
strcpy(tempClass, cl);
*tempID = atoi(id);
return;
}
void fileOpen(FILE **inFilePtr, char *argv[])
{
if(( *inFilePtr = fopen(argv[ARGSZ-ONE], "r")) == NULL)
{
printf("File Open Failed. EXITING PROGRAM.\n");
exit(1);
}
return;
}
void insertNode(FILE *inFile, struct employeeNode **startPtr, char readData[])
{
int x=0, tempID=0;
char *firstName=NULL, *lastName=NULL, *id=NULL, *cl=NULL;
char tempClass[20], tempLName[20], tempFName[20];
struct employeeNode *newPtr = NULL;
struct employeeNode *previousPtr = NULL;
struct employeeNode *currentPtr = NULL;
for(x=0; x<50; x++)
{
readData[x] = '\0';
}
fgets(readData, 50, inFile);
do
{
dataProcess(readData, firstName, lastName, id, cl, tempLName, tempFName, tempClass, &tempID);
memAlloc(tempLName, tempFName, tempClass, tempID, &newPtr);
if(newPtr != NULL)
{
memStore(tempLName, tempFName, tempClass, tempID, &newPtr);
previousPtr = NULL;
currentPtr = *startPtr;
while( (currentPtr != NULL) && (strcmp(tempLName, currentPtr->lastName) > 0) )
{
previousPtr = currentPtr;
currentPtr = currentPtr->nextNode;
}
if( (previousPtr == NULL) )
{
newPtr->nextNode = *startPtr;
*startPtr = newPtr;
}
else
{
previousPtr->nextNode = newPtr;
newPtr->nextNode = currentPtr;
}
}
else
{
printf("No Memory Available.\n");
}
fgets(readData, 50, inFile);
}while(readData[0] != '*');
return;
}
void memAlloc(char lName[], char fName[], char eClass[], int tempID, struct employeeNode **newPtr)
{
int firstNum, lastNum, classNum;
*newPtr = calloc(ONE, sizeof(struct employeeNode));
if (newPtr != NULL)
{
firstNum = strlen(fName);
lastNum = strlen(lName);
classNum = strlen(eClass);
lName[lastNum] = '\0';
fName[firstNum] = '\0';
eClass[classNum] = '\0';
(**newPtr).firstName = calloc((firstNum+ONE), sizeof(char));
(**newPtr).lastName = calloc((lastNum+ONE), sizeof(char));
(**newPtr).employeeClass = calloc((classNum+ONE), sizeof(char));
}
else
{
printf("No Memory Available. Exiting Program");
exit(1);
}
return;
}
void memStore(char lName[], char fName[], char eClass[], int id, struct employeeNode **newPtr)
{
int x;
strcpy( (**newPtr).firstName, fName);
strcpy( (**newPtr).lastName, lName);
for(x=0; x<50; x++)
{
if (eClass[x] == '\n')
{
eClass[x] = '\0';
}
}
strcpy( (**newPtr).employeeClass, eClass);
(**newPtr).id = id;
(**newPtr).nextNode = NULL;
return;
}
void printList(struct employeeNode *currentPtr)
{
int numEmployee=1;
if (currentPtr == NULL)
{
printf("List is empty.\n\n");
}
else
{
printf("The Employees Are:\n");
printf("\n");
printf("\n");
printf("----------------------------------------------------------------------------------------\n");
while (currentPtr != NULL)
{
printf("%3d %X %-10s%-10s %3d %-15s$%9.2f $%9.2f%13X\n",
numEmployee,
currentPtr,
currentPtr->firstName,
currentPtr->lastName,
currentPtr->id,
currentPtr->employeeClass,
currentPtr->salary[0],
currentPtr->salary[1],
currentPtr->nextNode);
currentPtr = currentPtr->nextNode;
numEmployee+=1;
}
printf("NULL\n\n");
}
return;
}
void read(FILE *inFile, char readData[50])
{
fgets(readData, 50, inFile);
return;
}
void readInitSal(FILE *inFile, struct employeeNode **searchPtr, char readData[])
{
int idTrue=0, x=0;
double salaryTrue=0.0;
char *idStr = NULL, *salaryString = NULL;
struct employeeNode *currentPtr;
fgets(readData, 50, inFile);
do
{
idStr = strtok(readData, " ");
salaryString = strtok(NULL, " ");
idTrue = atoi(idStr);
salaryTrue = atof(salaryString);
fgets(readData, 50, inFile);
currentPtr = *searchPtr;
while (currentPtr != NULL)
{
if ( (currentPtr->id) == idTrue)
{
currentPtr->salary[0] = salaryTrue;
}
currentPtr = currentPtr->nextNode;
}
}while(readData[0] != '*');
return;
}
void readPromotions(FILE *inFile, struct employeeNode **searchPtr, char readData[])
{
int idTrue=0, x=0;
double pSalaryTrue=0.00;
char pClass[20]={'\0'}, *idStr=NULL, *pSalaryString=NULL, *pClassString=NULL;
struct employeeNode *currentPtr;
fgets(readData, 50, inFile);
do
{
idStr = strtok(readData, " ");
pSalaryString = strtok(NULL, " ");
pClassString = strtok(NULL, " ");
idTrue = atoi(idStr);
pSalaryTrue = atof(pSalaryString);
strcpy(pClass, pClassString);
for (x=0; x<20; x++)
{
if (pClass[x] == '\n')
pClass[x] = '\0';
}
currentPtr = *searchPtr;
while (currentPtr != NULL)
{
if ((currentPtr->id) == idTrue)
{
currentPtr->salary[1] = pSalaryTrue;
strcpy((currentPtr->employeeClass), pClass);
}
currentPtr = currentPtr->nextNode;
}
fgets(readData, 50, inFile);
}while(readData[0] != '*');
return;
}
void readTransfers(FILE *inFile, struct employeeNode **startPtr, char readData[])
{
char idStr[10];
int idTrue;
struct employeeNode *previousPtr = NULL;
struct employeeNode *currentPtr = NULL;
struct employeeNode *tempPtr = NULL;
fgets(readData, 50, inFile);
do
{
strcpy(idStr, readData);
idTrue = atoi(idStr);
if (idTrue == (*startPtr)->id)
{
tempPtr = *startPtr;
*startPtr = (*startPtr)->nextNode;
free(tempPtr);
tempPtr = NULL;
}
else
{
previousPtr = *startPtr;
currentPtr = (*startPtr)->nextNode;
while ((currentPtr != NULL) && (currentPtr->id != idTrue))
{
previousPtr = currentPtr;
currentPtr = currentPtr->nextNode;
}
if (currentPtr != NULL)
{
tempPtr = currentPtr;
previousPtr->nextNode = currentPtr->nextNode;
free(tempPtr);
}
}
fgets(readData, 50, inFile);
}while(readData[0] != '*');
return;
}
Ouput is as Follows after Sorting By Last Name: