mirror of
https://github.com/caperren/school_archives.git
synced 2025-11-09 13:41:13 +00:00
Added VERY old code. Very cringy to look at, but hey, we all had to start somewhere...
This commit is contained in:
@@ -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>
|
||||
@@ -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>
|
||||
146
OSU Coursework/CS 261 - Data Structures/Assignment 2/calc.c
Normal file
146
OSU Coursework/CS 261 - Data Structures/Assignment 2/calc.c
Normal 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;
|
||||
}
|
||||
@@ -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 */
|
||||
}
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?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.base.698156890">
|
||||
<storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="cdt.managedbuild.toolchain.gnu.base.698156890" 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 buildProperties="" id="cdt.managedbuild.toolchain.gnu.base.698156890" name="Default" parent="org.eclipse.cdt.build.core.emptycfg">
|
||||
<folderInfo id="cdt.managedbuild.toolchain.gnu.base.698156890.240830756" name="/" resourcePath="">
|
||||
<toolChain id="cdt.managedbuild.toolchain.gnu.base.884460920" name="cdt.managedbuild.toolchain.gnu.base" superClass="cdt.managedbuild.toolchain.gnu.base">
|
||||
<targetPlatform archList="all" binaryParser="org.eclipse.cdt.core.ELF" id="cdt.managedbuild.target.gnu.platform.base.1378688406" name="Debug Platform" osList="linux,hpux,aix,qnx" superClass="cdt.managedbuild.target.gnu.platform.base"/>
|
||||
<builder id="cdt.managedbuild.target.gnu.builder.base.148477352" managedBuildOn="false" name="Gnu Make Builder.Default" superClass="cdt.managedbuild.target.gnu.builder.base"/>
|
||||
<tool id="cdt.managedbuild.tool.gnu.archiver.base.888444366" name="GCC Archiver" superClass="cdt.managedbuild.tool.gnu.archiver.base"/>
|
||||
<tool id="cdt.managedbuild.tool.gnu.cpp.compiler.base.495720762" name="GCC C++ Compiler" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.base"/>
|
||||
<tool id="cdt.managedbuild.tool.gnu.c.compiler.base.479463272" name="GCC C Compiler" superClass="cdt.managedbuild.tool.gnu.c.compiler.base"/>
|
||||
<tool id="cdt.managedbuild.tool.gnu.c.linker.base.1889370225" name="GCC C Linker" superClass="cdt.managedbuild.tool.gnu.c.linker.base"/>
|
||||
<tool id="cdt.managedbuild.tool.gnu.cpp.linker.base.475736613" name="GCC C++ Linker" superClass="cdt.managedbuild.tool.gnu.cpp.linker.base"/>
|
||||
<tool id="cdt.managedbuild.tool.gnu.assembler.base.516392826" name="GCC Assembler" superClass="cdt.managedbuild.tool.gnu.assembler.base"/>
|
||||
</toolChain>
|
||||
</folderInfo>
|
||||
</configuration>
|
||||
</storageModule>
|
||||
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
|
||||
</cconfiguration>
|
||||
</storageModule>
|
||||
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
|
||||
<project id="assign5.null.1525443262" name="assign5"/>
|
||||
</storageModule>
|
||||
<storageModule moduleId="scannerConfiguration">
|
||||
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
|
||||
</storageModule>
|
||||
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
|
||||
</cproject>
|
||||
Binary file not shown.
@@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>assign5</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>
|
||||
@@ -0,0 +1,27 @@
|
||||
all: prog1 prog2
|
||||
|
||||
prog1: dynamicArray.o main.o toDoList.o
|
||||
gcc -g -Wall -std=c99 -o prog1 dynamicArray.o toDoList.o main.o
|
||||
|
||||
prog2: dynamicArray.o main2.o toDoList.o
|
||||
gcc -g -Wall -std=c99 -o prog2 dynamicArray.o toDoList.o main2.o
|
||||
|
||||
main.o: main.c dynamicArray.h type.h toDoList.h
|
||||
gcc -g -Wall -std=c99 -c main.c
|
||||
|
||||
main2.o: main2.c dynamicArray.h type.h toDoList.h
|
||||
gcc -g -Wall -std=c99 -c main2.c
|
||||
|
||||
dynamicArray.o: dynamicArray.c dynamicArray.h
|
||||
gcc -g -Wall -std=c99 -c dynamicArray.c
|
||||
|
||||
toDoList.o: toDoList.c toDoList.h
|
||||
gcc -g -Wall -std=c99 -c toDoList.c
|
||||
|
||||
clean:
|
||||
rm dynamicArray.o
|
||||
rm toDoList.o
|
||||
rm main.o
|
||||
rm main2.o
|
||||
rm prog1
|
||||
rm prog2
|
||||
@@ -0,0 +1,572 @@
|
||||
/* dynArr.c: Dynamic Array implementation. */
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.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 */
|
||||
};
|
||||
|
||||
struct bag
|
||||
{
|
||||
struct DynArr *dynArr;
|
||||
};
|
||||
|
||||
|
||||
/* ************************************************************************
|
||||
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 capacity elements
|
||||
post: v->data is not null
|
||||
*/
|
||||
void _initDynArr(DynArr *v, int capacity)
|
||||
{
|
||||
assert(capacity > 0);
|
||||
assert(v!= 0);
|
||||
v->data = 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)
|
||||
{
|
||||
DynArr *r;
|
||||
assert(cap > 0);
|
||||
r = 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: v is not null
|
||||
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)
|
||||
{
|
||||
assert(v!=0);
|
||||
|
||||
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: v is not null
|
||||
post: the memory used by v->data is freed
|
||||
post: the memory used by d is freed
|
||||
*/
|
||||
void deleteDynArr(DynArr *v)
|
||||
{
|
||||
assert (v!= 0);
|
||||
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)
|
||||
{
|
||||
int i;
|
||||
TYPE *oldData;
|
||||
int oldSize = v->size;
|
||||
oldData = v->data;
|
||||
|
||||
printf("========Resizing========\n");
|
||||
/* Create a new dyn array with larger underlying array */
|
||||
_initDynArr(v, newCap);
|
||||
|
||||
for(i = 0; i < oldSize; i++){
|
||||
v->data[i] = oldData[i];
|
||||
}
|
||||
|
||||
v->size = oldSize;
|
||||
/* Remember, init did not free the original data */
|
||||
free(oldData);
|
||||
|
||||
#ifdef ALTERNATIVE
|
||||
int i;
|
||||
|
||||
/* Create a new underlying array*/
|
||||
TYPE *newData = (TYPE*)malloc(sizeof(TYPE)*newCap);
|
||||
assert(newData != 0);
|
||||
|
||||
/* copy elements to it */
|
||||
|
||||
for(i = 0; i < v->size; i++)
|
||||
{
|
||||
newData[i] = v->data[i];
|
||||
}
|
||||
|
||||
/* Delete the oldunderlying array*/
|
||||
free(v->data);
|
||||
/* update capacity and size and data*/
|
||||
v->data = newData;
|
||||
v->capacity = newCap;struct Task {
|
||||
char description[TASK_DESC_SIZE]; /* description of the task */
|
||||
int priority; /* task priority */
|
||||
};
|
||||
#endif
|
||||
}
|
||||
|
||||
/* 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)
|
||||
{
|
||||
assert(v!=0);
|
||||
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)
|
||||
{
|
||||
|
||||
assert(v!=0);
|
||||
|
||||
/* Check to see if a resize is necessary */
|
||||
if(v->size >= v->capacity)
|
||||
_dynArrSetCapacity(v, 2 * v->capacity);
|
||||
|
||||
v->data[v->size] = val;
|
||||
v->size++;
|
||||
|
||||
}
|
||||
|
||||
/* 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)
|
||||
{
|
||||
assert(v!=0);
|
||||
assert(pos < v->size);
|
||||
assert(pos >= 0);
|
||||
|
||||
return v->data[pos];
|
||||
}
|
||||
|
||||
/* 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)
|
||||
{
|
||||
assert(v!=0);
|
||||
assert(pos < v->size);
|
||||
assert(pos >= 0);
|
||||
v->data[pos] = val;
|
||||
}
|
||||
|
||||
/* 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)
|
||||
{
|
||||
TYPE temp;
|
||||
assert(v!=0);
|
||||
assert(i < v->size);
|
||||
assert(j < v->size);
|
||||
assert(i >= 0);
|
||||
assert(j >= 0);
|
||||
|
||||
temp = v->data[i];
|
||||
v->data[i] = v->data[j];
|
||||
v->data[j] = temp;
|
||||
|
||||
}
|
||||
|
||||
/* 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){
|
||||
int i;
|
||||
assert(v!= 0);
|
||||
assert(idx < v->size);
|
||||
assert(idx >= 0);
|
||||
|
||||
//Move all elements up
|
||||
|
||||
for(i = idx; i < v->size-1; i++){
|
||||
v->data[i] = v->data[i+1];
|
||||
}
|
||||
|
||||
v->size--;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* ************************************************************************
|
||||
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: v is not null
|
||||
post: none
|
||||
ret: >0 if empty, otherwise 0
|
||||
*/
|
||||
int isEmptyDynArr(DynArr *v)
|
||||
{
|
||||
assert(v!= 0);
|
||||
return !(v->size);
|
||||
/* alternatively:
|
||||
|
||||
if(v->size == 0)
|
||||
return 1;
|
||||
else return 0;
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/* 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)
|
||||
{
|
||||
assert(v!=0);
|
||||
addDynArr(v, val);
|
||||
}
|
||||
|
||||
/* 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)
|
||||
{
|
||||
assert(v!=0);
|
||||
assert(!isEmptyDynArr(v));
|
||||
return v->data[v->size-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)
|
||||
{
|
||||
assert(v!=0);
|
||||
assert(! isEmptyDynArr(v));
|
||||
v->size--;
|
||||
}
|
||||
|
||||
/* ************************************************************************
|
||||
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)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
assert(v!=0);
|
||||
assert(!isEmptyDynArr(v));
|
||||
|
||||
for(i = 0; i < sizeDynArr(v); i++)
|
||||
if(compare(v->data[i], val) == 0)
|
||||
return 1;
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
/* 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)
|
||||
{
|
||||
int i = 0;
|
||||
assert(v!=0);
|
||||
assert(!isEmptyDynArr(v));
|
||||
assert(containsDynArr(v,val)); /* Design decision: Error if they try to remove something not in there! */
|
||||
|
||||
for(i = 0; i < sizeDynArr(v); i++)
|
||||
if(compare(v->data[i], val) == 0)
|
||||
{
|
||||
removeAtDynArr(v,i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Copy elements from a dynamic array to another dynamic array
|
||||
|
||||
param: source pointer to the source dynamic array
|
||||
param: destination pointer to the destination dynamic array
|
||||
pre: s is not null and s is not empty
|
||||
post: destination is initialized
|
||||
post: the elements from source are copied to destination
|
||||
*/
|
||||
void copyDynArr(DynArr *source, DynArr *destination)
|
||||
{
|
||||
int i;
|
||||
assert(source->size > 0);
|
||||
_initDynArr(destination, source->capacity);
|
||||
/* copy elements to destination array */
|
||||
for(i = 0; i < source->size; i++)
|
||||
destination->data[i] = source->data[i];
|
||||
|
||||
destination->size = source->size;
|
||||
}
|
||||
|
||||
|
||||
/* ************************************************************************
|
||||
Heap-based Priority Queue Implementation
|
||||
************************************************************************ */
|
||||
|
||||
/* internal function prototypes */
|
||||
int _smallerIndexHeap(DynArr *heap, int i, int j);
|
||||
void _adjustHeap(DynArr *heap, int max, int pos);
|
||||
|
||||
/* Get the index of the smaller node between two nodes in a heap
|
||||
|
||||
param: heap pointer to the heap
|
||||
param: i index of one node
|
||||
param: j index of other node
|
||||
pre: i < size and j < size
|
||||
ret: the index of the smaller node
|
||||
*/
|
||||
int _smallerIndexHeap(DynArr *heap, int i, int j)
|
||||
{
|
||||
/* FIXME */
|
||||
/* DONE */
|
||||
assert(i < heap->size);
|
||||
assert(j < heap->size);
|
||||
if(((struct Task*)heap->data[i])->priority > ((struct Task*)heap->data[j])->priority){
|
||||
return j;
|
||||
}else{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
/* Get the first node, which has the min priority, from the heap
|
||||
|
||||
param: heap pointer to the heap
|
||||
pre: heap is not empty
|
||||
ret: value of first node
|
||||
*/
|
||||
TYPE getMinHeap(DynArr *heap)
|
||||
{
|
||||
/* FIXME */
|
||||
/* DONE */
|
||||
assert(heap != 0);
|
||||
return heap->data[0];
|
||||
}
|
||||
|
||||
/* Add a node to the heap
|
||||
|
||||
param: heap pointer to the heap
|
||||
param: node node to be added to the heap
|
||||
pre: heap is not null
|
||||
post: node is added to the heap
|
||||
*/
|
||||
void addHeap(DynArr *heap, TYPE val)
|
||||
{
|
||||
/* FIXME */
|
||||
/* DONE */
|
||||
int pos;
|
||||
int parent;
|
||||
assert(heap != 0);
|
||||
addDynArr(heap, val);
|
||||
pos = sizeDynArr(heap)-1;
|
||||
while(pos != 0){
|
||||
parent = (pos -1)/2;
|
||||
if(((struct Task*)heap->data[pos])->priority < ((struct Task*)heap->data[parent])->priority){
|
||||
swapDynArr(heap, pos, parent);
|
||||
pos = parent;
|
||||
}else{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Adjust heap to maintain heap property
|
||||
|
||||
param: heap pointer to the heap
|
||||
param: max index to adjust up to (but not included)
|
||||
param: pos position index where the adjustment starts
|
||||
pre: max <= size
|
||||
post: heap property is maintained for nodes from index pos to index max-1 (ie. up to, but not including max)
|
||||
*/
|
||||
void _adjustHeap(DynArr *heap, int max, int pos)
|
||||
{
|
||||
/* FIXME */
|
||||
/* DONE */
|
||||
int leftChild = 2*pos + 1;
|
||||
int rightChild = 2*pos + 2;
|
||||
|
||||
if(rightChild < max){
|
||||
int smallestindex = _smallerIndexHeap(heap, leftChild, rightChild);
|
||||
if(((struct Task*)heap->data[pos])->priority > ((struct Task*)heap->data[smallestindex])->priority){
|
||||
swapDynArr(heap, pos, smallestindex);
|
||||
_adjustHeap(heap, max, smallestindex);
|
||||
}
|
||||
}else if(leftChild < max){
|
||||
if(((struct Task*)heap->data[pos])->priority > ((struct Task*)heap->data[leftChild])->priority){
|
||||
swapDynArr(heap, pos, leftChild);
|
||||
_adjustHeap(heap, max, leftChild);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Remove the first node, which has the min priority, from the heap
|
||||
|
||||
param: heap pointer to the heap
|
||||
pre: heap is not empty
|
||||
post: the first node is removed from the heap
|
||||
*/
|
||||
void removeMinHeap(DynArr *heap)
|
||||
{
|
||||
/* FIXME */
|
||||
/* DONE */
|
||||
int last = sizeDynArr(heap)-1;
|
||||
//assert(last !=0);
|
||||
putDynArr(heap, 0, getDynArr(heap, last));
|
||||
removeAtDynArr(heap, last);
|
||||
_adjustHeap(heap, last, 0);
|
||||
}
|
||||
|
||||
/* builds a heap from an arbitrary dynArray
|
||||
|
||||
param: v dynamicArray
|
||||
pre: v is not empty
|
||||
post: v is a proper heap
|
||||
*/
|
||||
|
||||
void _buildHeap(DynArr *heap)
|
||||
{
|
||||
/* FIXME */
|
||||
/* DONE */
|
||||
int max = sizeDynArr(heap);
|
||||
for(int i = max/2 -1 ; i >=0 ; i--){
|
||||
_adjustHeap(heap, max, i);
|
||||
}
|
||||
}
|
||||
/*
|
||||
In-place sort of the heap
|
||||
|
||||
param: heap pointer to the heap
|
||||
pre: heap is not empty
|
||||
post: the dynArr is in reverse sorted order
|
||||
*/
|
||||
|
||||
void sortHeap(DynArr *heap)
|
||||
{
|
||||
/* FIXME */
|
||||
/* DONE */
|
||||
_buildHeap(heap);
|
||||
for(int i = sizeDynArr(heap)-1 ; i > 0 ; i--){
|
||||
swapDynArr(heap, 0, i);
|
||||
_adjustHeap(heap, i, 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
/* dynArr.h : Dynamic Array implementation. */
|
||||
#ifndef DYNAMIC_ARRAY_INCLUDED
|
||||
#define DYNAMIC_ARRAY_INCLUDED 1
|
||||
#include "type.h"
|
||||
|
||||
# ifndef TYPE
|
||||
# define TYPE void*
|
||||
# endif
|
||||
|
||||
/* function used to compare two TYPE values to each other, define this in your compare.c file */
|
||||
int compare(TYPE left, TYPE right);
|
||||
/* function used to print TYPE values, define this in your compare.c file */
|
||||
void print_type(TYPE curval);
|
||||
|
||||
typedef struct DynArr DynArr;
|
||||
struct bag;
|
||||
|
||||
/* 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);
|
||||
|
||||
|
||||
/* Heap-based Priority Queue Interface */
|
||||
TYPE getMinHeap(DynArr *heap);
|
||||
void addHeap(DynArr *heap, TYPE node);
|
||||
void removeMinHeap(DynArr *heap);
|
||||
void sortHeap(DynArr *heap);
|
||||
|
||||
|
||||
/* Utility function*/
|
||||
void copyDynArr(DynArr *source, DynArr *destination);
|
||||
|
||||
|
||||
#endif
|
||||
Binary file not shown.
164
OSU Coursework/CS 261 - Data Structures/Assignment 5/main.c
Normal file
164
OSU Coursework/CS 261 - Data Structures/Assignment 5/main.c
Normal file
@@ -0,0 +1,164 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include "toDoList.h"
|
||||
|
||||
int main (int argc, const char * argv[])
|
||||
{
|
||||
Task* newTask;
|
||||
Task* firstTask;
|
||||
char desc[TASK_DESC_SIZE], filename[50], *nlptr;
|
||||
int priority;
|
||||
char cmd = ' ';
|
||||
FILE *filePointer;
|
||||
DynArr* mainList = createDynArr(10);
|
||||
|
||||
printf("\n\n** TO-DO LIST APPLICATION **\n\n");
|
||||
|
||||
do
|
||||
{
|
||||
printf("Press:\n"
|
||||
"'l' to load to-do list from a file\n"
|
||||
"'s' to save to-do list to a file\n"
|
||||
"'a' to add a new task\n"
|
||||
"'g' to get the first task\n"
|
||||
"'r' to remove the first task\n"
|
||||
"'p' to print the list\n"
|
||||
"'e' to exit the program\n"
|
||||
);
|
||||
/* get input command (from the keyboard) */
|
||||
cmd = getchar();
|
||||
/* clear the trailing newline character */
|
||||
while (getchar() != '\n');
|
||||
|
||||
switch (cmd)
|
||||
{
|
||||
case 'a': /* add new task */
|
||||
printf("Please enter the task description: ");
|
||||
/* get task description from user input (from keyboard) */
|
||||
if (fgets(desc, sizeof(desc), stdin) != NULL)
|
||||
{
|
||||
/* remove trailing newline character */
|
||||
nlptr = strchr(desc, '\n');
|
||||
if (nlptr)
|
||||
*nlptr = '\0';
|
||||
}
|
||||
/* get task priority from user input (from keyboard) */
|
||||
do {
|
||||
printf("Please enter the task priority (0-999): ");
|
||||
scanf("%d", &priority);
|
||||
} while(!(priority >= 0 && priority <= 999));
|
||||
|
||||
/* clear the trailing newline character */
|
||||
while (getchar() != '\n');
|
||||
|
||||
/* create task and add the task to the heap */
|
||||
newTask = createTask(priority, desc);
|
||||
addHeap(mainList, newTask);
|
||||
printf("The task '%s' has been added to your to-do list.\n\n", desc);
|
||||
break;
|
||||
|
||||
case 'g': /* get the first task */
|
||||
if (sizeDynArr(mainList) > 0)
|
||||
{
|
||||
firstTask = (Task*)getMinHeap(mainList);
|
||||
printf("Your first task is: %s\n\n", firstTask->description);
|
||||
}
|
||||
else
|
||||
printf("Your to-do list is empty!\n\n");
|
||||
|
||||
break;
|
||||
|
||||
case 'r': /* remove the first task */
|
||||
if (sizeDynArr(mainList) > 0)
|
||||
{
|
||||
firstTask = (Task*)getMinHeap(mainList);
|
||||
removeMinHeap(mainList);
|
||||
printf("Your first task '%s' has been removed from the list.\n\n", firstTask->description);
|
||||
/* need to free up memory occupied by this task */
|
||||
free(firstTask);
|
||||
}
|
||||
else
|
||||
printf("Your to-do list is empty!\n\n");
|
||||
|
||||
break;
|
||||
|
||||
case 'p': /* print the list */
|
||||
if (sizeDynArr(mainList) > 0)
|
||||
{
|
||||
printList(mainList);
|
||||
}
|
||||
else
|
||||
printf("Your to-do list is empty!\n\n");
|
||||
|
||||
break;
|
||||
|
||||
|
||||
case 's': /* save the list to file */
|
||||
if (sizeDynArr(mainList) > 0)
|
||||
{
|
||||
/* get filename from user input (from keyboard) */
|
||||
printf("Please enter the filename: ");
|
||||
if (fgets(filename, sizeof(filename), stdin) != NULL)
|
||||
{
|
||||
/* remove trailing newline character */
|
||||
nlptr = strchr(filename, '\n');
|
||||
if (nlptr)
|
||||
*nlptr = '\0';
|
||||
}
|
||||
/* open the file */
|
||||
filePointer = fopen(filename, "w");
|
||||
if (filePointer == NULL) {
|
||||
fprintf(stderr, "Cannot open %s\n", filename);
|
||||
break;
|
||||
}
|
||||
/* save the list to the file */
|
||||
saveList(mainList, filePointer);
|
||||
/* close the file */
|
||||
fclose(filePointer);
|
||||
printf("The list has been saved into the file successfully.\n\n");
|
||||
}
|
||||
else
|
||||
printf("Your to-do list is empty!\n\n");
|
||||
|
||||
break;
|
||||
|
||||
case 'l': /* load the list from the file */
|
||||
printf("Please enter the filename: ");
|
||||
/* get filename from user input (from keyboard) */
|
||||
if (fgets(filename, sizeof(filename), stdin) != NULL)
|
||||
{
|
||||
/* remove trailing newline character */
|
||||
nlptr = strchr(filename, '\n');
|
||||
if (nlptr)
|
||||
*nlptr = '\0';
|
||||
}
|
||||
/* open the file */
|
||||
filePointer = fopen(filename, "r");
|
||||
if (filePointer == NULL) {
|
||||
fprintf(stderr, "Cannot open %s\n", filename);
|
||||
break;
|
||||
}
|
||||
/* load the list from the file */
|
||||
loadList(mainList, filePointer);
|
||||
/* close the file */
|
||||
fclose(filePointer);
|
||||
printf("The list has been loaded from file successfully.\n\n");
|
||||
break;
|
||||
|
||||
case 'e': /* exit the program */
|
||||
printf("Bye!\n\n");
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("What is your command anyway?\n\n" );
|
||||
break;
|
||||
}
|
||||
}
|
||||
while(cmd != 'e');
|
||||
/* delete the list */
|
||||
deleteList(mainList);
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
OSU Coursework/CS 261 - Data Structures/Assignment 5/main.o
Normal file
BIN
OSU Coursework/CS 261 - Data Structures/Assignment 5/main.o
Normal file
Binary file not shown.
74
OSU Coursework/CS 261 - Data Structures/Assignment 5/main2.c
Normal file
74
OSU Coursework/CS 261 - Data Structures/Assignment 5/main2.c
Normal file
@@ -0,0 +1,74 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include "dynamicArray.h"
|
||||
#include "toDoList.h"
|
||||
|
||||
/* NOTE: Switch between TESTSORT and TESTHEAP to test either the heap sort algoritm or the heap operations*/
|
||||
#define TESTHEAP
|
||||
|
||||
int main(int argc, const char * argv[])
|
||||
{
|
||||
Task *task1, *task2, *task3, *task4, *task5, *task6, *task7, *task8, *task9, *task10;
|
||||
DynArr *mainList;
|
||||
int i;
|
||||
mainList = createDynArr(10);
|
||||
|
||||
/* create tasks */
|
||||
task1 = createTask(9, "task 1");
|
||||
task2 = createTask(3, "task 2");
|
||||
task3 = createTask(2, "task 3");
|
||||
task4 = createTask(4, "task 4");
|
||||
task5 = createTask(5, "task 5");
|
||||
task6 = createTask(7, "task 6");
|
||||
task7 = createTask(8, "task 7");
|
||||
task8 = createTask(6, "task 8");
|
||||
task9 = createTask(1, "task 9");
|
||||
task10 = createTask(0, "task 10");
|
||||
|
||||
/* add tasks to the dynamic array */
|
||||
addHeap(mainList, task1);
|
||||
addHeap(mainList, task2);
|
||||
addHeap(mainList, task3);
|
||||
addHeap(mainList, task4);
|
||||
addHeap(mainList, task5);
|
||||
addHeap(mainList, task6);
|
||||
addHeap(mainList, task7);
|
||||
addHeap(mainList, task8);
|
||||
addHeap(mainList, task9);
|
||||
addHeap(mainList, task10);
|
||||
|
||||
#ifdef TESTHEAP
|
||||
for(i = 0; i < sizeDynArr(mainList);i++)
|
||||
printf("DynArr[%d] = %d\n", i, ((Task*)getDynArr(mainList,i))->priority);
|
||||
|
||||
|
||||
while(!isEmptyDynArr(mainList))
|
||||
{
|
||||
Task* v;
|
||||
v = getMinHeap(mainList);
|
||||
printf("Val = %s___%d\n", v->description, v->priority);
|
||||
removeMinHeap(mainList);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef TESTSORT
|
||||
|
||||
printf("Before Sort Called \n");
|
||||
for(i = 0; i < sizeDynArr(mainList);i++)
|
||||
printf("DynArr[%d] = %d\n", i, ((Task*)getDynArr(mainList,i))->priority);
|
||||
|
||||
|
||||
/* sort tasks */
|
||||
sortHeap(mainList);
|
||||
printf("After Sort Called \n");
|
||||
|
||||
/* print sorted tasks from the dynamic array */
|
||||
for(i = 0; i < sizeDynArr(mainList);i++)
|
||||
printf("DynArr[%d] = %d\n", i, ((Task*)getDynArr(mainList,i))->priority);
|
||||
|
||||
return 0;
|
||||
|
||||
#endif
|
||||
}
|
||||
BIN
OSU Coursework/CS 261 - Data Structures/Assignment 5/main2.o
Normal file
BIN
OSU Coursework/CS 261 - Data Structures/Assignment 5/main2.o
Normal file
Binary file not shown.
BIN
OSU Coursework/CS 261 - Data Structures/Assignment 5/prog1
Normal file
BIN
OSU Coursework/CS 261 - Data Structures/Assignment 5/prog1
Normal file
Binary file not shown.
BIN
OSU Coursework/CS 261 - Data Structures/Assignment 5/prog2
Normal file
BIN
OSU Coursework/CS 261 - Data Structures/Assignment 5/prog2
Normal file
Binary file not shown.
@@ -0,0 +1,223 @@
|
||||
flip ~/cs261/as5/todo_list 605% ./prog
|
||||
|
||||
|
||||
** TO-DO LIST APPLICATION **
|
||||
|
||||
Press:
|
||||
'l' to load to-do list from a file
|
||||
's' to save to-do list to a file
|
||||
'a' to add a new task
|
||||
'g' to get the first task
|
||||
'r' to remove the first task
|
||||
'p' to print the list
|
||||
'e' to exit the program
|
||||
g
|
||||
Your to-do list is empty!
|
||||
|
||||
Press:
|
||||
'l' to load to-do list from a file
|
||||
's' to save to-do list to a file
|
||||
'a' to add a new task
|
||||
'g' to get the first task
|
||||
'r' to remove the first task
|
||||
'p' to print the list
|
||||
'e' to exit the program
|
||||
a
|
||||
Please enter the task description: do assignment 5
|
||||
Please enter the task priority (0-999): 3
|
||||
The task 'do assignment 5' has been added to your to-do list.
|
||||
|
||||
Press:
|
||||
'l' to load to-do list from a file
|
||||
's' to save to-do list to a file
|
||||
'a' to add a new task
|
||||
'g' to get the first task
|
||||
'r' to remove the first task
|
||||
'p' to print the list
|
||||
'e' to exit the program
|
||||
a
|
||||
Please enter the task description: study heap-based priority queue
|
||||
Please enter the task priority (0-999): 1
|
||||
The task 'study heap-based priority queue' has been added to your to-do list.
|
||||
|
||||
Press:
|
||||
'l' to load to-do list from a file
|
||||
's' to save to-do list to a file
|
||||
'a' to add a new task
|
||||
'g' to get the first task
|
||||
'r' to remove the first task
|
||||
'p' to print the list
|
||||
'e' to exit the program
|
||||
a
|
||||
Please enter the task description: review trees for Final exam
|
||||
Please enter the task priority (0-999): 101
|
||||
The task 'review trees for Final exam' has been added to your to-do list.
|
||||
|
||||
Press:
|
||||
'l' to load to-do list from a file
|
||||
's' to save to-do list to a file
|
||||
'a' to add a new task
|
||||
'g' to get the first task
|
||||
'r' to remove the first task
|
||||
'p' to print the list
|
||||
'e' to exit the program
|
||||
g
|
||||
Your first task is: study heap-based priority queue
|
||||
|
||||
Press:
|
||||
'l' to load to-do list from a file
|
||||
's' to save to-do list to a file
|
||||
'a' to add a new task
|
||||
'g' to get the first task
|
||||
'r' to remove the first task
|
||||
'p' to print the list
|
||||
'e' to exit the program
|
||||
a
|
||||
Please enter the task description: take a nap
|
||||
Please enter the task priority (0-999): 0
|
||||
The task 'take a nap' has been added to your to-do list.
|
||||
|
||||
Press:
|
||||
'l' to load to-do list from a file
|
||||
's' to save to-do list to a file
|
||||
'a' to add a new task
|
||||
'g' to get the first task
|
||||
'r' to remove the first task
|
||||
'p' to print the list
|
||||
'e' to exit the program
|
||||
s
|
||||
Please enter the filename: todo.txt
|
||||
The list has been saved into the file successfully.
|
||||
|
||||
Press:
|
||||
'l' to load to-do list from a file
|
||||
's' to save to-do list to a file
|
||||
'a' to add a new task
|
||||
'g' to get the first task
|
||||
'r' to remove the first task
|
||||
'p' to print the list
|
||||
'e' to exit the program
|
||||
e
|
||||
Bye!
|
||||
|
||||
============
|
||||
|
||||
flip ~/cs261/as5/todo_list 613% ./prog
|
||||
|
||||
|
||||
** TO-DO LIST APPLICATION **
|
||||
|
||||
Press:
|
||||
'l' to load to-do list from a file
|
||||
's' to save to-do list to a file
|
||||
'a' to add a new task
|
||||
'g' to get the first task
|
||||
'r' to remove the first task
|
||||
'p' to print the list
|
||||
'e' to exit the program
|
||||
g
|
||||
Your to-do list is empty!
|
||||
|
||||
Press:
|
||||
'l' to load to-do list from a file
|
||||
's' to save to-do list to a file
|
||||
'a' to add a new task
|
||||
'g' to get the first task
|
||||
'r' to remove the first task
|
||||
'p' to print the list
|
||||
'e' to exit the program
|
||||
l
|
||||
Please enter the filename: todo.txt
|
||||
The list has been loaded from file successfully.
|
||||
|
||||
Press:
|
||||
'l' to load to-do list from a file
|
||||
's' to save to-do list to a file
|
||||
'a' to add a new task
|
||||
'g' to get the first task
|
||||
'r' to remove the first task
|
||||
'p' to print the list
|
||||
'e' to exit the program
|
||||
g
|
||||
Your first task is: take a nap
|
||||
|
||||
Press:
|
||||
'l' to load to-do list from a file
|
||||
's' to save to-do list to a file
|
||||
'a' to add a new task
|
||||
'g' to get the first task
|
||||
'r' to remove the first task
|
||||
'p' to print the list
|
||||
'e' to exit the program
|
||||
r
|
||||
Your first task 'take a nap' has been removed from the list.
|
||||
|
||||
Press:
|
||||
'l' to load to-do list from a file
|
||||
's' to save to-do list to a file
|
||||
'a' to add a new task
|
||||
'g' to get the first task
|
||||
'r' to remove the first task
|
||||
'p' to print the list
|
||||
'e' to exit the program
|
||||
p
|
||||
study heap-based priority queue
|
||||
|
||||
do assignment 5
|
||||
|
||||
review trees for Final exam
|
||||
|
||||
Press:
|
||||
'l' to load to-do list from a file
|
||||
's' to save to-do list to a file
|
||||
'a' to add a new task
|
||||
'g' to get the first task
|
||||
'r' to remove the first task
|
||||
'p' to print the list
|
||||
'e' to exit the program
|
||||
r
|
||||
Your first task 'study heap-based priority queue' has been removed from the list.
|
||||
|
||||
Press:
|
||||
'l' to load to-do list from a file
|
||||
's' to save to-do list to a file
|
||||
'a' to add a new task
|
||||
'g' to get the first task
|
||||
'r' to remove the first task
|
||||
'p' to print the list
|
||||
'e' to exit the program
|
||||
r
|
||||
Your first task 'do assignment 5' has been removed from the list.
|
||||
|
||||
Press:
|
||||
'l' to load to-do list from a file
|
||||
's' to save to-do list to a file
|
||||
'a' to add a new task
|
||||
'g' to get the first task
|
||||
'r' to remove the first task
|
||||
'p' to print the list
|
||||
'e' to exit the program
|
||||
r
|
||||
Your first task 'review trees for Final exam' has been removed from the list.
|
||||
|
||||
Press:
|
||||
'l' to load to-do list from a file
|
||||
's' to save to-do list to a file
|
||||
'a' to add a new task
|
||||
'g' to get the first task
|
||||
'r' to remove the first task
|
||||
'p' to print the list
|
||||
'e' to exit the program
|
||||
r
|
||||
Your to-do list is empty!
|
||||
|
||||
Press:
|
||||
'l' to load to-do list from a file
|
||||
's' to save to-do list to a file
|
||||
'a' to add a new task
|
||||
'g' to get the first task
|
||||
'r' to remove the first task
|
||||
'p' to print the list
|
||||
'e' to exit the program
|
||||
e
|
||||
Bye!
|
||||
180
OSU Coursework/CS 261 - Data Structures/Assignment 5/toDoList.c
Normal file
180
OSU Coursework/CS 261 - Data Structures/Assignment 5/toDoList.c
Normal file
@@ -0,0 +1,180 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include "toDoList.h"
|
||||
|
||||
|
||||
/* Create a task from the description and the priority
|
||||
|
||||
param: priority priority of the task
|
||||
param: desc pointer to the description string
|
||||
pre: none
|
||||
post: none
|
||||
ret: a task with description and priority
|
||||
*/
|
||||
Task* createTask (int priority, char *desc)
|
||||
{
|
||||
/* FIXME */
|
||||
/* DONE */
|
||||
struct Task *temp = malloc(sizeof(struct Task));
|
||||
strcpy(temp->description, desc);
|
||||
temp->priority = priority;
|
||||
return temp;
|
||||
}
|
||||
|
||||
/* Save the list to a file
|
||||
|
||||
param: heap pointer to the list
|
||||
param: filePtr pointer to the file to which the list is saved
|
||||
pre: The list is not empty
|
||||
post: The list is saved to the file in tab-delimited format.
|
||||
Each line in the file stores a task, starting with the
|
||||
task priority, followed by a tab character (\t), and
|
||||
the task description.
|
||||
|
||||
The tasks are not necessarily stored in the file in
|
||||
priority order.
|
||||
|
||||
*/
|
||||
void saveList(DynArr *heap, FILE *filePtr)
|
||||
{
|
||||
int i;
|
||||
Task* task;
|
||||
assert(sizeDynArr(heap) > 0);
|
||||
for(i = 0; i < sizeDynArr(heap); i++)
|
||||
{
|
||||
task = getDynArr(heap, i);
|
||||
fprintf(filePtr, "%d\t%s\n", task->priority, task->description);
|
||||
}
|
||||
}
|
||||
|
||||
/* Load the list from a file
|
||||
|
||||
param: heap pointer to the list
|
||||
param: filePtr pointer to the file
|
||||
pre: none
|
||||
post: The tasks are retrieved from the file and are added to the list.
|
||||
Refer to the saveList() function for the format of tasks in the file
|
||||
*/
|
||||
void loadList(DynArr *heap, FILE *filePtr)
|
||||
{
|
||||
Task* task;
|
||||
char line[100]; /* Assume lines < 100 */
|
||||
char desc[TASK_DESC_SIZE], *nlptr;
|
||||
int priority;
|
||||
|
||||
/* Read the priority first, then the description.
|
||||
* fgets() is used to read string with spaces
|
||||
*/
|
||||
#ifdef NOTDEF
|
||||
while (fscanf(filePtr, "%d\t", &priority) != EOF)
|
||||
{
|
||||
/* fgets() stops reading at \n character */
|
||||
fgets(desc, sizeof(desc), filePtr);
|
||||
/* remove trailing newline character */
|
||||
nlptr = strchr(desc, '\n');
|
||||
if (nlptr)
|
||||
*nlptr = '\0';
|
||||
|
||||
task = createTask(priority, desc);
|
||||
addHeap(heap, task);
|
||||
}
|
||||
#endif
|
||||
|
||||
while(fgets(line, sizeof(line), filePtr) != 0)
|
||||
{
|
||||
sscanf(line, "%d\t%[^\n]", &priority, desc);
|
||||
task = createTask(priority, desc);
|
||||
addHeap(heap, task);
|
||||
} /* should use feof to make sure it found eof and not error*/
|
||||
|
||||
}
|
||||
|
||||
/* Print the list
|
||||
|
||||
param: heap pointer to the list
|
||||
pre: the list is not empty
|
||||
post: The tasks from the list are printed out in priority order.
|
||||
The tasks are not removed from the list.
|
||||
*/
|
||||
void printList(DynArr *heap)
|
||||
{
|
||||
/* FIXME: Write this */
|
||||
/* DONE: May need adjustment */
|
||||
Task *temp;
|
||||
|
||||
for(int i = 0 ; i < sizeDynArr(heap) ; i++){
|
||||
temp = (Task*)getDynArr(heap, i);
|
||||
printf("Priority: %d\tDescription: %s\n", temp->priority, temp->description);
|
||||
}
|
||||
}
|
||||
|
||||
/* Delete the list
|
||||
|
||||
param: heap pointer to the list
|
||||
post: The tasks from the list are removed and their occupied memories are freed
|
||||
|
||||
*/
|
||||
void deleteList(DynArr *heap)
|
||||
{
|
||||
|
||||
Task* task;
|
||||
while(sizeDynArr(heap) > 0)
|
||||
{
|
||||
/* get the task */
|
||||
task = getMinHeap(heap);
|
||||
/* remove the task */
|
||||
removeMinHeap(heap);
|
||||
/* free the task */
|
||||
free(task);
|
||||
}
|
||||
/* free the heap */
|
||||
deleteDynArr(heap);
|
||||
}
|
||||
|
||||
/* Compare two tasks by priority
|
||||
|
||||
param: left first task
|
||||
param: right second task
|
||||
pre: none
|
||||
post: none
|
||||
ret: -1 if priority of left < priority of right
|
||||
1 if priority of left > priority of right
|
||||
0 if priority of left = priority of right
|
||||
*/
|
||||
/*Define this function, type casting the value of void * to the desired type.
|
||||
The current definition of TYPE in dynamicArray.h is void*, which means that left and
|
||||
right are void pointers. To compare left and right, you should first cast
|
||||
left and right to the corresponding pointer type (struct Task*), and then
|
||||
compare the values pointed by the casted pointers.
|
||||
|
||||
DO NOT compare the addresses pointed by left and right, i.e. "if (left < right)",
|
||||
which is really wrong.
|
||||
*/
|
||||
int compare(TYPE left, TYPE right)
|
||||
{
|
||||
/*FIXME: write this*/
|
||||
/* DONE */
|
||||
struct Task *temp1 = (struct Task*)left;
|
||||
struct Task *temp2 = (struct Task*)right;
|
||||
|
||||
if(temp1->priority < temp2->priority){
|
||||
return -1;
|
||||
}else if(temp1->priority > temp2->priority){
|
||||
return 1;
|
||||
}else{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/*Define this function, type casting the value of void * to the desired type*/
|
||||
void print_type(TYPE curval)
|
||||
{
|
||||
/*FIXME: write this*/
|
||||
/* DONE */
|
||||
Task* data1;
|
||||
data1 = (Task*)curval;
|
||||
printf("Priority: %d\tDescription: %s\n", data1->priority, data1->description);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef __TODOLIST_H
|
||||
#define __TODOLIST_H
|
||||
|
||||
#include "dynamicArray.h"
|
||||
#include "type.h"
|
||||
|
||||
Task* createTask (int priority, char *desc);
|
||||
|
||||
void saveList(DynArr *heap, FILE *filePtr);
|
||||
|
||||
void loadList(DynArr *heap, FILE *filePtr);
|
||||
|
||||
void printList(DynArr *heap);
|
||||
|
||||
void deleteList(DynArr *heap);
|
||||
|
||||
#endif
|
||||
BIN
OSU Coursework/CS 261 - Data Structures/Assignment 5/toDoList.o
Normal file
BIN
OSU Coursework/CS 261 - Data Structures/Assignment 5/toDoList.o
Normal file
Binary file not shown.
@@ -0,0 +1,3 @@
|
||||
1 pet kitty
|
||||
101 review trees for Midterm 2
|
||||
3 do assignment 5
|
||||
16
OSU Coursework/CS 261 - Data Structures/Assignment 5/type.h
Normal file
16
OSU Coursework/CS 261 - Data Structures/Assignment 5/type.h
Normal file
@@ -0,0 +1,16 @@
|
||||
/* You can modify the structure to store whatever you'd like in your dynamic array.
|
||||
We define struct Task for the to-do list application in this example */
|
||||
|
||||
#ifndef __TYPE_H
|
||||
#define __TYPE_H
|
||||
|
||||
#define TASK_DESC_SIZE 128
|
||||
|
||||
struct Task {
|
||||
char description[TASK_DESC_SIZE]; /* description of the task */
|
||||
int priority; /* task priority */
|
||||
};
|
||||
|
||||
typedef struct Task Task;
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user