Skip to content
Snippets Groups Projects
Commit aeda2b50 authored by Miguel Espino's avatar Miguel Espino
Browse files

added all the lab 0 files

parent 57b76df3
No related branches found
No related tags found
No related merge requests found
/* complexComp.c
* Implementation of comparison function
* @author Dwight Makaroff
* @date: July 2023
*/
#include <stdlib.h>
#include <qsort.h>
/* returns -1 if first < second
* returns 0 if first == second
* returns 1 if first > second
* Note that these void pointers as input are strings. That's
* why strtol is being used to get the right values in the
* structure.
* a complex number is composed of 2 fields: real and imaginary, which are
* both doubles
*/
int compareComplex(void *first, void *second)
{
Complex *cFirst;
char *index;
Complex *cSnd;
int sizeFirst, sizeSecond;
cFirst->real = strtod(first, &index, 10);
cFirst->imag = strtod(index, NULL, 10);
cSnd->real = strtod(second, &index, 10);
cSnd->imag = strtod(index, NULL, 10);
sizeFirst = sqrt(cFirst->real * cFirst->real + cFirst->imag * cFirst->imag);
sizeSecond = sqrt(cSnd->real * cSnd->real + cSnd->imag * cSnd->imag);
if (sizeFirst < sizeSecond)
return(-1);
else if (sizeFirst > sizeSecond)
return(1);
else
return (0);
}
/* intComp.cd
* Implementation of comparison function
* @author Dwight Makaroff
* @date: July 2023
*/
/* returns -1 if first < second
* returns 0 if first == second
* returns 1 if first > second
*/
int compareDouble(void *first, void *second)
{
/* fill in the details of comparing 2 doubles */
}
/* string input routines
* @author: Kernighan/Ritchie
* @date: 1978
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXLEN 100
int myGetline(char s[], int lim)
{
int c, i;
for (i=0;i<lim-1 && (c=getchar()) != EOF && c!='\n'; ++i)
s[i] = c;
if (c = '\n')
{
s[i] = c;
++i;
}
return i;
}
int *readlines(char *lineptr[], int maxlines)
{
int len, *nlines;
char *p, line[MAXLEN];
*nlines=0;
while ((len = myGetline(line, MAXLEN)) > 0)
{
if (*nlines >= maxlines || (p = (char *)malloc(len)) == NULL)
return NULL;
else
{
line [len-1] = '\0';
memmove(p, line, len);
lineptr[*nlines++] = p;
}
}
return nlines;
}
void writelines(char *lineptr[], int nlines)
{
int linenum;
while (nlines-- >0)
printf("%s\n", *lineptr++);
}
/* intComp.c
* Implementation of comparison function
* @author Dwight Makaroff
* @date: July 2023
*/
/* returns -1 if first < second
* returns 0 if first == second
* returns 1 if first > second
*/
#include <stdio.h>
#include <stdlib.h>
int compareInt(void *first, void *second)
{
/* fill in details of comparing 2 integers */
/* look at complexComp.c for the idea behind this solution */
}
File added
qsort.c 0 → 100644
/* @author Kernighan/Ritchie
* @modified by: Dwight Makaroff
* @date: July 2023
* @purpose: review of CMPT 214 programming style and
* debugging skills
*/
/*
* qsort: sort v[left]...v[right] into increasing order.
* The array v is void *, but from the calling program we see that
* these are strings.
*/
#include <qsort.h>
void myQsort(void *v[], int left, int right, Comparator comp)
{
int index, last;
if (left >= right)
return;
(*swap)(v, left, (left + right)/2);
last = left;
for (index = left+1; index < right; index++)
{
if ((*comp)(v[index],v[left]) < 0)
(*swap)(v, ++last, index);
}
(*swap)(v, left, last);
myQsort(v, left, last-1, comp);
myQsort(v, last+1, right, comp);
}
/* main program for qSort
* @author: Kernighan/Ritchie
* @date: 1978
* @modified by: Dwight Makaroff
* July 2023
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <qsort.h>
enum sType
{
STRING, INTEGER, DOUBLE, COMPLEX
};
#define MAXLINES 100
char *lineptr[MAXLINES];
int main(int argc, char *argv[])
{
int nlines;
enum sType sortType = STRING;
int (*comparing)(void*, void*);
if (argc != 2)
{
perror("Usage: perror. wrong number of arguments");
return -1;
}
sortType = atoi(argv[1]);
if (sortType == STRING) comparing = (Comparator) strcmp;
if (sortType == INTEGER) comparing = compareInt;
if (sortType == DOUBLE) comparing = compareDouble;
if (sortType == COMPLEX) comparing = compareComplex;
if ((nlines = readlines(lineptr, MAXLINES)) >=0)
{
printf("UNSORTED ORDER\n");
writelines(lineptr, nlines);
myQsort ((void **)lineptr, 0, nlines-1, comparing);
printf("\nSORTED ORDER \n");
writelines(lineptr, nlines);
return 0;
}
else
{
perror("problem with input");
return 1;
}
}
swap.c 0 → 100644
/* swap.c
* implementation of swap function
* @author Dwight Makaroff
* @date: July 2023
*/
void swap(void *v[], int i, int j)
{
void *temp;
temp = v[i];
v[i] = v[j];
v[j] = temp;
}
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment