CS249-Projects/PA10-1115/GeneralUtility.c
cdez 6969f405e3 Fixed Makefile, Fixed Broken Inclusions, etc.
+- GeneralUtility.c
+- HashUtility.c
+- StateDataUtility.c
+- Makefile for PA10
2023-11-13 11:55:58 -07:00

116 lines
No EOL
3.4 KiB
C

// Header files ///////////////////////////////////////////////////////////////
#include "GeneralUtility.h"
// global constants ////////////////////////////////////////////////////////////
// none
// data structures /////////////////////////////////////////////////////////////
// None
/*
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: privateGetStringLength
*/
int privateCompareStrings( const char *leftStr, const char *rightStr )
{
int diff, index = 0;
int leftStrLen = privateGetStringLength(leftStr);
int rightStrLen = privateGetStringLength(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: privateGetStringLength
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 privateGetStringLength( 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: privateToPower
Process: recursively calculates result of given base to given exponent,
for exponents >= 0
Function input/parameters: base and exponent (int)
Function output/parameters: none
Function output/returned: result of specified calculations (int)
Device input/---: none
Device output/---: none
Dependencies: toPower (recursively)
*/
int privateToPower( int base, int exponent )
{
// if the exponent is 0, return one.
if(exponent == 0)
{
return 1;
}
// if not, then return base * privateToPower(base, exponent - 1)
return base * privateToPower(base, exponent - 1);
}