Added VERY old code. Very cringy to look at, but hey, we all had to start somewhere...

This commit is contained in:
2018-01-08 23:37:31 -08:00
parent 7b18f6a807
commit df19ed7631
141 changed files with 26107 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?fileVersion 4.0.0?><cproject storage_type_id="org.eclipse.cdt.core.XmlProjectDescriptionStorage">
<storageModule moduleId="org.eclipse.cdt.core.settings">
<cconfiguration id="cdt.managedbuild.toolchain.gnu.cross.base.1400812991">
<storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="cdt.managedbuild.toolchain.gnu.cross.base.1400812991" moduleId="org.eclipse.cdt.core.settings" name="Default">
<externalSettings/>
<extensions>
<extension id="org.eclipse.cdt.core.ELF" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.GmakeErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.CWDLocator" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
</extensions>
</storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<configuration artifactName="Assignment 2" buildProperties="" description="" id="cdt.managedbuild.toolchain.gnu.cross.base.1400812991" name="Default" parent="org.eclipse.cdt.build.core.emptycfg">
<folderInfo id="cdt.managedbuild.toolchain.gnu.cross.base.1400812991.35170586" name="/" resourcePath="">
<toolChain id="cdt.managedbuild.toolchain.gnu.cross.base.1508668141" name="cdt.managedbuild.toolchain.gnu.cross.base" superClass="cdt.managedbuild.toolchain.gnu.cross.base">
<option id="cdt.managedbuild.option.gnu.cross.prefix.2079751801" name="Prefix" superClass="cdt.managedbuild.option.gnu.cross.prefix"/>
<option id="cdt.managedbuild.option.gnu.cross.path.163334932" name="Path" superClass="cdt.managedbuild.option.gnu.cross.path"/>
<targetPlatform archList="all" binaryParser="org.eclipse.cdt.core.ELF" id="cdt.managedbuild.targetPlatform.gnu.cross.107229620" isAbstract="false" osList="all" superClass="cdt.managedbuild.targetPlatform.gnu.cross"/>
<builder id="cdt.managedbuild.builder.gnu.cross.217779992" keepEnvironmentInBuildfile="false" managedBuildOn="false" name="Gnu Make Builder" superClass="cdt.managedbuild.builder.gnu.cross"/>
<tool id="cdt.managedbuild.tool.gnu.cross.c.compiler.926370999" name="Cross GCC Compiler" superClass="cdt.managedbuild.tool.gnu.cross.c.compiler">
<inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.1328834840" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/>
</tool>
<tool id="cdt.managedbuild.tool.gnu.cross.cpp.compiler.1744738686" name="Cross G++ Compiler" superClass="cdt.managedbuild.tool.gnu.cross.cpp.compiler"/>
<tool id="cdt.managedbuild.tool.gnu.cross.c.linker.243486500" name="Cross GCC Linker" superClass="cdt.managedbuild.tool.gnu.cross.c.linker">
<inputType id="cdt.managedbuild.tool.gnu.c.linker.input.569562329" superClass="cdt.managedbuild.tool.gnu.c.linker.input">
<additionalInput kind="additionalinputdependency" paths="$(USER_OBJS)"/>
<additionalInput kind="additionalinput" paths="$(LIBS)"/>
</inputType>
</tool>
<tool id="cdt.managedbuild.tool.gnu.cross.cpp.linker.2108641884" name="Cross G++ Linker" superClass="cdt.managedbuild.tool.gnu.cross.cpp.linker"/>
<tool id="cdt.managedbuild.tool.gnu.cross.archiver.1691225243" name="Cross GCC Archiver" superClass="cdt.managedbuild.tool.gnu.cross.archiver"/>
<tool id="cdt.managedbuild.tool.gnu.cross.assembler.858869286" name="Cross GCC Assembler" superClass="cdt.managedbuild.tool.gnu.cross.assembler">
<inputType id="cdt.managedbuild.tool.gnu.assembler.input.2083482904" superClass="cdt.managedbuild.tool.gnu.assembler.input"/>
</tool>
</toolChain>
</folderInfo>
</configuration>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
</cconfiguration>
</storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<project id="Assignment 2.null.1988302796" name="Assignment 2"/>
</storageModule>
<storageModule moduleId="scannerConfiguration">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
<storageModule moduleId="org.eclipse.cdt.internal.ui.text.commentOwnerProjectMappings"/>
</cproject>

