Initialize PA09

++ PA09 Files
++ Makefile for PA09
This commit is contained in:
Charles Descamps 2023-11-05 11:29:51 -07:00
parent ba683c4f44
commit 146e26b5cf
17 changed files with 3832 additions and 0 deletions

View file

@ -0,0 +1,590 @@
// Header files included
#include "Console_IO_Utility.h"
// Function Implementations
/*
Name: clearLeadingCharacters
Process: Clears all leading non-printable characters from the console stream
Function input/parameters: allow space return (Boolean)
Function output/parameters: none
Function output/returned: first printable character as integer
Device input/keyboard: none
Device output/monitor: none
Dependencies: getchar
*/
int clearLeadingCharacters( bool allowSpaceReturn )
{
// initialize data, set input character to getChar, set count to zero
// function: getchar
int intChar = getchar();
int charCount = 0;
// loop while non-printable characters and space if not set
// also limits looping to constant limit
while( ( intChar < (int)SPACE
|| ( !allowSpaceReturn && intChar == SPACE ) )
&& charCount < MAX_STR_LEN )
{
// update character count
charCount++;
// get new input character
// function: getchar
intChar = getchar();
}
// return captured character
return intChar;
}
/*
Name: getChar
Process: returns next character in stream,
ignores all white space (including SPACE) prior to character
Function input/parameters: flag to accept space (Boolean)
Function output/parameters: none
Function output/returned: one character (char) is returned to calling method
Device input/keyboard: places one or more characters in stream
Device output/monitor: none
Dependencies: getCharFiltered
*/
char getChar()
{
// initialize variables, set space accept flag to false
bool dontAcceptSpace = false;
// call filtered function with space accept flag
// function: getCharFiltered
return getCharFiltered( dontAcceptSpace );
}
/*
Name: getCharFiltered
Process: returns next character in stream,
ignores non printable characters prior to input character
but may accept space depending on flag
Function input/parameters: flag to accept space (boolean)
Function output/parameters: none
Function output/returned: first printable character or space (if configured)
returned (char)
Device input/keyboard: places one or more characters in stream
Device output/monitor: none
Dependencies: getchar(), clearLeadingCharacters
*/
char getCharFiltered( bool acceptSpace )
{
//Initialize variables
int inCharInt;
// clear leading characters, capture first usable character
// function: clearLeadingCharacters
inCharInt = clearLeadingCharacters( acceptSpace );
// check for input character set to end of file
if( inCharInt == EOF )
{
// return character zero
return '0';
}
// return input character as character
return (char)inCharInt;
}
/*
Name: getInputString
Process: acquires input string from keyboard, if clear white space flag set,
ignores all white space, including space character
Function input/parameters: clear white space control (Boolean)
Function output/parameters: character array (char[])
Function output/returned: none
Device input/keyboard: user entry of a character stream
Device output/monitor: none
Dependencies: getchar(), clearLeadingCharacters
*/
void getInputString( char inputArr[], bool clearWhiteSpace )
{
//Initialize Variables, set character count and index to zero
int charCount = 0;
int index = 0;
int intChar;
// check for clear white space flag
if( clearWhiteSpace )
{
// clear leading characters, set flag to ignore space
// function: clearLeadingCharacters
intChar = clearLeadingCharacters( !clearWhiteSpace );
}
// otherwise, assume white space flag not set
else
{
// get first character
// function: getchar
intChar = getChar();
}
// loop until enter key found, limit loop to constant
while( intChar != (int)NEWLINE_CHAR && charCount < MAX_STR_LEN )
{
// set current value to array element
inputArr[ index ] = (char)intChar;
// increment string array index
index++;
// add null character to end of string array
inputArr[ index ] = NULL_CHAR;
// increment character count
charCount++;
// get next character
// function: getchar
intChar = getchar();
}
}
/*
Name: printCharacter
Process: prints a single character
Function input/parameters: character to be printed (char)
Function output/parameters: None
Function output/returned: None
Device input/keyboard: None
Device output/monitor: character printed as specified
Dependencies: printCharacters
*/
void printCharacter( char outputChar )
{
// initialize number of characters to one
int numberOfChars = 1;
// print one character
// function: printChars
printCharacters( numberOfChars, outputChar );
}
/*
Name: printCharacters
Process: prints given number of characters
Function input/parameters: number of characters (int),
character to be printed (char)
Function output/parameters: None
Function output/returned: None
Device input/keyboard: None
Device output/monitor: Number of specified characters printed
Dependencies: printf
*/
void printCharacters( int numChars, char outputChar )
{
// check for reminaing characters to print
if( numChars > 0 )
{
// print one character
// function: printf
printf( "%c", outputChar );
// (recursive) print remaining characters
// function: printChars
printCharacters( numChars - 1, outputChar );
}
}
/*
Name: printDouble
Process: simple print double function
Function input/parameters:
outVal - the double value to be output (double)
precision - the number of digits to the right of the decimal point (int)
Function output/parameters: none
Function output/returned: none
Device input: none
Device output/monitor: one double value displayed
Dependencies: printDoubleJustified
*/
void printDouble( double outVal, int precision )
{
// initialize variables, set block size to zero and justified to "LEFT"
int blockSize = 0;
char justified[] = "LEFT";
// call justified function with specified parameters
// function: printDoubleJustified
printDoubleJustified( outVal, precision, blockSize, justified );
}
/*
Name: printDoubleJustified
Process: the precision is set for the double value, then the pre-spaces
and post-spaces are calculated for the requested justification,
and then the pre-spaces are printed, the double value is printed,
and then the post-spaces are printed
Function input/parameters:
outVal - the double value to be output (double)
blockSize - the width of the block within which
to print the integer value (int)
justify - "LEFT," "RIGHT," or "CENTER" justification in block (c-string)
Function output/parameters: none
Function output/returned: none
Device input: none
Device output/monitor: one double value displayed, justified, with precision
Dependencies: formatDouble, printStringJustified
*/
void printDoubleJustified( double outVal, int precision,
int blockSize, const char* justify )
{
// initialize variables, create temp string for configuration string
char specStr[ MIN_STR_LEN ];
// initialize variables, create temporary output string
char outStr[ MIN_STR_LEN ];
// set configuration string with precision value
// function: sprintf
sprintf( specStr, "%s0.%dlf", "%", precision );
// set output string
// function: sprintf
sprintf( outStr, specStr, outVal );
// print string justified
// function: printStringJustified
printStringJustified( outStr, blockSize, justify );
}
/*
Name: printEndline
Process: prints single endline character to end a line or create vertical space
Function input/parameters: none
Function output/parameters: none
Function output/returned: none
Device input: none
Device output/monitor: one endline character output
Dependencies: printf
*/
void printEndline()
{
// print one newline character
// function: printf
printf( "\n" );
}
/*
Name: printEndlines
Process: prints the specified number of endline characters
to end a line and/or create vertical space(s)
Function input/parameters: number of endlines (int)
Function output/parameters: none
Function output/returned: none
Device input: none
Device output/monitor: endline characters output as specified
Dependencies: printEndline
*/
void printEndlines( int numEndlines )
{
// check for number of end lines remaining
if( numEndlines > 0 )
{
// print one endling
// function: printEndline
printEndline();
// (recursive) print remaining end lines
printEndlines( numEndlines - 1 );
}
}
/*
Name: printInteger
Process: prints integer value
Function input/parameters: outVal - the integer value to be output
Output: None
Device Input: None
Device Output: Integer is displayed as specified
Dependencies: printIntegerJustified
*/
void printInteger( int outVal )
{
// initialize variables, set block size to zero, justifification to "LEFT"
int blockSize = 0;
char justified[] = "LEFT";
// print justified integer with specified parameters
// function: printIntegerJustified
printIntegerJustified( outVal, blockSize, justified );
}
/*
Name: printIntegerJustified
Process: prints integer value with the specified justification
within the specified blockSize
Function input/parameters:
outVal - the integer value to be output
blockSize - the width of the block within which
to print the integer value (int)
justify - "LEFT," "RIGHT," or "CENTER" justification in block (c-string)
Output: None
Device Input: None
Device Output: Integer is displayed as specified
Dependencies: sprintf, printStringJustified
*/
void printIntegerJustified( int outVal, int blockSize, const char *justify )
{
// initialize variables, create temporary output string
char outStr[ MIN_STR_LEN ];
// convert integer to string
// function: sprintf
sprintf( outStr, "%d", outVal );
// print justified string
// function: printStringJustified
printStringJustified( outStr, blockSize, justify );
}
/*
Name: printLongInteger
Process: prints long integer
Function input/Parameters: outVal - the long integer value to be output
Function output/parameters: None
Function output/returned: None
Device Input/keyboard: None
Device Output/Monitor: integer is displayed as specified
Dependencies: printLongIntJustified
*/
void printLongInteger( long outVal )
{
// initialize variables, set block size to zero, justification to "LEFT"
int blockSize = 0;
char justified[] = "LEFT";
// print justified long integer with specified parameters
// function: printLongIntegerJustified
printLongIntegerJustified( outVal, blockSize, justified );
}
/*
Name: printLongIntegerJustified
Process: prints long integer with the specified justification
within the specified blockSize
Function input/Parameters:
outVal - the long integer value to be output
blockSize - the width of the block within which
to print the integer value (int)
justify - "LEFT," "RIGHT," or "CENTER" justification in block (c-string)
Function output/parameters: None
Function output/returned: None
Device Input/keyboard: None
Device Output/Monitor: integer is displayed as specified
Dependencies: sprintf, printStringJustified
*/
void printLongIntegerJustified( long outVal,
int blockSize, const char *justify )
{
// initialize variables, create temporary output string
char outStr[ MIN_STR_LEN ];
// convert long integer to string
// function: sprintf
sprintf( outStr, "%ld", outVal );
// print justified string
// function: printStringJustified
printStringJustified( outStr, blockSize, justify );
}
/*
Name: printString
Process: prints string
Function input/parameters: outVal - the string value to be output
Function output/parameters: None
Function output/returned: None
Function input/keyboard: None
Device output/monitor: string is displayed as specified
Dependencies: printStringJustified
*/
void printString( const char *outStr )
{
// initialize variables, set block size to zero, justification to "LEFT"
int blockSize = 0;
char justified[] = "LEFT";
// print justified string with specified parameters
// function: printStringJustified
printStringJustified( outStr, blockSize, justified );
}
/*
Name: printStringJustified
Process: prints string within specified block size
and with specified justification
Function input/parameters:
outVal - the string value to be output
blockSize - the width of the block within which
to print the integer value (int)
justify - "LEFT," "RIGHT," or "CENTER" justification in block (c-string)
Function output/parameters: None
Function output/returned: None
Function input/keyboard: None
Device output/monitor: string is displayed as specified
Dependencies: strlen, strcmp, printCharacters, printf
*/
void printStringJustified ( const char *outStr,
int blockSize, const char *justify )
{
// initialize variables, set pre-, post- spaces to zero
int preSpaces = 0, postSpaces = 0, strLength;
// find length of string
// function: strlen
strLength = strlen( outStr );
// check justification for "LEFT"
// function: strcmp
if( strcmp( justify, "LEFT" ) == 0 )
{
// set post spaces for left justification
postSpaces = blockSize - strLength;
}
// otherwise, check justification for "RIGHT"
// function: strcmp
else if( strcmp( justify, "RIGHT" ) == 0 )
{
// set pre spaces for right justification
preSpaces = blockSize - strLength;
}
// Otherwise, assume "CENTER" justification
else
{
// set pre spaces for centered
preSpaces = blockSize / 2 - strLength / 2;
// set post spaces for centered
postSpaces = blockSize - strLength - preSpaces;
}
// print pre spaces
// function: printChars
printCharacters( preSpaces, SPACE );
// print string
// function: printf
printf( "%s", outStr );
// print post spaces
// function: printChars
printCharacters( postSpaces, SPACE );
}
/*
Name: promptForCharacter
Process: prompts user for character, then returns it
Function input/parameters: text to prompt user (c-string)
Function output/parameters: None
Function output/returned: one character (chars
Device input/keyboard: user entry of a character
Device output/monitor: prompt string displayed
Dependencies: printf, getChar
*/
char promptForCharacter( const char *promptString )
{
// initialize variables
char outChar;
// print prompt string
// function: printf
printf( "%s", promptString );
// get character from stream
// function: getChar
outChar = (char)getChar();
// return captured character
return outChar;
}
/*
Name: promptForDouble
Process: prompts user for double, then returns it
Function input/parameters: test to prompt user (c-string)
Function output/parameters: None
Function output/returned: value (double)
Device input/keyboard: user entry of an integer value
Device output/monitor: prompt string displayed
Dependencies: printf, scanf
*/
double promptForDouble( const char *promptString )
{
// initialize variables
double outDouble;
// print prompt string
// function: printf
printf( "%s", promptString );
// get double value
// function: scanf
scanf( "%lf", &outDouble );
// return captured double
return outDouble;
}
/*
Name: promptForInteger
Process: prompts user for integer, then returns it
Function input/parameters: text to prompt user (c-string)
Function output/parameters: none
Function output/returned: integer value (int)
Device input/keyboard: user entry of a double value
Device output/monitor: prompt string displayed
Dependencies: printf, scanf
*/
int promptForInteger( const char *promptString )
{
// initialize variables
int outInt;
// print prompt string
// function: printf
printf( "%s", promptString );
// input integer
// function: scanf
scanf( "%d", &outInt );
// return captured integer
return outInt;
}
/*
Name: promptForString
Process: prompts user for string, then returns it
Function input/parameters: text to prompt user (c-string)
Function output/parameters: input string from user(c-string)
Function output/returned: none
Device input/keyboard: user entry of a string value
Device output/monitor: prompt string displayed
Dependencies: printf, getInputString
*/
void promptForString( const char *promptString, char returnStr[] )
{
// initialize variables, don't allow white space capture
bool dontClearWhitespace = false;
// pring prompt string
// function: printf
printf( "%s", promptString );
// get input string from user (returned via parameter)
// function: getInputString
getInputString( returnStr, dontClearWhitespace );
}

View file

@ -0,0 +1,289 @@
// Preprocessor directive
#ifndef CONSOLE_IO_UTILITY_H
#define CONSOLE_IO_UTILITY_H
//Header Files
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "StandardConstants.h"
//Global Constants
// none
//Function Prototypes
/*
Name: clearLeadingCharacters
Process: Clears all leading non-printable characters from the console stream
Function input/parameters: allow space return (bool)
Function output/parameters: none
Function output/returned: first printable character as integer
Device input/keyboard: none
Device output/monitor: none
Dependencies: getchar
*/
int clearLeadingCharacters( bool allowSpaceReturn );
/*
Name: getChar
Process: returns next character in stream,
ignores all white space (including SPACE) prior to character
Function input/parameters: flag to accept space (bool)
Function output/parameters: none
Function output/returned: one character (char) is returned to calling method
Device input/keyboard: places one or more characters in stream
Device output/monitor: none
Dependencies:
*/
char getChar();
/*
Name: getCharFiltered
Process: returns next character in stream,
ignores non printable characters prior to input character
but may accept space depending on flag
Function input/parameters: flag to accept space (bool)
Function output/parameters: none
Function output/returned: first printable character or space (if configured)
returned (char)
Device input/keyboard: places one or more characters in stream
Device output/monitor: none
Dependencies: getchar(), clearLeadingCharacters
*/
char getCharFiltered( bool acceptSpace );
/*
Name: getInputString
Process: acquires input string from keyboard, if clear white space flag set,
ignores all white space, including space character
Function input/parameters: clear white space control (bool)
Function output/parameters: character array (char[])
Function output/returned: none
Device input/keyboard: user entry of a character stream
Device output/monitor: none
Dependencies: getchar(), clearLeadingCharacters
*/
void getInputString(char inputArr[], bool clearWhiteSpace );
/*
Name: printCharacter
Process: prints a single character
Function input/parameters: character to be printed (char)
Function output/parameters: None
Function output/returned: None
Device input/keyboard: None
Device output/monitor: character printed as specified
Dependencies: printCharacters
*/
void printCharacter( char outputChar );
/*
Name: printCharacters
Process: prints given number of characters
Function input/parameters: number of characters (int),
character to be printed (char)
Function output/parameters: None
Function output/returned: None
Device input/keyboard: None
Device output/monitor: Number of specified characters printed
Dependencies: printf
*/
void printCharacters( int numChars, char outputChar );
/*
Name: printDouble
Process: simple print double function
Function input/parameters:
outVal - the double value to be output (double)
precision - the number of digits to the right of the decimal point (int)
Function output/parameters: none
Function output/returned: none
Device input: none
Device output/monitor: one double value displayed
Dependencies: printDoubleJustified
*/
void printDouble( double outVal, int precision );
/*
Name: printDoubleJustified
Process: the precision is set for the double value, then the pre-spaces
and post-spaces are calculated for the requested justification,
and then the pre-spaces are printed, the double value is printed,
and then the post-spaces are printed
Function input/parameters:
outVal - the double value to be output (double)
blockSize - the width of the block within which
to print the integer value (int)
justify - "LEFT," "RIGHT," or "CENTER" justification in block (c-string)
Function output/parameters: none
Function output/returned: none
Device input: none
Device output/monitor: one double value displayed, justified, with precision
Dependencies: formatDouble, printStringJustified
*/
void printDoubleJustified( double outVal, int precision,
int blockSize, const char* justify );
/*
Name: printEndline
Process: prints single endline character to end a line or create vertical space
Function input/parameters: none
Function output/parameters: none
Function output/returned: none
Device input: none
Device output/monitor: one endline character output
Dependencies: printEndline
*/
void printEndline();
/*
Name: printEndlines
Process: prints the specified number of endline characters
to end a line and/or create vertical space(s)
Function input/parameters: number of endlines (int)
Function output/parameters: none
Function output/returned: none
Device input: none
Device output/monitor: endline characters output as specified
Dependencies: printEndline
*/
void printEndlines( int numEndlines );
/*
Name: printInteger
Process: prints integer value
Function input/parameters: outVal - the integer value to be output
Function output/parameters: None
Device Input: None
Device Output: Integer is displayed as specified
Dependencies: printIntegerJustified
*/
void printInteger( int outVal );
/*
Name: printIntegerJustified
Process: prints integer value with the specified justification
within the specified blockSize
Function input/parameters:
outVal - the integer value to be output (int)
blockSize - the width of the block within which
to print the integer value (int)
justify - "LEFT," "RIGHT," or "CENTER" justification in block (c-string)
Function output/parameters: none
Device Input: None
Device Output: Integer is displayed as specified
Dependencies: sprintf, printStringJustified
*/
void printIntegerJustified( int outVal, int blockSize, const char *justify );
/*
Name: printLongInteger
Process: prints long integer
Function input/Parameters: outVal - the long integer value to be output
Function output/parameters: None
Function output/returned: None
Device Input/keyboard: None
Device Output/Monitor: integer is displayed as specified
Dependencies: printLongIntJustified
*/
void printLongInteger( long outVal );
/*
Name: printLongIntegerJustified
Process: prints long integer with the specified justification
within the specified blockSize
Function Input/Parameters:
outVal - the long integer value to be output (long int)
blockSize - the width of the block within which
to print the integer value (int)
justify - "LEFT," "RIGHT," or "CENTER" justification in block (c-string)
Function output/parameters: None
Function output/returned: None
Device Input/keyboard: None
Device Output/Monitor: integer is displayed as specified
Dependencies: sprintf, printStringJustified
*/
void printLongIntegerJustified( long outVal,
int blockSize, const char *justify );
/*
Name: printString
Process: prints string
Function input/parameters: outVal - the string value to be output
Function output/parameters: None
Function output/returned: None
Function input/keyboard: None
Device output/monitor: string is displayed as specified
Dependencies: printStringJustified
*/
void printString( const char *outStr );
/*
Name: printStringJustified
Process: prints string within specified block size
and with specified justification
Function input/parameters:
outStr - the string value to be output (c-string)
blockSize - the width of the block within which
to print the integer value (int)
justify - "LEFT," "RIGHT," or "CENTER" justification in block (c-string)
Function output/parameters: None
Function output/returned: None
Function input/keyboard: None
Device output/monitor: string is displayed as specified
Dependencies: strlen, strcmp, printCharacters, printf
*/
void printStringJustified( const char *outStr,
int blockSize, const char *justify );
/*
Name: promptForCharacter
Process: prompts user for character, then returns it
Function input/parameters: text to prompt user (c-string)
Function output/parameters: None
Function output/returned: one character (chars
Device input/keyboard: user entry of a character
Device output/monitor: prompt string displayed
Dependencies: printf, getChar
*/
char promptForCharacter( const char *promptString );
/*
Name: promptForDouble
Process: prompts user for double, then returns it
Function input/parameters: test to prompt user (c-string)
Function output/parameters: None
Function output/returned: value (double)
Device input/keyboard: user entry of an integer value
Device output/monitor: prompt string displayed
Dependencies: printf, scanf
*/
double promptForDouble( const char *promptString );
/*
Name: promptForInteger
Process: prompts user for integer, then returns it
Function input/parameters: text to prompt user (c-string)
Function output/parameters: none
Function output/returned: integer value (int)
Device input/keyboard: user entry of a double value
Device output/monitor: prompt string displayed
Dependencies: printf, scanf
*/
int promptForInteger( const char *promptString );
/*
Name: promptForString
Process: prompts user for string, then returns it
Function input/parameters: text to prompt user (c-string)
Function output/parameters: input string captured (c-string)
Function output/returned: none
Device input/keyboard: user entry of a string value
Device output/monitor: prompt string displayed
Dependencies: printf, getInputString
*/
void promptForString( const char *promptString, char returnStr[] );
#endif // end CONSOLE_IO_UTILITY_H

View file

@ -0,0 +1,717 @@
/*
Formatted file input utility, function implementations
Copyright (c) 2021 by Michael E. Leverington
Code is free for use in whole or in part and may be modified for other
non-commercial use as long as the above copyright statement is included.
*/
// header files
#include "File_Input_Utility.h"
#include <stdlib.h> //////////////////////////test
// local global constants, used only in this file
// dummy file pointer for accessInputFilePointer
const FILE *DUMMY_INPUT_FILE_PTR = NULL;
// dummy Boolean for accessEndOfInputFileFlag, accessOpenFlag
const char DUMMY_INPUT_FLAG = false;
// control code for accessing file pointer, accessInputFilePointer
const int GET_INPUT_FILE_PTR = 3003;
// control code for accessing flag value,
// accessEndOfInputFileFlag, accessOpenFlag
const int GET_INPUT_FLAG = 2002;
// control code for setting file pointer, accessInputFilePointer
const int SET_INPUT_FILE_PTR = 4004;
// control code for setting flag value, accessEndOfInputFileFlag,
// accessOpenFlag
const int SET_INPUT_FLAG = 1001;
/*
Name: accessEndOfInputFileFlag
process: allows accessing or modifying end of file flag state;
note: flag is maintained (static) value initialized to false
Function input/parameters: control code (int), flag state (bool)
Function output/parameters: none
Function output/returned: result of flag state (bool)
Device input/file: none
Device output/monitor: none
Dependencies: none
*/
bool accessEndOfInputFileFlag( int ctrlCode, bool setFlag )
{
// initialize end of file flag to false
static bool endOfFileFlag = false;
// test for set flag
if( ctrlCode == SET_INPUT_FLAG )
{
// set flag to given state
endOfFileFlag = setFlag;
}
// return flag state
return endOfFileFlag;
}
/*
Name: accessInputFilePointer
process: allows setting or accessing file pointer;
note: pointer is maintained (static) value initialized to NULL
Function input/parameters: control code (int), file pointer (FILE *)
DUMMY_INPUT_FILE_PTR used
if not setting file pointer
Function output/parameters: none
Function output/returned: file pointer (FILE *)
Device input/file: none
Device output/monitor: none
Dependencies: none
*/
FILE *accessInputFilePointer( int ctrlCode, const FILE *filePtr )
{
// initialize file pointer to null
static FILE *filePointer = NULL;
// check for set new pointer
if( ctrlCode == SET_INPUT_FILE_PTR )
{
// set file pointer to parameter
filePointer = (FILE *)filePtr;
}
// return file pointer
return filePointer;
}
/*
Name: checkForEndOfInputFile
process: checks to see if end of file flag has been encountered,
returns true if EOF has been encountered, false otherwise
Function input/parameters: none
Function output/parameters: none
Function output/returned: result of specified test (bool)
Device input/file: none
Device output/monitor: none
Dependencies: accessEndOfInputFileFlag
*/
bool checkForEndOfInputFile()
{
// return access to end of file flag
// function: accessEndOfInputFileFlag
return accessEndOfInputFileFlag( GET_INPUT_FLAG, DUMMY_INPUT_FLAG );
}
/*
Name: checkForInputFileOpen
process: checks to see if file is currently open
returns true if file pointer not null, false otherwise
Function input/parameters: none
Function output/parameters: none
Function output/returned: result of specified test (bool)
Device input/file: none
Device output/monitor: none
Dependencies: accessInputFilePointer
*/
bool checkForInputFileOpen()
{
// return access to end of file flag
// function: accessInputFilePointer
return accessInputFilePointer( GET_INPUT_FLAG,
DUMMY_INPUT_FILE_PTR ) != NULL;
}
/*
Name: clearLeadingWhiteSpace
process: captures non printable characters,
and space if flag set,
returns first non white space value found as integer
Function input/parameters: file pointer (FILE *), clear space flag (bool)
Function output/parameters: none
Function output/returned: first non WS character as integer
Device input/file: value captured from file
Device output/monitor: none
Dependencies: fgetc
*/
int clearLeadingWhiteSpace( FILE *filePtr, bool clearSpace )
{
// initialize character input value
// function: fgetc
int charInt = fgetc( filePtr );
while(
// continue if not at end of file
( charInt != EOF )
// AND
// continue if character is non printable
&& ( ( charInt < (int)SPACE || charInt > (int)HIGHEST_PRINTABLE_CHAR )
// OR
// continue if clear space flag set and space is found
|| ( clearSpace && charInt == SPACE ) )
)
{
// capture next character
// function: fgetc
charInt = fgetc( filePtr );
}
// return input value as character
return (char)charInt;
}
/*
Name: closeInputFile
process: closes input file, returns true if successful, false otherwise
Function input/parameters: none
Function output/parameters: none
Function output/returned: none
Device input/file: file closed
Device output/monitor: none
Dependencies: accessInputFilePointer, fclose, accessEndOfInputFileFlag
*/
bool closeInputFile()
{
// set local file pointer to working file pointer
// function: accessInputFilePointer
FILE *filePtr = accessInputFilePointer( GET_INPUT_FILE_PTR,
DUMMY_INPUT_FILE_PTR );
// check for valid file pointer
if( filePtr != NULL )
{
// close file
fclose( filePtr );
// set pointer to NULL
// function: accessInputFilePointer
accessInputFilePointer( SET_INPUT_FILE_PTR, NULL );
// set end of file flag to false
// function: accessEndOfInputFileFlag
accessEndOfInputFileFlag( SET_INPUT_FLAG, false );
// return successful operation
return true;
}
// return failed operation
return false;
}
/*
Name: openInputFile
process: opens input file, sets file pointer and unsets end of file flag,
returns true if successful, false otherwise
Function input/parameters: file name (c-string)
Function output/parameters: none
Function output/returned: success of operation (bool)
Device input/file: file opened
Device output/monitor: none
Dependencies: fopen, accessInputFilePointer, accessEndOfInputFileFlag
*/
bool openInputFile( const char *fileName )
{
// initialize variables
FILE *filePtr = NULL;
char readOnlyCharacter[] = "r";
// check for file not open
// function: checkForInputFileOpen
if( !checkForInputFileOpen() )
{
// open file
// function: fopen
filePtr = fopen( fileName, readOnlyCharacter );
// check for success of file open
if( filePtr != NULL )
{
// set pointer in access function
// function: accessInputFilePointer
accessInputFilePointer( SET_INPUT_FILE_PTR, filePtr );
// reset end of file flag, if needed
// accessEndOfInputFileFlag
accessEndOfInputFileFlag( SET_INPUT_FLAG, false );
// return operation success
return true;
}
}
// end of file not previously opened block
// return operation failure
return false;
}
/*
Name: readCharacterFromFile
process: ignores leading unprintable characters, including space,
captures first printable character
Function input/parameters: allow space capture (bool)
Function output/parameters: none
Function output/returned: character found if successful, ZERO_CHAR otherwise
Device input/file: character(s) captured from file as needed
Device output/monitor: none
Dependencies: readCharacterFromFileConfigured
*/
char readCharacterFromFile()
{
// initialize parameter to not allow space
bool noSpaceCapture = false;
// capture character, return
// function: getCharFromFileConfigured
return readCharacterFromFileConfigured( noSpaceCapture );
}
/*
Name: readCharacterFromFileConfigured
process: ignores leading unprintable characters,
captures first printable character,
including space if allowed
Function input/parameters: allow space capture (bool)
Function output/parameters: none
Function output/returned: character found if successful, ZERO_CHAR otherwise
Device input/file: character(s) captured from file as needed
Device output/monitor: none
Dependencies: checkForInputFileOpen, checkForEndOfInputFile,
accessInputFilePointer, clearLeadingWhiteSpace,
accessEndOfInputFileFlag
*/
char readCharacterFromFileConfigured( bool captureSpace )
{
// initialize variables
FILE *filePtr;
int intChar = ZERO_CHAR;
// check for file open and data accessible
// function: checkForInputFileOpen, checkForEndOfInputFile
if( checkForInputFileOpen() && !checkForEndOfInputFile() )
{
// set file pointer
// function: accessInputFilePointer
filePtr = accessInputFilePointer( GET_INPUT_FILE_PTR,
DUMMY_INPUT_FILE_PTR );
// get character
// function: clearLeadingWhiteSpace
intChar = clearLeadingWhiteSpace( filePtr, !captureSpace );
// check for end of file found
if( intChar == EOF )
{
// set end of file flag
// function: accessEndOfInputFileFlag
accessEndOfInputFileFlag( SET_INPUT_FLAG, true );
// set return value to zero
intChar = ZERO_CHAR;
}
}
// return value as character
return intChar;
}
/*
Name: readDoubleFromFile
process: ignores leading unprintable characters,
captures first contiguous double value
Function input/parameters: none
Function output/parameters: none
Function output/returned: double value found if successful (double)
Device input/file: double value captured from file
Device output/monitor: none
Dependencies: checkForInputFileOpen, checkForEndOfInputFile,
accessInputFilePointer, fscanf, accessEndOfInputFileFlag
*/
double readDoubleFromFile()
{
// initialize variables
FILE *filePtr = NULL;
int fscanfReturn = EOF;
double doubleVal = ZERO_VALUE;
// check for file open and data accessible
// function: checkForInputFileOpen, checkForEndOfInputFile
if( checkForInputFileOpen() && !checkForEndOfInputFile() )
{
// set file pointer
// function: accessInputFilePointer
filePtr = accessInputFilePointer( GET_INPUT_FILE_PTR,
DUMMY_INPUT_FILE_PTR );
// get double value
// function: fscanf
fscanfReturn = fscanf( filePtr, "%lf", &doubleVal );
// check for end of file found
if( fscanfReturn == EOF )
{
// set end of file flag
// function: accessEndOfInputFileFlag
accessEndOfInputFileFlag( SET_INPUT_FLAG, true );
// set return value to zero
doubleVal = ZERO_VALUE;
}
}
// return acquired value
return doubleVal;
}
/*
Name: readIntegerFromFile
process: ignores leading unprintable characters,
captures first contiguous integer
Function input/parameters: none
Function output/parameters: none
Function output/returned: integer found if successful, ZERO_VALUE otherwise
Device input/file: character(s) captured from file as needed
Device output/monitor: none
Dependencies: checkForInputFileOpen, checkForEndOfInputFile,
accessInputFilePointer, fscanf, accessEndOfInputFileFlag
*/
int readIntegerFromFile()
{
// initialize variables
FILE *filePtr = NULL;
int fscanfReturn = EOF;
int intVal = ZERO_VALUE;
// check for file open and data accessible
// function: checkForInputFileOpen, checkForEndOfInputFile
if( checkForInputFileOpen() && !checkForEndOfInputFile() )
{
// set file pointer
// function: accessInputFilePointer
filePtr = accessInputFilePointer( GET_INPUT_FILE_PTR,
DUMMY_INPUT_FILE_PTR );
// get integer value
// function: fscanf
fscanfReturn = fscanf( filePtr, "%d", &intVal );
// check for end of file found
if( fscanfReturn == EOF )
{
// set end of file flag
// function: accessEndOfInputFileFlag
accessEndOfInputFileFlag( SET_INPUT_FLAG, true );
// set return value to zero
intVal = ZERO_VALUE;
}
}
// return acquired value
return intVal;
}
/*
Name: readLongFromFile
process: ignores leading unprintable characters,
captures first contiguous long int
Function input/parameters: none
Function output/parameters: none
Function output/returned: long integer found if successful,
ZERO_VALUE otherwise
Device input/file: long value captured from file
Device output/monitor: none
Dependencies: checkForInputFileOpen, checkForEndOfInputFile,
accessInputFilePointer, fscanf, accessEndOfInputFileFlag
*/
long readLongFromFile()
{
// initialize variables
FILE *filePtr = NULL;
int fscanfReturn = EOF;
long longVal = ZERO_VALUE;
// check for file open and data accessible
// function: checkForInputFileOpen, checkForEndOfInputFile
if( checkForInputFileOpen() && !checkForEndOfInputFile() )
{
// set file pointer
// function: accessInputFilePointer
filePtr = accessInputFilePointer( GET_INPUT_FILE_PTR,
DUMMY_INPUT_FILE_PTR );
// get long value
// function: fscanf
fscanfReturn = fscanf( filePtr, "%ld", &longVal );
// check for end of file found
if( fscanfReturn == EOF )
{
// set end of file flag
// function: accessEndOfInputFileFlag
accessEndOfInputFileFlag( SET_INPUT_FLAG, true );
// set return value to zero
longVal = ZERO_VALUE;
}
}
// return acquired value
return longVal;
}
/*
Name: readRawCharFromFile
process: captures first character in file stream including non-printable
Function input/parameters: none
Function output/parameters: none
Function output/returned: character found if successful, ZERO_CHAR otherwise
Device input/file: character captured from file
Device output/monitor: none
Dependencies: checkForInputFileOpen, checkForEndOfInputFile,
accessInputFilePointer, fgetc, accessEndOfInputFileFlag
*/
char readRawCharFromFile()
{
// initialize variables
FILE *filePtr = NULL;
int intChar = (int)ZERO_CHAR;
// check for file open and data accessible
// function: checkForInputFileOpen, checkForEndOfInputFile
if( checkForInputFileOpen() && !checkForEndOfInputFile() )
{
// set file pointer
// function: accessInputFilePointer
filePtr = accessInputFilePointer( GET_INPUT_FILE_PTR,
DUMMY_INPUT_FILE_PTR );
// get character
// function: fgetc
intChar = fgetc( filePtr );
// check for end of file found
if( intChar == EOF )
{
// set end of file flag
// function: accessEndOfInputFileFlag
accessEndOfInputFileFlag( SET_INPUT_FLAG, true );
// set return value to zero
intChar = (int)ZERO_CHAR;
}
}
// return acquired value
return (char)intChar;
}
/*
Name: readStringConfiguredFromFile
Process: captures string with options:
- clear leading non printable (bool)
- clear leading space character (bool)
- adquires characters up to any non-printable (bool)
- adquires characters up to any printable delimiter (char)
- use constant NON_PRINTABLE if no delimiter
- also stops capture when full (MAX_STR_LEN)
Note: This function consumes the last character input,
including the space, delimiter, or non-printable character
Function input/parameters: see process above
Function output/parameters: captured string (char *), EMPTY_STRING otherwise
Function output/returned: success of operation (bool)
Device input/file: character(s) captured from file as needed
Device output/monitor: none
Dependencies: checkForInputFileOpen, checkForEndOfInputFile,
clearLeadingWhiteSpace, fgetc, accessEndOfInputFileFlag
*/
bool readStringConfiguredFromFile( bool clearLeadingNonPrintable,
bool clearLeadingSpace,
bool stopAtNonPrintable,
char delimiter,
char *capturedString )
{
// initialize variables
int intChar = EOF, index = 0;
FILE *filePtr = NULL;
// initialize output string
capturedString[ index ] = NULL_CHAR;
// check for file open and data accessible
// function: checkForInputFileOpen, checkForEndOfInputFile
if( checkForInputFileOpen() && !checkForEndOfInputFile() )
{
// get file pointer
// function: accessFilePtr
filePtr = accessInputFilePointer( GET_INPUT_FILE_PTR,
DUMMY_INPUT_FILE_PTR );
// check for clearing non printable
if( clearLeadingNonPrintable )
{
// clear leading white space, with clear space character flag
// function: clearLeadingWhiteSpace
intChar = clearLeadingWhiteSpace( filePtr, clearLeadingSpace );
}
// otherwise, assume non printable not cleared
else
{
// get first character
// function: fgetc
intChar = fgetc( filePtr );
// check for clearing spaces
if( clearLeadingSpace )
{
// loop while spaces found and end of file not found
while( intChar != EOF && intChar == (int)SPACE )
{
// capture new character
// function: fgetc
intChar = fgetc( filePtr );
}
}
} // end leading character clearing process
// check for end of file found
if( intChar == EOF )
{
// set end of file flag
// function: accessEndOfInputFileFlag
accessEndOfInputFileFlag( SET_INPUT_FLAG, true );
// return failed operation
return false;
}
// loop to capture input
while(
// continues if not at end of file and max string length not reached
( intChar != EOF && index < MAX_STR_LEN - 1 )
// AND
// continues if not printable flag set and characters are printable
// OR not printable flag not set
&& ( ( stopAtNonPrintable && intChar >= (int)SPACE )
|| ( !stopAtNonPrintable ) )
// AND
// continues if specified delimiter is not found
&& ( intChar != (char)delimiter )
)
{
// place character in array element
capturedString[ index ] = (char)intChar;
// increment array index
index++;
// set next element to null character / end of c-string
capturedString[ index ] = NULL_CHAR;
// get next character as integer
// function: fgetc
intChar = fgetc( filePtr );
}
// end loop
// return successful operation
return true;
}
// end of open file and accessible data block
return false; // temporary stub return
}
/*
Name: readStringSegmentFromFile
process: ignores leading white space, including space character,
captures series of characters up to first space,
or end of line, or full string
Function input/parameters: none
Function output/parameters: captured string (char *), EMPTY_STRING otherwise
Function output/returned: success of operation (bool)
Device input/file: character(s) captured from file as needed
Device output/monitor: none
Dependencies: readStringConfigured with appropriate parameters
*/
bool readStringSegmentFromFile( char *capturedString )
{
// initialize variables
bool clearLeadingNonPrintable = true;
bool clearLeadingSpace = true;
bool stopAtNonPrintable = true;
char delimiter = SPACE;
// call utility function, return
return readStringConfiguredFromFile( clearLeadingNonPrintable,
clearLeadingSpace,
stopAtNonPrintable,
delimiter,
capturedString );
}
/*
Name: readStringToLineEndFromFile
process: ignores leading white space, including space character,
captures series of characters up to end of current line
or full string
Function input/parameters: none
Function output/parameters: captured string (char *), EMPTY_STRING otherwise
Function output/returned: string found if successful
Device input/file: character(s) captured from file as needed
Device output/monitor: none
Dependencies: readStringConfigured with appropriate parameters
*/
bool readStringToLineEndFromFile( char *capturedString )
{
// initialize variables
bool clearLeadingNonPrintable = true;
bool clearLeadingSpace = true;
bool stopAtNonPrintable = true;
char delimiter = NON_PRINTABLE;
// call utility function, return
return readStringConfiguredFromFile( clearLeadingNonPrintable,
clearLeadingSpace,
stopAtNonPrintable,
delimiter,
capturedString );
}
/*
Name: readStringToDelimiterFromFile
process: ignores leading white space, including space character,
captures series of characters up to specified character
Function input/parameters: none
Function output/parameters: captured string (char *), EMPTY_STRING otherwise
Function output/returned: string found if successful
Device input/file: character(s) captured from file as needed
Device output/monitor: none
Dependencies: readStringConfigured with appropriate parameters
*/
bool readStringToDelimiterFromFile( char delimiter, char *capturedString )
{
// initialize variables
bool clearLeadingNonPrintable = true;
bool clearLeadingSpace = true;
bool stopAtNonPrintable = true;
// call utility function, return
return readStringConfiguredFromFile( clearLeadingNonPrintable,
clearLeadingSpace,
stopAtNonPrintable,
delimiter,
capturedString );
}

View file

@ -0,0 +1,295 @@
/*
Formatted file input utility, function prototypes
Copyright (c) 2021 by Michael E. Leverington
Code is free for use in whole or in part and may be modified for other
non-commercial use as long as the above copyright statement is included.
*/
/*
Rev 0.0: 30 June 2021, Initial creation of file
UTILITY USAGE INSTRUCTIONS:
1) This utility contains functions that may be used for file input operations
using the C programming language with text files.
The utility must be linked with the program code with which
it will be used.
2) Specifications for all the functions
are provided in a standardized format below.
3) Use this code at your own risk. There is no guarantee of any performance,
implied or otherwise, for any part of this code
END OF INSTRUCTIONS
*/
// PreProcessor test
#ifndef FILE_INPUT_UTILITY_H
#define FILE_INPUT_UTILITY_H
// header files
#include <stdbool.h>
#include <stdio.h>
#include "StandardConstants.h"
// constants shared with other files
// non printable flag, used in getStringConfigured
static const char NON_PRINTABLE = (char)(127);
// constant used for zero character return
static const char ZERO_CHAR = '0';
// constant used for zero int/double value return
static const char ZERO_VALUE = 0;
// function prototypes
/*
Name: accessEndOfInputFileFlag
process: allows accessing or modifying end of file flag state;
note: flag is maintained (static) value initialized to false
Function input/parameters: control code (int), flag state (bool)
Function output/parameters: none
Function output/returned: result of flag state (bool)
Device input/file: none
Device output/monitor: none
Dependencies: none
*/
bool accessEndOfInputFileFlag( int ctrlCode, bool setFlag );
/*
Name: accessInputFilePointer
process: allows setting or accessing file pointer;
note: pointer is maintained (static) value initialized to NULL
Function input/parameters: control code (int), file pointer (FILE *)
DUMMY_INPUT_FILE_PTR used
if not setting file pointer
Function output/parameters: none
Function output/returned: file pointer (FILE *)
Device input/file: none
Device output/monitor: none
Dependencies: none
*/
FILE *accessInputFilePointer( int ctrlCode, const FILE *filePtr );
/*
Name: checkForEndOfInputFile
process: checks to see if end of file flag has been encountered,
returns true if EOF has been encountered, false otherwise
Function input/parameters: none
Function output/parameters: none
Function output/returned: result of specified test (bool)
Device input/file: none
Device output/monitor: none
Dependencies: accessEndOfInputFileFlag
*/
bool checkForEndOfInputFile();
/*
Name: checkForInputFileOpen
process: checks to see if file is currently open
returns true if file pointer not null, false otherwise
Function input/parameters: none
Function output/parameters: none
Function output/returned: result of specified test (bool)
Device input/file: none
Device output/monitor: none
Dependencies: accessInputFilePointer
*/
bool checkForInputFileOpen();
/*
Name: clearLeadingWhiteSpace
process: captures non printable characters,
and space if flag set,
returns first non white space value found as integer
Function input/parameters: file pointer (FILE *), clear space flag (bool)
Function output/parameters: none
Function output/returned: first non WS character as integer
Device input/file: value captured from file
Device output/monitor: none
Dependencies: fgetc
*/
int clearLeadingWhiteSpace( FILE *filePtr, bool clearSpace );
/*
Name: closeInputFile
process: closes input file, returns true if successful, false otherwise
Function input/parameters: none
Function output/parameters: none
Function output/returned: none
Device input/file: file closed
Device output/monitor: none
Dependencies: accessInputFilePointer, fclose, accessEndOfInputFileFlag
*/
bool closeInputFile();
/*
Name: openInputFile
process: opens input file, sets file pointer and unsets end of file flag,
returns true if successful, false otherwise
Function input/parameters: file name (c-string)
Function output/parameters: none
Function output/returned: success of operation (bool)
Device input/file: file opened
Device output/monitor: none
Dependencies: fopen, accessInputFilePointer, accessEndOfInputFileFlag
*/
bool openInputFile( const char *fileName );
/*
Name: readCharacterFromFile
process: ignores leading unprintable characters, including space,
captures first printable character
Function input/parameters: allow space capture (bool)
Function output/parameters: none
Function output/returned: character found if successful, ZERO_CHAR otherwise
Device input/file: character(s) captured from file as needed
Device output/monitor: none
Dependencies: readCharacterFromFileConfigured
*/
char readCharacterFromFile();
/*
Name: readCharacterFromFileConfigured
process: ignores leading unprintable characters,
captures first printable character,
including space if allowed
Function input/parameters: allow space capture (bool)
Function output/parameters: none
Function output/returned: character found if successful, ZERO_CHAR otherwise
Device input/file: character(s) captured from file as needed
Device output/monitor: none
Dependencies: checkForInputFileOpen, checkForEndOfInputFile,
accessInputFilePointer, clearLeadingWhiteSpace,
accessEndOfInputFileFlag
*/
char readCharacterFromFileConfigured( bool captureSpace );
/*
Name: readDoubleFromFile
process: ignores leading unprintable characters,
captures first contiguous double value
Function input/parameters: none
Function output/parameters: none
Function output/returned: double value found if successful (double)
Device input/file: double value captured from file
Device output/monitor: none
Dependencies: checkForInputFileOpen, checkForEndOfInputFile,
accessInputFilePointer, fscanf, accessEndOfInputFileFlag
*/
double readDoubleFromFile();
/*
Name: readIntegerFromFile
process: ignores leading unprintable characters,
captures first contiguous integer
Function input/parameters: none
Function output/parameters: none
Function output/returned: integer found if successful, ZERO_VALUE otherwise
Device input/file: character(s) captured from file as needed
Device output/monitor: none
Dependencies: checkForInputFileOpen, checkForEndOfInputFile,
accessInputFilePointer, fscanf, accessEndOfInputFileFlag
*/
int readIntegerFromFile();
/*
Name: readLongFromFile
process: ignores leading unprintable characters,
captures first contiguous long int
Function input/parameters: none
Function output/parameters: none
Function output/returned: long integer found if successful,
ZERO_VALUE otherwise
Device input/file: long value captured from file
Device output/monitor: none
Dependencies: checkForInputFileOpen, checkForEndOfInputFile,
accessInputFilePointer, fscanf, accessEndOfInputFileFlag
*/
long readLongFromFile();
/*
Name: readRawCharFromFile
process: captures first character in file stream including non-printable
Function input/parameters: none
Function output/parameters: none
Function output/returned: character found if successful, ZERO_CHAR otherwise
Device input/file: character captured from file
Device output/monitor: none
Dependencies: checkForInputFileOpen, checkForEndOfInputFile,
accessInputFilePointer, fgetc, accessEndOfInputFileFlag
*/
char readRawCharFromFile();
/*
Name: readStringConfiguredFromFile
Process: captures string with options:
- clear leading non printable (bool)
- clear leading space character (bool)
- adquires characters up to any non-printable (bool)
- adquires characters up to any printable delimiter (char)
- use constant NON_PRINTABLE if no delimiter
- also stops capture when full (MAX_STR_LEN)
Note: This function consumes the last character input,
including the space, delimiter, or non-printable character
Function input/parameters: see process above
Function output/parameters: captured string (char *), EMPTY_STRING otherwise
Function output/returned: success of operation (bool)
Device input/file: character(s) captured from file as needed
Device output/monitor: none
Dependencies: checkForInputFileOpen, checkForEndOfInputFile,
clearLeadingWhiteSpace, fgetc, accessEndOfInputFileFlag
*/
bool readStringConfiguredFromFile( bool clearLeadingNonPrintable,
bool clearLeadingSpace,
bool stopAtNonPrintable,
char delimiter,
char *capturedString );
/*
Name: readStringSegmentFromFile
process: ignores leading white space, including space character,
captures series of characters up to first space,
or end of line, or full string
Function input/parameters: none
Function output/parameters: captured string (char *), EMPTY_STRING otherwise
Function output/returned: success of operation (bool)
Device input/file: character(s) captured from file as needed
Device output/monitor: none
Dependencies: readStringConfigured with appropriate parameters
*/
bool readStringSegmentFromFile( char *capturedString );
/*
Name: readStringToLineEndFromFile
process: ignores leading white space, including space character,
captures series of characters up to end of current line
or full string
Function input/parameters: none
Function output/parameters: captured string (char *), EMPTY_STRING otherwise
Function output/returned: string found if successful
Device input/file: character(s) captured from file as needed
Device output/monitor: none
Dependencies: readStringConfigured with appropriate parameters
*/
bool readStringToLineEndFromFile( char *capturedString );
/*
Name: readStringToDelimiterFromFile
process: ignores leading white space, including space character,
captures series of characters up to specified character
Function input/parameters: none
Function output/parameters: captured string (char *), EMPTY_STRING otherwise
Function output/returned: string found if successful
Device input/file: character(s) captured from file as needed
Device output/monitor: none
Dependencies: readStringConfigured with appropriate parameters
*/
bool readStringToDelimiterFromFile( char delimiter, char *capturedString );
#endif // FILE_INPUT_UTILITY_H

View file

@ -0,0 +1,511 @@
/*
Formatted file ouput utility, function implementations
Copyright (c) 2021 by Michael E. Leverington
Code is free for use in whole or in part and may be modified for other
non-commercial use as long as the above copyright statement is included.
*/
// header files
#include "File_Output_Utility.h"
// local global constants, used only in this file
// dummy file pointer for accessInputFilePointer
const FILE *DUMMY_OUTPUT_FILE_PTR = NULL;
// dummy Boolean for accessEndOfInputFileFlag, accessOpenFlag
// const char DUMMY_OUTPUT_FLAG = false;
// control code for accessing file pointer, accessInputFilePointer
const int GET_OUTPUT_FILE_PTR = 6006;
// control code for accessing flag value,
// accessEndOfInputFileFlag, accessOpenFlag
const int GET_OUTPUT_FLAG = 9009;
// control code for setting file pointer, accessInputFilePointer
const int SET_OUTPUT_FILE_PTR = 8008;
// control code for setting flag value, accessEndOfInputFileFlag,
// accessOpenFlag
// const int SET_OUTPUT_FLAG = 1001;
/*
Name: accessOutputFilePointer
process: allows setting or accessing file pointer;
note: pointer is maintained (static) value initialized to NULL
Function input/parameters: control code (int), file pointer (FILE *)
DUMMY_OUTPUT_FILE_PTR used
if not setting file pointer
Function output/parameters: none
Function output/returned: file pointer (FILE *)
Device input/file: none
Device output/monitor: none
Dependencies: none
*/
FILE *accessOutputFilePointer( int ctrlCode, const FILE *filePtr )
{
static FILE *filePointer = NULL;
// check for set new pointer
if( ctrlCode == SET_OUTPUT_FILE_PTR )
{
// set file pointer to parameter
filePointer = (FILE *)filePtr;
}
// return file pointer
return filePointer;
}
/*
Name: closeOutputFile
Process: closes output file, returns true if successful, false otherwise
Function input/parameters: none
Function output/parameters: none
Function output/returned: operation success (bool)
Device input/file: none
Device output/file: output file closed
Dependencies: accessOutputFilePointer, fclose
*/
bool closeOutputFile()
{
// set local file pointer to working file pointer
// function: accessOutputFilePointer
FILE *filePtr = accessOutputFilePointer( GET_OUTPUT_FILE_PTR,
DUMMY_OUTPUT_FILE_PTR );
// check for valid file pointer
if( filePtr != NULL )
{
// add two more endlines to verify that the data is ended
// and has at least one extra newline after the data
// function: fprintf
fprintf( filePtr, "\n\n" );
// flush all data to the device
// function: fflush
fflush( filePtr );
// close file
// function: fclose
fclose( filePtr );
// set pointer to NULL
// function: accessOutputFilePointer
accessOutputFilePointer( SET_OUTPUT_FILE_PTR, NULL );
// return successful operation
return true;
}
// return failed operation
return false;
}
/*
Name: openOutputFile
Process: opens input file, returns true if successful, false otherwise
Function input/parameters: file name (c-string)
Function output/parameters: none
Function output/returned: operation success (bool)
Device input/file: none
Device output/file: output file opened
Dependencies: accessOutputFilePointer, fopen
*/
bool openOutputFile( const char *fileName )
{
// initialize variables
FILE *filePtr = accessOutputFilePointer( GET_OUTPUT_FILE_PTR,
DUMMY_OUTPUT_FILE_PTR );
char writeCharacter[] = "w";
// check for file not open
if( filePtr == NULL )
{
// open file
// function: fopen
filePtr = fopen( fileName, writeCharacter );
// check for success of file open
if( filePtr != NULL )
{
// set pointer in access function
// function: accessInputFilePointer
accessOutputFilePointer( SET_OUTPUT_FILE_PTR, filePtr );
// return operation success
return true;
}
}
// end of file not previously opened block
// return operation failure
return false;
}
/*
Name: writeCharacterToFile
Process: writes individual character to file,
returns true if successful, false otherwise
Function input/parameters: character to output (char)
Function output/parameters: none
Function output/returned: operation success (bool)
Device input/file: none
Device output/file: character written to file
Dependencies: accessOutputFilePointer, fprintf
*/
bool writeCharacterToFile( char outChar )
{
// initialize variables, set file pointer
// function: accessOutputFilePointer
FILE *filePtr = accessOutputFilePointer( GET_OUTPUT_FILE_PTR,
DUMMY_OUTPUT_FILE_PTR );
// check for open file
// function: checkForOutputFileOpen
if( filePtr != NULL )
{
// write character
// function: fprintf
fprintf( filePtr, "%c", outChar );
// return successful operation
return true;
}
// return failed operation
return false;
}
/*
Name: writeCharactersToFile
Process: writes multiple characters to file,
returns true if successful, false otherwise
Function input/parameters: number of characters to output (int),
character to output (char)
Function output/parameters: none
Function output/returned: operation success (bool)
Device input/file: none
Device output/file: multiple characters written to file
Dependencies: writeCharacterToFile
*/
bool writeCharactersToFile( int numChars, char outChar )
{
// check for number of characters remaining,
if( numChars > 0 )
{
// write character, check for success
// function: writeChar
if( writeCharacterToFile( outChar ) )
{
// continue recursion,
// check for downstream success
// function: writeChars
return writeCharactersToFile( numChars - 1, outChar );
}
// quit recursion
// return unsuccessful operation - write operation failed
return false;
}
// return successful operation - all characters have been written
return true;
}
/*
Name: writeDoubleToFile
Process: writes individual formatted double value to file,
returns true if successful, false otherwise
Function input/parameters: double value to output (double),
number of digits to right of radix point
(precision) (int)
Function output/parameters: none
Function output/returned: operation success (bool)
Device input/file: none
Device output/file: double written to file
Dependencies: writeDoubleJustifiedToFile
*/
bool writeDoubleToFile( double outVal, int precision )
{
// initialize variables, set block size to zero and justified to "LEFT"
int blockSize = 0;
char justified[] = "LEFT";
// call to justified function
// function: writeDoubleJustifiedToFile
return writeDoubleJustifiedToFile( outVal, precision,
blockSize, justified );
}
/*
Name: writeDoubleToFileJustified
Process: writes individual formatted double value to file justified,
returns true if successful, false otherwise
outVal - the double value to be output
precision - number of digits to the right of the radix point
blockSize - the width of the block within which
to print the integer value (int)
justify - "LEFT," "RIGHT," or "CENTER" justification
in block (c-string)
Function output/parameters: none
Function output/returned: operation success (bool)
Device input/file: none
Device output/file: double written to file as specified
Dependencies: sprintf, writeStringJustifiedToFile
*/
bool writeDoubleJustifiedToFile( double outVal, int precision,
int blockSize, const char *justified )
{
// initialize variables, create temp string for configuration string
char specStr[ MIN_STR_LEN ];
// initialize variables, create temporary output string
char outStr[ MIN_STR_LEN ];
// set configuration string with precision value
// function: sprintf
sprintf( specStr, "%s0.%dlf", "%", precision );
// set double to string
// function: sprintf
sprintf( outStr, specStr, outVal );
// write as string, return result
// function: writeStringJustified
return writeStringJustifiedToFile( outStr, blockSize, justified );
}
/*
Name: writeEndlineToFile
Process: writes newline ('\n') to file,
returns true if successful, false otherwise
Function input/parameters: none
Function output/parameters: none
Function output/returned: operation success (bool)
Device input/file: none
Device output/file: newline written to file
Dependencies: accessOutputFilePointer, fprintf
*/
bool writeEndlineToFile()
{
// get file pointer, initialize variable
// function: accessOutputFilePointer
FILE *filePtr = accessOutputFilePointer( GET_OUTPUT_FILE_PTR,
DUMMY_OUTPUT_FILE_PTR );
// check for open file
if( filePtr != NULL )
{
// print endline to file
// function: fprintf
fprintf( filePtr, "%c", NEWLINE_CHAR );
// return successful operation
return true;
}
// file not open, return failed operation
return false;
}
/*
Name: writeEndlinesToFile
Process: writes multiple carriage return ('\r')
and newline ('\n') pairs to file,
returns true if successful, false otherwise
Function input/parameters: number of endline pairs to write (int)
Function output/parameters: none
Function output/returned: operation success (bool)
Device input/file: none
Device output/file: multiple newline characters written to file
Dependencies: writeString
*/
bool writeEndlinesToFile( int numEndlines )
{
// check for number of characters remaining,
if( numEndlines > 0 )
{
// write endline, check for success
// function: writeEndlineTofile
if( writeEndlineToFile() )
{
// continue recursion,
// check for downstream success
// function: writeEndlinesToFile
return writeEndlinesToFile( numEndlines - 1 );
}
// quit recursion
// return unsuccessful operation - write operation failed
return false;
}
// return successful operation - all endlines have been written
return true;
}
/*
Name: writeIntegerToFile
Process: writes individual integer to file,
returns true if successful, false otherwise
Function input/parameters: integer to output (int)
Function output/parameters: none
Function output/returned: operation success (bool)
Device input/file: none
Device output/file: integer written to file
Dependencies: writeIntegerJustifiedToFile
*/
bool writeIntegerToFile( int outVal )
{
// initialize variables, set block size to zero and justified to "LEFT"
int blockSize = 0;
char justified[] = "LEFT";
// call to justified function
// function: writeDoubleJustifiedToFile
return writeIntegerJustifiedToFile( outVal, blockSize, justified );
}
/*
Name: writeIntegerJustifiedToFile
Process: writes individual integer to file,
returns true if successful, false otherwise
Function input/parameters:
outVal - the integer value to be output
precision - number of digits to the right of the radix point
blockSize - the width of the block within which
to print the integer value (int)
justify - "LEFT," "RIGHT," or "CENTER" justification
in block (c-string)
Function output/parameters: none
Function output/returned: operation success (bool)
Device input/file: none
Device output/file: integer written to file as specified
Dependencies: sprintf, writeStringJustifiedToFile
*/
bool writeIntegerJustifiedToFile( int outVal, int blockSize,
const char *justified )
{
// initialize variables, create temporary output string
char outStr[ MIN_STR_LEN ];
// set integer to string
// function: sprintf
sprintf( outStr, "%d", outVal );
// write as string, return result
// function: writeStringJustified
return writeStringJustifiedToFile( outStr, blockSize, justified );
}
/*
Name: writeStringToFile
Process: writes String value to file,
returns true if successful, false otherwise
Function input/parameters: double value to output (double),
number of digits to right of radix point
(precision) (int)
Function output/parameters: none
Function output/returned: operation success (bool)
Device input/file: none
Device output/file: String written to file
Dependencies: writeStringJustifiedToFile
*/
bool writeStringToFile( const char *outString )
{
// initialize variables, set block size to zero and justified to "LEFT"
int blockSize = 0;
char justified[] = "LEFT";
// call to justified function
// function: writeStringJustifiedToFile
return writeStringJustifiedToFile( outString, blockSize, justified );
}
/*
Name: writeStringJustifiedToFile
Process: write string to file within specified block size
and with specified justification
Function input/parameters:
outVal - the string value to be output
blockSize - the width of the block within which
to print the integer value (int)
justify - "LEFT," "RIGHT," or "CENTER" justification
in block (c-string)
Function output/parameters: None
Function output/returned: success of output operation (bool)
Function input/keyboard: None
Device output/file: string written to file as specified
Dependencies: strlen, strcmp, writeCharactersToFile, fprint
*/
bool writeStringJustifiedToFile( const char *outStr,
int blockSize, const char *justify )
{
// initialize variables, set pre-, post- spaces to zero
int preSpaces = 0, postSpaces = 0, strLength;
// get file pointer
// function: accessFileOutputFilePointer
FILE *filePtr = accessOutputFilePointer( GET_OUTPUT_FILE_PTR,
DUMMY_OUTPUT_FILE_PTR );
if( filePtr != NULL )
{
// find length of string
// function: strlen
strLength = strlen( outStr );
// check justification for "LEFT"
// function: strcmp
if( strcmp( justify, "LEFT" ) == 0 )
{
// set post spaces for left justification
postSpaces = blockSize - strLength;
}
// otherwise, check justification for "RIGHT"
// function: strcmp
else if( strcmp( justify, "RIGHT" ) == 0 )
{
// set pre spaces for right justification
preSpaces = blockSize - strLength;
}
// Otherwise, assume "CENTER" justification
else
{
// set pre spaces for centered
preSpaces = blockSize / 2 - strLength / 2;
// set post spaces for centered
postSpaces = blockSize - strLength - preSpaces;
}
// print pre spaces, check for success
// function: writeCharactersToFile
writeCharactersToFile( preSpaces, SPACE );
// print string
// function: fprintf
fprintf( filePtr, "%s", outStr );
// print post spaces
// function: printChars
writeCharactersToFile( postSpaces, SPACE );
// return successful operation
return true;
}
// return failed operation
return false;
}

