CS249-Projects/PA09-1108/GeneralUtility.c
cdez d9e90f6501 "Finish" PA09
+- GeneralUtility.c
+- HeapUtility.c
+- StudentUtility.c
+- PA09_Main
2023-11-08 17:15:25 -07:00

206 lines
No EOL
6.3 KiB
C

// header file
#include "GeneralUtility.h"
// constants
/* Data Structures
typedef enum { NO_RESULT = -1, INITIALIZE_RAND_GENERATOR = 101,
GET_RANDOM_VALUE, CLEAR_RAND_GENERATOR } RandControlCode;
typedef enum { LOW_RAND_LIMIT = 10, HIGH_RAND_LIMIT = 99 } RandLimits;
Data Structures */
// function implementations
/*
Name: privateCompareStrings
Process: compares two strings as follows:
- if left string is alphabetically greater than the right string,
returns value greater than zero
- if left string is alphabetically less than the right string,
returns value less than zero
- if strings are alphabetically equal but one is longer,
longer one is greater
- otherwise, returns zero
Function input/parameters: two strings to be compared (const char *)
Function output/parameters: none
Function output/returned: result as specified above (int)
Device input/ ---: none
Device output/ ---: none
Dependencies: privateGetStringLen
*/
int privateCompareStrings(const char *leftStr, const char *rightStr)
{
int diff, index = 0;
int leftStrLen = privateGetStringLen(leftStr);
int rightStrLen = privateGetStringLen(rightStr);
while (index < leftStrLen && index < rightStrLen)
{
diff = leftStr[index] - rightStr[index];
if (diff != 0)
{
return diff;
}
index = index + 1;
}
return leftStrLen - rightStrLen;
}
/*
Name: privateCopyString
Process: copies string character for character up to NULL_CHAR of source string
Function input/parameters: source string to be copied (const char *)
Function output/parameters: destination string (char *)
Function output/returned: none
Device input/---: none
Device output/---: none
Dependencies: none
*/
void privateCopyString(char *dest, const char *source)
{
int characterCount = 0;
// use a while loop, check for !NULL_CHAR
while (source[characterCount] != NULL_CHAR)
{
// copy the character accessed into dest string
dest[characterCount] = source[characterCount];
// iterate the number of characters
characterCount = characterCount + 1;
}
// add a NULL_CHAR once done, characterCount is in the right place for \0
dest[characterCount] = NULL_CHAR;
// no return, void.
}
/*
Name: privateGenUniqueRandom
Process: generates a unique random value for a given set of data
during a program,
for control code:
INITIALIZE_RAND_GENERATOR - initializes lookup array for use
GET_RANDOM_VALUE - creates, tests, and returns random value
CLEAR_RAND_GENERATOR - deallocates lookup array
Method input/parameters: HeapType item (const HeapType),
control code (RandControlCode)
Method output/parameters: none
Method output/returned: unique random value (int)
Device input/---: none
Device output/---: none
Dependencies: srand, privateGetRandBetween, privateIsInArray, malloc, free
Notes: protects from:
accidental reinitialization,
acquiring value without initialization,
or deallocation without initialization
*/
int privateGenUniqueRandom(int heapCapacity, RandControlCode ctrlCode)
{
// define the static array and size
static int *randArray = NULL;
static int size = 0;
// check which ctrlCode is used
if(ctrlCode == INITIALIZE_RAND_GENERATOR)
{
// if initializing
// create the array with the capacity given
randArray = (int *)malloc(sizeof(int) * heapCapacity);
// set random seed
srand(time(NULL));
// set size to zero(just to be safe)
size = 0;
}
else if(ctrlCode == GET_RANDOM_VALUE)
{
// if requesting for rand
// generate random value,
// makes sure the value currently isn't in the array
int currentRand = privateGetRandBetween(LOW_RAND_LIMIT, HIGH_RAND_LIMIT);
while (privateIsInArray(randArray, size, currentRand))
{
// generate new random.
currentRand = privateGetRandBetween(LOW_RAND_LIMIT, HIGH_RAND_LIMIT);
}
// once found a number that isn't in array, put into array.
randArray[size] = currentRand;
// increment size
size = size + 1;
return currentRand;
}
// check to see if the array is actually initialized
if(size != 0 && randArray != NULL)
{
// otherwise, clearing and freeing things if initialized.
free(randArray);
}
return NO_RESULT;
}
/*
Name: privateGetRandBetween
Process: generates a random value between two limits, inclusive
Method input/parameters: lower and upper limits (int)
Method output/parameters: none
Method output/returned: random value generated as specified (int)
Device input/---: none
Device output/---: none
Dependencies: rand
*/
int privateGetRandBetween(int lowLimit, int highLimit)
{
// return random number that was previously seeded
return ((rand() % (highLimit - lowLimit + 1)) + lowLimit);
}
/*
Name: privateGetStringLen
Process: finds length of string
Function input/parameters: c-style string (const char *)
Function output/parameters: none
Function output/returned: calculated length of string (int)
Device input/ ---: none
Device output/ ---: none
Dependencies: none
*/
int privateGetStringLen(const char *str)
{
int strLen = 0;
// while through string until hit null.
while (str[strLen] != NULL_CHAR)
{
// increment the length string iterator
strLen = strLen + 1;
}
// return strLen
return strLen;
}
/*
Name: privateIsInArray
Process: provided integer array, its size, and a test value,
returns true if test value is in the array, false otherwise
Method input/parameters: integer array (int *),
test value (int)
Method output/parameters: none
Method output/returned: Boolean result of operation (bool)
Device input/---: none
Device output/---: none
Dependencies: none
*/
bool privateIsInArray(const int *array, int size, int testVal)
{
int arrayIterator;
// for loop through the array.
for(arrayIterator = 0; arrayIterator < size; arrayIterator++)
{
// check to see if the testVal is at the location.
if(array[arrayIterator] == testVal)
{
// if there, return true
return true;
}
}
// otherwise, return false since not found
return false;
}