View File

@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Assignment 2</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
<triggers>clean,full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
<triggers>full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.cdt.core.cnature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
</natures>
</projectDescription>

View File

@@ -0,0 +1,146 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "dynamicArray.h"
/* param: s the string
param: num a pointer to double
returns: true (1) if s is a number else 0 or false.
postcondition: if it is a number, num will hold
the value of the number
*/
int isNumber(char *s, double *num)
{
char *end;
double returnNum;
if(strcmp(s, "0") == 0)
{
*num = 0;
return 1;
}
else
{
returnNum = strtod(s, &end);
/* If there's anythin in end, it's bad */
if((returnNum != 0.0) && (strcmp(end, "") == 0))
{
*num = returnNum;
return 1;
}
}
return 0; //if got here, it was not a number
}
/* param: stack the stack being manipulated
pre: the stack contains at least two elements
post: the top two elements are popped and
their sum is pushed back onto the stack.
*/
void add (struct DynArr *stack)
{
/* FIXME: You will write this function */
}
/* param: stack the stack being manipulated
pre: the stack contains at least two elements
post: the top two elements are popped and
their difference is pushed back onto the stack.
*/
void subtract(struct DynArr *stack)
{
/* FIXME: You will write this function */
}
/* param: stack the stack being manipulated
pre: the stack contains at least two elements
post: the top two elements are popped and
their quotient is pushed back onto the stack.
*/
void divide(struct DynArr *stack)
{
/* FIXME: You will write this function */
}
double calculate(int numInputTokens, char **inputString)
{
int i;
double result = 0.0;
char *s;
struct DynArr *stack;
//set up the stack
stack = createDynArr(20);
// start at 1 to skip the name of the calculator calc
for(i=1;i < numInputTokens;i++)
{
s = inputString[i];
// Hint: General algorithm:
// (1) Check if the string s is in the list of operators.
// (1a) If it is, perform corresponding operations.
// (1b) Otherwise, check if s is a number.
// (1b - I) If s is not a number, produce an error.
// (1b - II) If s is a number, push it onto the stack
if(strcmp(s, "+") == 0)
add(stack);
else if(strcmp(s,"-") == 0)
subtract(stack);
else if(strcmp(s, "/") == 0)
divide(stack);
else if(strcmp(s, "x") == 0)
/* FIXME: replace printf with your own function */
printf("Multiplying\n");
else if(strcmp(s, "^") == 0)
/* FIXME: replace printf with your own function */
printf("Power\n");
else if(strcmp(s, "^2") == 0)
/* FIXME: replace printf with your own function */
printf("Squaring\n");
else if(strcmp(s, "^3") == 0)
/* FIXME: replace printf with your own function */
printf("Cubing\n");
else if(strcmp(s, "abs") == 0)
/* FIXME: replace printf with your own function */
printf("Absolute value\n");
else if(strcmp(s, "sqrt") == 0)
/* FIXME: replace printf with your own function */
printf("Square root\n");
else if(strcmp(s, "exp") == 0)
/* FIXME: replace printf with your own function */
printf("Exponential\n");
else if(strcmp(s, "ln") == 0)
/* FIXME: replace printf with your own function */
printf("Natural Log\n");
else if(strcmp(s, "log") == 0)
/* FIXME: replace printf with your own function */
printf("Log\n");
else
{
// FIXME: You need to develop the code here (when s is not an operator)
// Remember to deal with special values ("pi" and "e")
}
} //end for
/* FIXME: You will write this part of the function (2 steps below)
* (1) Check if everything looks OK and produce an error if needed.
* (2) Store the final value in result and print it out.
*/
return result;
}
int main(int argc , char** argv)
{
// assume each argument is contained in the argv array
// argc-1 determines the number of operands + operators
if (argc == 1)
return 0;
calculate(argc,argv);
return 0;
}

View File

@@ -0,0 +1,291 @@
/* dynamicArray.c: Dynamic Array implementation. */
#include <assert.h>
#include <stdlib.h>
#include "dynamicArray.h"
struct DynArr
{
TYPE *data; /* pointer to the data array */
int size; /* Number of elements in the array */
int capacity; /* capacity ofthe array */
};
/* ************************************************************************
Dynamic Array Functions
************************************************************************ */
/* Initialize (including allocation of data array) dynamic array.
param: v pointer to the dynamic array
param: cap capacity of the dynamic array
pre: v is not null
post: internal data array can hold cap elements
post: v->data is not null
*/
void initDynArr(DynArr *v, int capacity)
{
assert(capacity > 0);
assert(v!= 0);
v->data = (TYPE *) malloc(sizeof(TYPE) * capacity);
assert(v->data != 0);
v->size = 0;
v->capacity = capacity;
}
/* Allocate and initialize dynamic array.
param: cap desired capacity for the dyn array
pre: none
post: none
ret: a non-null pointer to a dynArr of cap capacity
and 0 elements in it.
*/
DynArr* createDynArr(int cap)
{
assert(cap > 0);
DynArr *r = (DynArr *)malloc(sizeof( DynArr));
assert(r != 0);
initDynArr(r,cap);
return r;
}
/* Deallocate data array in dynamic array.
param: v pointer to the dynamic array
pre: none
post: d.data points to null
post: size and capacity are 0
post: the memory used by v->data is freed
*/
void freeDynArr(DynArr *v)
{
if(v->data != 0)
{
free(v->data); /* free the space on the heap */
v->data = 0; /* make it point to null */
}
v->size = 0;
v->capacity = 0;
}
/* Deallocate data array and the dynamic array ure.
param: v pointer to the dynamic array
pre: none
post: the memory used by v->data is freed
post: the memory used by d is freed
*/
void deleteDynArr(DynArr *v)
{
freeDynArr(v);
free(v);
}
/* Resizes the underlying array to be the size cap
param: v pointer to the dynamic array
param: cap the new desired capacity
pre: v is not null
post: v has capacity newCap
*/
void _dynArrSetCapacity(DynArr *v, int newCap)
{
/* FIXME: You will write this function */
}
/* Get the size of the dynamic array
param: v pointer to the dynamic array
pre: v is not null
post: none
ret: the size of the dynamic array
*/
int sizeDynArr(DynArr *v)
{
return v->size;
}
/* Adds an element to the end of the dynamic array
param: v pointer to the dynamic array
param: val the value to add to the end of the dynamic array
pre: the dynArry is not null
post: size increases by 1
post: if reached capacity, capacity is doubled
post: val is in the last utilized position in the array
*/
void addDynArr(DynArr *v, TYPE val)
{
/* FIXME: You will write this function */
}
/* Get an element from the dynamic array from a specified position
param: v pointer to the dynamic array
param: pos integer index to get the element from
pre: v is not null
pre: v is not empty
pre: pos < size of the dyn array and >= 0
post: no changes to the dyn Array
ret: value stored at index pos
*/
TYPE getDynArr(DynArr *v, int pos)
{
/* FIXME: You will write this function */
/* FIXME: you must change this return value */
return 1;
}
/* Put an item into the dynamic array at the specified location,
overwriting the element that was there
param: v pointer to the dynamic array
param: pos the index to put the value into
param: val the value to insert
pre: v is not null
pre: v is not empty
pre: pos >= 0 and pos < size of the array
post: index pos contains new value, val
*/
void putDynArr(DynArr *v, int pos, TYPE val)
{
/* FIXME: You will write this function */
}
/* Swap two specified elements in the dynamic array
param: v pointer to the dynamic array
param: i,j the elements to be swapped
pre: v is not null
pre: v is not empty
pre: i, j >= 0 and i,j < size of the dynamic array
post: index i now holds the value at j and index j now holds the value at i
*/
void swapDynArr(DynArr *v, int i, int j)
{
/* FIXME: You will write this function */
}
/* Remove the element at the specified location from the array,
shifts other elements back one to fill the gap
param: v pointer to the dynamic array
param: idx location of element to remove
pre: v is not null
pre: v is not empty
pre: idx < size and idx >= 0
post: the element at idx is removed
post: the elements past idx are moved back one
*/
void removeAtDynArr(DynArr *v, int idx)
{
/* FIXME: You will write this function */
}
/* ************************************************************************
Stack Interface Functions
************************************************************************ */
/* Returns boolean (encoded in an int) demonstrating whether or not the
dynamic array stack has an item on it.
param: v pointer to the dynamic array
pre: the dynArr is not null
post: none
ret: 1 if empty, otherwise 0
*/
int isEmptyDynArr(DynArr *v)
{
/* FIXME: You will write this function */
/* FIXME: You will change this return value*/
return 1;
}
/* Push an element onto the top of the stack
param: v pointer to the dynamic array
param: val the value to push onto the stack
pre: v is not null
post: size increases by 1
if reached capacity, capacity is doubled
val is on the top of the stack
*/
void pushDynArr(DynArr *v, TYPE val)
{
/* FIXME: You will write this function */
}
/* Returns the element at the top of the stack
param: v pointer to the dynamic array
pre: v is not null
pre: v is not empty
post: no changes to the stack
*/
TYPE topDynArr(DynArr *v)
{
/* FIXME: You will write this function */
/* FIXME: You will change this return value*/
return 1;
}
/* Removes the element on top of the stack
param: v pointer to the dynamic array
pre: v is not null
pre: v is not empty
post: size is decremented by 1
the top has been removed
*/
void popDynArr(DynArr *v)
{
/* FIXME: You will write this function */
}
/* ************************************************************************
Bag Interface Functions
************************************************************************ */
/* Returns boolean (encoded as an int) demonstrating whether or not
the specified value is in the collection
true = 1
false = 0
param: v pointer to the dynamic array
param: val the value to look for in the bag
pre: v is not null
pre: v is not empty
post: no changes to the bag
*/
int containsDynArr(DynArr *v, TYPE val)
{
/* FIXME: You will write this function */
/* FIXME: You will change this return value */
return 1;
}
/* Removes the first occurrence of the specified value from the collection
if it occurs
param: v pointer to the dynamic array
param: val the value to remove from the array
pre: v is not null
pre: v is not empty
post: val has been removed
post: size of the bag is reduced by 1
*/
void removeDynArr(DynArr *v, TYPE val)
{
/* FIXME: You will write this function */
}

View File

@@ -0,0 +1,41 @@
/* dynamicArray.h : Dynamic Array implementation. */
#include<math.h>
#ifndef DYNAMIC_ARRAY_INCLUDED
#define DYNAMIC_ARRAY_INCLUDED 1
# ifndef TYPE
# define TYPE double
# define TYPE_SIZE sizeof(double)
# endif
# ifndef EQ
# define EQ(A, B) (fabs(A - B) < 10e-7)
# endif
typedef struct DynArr DynArr;
/* Dynamic Array Functions */
DynArr *createDynArr(int cap);
void deleteDynArr(DynArr *v);
int sizeDynArr(DynArr *v);
void addDynArr(DynArr *v, TYPE val);
TYPE getDynArr(DynArr *v, int pos);
void putDynArr(DynArr *v, int pos, TYPE val);
void swapDynArr(DynArr *v, int i, int j);
void removeAtDynArr(DynArr *v, int idx);
/* Stack interface. */
int isEmptyDynArr(DynArr *v);
void pushDynArr(DynArr *v, TYPE val);
TYPE topDynArr(DynArr *v);
void popDynArr(DynArr *v);
/* Bag Interface */
int containsDynArr(DynArr *v, TYPE val);
void removeDynArr(DynArr *v, TYPE val);
#endif

View File

@@ -0,0 +1,23 @@
all: calc testda
calc: dynamicArray.o calc.o
gcc -g -Wall -std=c99 -o calc dynamicArray.o calc.o -lm
testda: dynamicArray.o testDynArray.o
gcc -g -Wall -std=c99 -o testda dynamicArray.o testDynArray.o
calc.o: calc.c dynamicArray.h
gcc -g -Wall -std=c99 -c calc.c
testDynArray.o: testDynArray.c dynamicArray.h
gcc -g -Wall -std=c99 -c testDynArray.c
dynamicArray.o: dynamicArray.c dynamicArray.h
gcc -g -Wall -std=c99 -c dynamicArray.c
clean:
rm dynamicArray.o
rm calc.o
rm testDynArray.o
rm calc
rm testda

View File

@@ -0,0 +1,87 @@
/* testDynArray.c
* This file is used for testing the dynamicArray.c file.
* This test suite is by no means complete or thorough.
* More testing is needed on your own.
*/
#include <stdio.h>
#include <stdlib.h>
#include "dynamicArray.h"
void assertTrue(int predicate, char *message)
{
printf("%s: ", message);
if (predicate)
printf("PASSED\n");
else
printf("FAILED\n");
}
// this main function contains some
int main(int argc, char* argv[]){
DynArr *dyn;
dyn = createDynArr(2);
printf("\n\nTesting addDynArr...\n");
addDynArr(dyn, 3);
addDynArr(dyn, 4);
addDynArr(dyn, 10);
addDynArr(dyn, 5);
addDynArr(dyn, 6);
printf("The array's content: [3,4,10,5,6]\n");
assertTrue(EQ(getDynArr(dyn, 0), 3), "Test 1st element == 3");
assertTrue(EQ(getDynArr(dyn, 1), 4), "Test 2nd element == 4");
assertTrue(EQ(getDynArr(dyn, 2), 10), "Test 3rd element == 10");
assertTrue(EQ(getDynArr(dyn, 3), 5), "Test 4th element == 5");
assertTrue(EQ(getDynArr(dyn, 4), 6), "Test 5th element == 6");
assertTrue(sizeDynArr(dyn) == 5, "Test size = 5");
printf("\n\nTesting putDynArr...\nCalling putDynArr(dyn, 2, 7)\n");
putDynArr(dyn, 2, 7);
printf("The array's content: [3,4,7,5,6]\n");
assertTrue(EQ(getDynArr(dyn, 2), 7), "Test 3rd element == 7");
assertTrue(sizeDynArr(dyn) == 5, "Test size = 5");
printf("\n\nTesting swapDynArr...\nCalling swapDynArr(dyn, 2, 4)\n");
swapDynArr(dyn, 2, 4);
printf("The array's content: [3,4,6,5,7]\n");
assertTrue(EQ(getDynArr(dyn, 2), 6), "Test 3rd element == 6");
assertTrue(EQ(getDynArr(dyn, 4), 7), "Test 5th element == 7");
printf("\n\nTesting removeAtDynArr...\nCalling removeAtDynArr(dyn, 1)\n");
removeAtDynArr(dyn, 1);
printf("The array's content: [3,6,5,7]\n");
assertTrue(EQ(getDynArr(dyn, 0), 3), "Test 1st element == 3");
assertTrue(EQ(getDynArr(dyn, 3), 7), "Test 4th element == 7");
assertTrue(sizeDynArr(dyn) == 4, "Test size = 4");
printf("\n\nTesting stack interface...\n");
printf("The stack's content: [3,6,5,7] <- top\n");
assertTrue(!isEmptyDynArr(dyn), "Testing isEmptyDynArr");
assertTrue(EQ(topDynArr(dyn), 7), "Test topDynArr == 7");
popDynArr(dyn);
printf("Poping...\nThe stack's content: [3,6,5] <- top\n");
assertTrue(EQ(topDynArr(dyn), 5), "Test topDynArr == 5");
pushDynArr(dyn, 9);
printf("Pushing 9...\nThe stack's content: [3,6,5,9] <- top\n");
assertTrue(EQ(topDynArr(dyn), 9), "Test topDynArr == 9");
printf("\n\nTesting bag interface...\n");
printf("The bag's content: [3,6,5,9]\n");
assertTrue(containsDynArr(dyn, 3), "Test containing 3");
assertTrue(containsDynArr(dyn, 6), "Test containing 6");
assertTrue(containsDynArr(dyn, 5), "Test containing 5");
assertTrue(containsDynArr(dyn, 9), "Test containing 9");
assertTrue(!containsDynArr(dyn, 7), "Test not containing 7");
removeDynArr(dyn, 3);
printf("Removing 3...\nThe stack's content: [6,5,9]\n");
assertTrue(!containsDynArr(dyn, 3), "Test not containing 3");
return 0;
}