View file

@ -0,0 +1,236 @@
/*
Formatted file output utility, function prototypes
Copyright (c) 2021 by Michael E. Leverington
Code is free for use in whole or in part and may be modified for other
non-commercial use as long as the above copyright statement is included.
*/
/*
Rev 0.0: 30 June 2021, Initial creation of file
UTILITY USAGE INSTRUCTIONS:
1) This utility contains functions that may be used for file input operations
using the C programming language with text files.
The utility must be linked with the program code with which
it will be used.
2) Specifications for all the functions
are provided in a standardized format below.
3) Use this code at your own risk. There is no guarantee of any performance,
implied or otherwise, for any part of this code
END OF INSTRUCTIONS
*/
// PreProcessor directives
#ifndef FILE_OUTPUT_UTILITY_H
#define FILE_OUTPUT_UTILITY_H
// header files
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "StandardConstants.h"
// function prototypes
/*
Name: accessOutputFilePointer
process: allows setting or accessing file pointer;
note: pointer is maintained (static) value initialized to NULL
Function input/parameters: control code (int), file pointer (FILE *)
DUMMY_OUTPUT_FILE_PTR used
if not setting file pointer
Function output/parameters: none
Function output/returned: file pointer (FILE *)
Device input/file: none
Device output/monitor: none
Dependencies: none
*/
FILE *accessOutputFilePointer( int ctrlCode, const FILE *filePtr );
/*
Name: closeOutputFile
Process: closes output file, returns true if successful, false otherwise
Function input/parameters: none
Function output/parameters: none
Function output/returned: operation success (bool)
Device input/file: none
Device output/file: output file closed
Dependencies: accessOutputFilePointer, fclose
*/
bool closeOutputFile();
/*
Name: openOutputFile
Process: opens input file, returns true if successful, false otherwise
Function input/parameters: file name (c-string)
Function output/parameters: none
Function output/returned: operation success (bool)
Device input/file: none
Device output/file: output file opened
Dependencies: accessOutputFilePointer, fopen
*/
bool openOutputFile( const char *fileName );
/*
Name: writeCharacterToFile
Process: writes individual character to file,
returns true if successful, false otherwise
Function input/parameters: character to output (char)
Function output/parameters: none
Function output/returned: operation success (bool)
Device input/file: none
Device output/file: character written to file
Dependencies: accessOutputFilePointer, fprintf
*/
bool writeCharacterToFile( char outChar );
/*
Name: writeCharactersToFile
Process: writes multiple characters to file,
returns true if successful, false otherwise
Function input/parameters: number of characters to output (int),
character to output (char)
Function output/parameters: none
Function output/returned: operation success (bool)
Device input/file: none
Device output/file: multiple characters written to file
Dependencies: writeCharacterToFile
*/
bool writeCharactersToFile( int numChars, char outChar );
/*
Name: writeDoubleToFile
Process: writes individual formatted double value to file,
returns true if successful, false otherwise
Function input/parameters: double value to output (double),
number of digits to right of radix point
(precision) (int)
Function output/parameters: none
Function output/returned: operation success (bool)
Device input/file: none
Device output/file: double written to file
Dependencies: writeDoubleJustifiedToFile
*/
bool writeDoubleToFile( double outVal, int precision );
/*
Name: writeDoubleToFileJustified
Process: writes individual formatted double value to file justified,
returns true if successful, false otherwise
outVal - the double value to be output
precision - number of digits to the right of the radix point
blockSize - the width of the block within which
to print the integer value (int)
justify - "LEFT," "RIGHT," or "CENTER" justification
in block (c-string)
Function output/parameters: none
Function output/returned: operation success (bool)
Device input/file: none
Device output/file: double written to file as specified
Dependencies: sprintf, writeStringJustifiedToFile
*/
bool writeDoubleJustifiedToFile( double outVal, int precision,
int blockSize, const char *justified );
/*
Name: writeEndlineToFile
Process: writes newline ('\n') to file,
returns true if successful, false otherwise
Function input/parameters: none
Function output/parameters: none
Function output/returned: operation success (bool)
Device input/file: none
Device output/file: newline written to file
Dependencies: accessOutputFilePointer, fprintf
*/
bool writeEndlineToFile();
/*
Name: writeEndlinesToFile
Process: writes multiple carriage return ('\r')
and newline ('\n') pairs to file,
returns true if successful, false otherwise
Function input/parameters: number of endline pairs to write (int)
Function output/parameters: none
Function output/returned: operation success (bool)
Device input/file: none
Device output/file: multiple newline characters written to file
Dependencies: writeString
*/
bool writeEndlinesToFile( int numEndlines );
/*
Name: writeIntegerToFile
Process: writes individual integer to file,
returns true if successful, false otherwise
Function input/parameters: integer to output (int)
Function output/parameters: none
Function output/returned: operation success (bool)
Device input/file: none
Device output/file: integer written to file
Dependencies: writeIntegerJustifiedToFile
*/
bool writeIntegerToFile( int outVal );
/*
Name: writeIntegerJustifiedToFile
Process: writes individual integer to file,
returns true if successful, false otherwise
Function input/parameters:
outVal - the integer value to be output
precision - number of digits to the right of the radix point
blockSize - the width of the block within which
to print the integer value (int)
justify - "LEFT," "RIGHT," or "CENTER" justification
in block (c-string)
Function output/parameters: none
Function output/returned: operation success (bool)
Device input/file: none
Device output/file: integer written to file as specified
Dependencies: sprintf, writeStringJustifiedToFile
*/
bool writeIntegerJustifiedToFile( int outVal, int blockSize,
const char *justified );
/*
Name: writeStringToFile
Process: writes String value to file,
returns true if successful, false otherwise
Function input/parameters: double value to output (double),
number of digits to right of radix point
(precision) (int)
Function output/parameters: none
Function output/returned: operation success (bool)
Device input/file: none
Device output/file: String written to file
Dependencies: writeStringJustifiedToFile
*/
bool writeStringToFile( const char *outString );
/*
Name: writeStringJustifiedToFile
Process: write string to file within specified block size
and with specified justification
Function input/parameters:
outVal - the string value to be output
blockSize - the width of the block within which
to print the integer value (int)
justify - "LEFT," "RIGHT," or "CENTER" justification
in block (c-string)
Function output/parameters: None
Function output/returned: success of output operation (bool)
Function input/keyboard: None
Device output/file: string written to file as specified
Dependencies: strlen, strcmp, writeCharactersToFile, fprint
*/
bool writeStringJustifiedToFile( const char *outStr,
int blockSize, const char *justify );
#endif // FILE_OUTPUT_UTILITY_H

118
PA09-1108/GeneralUtility.h Normal file
View file

@ -0,0 +1,118 @@
// Precompiler directives /////////////////////////////////////////////////////
#ifndef GENERAL_UTILITY_H
#define GENERAL_UTILITY_H
// Header files ///////////////////////////////////////////////////////////////
#include "StandardConstants.h"
#include <stdlib.h>
#include <time.h>
// global constants ////////////////////////////////////////////////////////////
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 /////////////////////////////////////////////////////////////
// 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: privateGetStringLen
*/
int privateCompareStrings( const char *leftStr, const char *rightStr );
/*
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 );
/*
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 );
/*
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 );
/*
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 );
/*
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 );
#endif // GENERAL_UTILITY_H

156
PA09-1108/HeapUtility.h Normal file
View file

@ -0,0 +1,156 @@
// Precompiler directives /////////////////////////////////////////////////////
#ifndef HEAP_UTILITY_H
#define HEAP_UTILITY_H
// Header files ///////////////////////////////////////////////////////////////
#include "GeneralUtility.h"
#include "StudentUtility.h"
// Data Structures /////////////////////////////////////////////////////////////
typedef struct HeapStruct
{
StudentType *array;
int size, capacity;
} HeapType;
/*
Name: addStudentWithData
Process: adds student data to the array with internally generated priority,
returns true if array not full, false otherwise
Method input/parameters: pointer to HeapType item (HeapType *),
name (const char *), student id (int),
gender (char), gpa (double), priority (int),
display flag (bool)
Method output/parameters: pointer to HeapType item (HeapType *)
Method output/returned: Boolean result of function (bool)
Device input/---: none
Device output/---: none
Dependencies: privateCopyString, privateGenUniqueRandom,
studentDataToString, privateBubbleUp
*/
bool addStudentWithData( HeapType *heap, const char *inName, int inStudentId,
char inGender, double inGpa, bool displayFlag );
/*
Name: addStudentFromStudentElement
Process: adds student element to the array from other element,
returns true if array not full, false otherwise
Method input/parameters: student data type item to be added (const StudentType)
Method output/parameters: pointer to HeapType (HeapType *)
Method output/returned: Boolean result of function
Device input/---: none
Device output/---: none
Dependencies: one line of code - addStudentWithData
*/
bool addStudentFromElement( HeapType *heap,
const StudentType sourceElement, bool displayFlag );
/*
Name: clearHeapType
Process: deallocates heap and internal array, clears unique random source,
returns NULL
Method input/parameters: pointer to HeapType (HeapType *)
Method output/parameters: none
Method output/returned: NULL (HeapType *)
Device input/---: none
Device output/---: none
Dependencies: free, privateGenUniqueRandom
*/
HeapType *clearHeapType( HeapType *heap);
/*
Name: createHeapType
Process: initializes heap, and internal array, creates unique random source
Method input/parameters: capacity (int)
Method output/parameters: none
Method output/returned: initialized heap (HeapType *)
Device input/---: none
Device output/---: none
Dependencies: malloc, privateGenUniqueRandom
*/
HeapType *createHeapType( int capacity );
/*
Name: getTreeHeight
Process: recursively finds tree height by using number of nodes
Method input/parameters: node count (int)
Method output/parameters: none
Method output/returned: calculated tree height (int)
Device input/---: none
Device output/---: none
Dependencies: findTreeHeight (recursively)
*/
int findTreeHeight( int nodeCount );
/*
Name: isEmpty
Process: checks for empty list, reports
Method input/parameters: none
Method output/parameters: none
Method output/returned: Boolean result of operation (bool)
Device input/---: none
Device output/---: none
Dependencies: none
*/
bool isEmpty( HeapType *heap );
/*
Name: privateBubbleUp
Process: recursively traverses tree to place new data element
in the correct location in the tree/array,
if display flag set, reports bubble up actions
Method input/parameters: pointer to heap (HeapType *),
index at current location in tree/array (int),
display flag (bool)
Method output/parameters: pointer to updated heap (HeapType *)
Method output/returned: none
Device input/---: none
Device output/---: none
Dependencies: deepCopyStudentData, printf, studentDataToString,
privateBubbleUp (recursive)
Notes: No more than two statements for any swap operation
*/
void privateBubbleUp( HeapType *heap, int currentIndex, bool displayFlag );
/*
Name: privateTrickleDown
Process: recursively traverses tree to place reordered data element
in the correct location in the tree/array,
if display flag set, reports trickle down actions
Method input/parameters: pointer to heap (HeapType *),
index at current location in tree/array (int),
display flag (bool)
Method output/parameters: pointer to updated heap (HeapType *)
Method output/returned: none
Device input/---: none
Device output/---: none
Dependencies: deepCopyStudentData, printf, studentDataToString,
privateTrickleDown (recursive)
Notes: No more than two copy operations for any swap operation
*/
void privateTrickleDown( HeapType *heap, int currentIndex, bool displayFlag );
/*
Name: removeItem
Process: removes item from heap, adjusts tree, returns
Method input/parameters: pointer to heap type (HeapType *)
Method output/parameters: returned data, or empty returnVal if not found
(StudentDataType *),
pointer to updated heap type (HeapType *)
Method output/returned: none
Device input/---: none
Device output/---: none
Dependencies: deepCopyStudentData, studentDataToString,
printf, privateTrickleDown
*/
void removeItem( StudentType *returnVal, HeapType *heap, bool displayFlag );
#endif // HEAP_UTILITY_H

43
PA09-1108/Makefile Normal file
View file

@ -0,0 +1,43 @@
CC = gcc -Wall
SOURCES = PA09_Main.c GeneralUtility.c HeapUtility.c StudentUtility.c Console_IO_Utility.c File_Input_Utility.c File_Output_Utility.c
OBJECTS = PA09_Main.o GeneralUtility.o HeapUtility.o StudentUtility.o Console_IO_Utility.o File_Input_Utility.o File_Output_Utility.o
# Uses the template structure defined in the TypicalUtils folder
all: PA09_Main
PA09_Main: $(OBJECTS)
$(CC) $(OBJECTS) -o PA09_Main
PA09_Main.o: PA09_Main.c
$(CC) -c PA09_Main.c -o PA09_Main.o
GeneralUtility.o: GeneralUtility.c
$(CC) -c GeneralUtility.c -o GeneralUtility.o
HeapUtility.o: HeapUtility.c
$(CC) -c HeapUtility.c -o HeapUtility.o
StudentUtility.o: StudentUtility.c
$(CC) -c StudentUtility.c -o StudentUtility.o
Console_IO_Utility.o: Console_IO_Utility.c
$(CC) -c Console_IO_Utility.c -o Console_IO_Utility.o
File_Input_Utility.o: File_Input_Utility.c
$(CC) -c File_Input_Utility.c -o File_Input_Utility.o
File_Output_Utility.o: File_Output_Utility.c
$(CC) -c File_Output_Utility.c -o File_Output_Utility.o
clean:
rm -f $(OBJECTS) PA09_Main PA09_Main.out
clean_objects:
rm -f $(OBJECTS)
run:
./PA09_Main
run_win:
PA09_Main

120
PA09-1108/PA09_Main.c Normal file
View file

@ -0,0 +1,120 @@
#include "StandardConstants.h"
#include "File_Input_Utility.h"
#include "GeneralUtility.h"
#include "StudentUtility.h"
#include "HeapUtility.h"
// Local Constants /////////////////////////////////////////////////////////////
#define MAX_STUDENTS 50
#define FILE_NAME_INDEX 1
#define VERBOSE_INDEX 2
#define SEMICOLON ';'
// Local function prototypes ///////////////////////////////////////////////////
bool uploadData( const char *fileName, HeapType *heap, bool displayFlag );
int main( int argc, char *argv[] )
{
HeapType *localHeap;
StudentType testVal;
int index;
char outStr[ MAX_STR_LEN ];
char fileName[ STD_STR_LEN ];
bool dispFlag = false;
printf( "\nHEAP TYPE TEST\n" );
printf( "==============\n\n" );
if( argc == 1 )
{
printf( "Error: Must provide file name as command line argument\n" );
printf( " May also provide -v (after file name) " );
printf( "to implement verbose operations\n" );
printf( "\nProgram Aborted\n" );
return 1; // failed program
}
// create heap type, initialize random priority source
localHeap = createHeapType( MAX_STUDENTS );
privateCopyString( fileName, argv[ FILE_NAME_INDEX ] );
if( argc > VERBOSE_INDEX
&& privateCompareStrings( argv[ VERBOSE_INDEX ], "-v" ) == 0 )
{
dispFlag = true;
}
if( uploadData( fileName, localHeap, dispFlag ) )
{
printf( "========================================" );
printf( "========================================\n" );
for( index = 0; index < 5; index++ )
{
removeItem( &testVal, localHeap, dispFlag );
studentDataToString( outStr, testVal );
printf( "\n%s removed\n", outStr );
}
}
// end program
// clear heap type, including clear random priority source generator
localHeap = clearHeapType( localHeap );
// report program end
printf( "\nProgram End\n" );
// return success
return 0;
}
bool uploadData( const char *fileName, HeapType *heap, bool displayFlag )
{
char nameString [ STD_STR_LEN ];
double inputGPA;
int inputID, treeHt, inputGender, counter = 1;
// Open file
if( openInputFile( fileName ) )
{
readStringToDelimiterFromFile( SEMICOLON, nameString );
while( !checkForEndOfInputFile() )
{
printf( "%3d) Uploading %s\n", counter, nameString );
counter = counter + 1;
inputID = readIntegerFromFile();
readCharacterFromFile();
inputGender = readCharacterFromFile();
readCharacterFromFile();
inputGPA = readDoubleFromFile();
addStudentWithData( heap, nameString,
inputID, inputGender, inputGPA, displayFlag );
readStringToDelimiterFromFile( SEMICOLON, nameString );
treeHt = findTreeHeight( heap->size );
printf( "Current count: %d, current tree height: %d\n\n",
heap->size, treeHt );
}
closeInputFile();
return true;
}
return false;
}

538
PA09-1108/SampleRun.txt Normal file
View file

