User Tools

Site Tools


user:jr018429:portfolio:set_library

Set Library

John T. Rine
Discrete Structures
Fall 2011

My library is available at:
http://lab46.corning-cc.edu/~jr018429/setLibrary.zip

The Discrete group to which I belong has a Wiki page is located at:
http://lab46.corning-cc.edu/notes/discrete/setman

My library is composed of the following functions:

set.h
set.h is the header file for the set library. It should be included in any .c library files whose function prototypes have been added to it. Also, it must be added to any programs utilizing the library's functions.
Example code:

set.h
//set.h
//John T. Rine
//October 15, 2011
//November 3, 2011
 
#ifndef _SET_H
#define _SET_H
 
struct Set
{
	char ** setPtr;
	int numberOfElements;
};
typedef struct Set set;
 
set parseSet(char *);
int cntChars(char *);
int checkSetNotation(char *);
int occurrences(char *, char);
void displaySet(set);
int strEqu(char *, char *);
 
#endif

int strEqu(char *, char*)
strEqu.c contains the function definition for the function that checks two strings for equality. If they are equal, TRUE is returned, otherwise FALSE is returned. This function will be used to test whether elements set elements are common between sets.
Example code:

strEqu.c
//strEqu.c
//John T. Rine
//November 3, 2011
 
#include"set.h"
 
int strEqu(char *str1, char *str2)
{
	int var = 1; //equal
	int i = 0;
 
 
	while (!(*(str1 + i) == '\0' && *(str2 + i) == '\0'))
	{
		if (*(str1 + i) != *(str2 + i))
		{
			var = 0;
			break;
		}
		i++;
	}
	return var;
}

set parseSet(char *)
parseSet.c contains the function definition for the function that parses an input string and creates, if possible, a set from the result of the parsing. parseSet contains the set library function int checkSetNotation(char *inputString)which checks that the input string is in proper set notation, if it isn't, an error is returned.
Example code:

parseSet.c
//parseSet.c
//John T. Rine
//October 15, 2011
 
#include "set.h"
#include<stdlib.h>
 
set parseSet(char *setInput)
{
	int i, count = 0;
	char **rowPtr = NULL;
	int stringCharacterPosition = 0;
	int elementNumber = 0;
	int elementCount = 0;
	int elementCharacterCount = 0;
	int oldElementCharacterCount = 0;
	int oldStringCharacterPosition = 0;
 
	set returnSet;
 
	if(checkSetNotation(setInput) == 0) exit(1);
 
	elementCount = occurrences(setInput, ',') + 1;
	rowPtr = malloc(sizeof(char *) * elementCount);
 
	while(*(setInput + stringCharacterPosition) != '\0')
	{
		if(*(setInput + stringCharacterPosition) == '{') stringCharacterPosition++;
		oldElementCharacterCount = elementCharacterCount;
		oldStringCharacterPosition = stringCharacterPosition;
		while(*(setInput + stringCharacterPosition) != ',' && *(setInput + stringCharacterPosition) != '}')
		{
			elementCharacterCount++;
			stringCharacterPosition++;
		}
		*(rowPtr + elementNumber) = malloc(sizeof(char) * (elementCharacterCount + 1));
		count = 0;
		while(*(setInput + oldStringCharacterPosition) != ',' && *(setInput + oldStringCharacterPosition) != '}')
		{
			*(*(rowPtr + elementNumber) + count) = *(setInput + oldStringCharacterPosition);
			oldStringCharacterPosition++;
			count++;
		}
		*(*(rowPtr + elementNumber) + count) = '\0';
		elementCharacterCount = 0;
		elementNumber++;
		stringCharacterPosition++;
	}
	returnSet.setPtr = rowPtr;
	returnSet.numberOfElements = elementNumber;
 
	return returnSet;
}

int cntChars(char *)
The function cntChars takes as its parameter a character pointer (the base address of a character array, a string, and returns the number of characters in the string up to the maximum size of an integer).
Example code:

cntChars.c
//cntChars.c
//John T. Rine
//September 23, 2011
 
#include"set.h"
 
int cntChars(char *inputString)
{
	int count = 0;
	while(*(inputString + count) != '\0') ++count;
	return(count);
}

int checkSetNotation(char *)
checkSetNotation.c checks that the input string is in proper set notation, if it isn't, an error is returned.

