Initial commit

This commit is contained in:
2023-05-22 23:28:51 -03:00
commit 5c1403aa91
467 changed files with 18649 additions and 0 deletions

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,28 @@
GCC = gcc
CFLAGS = -g -Wall -Wshadow
TARGETS = stringTest whereIsWaldo functionPointer sortInts
OBJS = Strings.o
HDRS = Strings.h
VAL = valgrind --tool=memcheck --leak-check=full
VAL += --verbose --log-file=
all: $(TARGETS)
stringTest: $(OBJS) $(HDRS)
$(GCC) $(CFLAGS) $(OBJS) $@.c -o $@
whereIsWaldo: $(OBJS) $(HDRS)
$(GCC) $(CFLAGS) $(OBJS) $@.c -o $@
functionPointer: $(OBJS) $(HDRS)
$(GCC) $(CFLAGS) $(OBJS) $@.c -o $@
sortedInts: $(OBJS) $(HDRS)
$(GCC) $(CFLAGS) $(OBJS) $@.c -o $@
%.o: %.c
$(GCC) $(CFLAGS) -c $*.c
clean:
rm -f $(TARGETS) *.o

View File

@ -0,0 +1,97 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "Strings.h"
char* getfc(FILE* pFIn, char* terminators, int n);
int charInString(char* t, char c);
// a cover function for malloc()
// malloc and return memory for a string of stringsize characters
// return (char*)NULL on failure
char* mallocString(int stringsize){
return (char*)malloc(sizeof(char)*(stringsize+1));
}
// just a cover function for free()
void freeString(char* s){
free(s);
}
// create a duplicate string of s
// return it
// return (char*)NULL on failure
// should call mallocString(), and then strcpy()
char* duplicateString(char* s){
char* sCopy = mallocString(strlen(s));
if(sCopy != (char*)NULL){
strcpy(sCopy,s);
}
return sCopy;
}
char** duplicateStringList(char** s,int n){
char** slCopy;
// Allocate the list
slCopy = (char**)malloc(sizeof(char*)*n);
if(slCopy == (char**)NULL) return slCopy;
// Allocate/duplicate the strings
for(int i = 0; i<n; i++){
slCopy[i] = duplicateString(s[i]);
if(slCopy[i] == (char*)NULL){
// Bad stuff - clean up and return
for(int j=0; j<i; j++){
freeString(slCopy[j]);
}
free(slCopy);
slCopy = (char**)NULL;
break;
}
}
return slCopy;
}
// Return an allocated string from an open file,
// Stop reading when any character is in terminators list
// return allocated string or (char*)NULL
char* getfString(FILE* pFIn, char* terminators){
char* s = getfc(pFIn, terminators, 0);
return s;
}
char* getString(char* terminators){
char* s;
s = getfc(stdin, terminators, 0);
return s;
}
char* getfc(FILE* pFIn, char* terminators, int n){
char* s;
char c = fgetc(pFIn);
//base case
if(c == EOF || charInString(terminators, c)){
// allocate a string
s = mallocString(n);
if(s != (char*)NULL){
// terminate the string
s[n+1] = (char)NULL;
}
return s;
}
s = getfc(pFIn, terminators, n+1);
s[n] = c;
return s;
}
int charInString(char* t, char c){
int i = 0;
while(t[i] != (char)NULL){
if(t[i] == c) return 1;
i++;
}
return 0;
}
//End

View File

@ -0,0 +1,30 @@
#ifndef STRINGS_H
#define STRINGS_H
// a cover function for malloc()
// malloc and return memory for a string of stringsize characters
// return (char*)NULL on failure
char* mallocString(int stringsize);
// just a cover function for free()
void freeString(char* s);
// create a duplicate string of s
// return it
// return (char*)NULL on failure
// should call mallocString(), and then strcpy()
char* duplicateString(char* s);
// create a duplicate of string list sl
// return it
// return (char**)NULL on failure
// uses other Strings module functions
char** duplicateStringList(char** sl,int n);
// Return an allocated string from an open file,
// Stop reading when any character is in terminators list
// return allocated string or (char*)NULL
char* getfString(FILE* pFIn, char* terminators);
char* getString(char* terminators);
#endif

View File

@ -0,0 +1,8 @@
int *a = malloc(n*sizeof(int));
...
// need to double the size of a
a = doubleSize(a, n);
if(a == NULL){
//error, exit program
}
// keep using a, now with twice the capacity

View File

@ -0,0 +1,20 @@
typedef struct point {
double x;
double y;
} Point2D, *pPoint2D;
pPoint2D pPtThis;
Point2D pt1 = {3.0, 4.2};
double x = pt1.x;
pt1->y = 3.1;
Point2D* pPt;
pPtThis = (pPtThis)malloc(pPtThis);
pPtThis = &pt1;
pPtThis = (pPtThis)malloc(sizeof(Point2D*));
pPtThis = (pPtThis)malloc(sizeof(Point2D));

View File

@ -0,0 +1,42 @@
#include <stdlib.h>
#include <stdio.h>
int compareInt(const void * arg1, const void * arg2);
// Note - this program *should* do error checking!!
int main(void){
int a;
int b;
int* pa = &a;
int* pb = &b;
printf("Two integers please: ");
scanf(" %d %d", pa, pb);
if(compareInt(pa, pb)) printf("Integers are in ascending order\n");
else printf("Integers are equal or in descending order\n");
return EXIT_SUCCESS;
}
int compareInt(const void * arg1, const void * arg2){
// convert void * to an int *
const int * ptr1 = (const int *) arg1;
const int * ptr2 = (const int *) arg2;
// get the value from the address
const int val1 = * ptr1;
const int val2 = * ptr2;
// compare the value
if (val1 < val2) return -1;
if (val1 == val2) return 0;
return 1;
}
/*
* [FCSSSDs-iMac-3:Lectures/L15 - Code Along/L15src] wightman% ./functionPointer
* Two integers please: 3 4
* Integers are in ascending order
* [FCSSSDs-iMac-3:Lectures/L15 - Code Along/L15src] wightman% ./functionPointer
* Two integers please: 4 3
* Integers are in ascending order
*/

View File

@ -0,0 +1,42 @@
#include <stdlib.h>
#include <stdio.h>
#define MAX_SIZE 10
int compareInt(const void * arg1, const void * arg2);
// Note - this program *should* do error checking!!
int main(void){
int a[MAX_SIZE];
printf("%d integers please: ", MAX_SIZE);
for(int i=0; i< MAX_SIZE; i++){
scanf("%d", &a[i]);
}
qsort(a, MAX_SIZE, sizeof(int), compareInt);
printf("Sorted values: ");
for(int i=0; i< MAX_SIZE; i++){
printf("%d ", a[i]);
}
printf("\n");
return EXIT_SUCCESS;
}
int compareInt(const void * arg1, const void * arg2){
// convert void * to an int *
const int * ptr1 = (const int *) arg1;
const int * ptr2 = (const int *) arg2;
// get the value from the address
const int val1 = * ptr1;
const int val2 = * ptr2;
// compare the value
if (val1 < val2) return -1;
if (val1 == val2) return 0;
return 1;
}
/*
* [FCSSSDs-iMac-3:Lectures/L15 - Code Along/L15src] wightman% ./sortInts
* 10 integers please: 5 34 -1 0 444 34 -34 5 34 -66
* Sorted values: -66 -34 -1 0 5 5 34 34 34 444
*/

View File

@ -0,0 +1,34 @@
#include <stdlib.h>
#include <stdio.h>
#include "Strings.h"
int main(int argc, char* argv[]){
FILE *pFIn;
char* string;
char terminators[2];
terminators[0] = 'E';
terminators[0]= (char)NULL;
/* Argument check */
if(argc != 2)
{
fprintf(stderr,"Usage: %s <filename>\n", argv[0]);
return EXIT_FAILURE;
}/* End if(argc... */
/* Open the file given through the command line */
pFIn = fopen(argv[1], "r");
if(pFIn == (FILE*) NULL)
{
fprintf(stderr,"Could not open %s\n", argv[1]);
return EXIT_FAILURE;
}/* End if(pFIn... */
string = getfString(pFIn, terminators);
fclose(pFIn);
printf("%s", string);
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,14 @@
#include <stdlib.h>
#include <stdio.h>
#include "Strings.h"
int main(void){
printf("Address of getStringf(): %p\n", getfString);
return EXIT_SUCCESS;
}
/*
* [FCSSSDs-iMac-3:Lectures/L15 - Code Along/L15src] wightman% ./whereIsWaldo
* Address of getStringf(): 0x102de2d60
*/