@ -0,0 +1,538 @@
HEAP TYPE TEST
==============
1) Uploading Johnson, Robert
Adding new student: Nm: Johnson, Robert, Id: 603667, Gndr: M, GPA: 2.8441, Priority: 16
Current count: 1, current tree height: 0
2) Uploading Elliott, Cayley
Adding new student: Nm: Elliott, Cayley, Id: 135658, Gndr: F, GPA: 2.9788, Priority: 92
- Bubble up swap:
- moving up child: Nm: Elliott, Cayley, Id: 135658, Gndr: F, GPA: 2.9788, Priority: 92
- moving down parent: Nm: Johnson, Robert, Id: 603667, Gndr: M, GPA: 2.8441, Priority: 16
Current count: 2, current tree height: 1
3) Uploading Reyes, Connor
Adding new student: Nm: Reyes, Connor, Id: 191261, Gndr: M, GPA: 3.2956, Priority: 72
Current count: 3, current tree height: 1
4) Uploading Sanchez, Susan
Adding new student: Nm: Sanchez, Susan, Id: 154838, Gndr: F, GPA: 2.0632, Priority: 96
- Bubble up swap:
- moving up child: Nm: Sanchez, Susan, Id: 154838, Gndr: F, GPA: 2.0632, Priority: 96
- moving down parent: Nm: Johnson, Robert, Id: 603667, Gndr: M, GPA: 2.8441, Priority: 16
- Bubble up swap:
- moving up child: Nm: Sanchez, Susan, Id: 154838, Gndr: F, GPA: 2.0632, Priority: 96
- moving down parent: Nm: Elliott, Cayley, Id: 135658, Gndr: F, GPA: 2.9788, Priority: 92
Current count: 4, current tree height: 2
5) Uploading Penn, Frederick
Adding new student: Nm: Penn, Frederick, Id: 819367, Gndr: M, GPA: 2.8290, Priority: 69
Current count: 5, current tree height: 2
6) Uploading Deangelis, Shawna
Adding new student: Nm: Deangelis, Shawna, Id: 764050, Gndr: F, GPA: 2.2033, Priority: 76
- Bubble up swap:
- moving up child: Nm: Deangelis, Shawna, Id: 764050, Gndr: F, GPA: 2.2033, Priority: 76
- moving down parent: Nm: Reyes, Connor, Id: 191261, Gndr: M, GPA: 3.2956, Priority: 72
Current count: 6, current tree height: 2
7) Uploading Shafer, Tristan
Adding new student: Nm: Shafer, Tristan , Id: 693686, Gndr: F, GPA: 3.1807, Priority: 61
Current count: 7, current tree height: 2
8) Uploading Ruan, Francisco
Adding new student: Nm: Ruan, Francisco, Id: 587182, Gndr: M, GPA: 3.7266, Priority: 32
- Bubble up swap:
- moving up child: Nm: Ruan, Francisco, Id: 587182, Gndr: M, GPA: 3.7266, Priority: 32
- moving down parent: Nm: Johnson, Robert, Id: 603667, Gndr: M, GPA: 2.8441, Priority: 16
Current count: 8, current tree height: 3
9) Uploading Werner, Riley
Adding new student: Nm: Werner, Riley, Id: 444253, Gndr: F, GPA: 2.3455, Priority: 83
- Bubble up swap:
- moving up child: Nm: Werner, Riley, Id: 444253, Gndr: F, GPA: 2.3455, Priority: 83
- moving down parent: Nm: Ruan, Francisco, Id: 587182, Gndr: M, GPA: 3.7266, Priority: 32
Current count: 9, current tree height: 3
10) Uploading Davis, Glen-Andrew
Adding new student: Nm: Davis, Glen-Andrew, Id: 488133, Gndr: M, GPA: 3.9377, Priority: 87
- Bubble up swap:
- moving up child: Nm: Davis, Glen-Andrew, Id: 488133, Gndr: M, GPA: 3.9377, Priority: 87
- moving down parent: Nm: Penn, Frederick, Id: 819367, Gndr: M, GPA: 2.8290, Priority: 69
Current count: 10, current tree height: 3
11) Uploading Thomas, Sena
Adding new student: Nm: Thomas, Sena, Id: 302398, Gndr: F, GPA: 2.9184, Priority: 44
Current count: 11, current tree height: 3
12) Uploading Nash, Tim
Adding new student: Nm: Nash, Tim, Id: 272835, Gndr: M, GPA: 2.4678, Priority: 31
Current count: 12, current tree height: 3
13) Uploading Hack, Jacque
Adding new student: Nm: Hack, Jacque, Id: 641605, Gndr: F, GPA: 3.6343, Priority: 98
- Bubble up swap:
- moving up child: Nm: Hack, Jacque, Id: 641605, Gndr: F, GPA: 3.6343, Priority: 98
- moving down parent: Nm: Reyes, Connor, Id: 191261, Gndr: M, GPA: 3.2956, Priority: 72
- Bubble up swap:
- moving up child: Nm: Hack, Jacque, Id: 641605, Gndr: F, GPA: 3.6343, Priority: 98
- moving down parent: Nm: Deangelis, Shawna, Id: 764050, Gndr: F, GPA: 2.2033, Priority: 76
- Bubble up swap:
- moving up child: Nm: Hack, Jacque, Id: 641605, Gndr: F, GPA: 3.6343, Priority: 98
- moving down parent: Nm: Sanchez, Susan, Id: 154838, Gndr: F, GPA: 2.0632, Priority: 96
Current count: 13, current tree height: 3
14) Uploading Ortega, Jane
Adding new student: Nm: Ortega, Jane, Id: 645168, Gndr: F, GPA: 2.8838, Priority: 53
Current count: 14, current tree height: 3
15) Uploading Catania, DeMarco
Adding new student: Nm: Catania, DeMarco, Id: 733002, Gndr: M, GPA: 2.5774, Priority: 49
Current count: 15, current tree height: 3
16) Uploading Johnson, Trevor
Adding new student: Nm: Johnson, Trevor, Id: 658753, Gndr: M, GPA: 3.7995, Priority: 30
- Bubble up swap:
- moving up child: Nm: Johnson, Trevor, Id: 658753, Gndr: M, GPA: 3.7995, Priority: 30
- moving down parent: Nm: Johnson, Robert, Id: 603667, Gndr: M, GPA: 2.8441, Priority: 16
Current count: 16, current tree height: 4
17) Uploading Sanchez, Kieley
Adding new student: Nm: Sanchez, Kieley, Id: 904592, Gndr: F, GPA: 2.8301, Priority: 79
- Bubble up swap:
- moving up child: Nm: Sanchez, Kieley, Id: 904592, Gndr: F, GPA: 2.8301, Priority: 79
- moving down parent: Nm: Johnson, Trevor, Id: 658753, Gndr: M, GPA: 3.7995, Priority: 30
Current count: 17, current tree height: 4
18) Uploading Williams, Richard
Adding new student: Nm: Williams, Richard, Id: 866271, Gndr: M, GPA: 2.0781, Priority: 12
Current count: 18, current tree height: 4
19) Uploading Becker, Anna
Adding new student: Nm: Becker, Anna, Id: 716502, Gndr: F, GPA: 2.8731, Priority: 84
- Bubble up swap:
- moving up child: Nm: Becker, Anna, Id: 716502, Gndr: F, GPA: 2.8731, Priority: 84
- moving down parent: Nm: Ruan, Francisco, Id: 587182, Gndr: M, GPA: 3.7266, Priority: 32
- Bubble up swap:
- moving up child: Nm: Becker, Anna, Id: 716502, Gndr: F, GPA: 2.8731, Priority: 84
- moving down parent: Nm: Werner, Riley, Id: 444253, Gndr: F, GPA: 2.3455, Priority: 83
Current count: 19, current tree height: 4
20) Uploading Bolling, Mohammad
Adding new student: Nm: Bolling, Mohammad, Id: 419808, Gndr: M, GPA: 2.1352, Priority: 63
Current count: 20, current tree height: 4
21) Uploading Amundson, David
Adding new student: Nm: Amundson, David, Id: 323273, Gndr: M, GPA: 2.9203, Priority: 36
Current count: 21, current tree height: 4
22) Uploading Gamboa, Wanisha
Adding new student: Nm: Gamboa, Wanisha, Id: 362001, Gndr: F, GPA: 3.1577, Priority: 99
- Bubble up swap:
- moving up child: Nm: Gamboa, Wanisha, Id: 362001, Gndr: F, GPA: 3.1577, Priority: 99
- moving down parent: Nm: Thomas, Sena, Id: 302398, Gndr: F, GPA: 2.9184, Priority: 44
- Bubble up swap:
- moving up child: Nm: Gamboa, Wanisha, Id: 362001, Gndr: F, GPA: 3.1577, Priority: 99
- moving down parent: Nm: Davis, Glen-Andrew, Id: 488133, Gndr: M, GPA: 3.9377, Priority: 87
- Bubble up swap:
- moving up child: Nm: Gamboa, Wanisha, Id: 362001, Gndr: F, GPA: 3.1577, Priority: 99
- moving down parent: Nm: Elliott, Cayley, Id: 135658, Gndr: F, GPA: 2.9788, Priority: 92
- Bubble up swap:
- moving up child: Nm: Gamboa, Wanisha, Id: 362001, Gndr: F, GPA: 3.1577, Priority: 99
- moving down parent: Nm: Hack, Jacque, Id: 641605, Gndr: F, GPA: 3.6343, Priority: 98
Current count: 22, current tree height: 4
23) Uploading Evangelista, Nicholas
Adding new student: Nm: Evangelista, Nicholas, Id: 982317, Gndr: M, GPA: 3.6453, Priority: 38
Current count: 23, current tree height: 4
24) Uploading Sibley, Ryan
Adding new student: Nm: Sibley, Ryan, Id: 827402, Gndr: F, GPA: 2.0451, Priority: 90
- Bubble up swap:
- moving up child: Nm: Sibley, Ryan, Id: 827402, Gndr: F, GPA: 2.0451, Priority: 90
- moving down parent: Nm: Nash, Tim, Id: 272835, Gndr: M, GPA: 2.4678, Priority: 31
- Bubble up swap:
- moving up child: Nm: Sibley, Ryan, Id: 827402, Gndr: F, GPA: 2.0451, Priority: 90
- moving down parent: Nm: Deangelis, Shawna, Id: 764050, Gndr: F, GPA: 2.2033, Priority: 76
Current count: 24, current tree height: 4
25) Uploading Nishimura, Erica
Adding new student: Nm: Nishimura, Erica, Id: 729129, Gndr: F, GPA: 2.2064, Priority: 70
Current count: 25, current tree height: 4
26) Uploading Wrong, Student
Adding new student: Nm: Wrong, Student, Id: 123456, Gndr: M, GPA: 3.0579, Priority: 25
Current count: 26, current tree height: 4
27) Uploading Johnson, Brenda
Adding new student: Nm: Johnson, Brenda, Id: 576650, Gndr: F, GPA: 3.9897, Priority: 19
Current count: 27, current tree height: 4
28) Uploading Benavidez, Bradley
Adding new student: Nm: Benavidez, Bradley, Id: 783372, Gndr: M, GPA: 3.6749, Priority: 28
Current count: 28, current tree height: 4
29) Uploading Hagans, Stefan
Adding new student: Nm: Hagans, Stefan, Id: 103521, Gndr: M, GPA: 3.6355, Priority: 67
- Bubble up swap:
- moving up child: Nm: Hagans, Stefan, Id: 103521, Gndr: M, GPA: 3.6355, Priority: 67
- moving down parent: Nm: Ortega, Jane, Id: 645168, Gndr: F, GPA: 2.8838, Priority: 53
- Bubble up swap:
- moving up child: Nm: Hagans, Stefan, Id: 103521, Gndr: M, GPA: 3.6355, Priority: 67
- moving down parent: Nm: Shafer, Tristan , Id: 693686, Gndr: F, GPA: 3.1807, Priority: 61
Current count: 29, current tree height: 4
30) Uploading Poston, Matthew
Adding new student: Nm: Poston, Matthew, Id: 653875, Gndr: M, GPA: 3.7512, Priority: 26
Current count: 30, current tree height: 4
31) Uploading Nethercott, Michael
Adding new student: Nm: Nethercott, Michael, Id: 812517, Gndr: F, GPA: 3.4304, Priority: 41
Current count: 31, current tree height: 4
32) Uploading Higbie, Hannah
Adding new student: Nm: Higbie, Hannah, Id: 462902, Gndr: F, GPA: 2.1995, Priority: 33
- Bubble up swap:
- moving up child: Nm: Higbie, Hannah, Id: 462902, Gndr: F, GPA: 2.1995, Priority: 33
- moving down parent: Nm: Johnson, Robert, Id: 603667, Gndr: M, GPA: 2.8441, Priority: 16
Current count: 32, current tree height: 5
33) Uploading Sims, Houston
Adding new student: Nm: Sims, Houston, Id: 391402, Gndr: M, GPA: 2.7454, Priority: 51
- Bubble up swap:
- moving up child: Nm: Sims, Houston, Id: 391402, Gndr: M, GPA: 2.7454, Priority: 51
- moving down parent: Nm: Higbie, Hannah, Id: 462902, Gndr: F, GPA: 2.1995, Priority: 33
Current count: 33, current tree height: 5
34) Uploading Serrano, Owen
Adding new student: Nm: Serrano, Owen, Id: 119410, Gndr: M, GPA: 2.2226, Priority: 56
- Bubble up swap:
- moving up child: Nm: Serrano, Owen, Id: 119410, Gndr: M, GPA: 2.2226, Priority: 56
- moving down parent: Nm: Johnson, Trevor, Id: 658753, Gndr: M, GPA: 3.7995, Priority: 30
Current count: 34, current tree height: 5
35) Uploading Diaz-Juarez, Tyler
Adding new student: Nm: Diaz-Juarez, Tyler, Id: 460126, Gndr: M, GPA: 2.6074, Priority: 24
Current count: 35, current tree height: 5
36) Uploading Gonzalez-Gascon, Jeaniel
Adding new student: Nm: Gonzalez-Gascon, Jeaniel, Id: 309226, Gndr: F, GPA: 3.6118, Priority: 40
- Bubble up swap:
- moving up child: Nm: Gonzalez-Gascon, Jeaniel, Id: 309226, Gndr: F, GPA: 3.6118, Priority: 40
- moving down parent: Nm: Williams, Richard, Id: 866271, Gndr: M, GPA: 2.0781, Priority: 12
Current count: 36, current tree height: 5
37) Uploading Medrano-Pacheco, Rachel
Adding new student: Nm: Medrano-Pacheco, Rachel, Id: 285416, Gndr: F, GPA: 3.8692, Priority: 82
- Bubble up swap:
- moving up child: Nm: Medrano-Pacheco, Rachel, Id: 285416, Gndr: F, GPA: 3.8692, Priority: 82
- moving down parent: Nm: Gonzalez-Gascon, Jeaniel, Id: 309226, Gndr: F, GPA: 3.6118, Priority: 40
Current count: 37, current tree height: 5
38) Uploading Dartt, Rahul
Adding new student: Nm: Dartt, Rahul, Id: 410206, Gndr: M, GPA: 2.1202, Priority: 62
- Bubble up swap:
- moving up child: Nm: Dartt, Rahul, Id: 410206, Gndr: M, GPA: 2.1202, Priority: 62
- moving down parent: Nm: Ruan, Francisco, Id: 587182, Gndr: M, GPA: 3.7266, Priority: 32
Current count: 38, current tree height: 5
39) Uploading Hagans, Mary
Adding new student: Nm: Hagans, Mary, Id: 214632, Gndr: F, GPA: 3.3662, Priority: 71
- Bubble up swap:
- moving up child: Nm: Hagans, Mary, Id: 214632, Gndr: F, GPA: 3.3662, Priority: 71
- moving down parent: Nm: Dartt, Rahul, Id: 410206, Gndr: M, GPA: 2.1202, Priority: 62
Current count: 39, current tree height: 5
40) Uploading Macneil, Katherine
Adding new student: Nm: Macneil, Katherine, Id: 193833, Gndr: F, GPA: 2.7270, Priority: 59
Current count: 40, current tree height: 5
41) Uploading Casolari, Lyle
Adding new student: Nm: Casolari, Lyle, Id: 554535, Gndr: M, GPA: 3.2607, Priority: 55
Current count: 41, current tree height: 5
42) Uploading Serate, Alexis
Adding new student: Nm: Serate, Alexis, Id: 183761, Gndr: F, GPA: 2.4170, Priority: 85
- Bubble up swap:
- moving up child: Nm: Serate, Alexis, Id: 183761, Gndr: F, GPA: 2.4170, Priority: 85
- moving down parent: Nm: Amundson, David, Id: 323273, Gndr: M, GPA: 2.9203, Priority: 36
- Bubble up swap:
- moving up child: Nm: Serate, Alexis, Id: 183761, Gndr: F, GPA: 2.4170, Priority: 85
- moving down parent: Nm: Penn, Frederick, Id: 819367, Gndr: M, GPA: 2.8290, Priority: 69
Current count: 42, current tree height: 5
43) Uploading Goldberg, Zachary
Adding new student: Nm: Goldberg, Zachary, Id: 557631, Gndr: M, GPA: 3.7818, Priority: 81
- Bubble up swap:
- moving up child: Nm: Goldberg, Zachary, Id: 557631, Gndr: M, GPA: 3.7818, Priority: 81
- moving down parent: Nm: Penn, Frederick, Id: 819367, Gndr: M, GPA: 2.8290, Priority: 69
Current count: 43, current tree height: 5
44) Uploading Niday, Breanna
Adding new student: Nm: Niday, Breanna, Id: 898497, Gndr: F, GPA: 3.9650, Priority: 27
Current count: 44, current tree height: 5
45) Uploading Andrieu, Andrew
Adding new student: Nm: Andrieu, Andrew, Id: 295673, Gndr: M, GPA: 3.8005, Priority: 10
Current count: 45, current tree height: 5
46) Uploading Ahmed, Alexandra
Adding new student: Nm: Ahmed, Alexandra, Id: 214029, Gndr: F, GPA: 2.5031, Priority: 58
- Bubble up swap:
- moving up child: Nm: Ahmed, Alexandra, Id: 214029, Gndr: F, GPA: 2.5031, Priority: 58
- moving down parent: Nm: Evangelista, Nicholas, Id: 982317, Gndr: M, GPA: 3.6453, Priority: 38
Current count: 46, current tree height: 5
47) Uploading Hunt, Stephanie
Adding new student: Nm: Hunt, Stephanie, Id: 597706, Gndr: F, GPA: 3.7864, Priority: 54
Current count: 47, current tree height: 5
48) Uploading Volkmar, Mason
Adding new student: Nm: Volkmar, Mason, Id: 641651, Gndr: M, GPA: 3.3256, Priority: 22
Current count: 48, current tree height: 5
49) Uploading Goffinet, Jahshua
Adding new student: Nm: Goffinet, Jahshua, Id: 722457, Gndr: M, GPA: 2.2585, Priority: 20
Current count: 49, current tree height: 5
50) Uploading Buck, Phoebe
Adding new student: Nm: Buck, Phoebe, Id: 729716, Gndr: F, GPA: 3.7016, Priority: 39
Current count: 50, current tree height: 5
================================================================================
Removing student: Nm: Gamboa, Wanisha, Id: 362001, Gndr: F, GPA: 3.1577, Priority: 99
- Trickle down swap:
- moving down parent: Nm: Buck, Phoebe, Id: 729716, Gndr: F, GPA: 3.7016, Priority: 39
- moving up left child: Nm: Hack, Jacque, Id: 641605, Gndr: F, GPA: 3.6343, Priority: 98
- Trickle down swap:
- moving down parent: Nm: Buck, Phoebe, Id: 729716, Gndr: F, GPA: 3.7016, Priority: 39
- moving up right child: Nm: Elliott, Cayley, Id: 135658, Gndr: F, GPA: 2.9788, Priority: 92
- Trickle down swap:
- moving down parent: Nm: Buck, Phoebe, Id: 729716, Gndr: F, GPA: 3.7016, Priority: 39
- moving up right child: Nm: Davis, Glen-Andrew, Id: 488133, Gndr: M, GPA: 3.9377, Priority: 87
- Trickle down swap:
- moving down parent: Nm: Buck, Phoebe, Id: 729716, Gndr: F, GPA: 3.7016, Priority: 39
- moving up right child: Nm: Ahmed, Alexandra, Id: 214029, Gndr: F, GPA: 2.5031, Priority: 58
Nm: Gamboa, Wanisha, Id: 362001, Gndr: F, GPA: 3.1577, Priority: 99 removed
Removing student: Nm: Hack, Jacque, Id: 641605, Gndr: F, GPA: 3.6343, Priority: 98
- Trickle down swap:
- moving down parent: Nm: Goffinet, Jahshua, Id: 722457, Gndr: M, GPA: 2.2585, Priority: 20
- moving up right child: Nm: Sanchez, Susan, Id: 154838, Gndr: F, GPA: 2.0632, Priority: 96
- Trickle down swap:
- moving down parent: Nm: Goffinet, Jahshua, Id: 722457, Gndr: M, GPA: 2.2585, Priority: 20
- moving up left child: Nm: Sibley, Ryan, Id: 827402, Gndr: F, GPA: 2.0451, Priority: 90
- Trickle down swap:
- moving down parent: Nm: Goffinet, Jahshua, Id: 722457, Gndr: M, GPA: 2.2585, Priority: 20
- moving up left child: Nm: Deangelis, Shawna, Id: 764050, Gndr: F, GPA: 2.2033, Priority: 76
- Trickle down swap:
- moving down parent: Nm: Goffinet, Jahshua, Id: 722457, Gndr: M, GPA: 2.2585, Priority: 20
- moving up right child: Nm: Nishimura, Erica, Id: 729129, Gndr: F, GPA: 2.2064, Priority: 70
Nm: Hack, Jacque, Id: 641605, Gndr: F, GPA: 3.6343, Priority: 98 removed
Removing student: Nm: Sanchez, Susan, Id: 154838, Gndr: F, GPA: 2.0632, Priority: 96
- Trickle down swap:
- moving down parent: Nm: Volkmar, Mason, Id: 641651, Gndr: M, GPA: 3.3256, Priority: 22
- moving up left child: Nm: Elliott, Cayley, Id: 135658, Gndr: F, GPA: 2.9788, Priority: 92
- Trickle down swap:
- moving down parent: Nm: Volkmar, Mason, Id: 641651, Gndr: M, GPA: 3.3256, Priority: 22
- moving up right child: Nm: Davis, Glen-Andrew, Id: 488133, Gndr: M, GPA: 3.9377, Priority: 87
- Trickle down swap:
- moving down parent: Nm: Volkmar, Mason, Id: 641651, Gndr: M, GPA: 3.3256, Priority: 22
- moving up left child: Nm: Serate, Alexis, Id: 183761, Gndr: F, GPA: 2.4170, Priority: 85
- Trickle down swap:
- moving down parent: Nm: Volkmar, Mason, Id: 641651, Gndr: M, GPA: 3.3256, Priority: 22
- moving up right child: Nm: Goldberg, Zachary, Id: 557631, Gndr: M, GPA: 3.7818, Priority: 81
- Trickle down swap:
- moving down parent: Nm: Volkmar, Mason, Id: 641651, Gndr: M, GPA: 3.3256, Priority: 22
- moving up right child: Nm: Penn, Frederick, Id: 819367, Gndr: M, GPA: 2.8290, Priority: 69
Nm: Sanchez, Susan, Id: 154838, Gndr: F, GPA: 2.0632, Priority: 96 removed
Removing student: Nm: Elliott, Cayley, Id: 135658, Gndr: F, GPA: 2.9788, Priority: 92
- Trickle down swap:
- moving down parent: Nm: Hunt, Stephanie, Id: 597706, Gndr: F, GPA: 3.7864, Priority: 54
- moving up right child: Nm: Sibley, Ryan, Id: 827402, Gndr: F, GPA: 2.0451, Priority: 90
- Trickle down swap:
- moving down parent: Nm: Hunt, Stephanie, Id: 597706, Gndr: F, GPA: 3.7864, Priority: 54
- moving up left child: Nm: Deangelis, Shawna, Id: 764050, Gndr: F, GPA: 2.2033, Priority: 76
- Trickle down swap:
- moving down parent: Nm: Hunt, Stephanie, Id: 597706, Gndr: F, GPA: 3.7864, Priority: 54
- moving up right child: Nm: Reyes, Connor, Id: 191261, Gndr: M, GPA: 3.2956, Priority: 72
Nm: Elliott, Cayley, Id: 135658, Gndr: F, GPA: 2.9788, Priority: 92 removed
Removing student: Nm: Sibley, Ryan, Id: 827402, Gndr: F, GPA: 2.0451, Priority: 90
- Trickle down swap:
- moving down parent: Nm: Evangelista, Nicholas, Id: 982317, Gndr: M, GPA: 3.6453, Priority: 38
- moving up left child: Nm: Davis, Glen-Andrew, Id: 488133, Gndr: M, GPA: 3.9377, Priority: 87
- Trickle down swap:
- moving down parent: Nm: Evangelista, Nicholas, Id: 982317, Gndr: M, GPA: 3.6453, Priority: 38
- moving up right child: Nm: Serate, Alexis, Id: 183761, Gndr: F, GPA: 2.4170, Priority: 85
- Trickle down swap:
- moving down parent: Nm: Evangelista, Nicholas, Id: 982317, Gndr: M, GPA: 3.6453, Priority: 38
- moving up left child: Nm: Goldberg, Zachary, Id: 557631, Gndr: M, GPA: 3.7818, Priority: 81
- Trickle down swap:
- moving down parent: Nm: Evangelista, Nicholas, Id: 982317, Gndr: M, GPA: 3.6453, Priority: 38
- moving up right child: Nm: Penn, Frederick, Id: 819367, Gndr: M, GPA: 2.8290, Priority: 69
Nm: Sibley, Ryan, Id: 827402, Gndr: F, GPA: 2.0451, Priority: 90 removed
Program End

View file

@ -0,0 +1,73 @@
// Preprocessor directives
#ifndef STANDARD_CONSTANTS_H
#define STANDARD_CONSTANTS_H
// Include Boolean
#include <stdbool.h>
// global constants
// constant for end of c-string null character
#define CARRIAGE_RETURN_CHAR '\r'
//constant for a COLON
#define COLON ':'
//constant for a COMMA
#define COMMA ','
//Constant to print a dash
#define DASH '-'
// highest printable character
#define HIGHEST_PRINTABLE_CHAR '~'
// constant for huge string length
#define HUGE_STR_LEN 256
// lowest printable character
#define LOWEST_PRINTABLE_CHAR ' '
// constant for small string length
#define MIN_STR_LEN 32
// constant for maximum string length
#define MAX_STR_LEN 128
// constant for end of c-string null character
#define NEWLINE_CHAR '\n'
// first non-printable above ASCII
#define DELETE_KEY 127
// constant for end of c-string null character
#define NULL_CHAR '\0'
// columns found in grade sheet before grading columns
#define NUM_BASE_COLS 5
// constant for number of grading frequency bins
#define NUM_FREQUENCY_BINS 10
//Constant for printing a pipe
#define PIPE '|'
//Constant for a period
#define PERIOD '.'
//Constant for PRECISION, 2 digits
#define PRECISION_2 2
//Constant for PRECISION, 4 digits
#define PRECISION_4 4
// constant for space character
#define SPACE ' '
// constant for standard string length
#define STD_STR_LEN 64
// constant for smallest string length
#define TINY_STR_LEN 16
#endif // STANDARD_CONSTANTS

View file

@ -0,0 +1,63 @@
// Precompiler directives /////////////////////////////////////////////////////
#ifndef STUDENT_UTILITY_H
#define STUDENT_UTILITY_H
// Header files ///////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <stdio.h>
#include "StandardConstants.h"
#include "GeneralUtility.h"
// global constants ////////////////////////////////////////////////////////////
// None
// data structures /////////////////////////////////////////////////////////////
typedef struct StudentStruct
{
char name[ MAX_STR_LEN ];
int studentId;
char gender;
double gpa;
int priority;
} StudentType;
typedef struct StudentArrayStruct
{
StudentType *array;
int size, capacity;
} StudentArrayType;
/*
Name: deepCopyStudentDAta
Process: copies one StudentType item into another
Function input/parameters: source data (const StudentType)
Function output/parameters: destination data (StudentType *)
Function output/returned: none
Device input/ ---: none
Device output/ ---: none
Dependencies: privateCopyString
*/
void deepCopyStudentData( StudentType *dest, const StudentType source );
/*
Name: studentDataToString
Process: creates display string from all object data
Method input/parameters: none
Method output/parameters: pointer to return string (char * const)
Method output/returned: none
Device input/keyboard: none
Device output/monitor: none
Dependencies: sprintf
*/
void studentDataToString( char *outString, const StudentType toDisplay );
#endif // STUDENT_UTILITY_H

50
PA09-1108/inData.txt Normal file
View file

@ -0,0 +1,50 @@
Johnson, Robert; 603667; M; 2.844077875
Elliott, Cayley; 135658; F; 2.978848017
Reyes, Connor; 191261; M; 3.295578577
Sanchez, Susan; 154838; F; 2.063213296
Penn, Frederick; 819367; M; 2.828974752
Deangelis, Shawna; 764050; F; 2.203316877
Shafer, Tristan ; 693686; F; 3.180700609
Ruan, Francisco; 587182; M; 3.726603614
Werner, Riley; 444253; F; 2.34553428
Davis, Glen-Andrew; 488133; M; 3.937722411
Thomas, Sena; 302398; F; 2.918400394
Nash, Tim; 272835; M; 2.467843954
Hack, Jacque; 641605; F; 3.634297476
Ortega, Jane; 645168; F; 2.883788343
Catania, DeMarco; 733002; M; 2.577448044
Johnson, Trevor; 658753; M; 3.799475821
Sanchez, Kieley; 904592; F; 2.830103354
Williams, Richard; 866271; M; 2.078074428
Becker, Anna; 716502; F; 2.87310837
Bolling, Mohammad; 419808; M; 2.135187788
Amundson, David; 323273; M; 2.920326965
Gamboa, Wanisha; 362001; F; 3.157668898
Evangelista, Nicholas; 982317; M; 3.645258579
Sibley, Ryan; 827402; F; 2.045116921
Nishimura, Erica; 729129; F; 2.20640475
Wrong, Student; 123456; M; 3.057886723
Johnson, Brenda; 576650; F; 3.98965474
Benavidez, Bradley; 783372; M; 3.674861734
Hagans, Stefan; 103521; M; 3.635450559
Poston, Matthew; 653875; M; 3.751225013
Nethercott, Michael; 812517; F; 3.430435346
Higbie, Hannah; 462902; F; 2.19950088
Sims, Houston; 391402; M; 2.745368991
Serrano, Owen; 119410; M; 2.222586817
Diaz-Juarez, Tyler; 460126; M; 2.607381048
Gonzalez-Gascon, Jeaniel; 309226; F; 3.611842345
Medrano-Pacheco, Rachel; 285416; F; 3.869157451
Dartt, Rahul; 410206; M; 2.120226852
Hagans, Mary; 214632; F; 3.366191982
Macneil, Katherine; 193833; F; 2.727002021
Casolari, Lyle; 554535; M; 3.260673916
Serate, Alexis; 183761; F; 2.417047022
Goldberg, Zachary; 557631; M; 3.781838392
Niday, Breanna; 898497; F; 3.964983416
Andrieu, Andrew; 295673; M; 3.800520395
Ahmed, Alexandra; 214029; F; 2.503090728
Hunt, Stephanie; 597706; F; 3.7864219
Volkmar, Mason; 641651; M; 3.325557771
Goffinet, Jahshua; 722457; M; 2.258524754
Buck, Phoebe; 729716; F; 3.701569389

17
PA09-1108/testSet.txt Normal file
View file

@ -0,0 +1,17 @@
Davis, Glen-Andrew; 488133; M; 3.937722411
Reyes, Connor; 191261; M; 3.295578577
Johnson, Robert; 603667; M; 2.844077875
Elliott, Cayley; 135658; F; 2.978848017
Sanchez, Susan; 154838; F; 2.063213296
Ruan, Francisco; 587182; M; 3.726603614
Penn, Frederick; 819367; M; 2.828974752
Johnson, Brenda; 576650; F; 3.98965474
Deangelis, Shawna; 764050; F; 2.203316877
Casolari, Lyle; 554535; M; 3.260673916
Shafer, Tristan ; 693686; F; 3.180700609
Amundson, David; 323273; M; 2.920326965
Thomas, Sena; 302398; F; 2.918400394
Hunt, Stephanie; 597706; F; 3.7864219
Goldberg, Zachary; 557631; M; 3.781838392
Bolling, Mohammad; 419808; M; 2.135187788
Serrano, Owen; 119410; M; 2.222586817

12
PA09-1108/testSet_2.txt Normal file
View file

@ -0,0 +1,12 @@
Davis, Glen-Andrew; 488133; M; 3.937722411
Reyes, Connor; 191261; M; 3.295578577
Johnson, Robert; 603667; M; 2.844077875
Elliott, Cayley; 135658; F; 2.978848017
Sanchez, Susan; 154838; F; 2.063213296
Penn, Frederick; 819367; M; 2.828974752
Deangelis, Shawna; 764050; F; 2.203316877
Shafer, Tristan ; 693686; F; 3.180700609
Amundson, David; 323273; M; 2.920326965
Thomas, Sena; 302398; F; 2.918400394
Bolling, Mohammad; 419808; M; 2.135187788
Serrano, Owen; 119410; M; 2.222586817

4
PA09-1108/testSet_3.txt Normal file
View file

@ -0,0 +1,4 @@
Davis, Glen-Andrew; 488133; M; 3.937722411
Reyes, Connor; 191261; M; 3.295578577
Johnson, Robert; 603667; M; 2.844077875