checkSetNotation.c
//checkSetNotation.c
//John T. Rine
//October 15, 2011
 
#include"set.h"
#include<stdio.h>
 
int checkSetNotation(char *inputString)
{
	char oldChar = 'C';
	int position = 0;
	int numberOfCharsInSet = 0;
 
	numberOfCharsInSet = cntChars(inputString);
	if(*(inputString + position) != '{')
	{
		printf("First character in a set representation must be a '{'\n");
		return 0;
	}
	else if (*(inputString + numberOfCharsInSet - 1) != '}')
	{
		printf("Last character in a set representation must be a '}'\n");
		return 0;
	}
	else if (*(inputString + numberOfCharsInSet - 2) == ',')
	{
		printf("Last two characters in a set representation can't be \",}\"\n");
		return 0;
	}
	else if (*(inputString + 1) == ',')
	{
		printf("First two characters in a set representation can't be \"{,\"\n");
		return 0;
	}
	while(*(inputString + position) != '\0')
	{
		if (*(inputString + position) == ',' && oldChar == ',')
		{
			printf("Can't have two adjacent commas in a set representation\n");
			return 0;
		}
		oldChar = *(inputString + position);
		position ++;
	}
	return 1;
}

The following setTest execution illustrates the capabilities of the checkSetNotation function.
This function checks for the following conditions:

  • beginning brace
  • ending brace
  • beginning brace and adjacent comma (error)
  • adjacent comma and ending brace (error)
  • adjacent commas (error)

The function therefore will ignore spaces. It should treat them as though they are part of an element of a set.

c:\MinGW\jrprogs\set\setProject>setTest {A,B,C,D,E}
{A,B,C,D,E}

c:\MinGW\jrprogs\set\setProject>setTest A,B,C,D,E}
First character in a set representation must be a '{'

c:\MinGW\jrprogs\set\setProject>setTest {,B,C,D,E}
First two characters in a set representation can't be "{,"

c:\MinGW\jrprogs\set\setProject>setTest {A,,C,D,E}
Can't have two adjacent commas in a set representation

c:\MinGW\jrprogs\set\setProject>setTest {A,B,,D,E}
Can't have two adjacent commas in a set representation

c:\MinGW\jrprogs\set\setProject>setTest {A,B,C,,E}
Can't have two adjacent commas in a set representation

c:\MinGW\jrprogs\set\setProject>setTest {A,B,C,D,}
Last two characters in a set representation can't be ",}"

c:\MinGW\jrprogs\set\setProject>setTest {A,B,C,D,E
Last character in a set representation must be a '}'

int occurrences(char *, char)
The function occurrences takes as function parameters a character and a character pointer (the base address of a character array, a string). occurences returns the number of times a particular character occurs in a string up to the maximum size of a integer.
Example code:

occurrences.c
//occurrences.c
//John T. Rine
//September 23, 2011
 
 
#include "set.h"
 
int occurrences(char *inputString, char c)
{
	int i = 0;
	int r = 0;
	while (*(inputString + i) != '\0')
	{
		if(*(inputString + i) == c) r++;
		i++;
	}
	return r;
}

void displaySet(set)
displaySet.c takes the set set datatype which consists of a structure wrapping a pointer to a two dimensional array and an integer to store the number of elements in the set, and formats and prints it in proper set notation.
Example code:

displaySet.c
//displaySet.c
//John T. Rine
//October 15, 2011
 
#define DEBUG 
 
#include"set.h"
#include<stdio.h>
 
void displaySet(set inputSet)
{
	int i;
	printf("{");
	for(i = 0; i < inputSet.numberOfElements; i++)
	{
		printf("%s", inputSet.setPtr[i]);
		if(i != inputSet.numberOfElements - 1) printf(",");
	}
	printf("}\n");
	#ifdef DEBUG
		if (strEqu(inputSet.setPtr[0], inputSet.setPtr[1])) printf("if DEBUG defined, then inputSet.setPtr[0] == inputSet.setPtr[1]\n");
		else printf("if DEBUG defined, inputSet.setPtr[0] != inputSet.setPtr[1]\n");
	#endif
}

setTest.c
setTest.c is the test file that utilizes the set library functions.
Example code:

setTest.c
//setTest.c
//John T. Rine
//October 16, 2011
#include<stdio.h>
#include<stdlib.h>
#include"set.h"
 
int main(int argc, char **argv)
{
	set setData;
	int i;
 
	if (argc == 1)
	{
		printf("Should be at least one command line argument supplied after the file name\n");
		exit(1);
	}
    else setData = parseSet(*(argv + 1));
	displaySet(setData);
	for (i = 0; i < setData.numberOfElements; i++) free(setData.setPtr[i]);
	free(setData.setPtr);
 
    return 0;
 
} 

makeSetTest.bat
The Windows batch file makeSetTest.bat automates, manufacture of all of the .o files, the creation of the static library file, compiling, linking, and execution of setTest, the test file.
Example code:

makeSetTest.bat
REM makeSetTest.bat
REM John T. Rine
REM October 16, 2011
del *.exe
del *.o
del *.a
gcc -c displaySet.c -o displaySet.o
gcc -c cntChars.c -o cntChars.o
gcc -c checkSetNotation.c -o checkSetNotation.o
gcc -c occurrences.c -o occurrences.o
gcc -c parseSet.c -o parseSet.o
gcc -c strEqu.c -o strEqu.o
 
ar src libset.a *.o
gcc -I. setTest.c -o setTest libset.a
SET /P M=What program to run?
setTest %M%
 
pause

Execution of makeSetTest.bat:

C:\Users\John\Desktop\setLibrary\setLibrary>REM makeSetTest.bat

C:\Users\John\Desktop\setLibrary\setLibrary>REM John T. Rine

C:\Users\John\Desktop\setLibrary\setLibrary>REM October 16, 2011

C:\Users\John\Desktop\setLibrary\setLibrary>del *.exe

C:\Users\John\Desktop\setLibrary\setLibrary>del *.o

C:\Users\John\Desktop\setLibrary\setLibrary>del *.a

C:\Users\John\Desktop\setLibrary\setLibrary>gcc -c displaySet.c -o displaySet.o


C:\Users\John\Desktop\setLibrary\setLibrary>gcc -c cntChars.c -o cntChars.o

C:\Users\John\Desktop\setLibrary\setLibrary>gcc -c checkSetNotation.c -o checkSe
tNotation.o

C:\Users\John\Desktop\setLibrary\setLibrary>gcc -c occurrences.c -o occurrences.
o

C:\Users\John\Desktop\setLibrary\setLibrary>gcc -c parseSet.c -o parseSet.o

C:\Users\John\Desktop\setLibrary\setLibrary>gcc -c strEqu.c -o strEqu.o

C:\Users\John\Desktop\setLibrary\setLibrary>ar src libset.a *.o

C:\Users\John\Desktop\setLibrary\setLibrary>gcc -I. setTest.c -o setTest libset.
a

C:\Users\John\Desktop\setLibrary\setLibrary>SET /P M=What program to run?
What program to run?{dog,dog,cat,bird}

C:\Users\John\Desktop\setLibrary\setLibrary>setTest {dog,dog,cat,bird}
{dog,dog,cat,bird}
if DEBUG defined, then inputSet.setPtr[0] == inputSet.setPtr[1]

C:\Users\John\Desktop\setLibrary\setLibrary>pause
Press any key to continue . . .

makeSetTest.sh
The bash shell script file makeSetTest.sh automates the manufacture of all of the .o files, the creation of the static library file, compiling, linking and execution of setTest, the test file.
Example code:

makeSetTest.sh
#!/bin/bash
gcc -c checkSetNotation.c -o checkSetNotation.o
gcc -c cntChars.c -o cntChars.o
gcc -c displaySet.c -o displaySet.o
gcc -c occurrences.c -o occurrences.o
gcc -c parseSet.c -o parseSet.o
gcc -c strEqu.c -o strEqu.o
 
ar scr libset.a *.o
gcc -I. setTest.c -o setTest libset.a
read input
run="./setTest "
concat="$run$input"
$concat

Execution of makeSetTest.sh:

lab46:~/src/setLibrary$ ./makeSetTest.sh
{dog,dog,cat}
{dog,dog,cat}
if DEBUG defined, then inputSet.setPtr[0] == inputSet.setPtr[1]
lab46:~/src/setLibrary$
user/jr018429/portfolio/set_library.txt · Last modified: 2011/11/03 23:08 by jr018429