Converted push/pop to pass by ref

This commit is contained in:
Jon Lewis
2018-10-16 20:58:34 -05:00
parent f8f3dfbac8
commit fa90ad17db
2 changed files with 44 additions and 29 deletions

59
ee.c
View File

@@ -1,34 +1,37 @@
#include "ee.h"
int error = 0;
void printStack(STACK);
int
main (int argc, const char* argv[]){
main (const int argc, const char* argv[]){
STACK expr = NULL;
const char** endPtr = argv+argc,
** arg = argv+1 ;
while ((arg < endPtr) && (!error)){
if (arrity(**arg))
expr = push(evaluate(&expr, **arg), expr);
push(evaluate(&expr, **arg), &expr);
else if(isNum(*arg))
expr = push(strtod(*arg,NULL), expr);
push(strtod(*arg,NULL), &expr);
else
error |= ERR_INVALID_INPUT;
arg++;
arg += 1;
}
if (expr == NULL)
error |= ERR_NS_OPERANDS;
else if (expr->tail!=NULL)
else if ((*expr).tail != NULL)
error |= ERR_NS_OPERATORS;
report(expr);
report(&expr);
return error;
}
@@ -131,8 +134,8 @@ operands(STACK* target_stack, int size){
double * endPtr = _operands + size;
while ((tPtr < endPtr) && !error)
{
*tPtr = peek((*target_stack));
(*target_stack) = pop(*target_stack);
*tPtr = peek((target_stack));
pop(target_stack);
tPtr += 1;
}
@@ -148,13 +151,14 @@ fact(int n){
}
void
report(STACK expr){
report(STACK* expr){
/*
* reportErrors() prints error strings according to the value of global
* bitvector error if it is nonzero, or the result of evaluation if error
* is zero.
*/
//printStack(expr);
if (error & ERR_INVALID_INPUT)
printf("Error: Unexpected input.\n");
@@ -171,20 +175,31 @@ report(STACK expr){
printf("%g\n", peek(expr));
}
STACK
push(double datum, STACK tail){
void
printStack(STACK s){
/* Given a stack, print "datum tail"*/
while (s != NULL){
printf("%g\t%p\n", s->datum, s->tail);
s = s->tail;
}
printf("%s\n", "-----");
}
void
push(double datum, STACK* tail){
/* Given a double value (datum), and a STACK (tail),
* push(datum,tail) is the STACK resulting from
* prepending datum to the STACK tail.
*/
NODE* new_node = (NODE*)malloc(sizeof(NODE));
(*new_node).datum = datum;
(*new_node).tail = tail;
return new_node;
(*new_node).tail = *tail;
(*tail) = new_node;
}
STACK
pop(STACK target_stack){
void
pop(STACK* target_stack){
/* Given a STACK (target_stack), pop(target_stack)
* is the STACK resulting from the removal of the first
* item in target_stack;
@@ -192,17 +207,17 @@ pop(STACK target_stack){
STACK tail = NULL;
if (target_stack != NULL){
tail = (*target_stack).tail;
free(target_stack);
if ((*target_stack) != NULL){
tail = (*target_stack)->tail;
free(*target_stack);
}
return tail;
(*target_stack) = tail;
}
double
peek(STACK target_stack){
peek(STACK* target_stack){
/* Given a STACK (target_stack), peek(STACK)
* is the first element's datum.
*/
return (target_stack)? (*target_stack).datum : 0;
return (*target_stack)? (*target_stack)->datum : 0;
}

8
ee.h
View File

@@ -18,9 +18,9 @@ struct EE_STACK_NODE {
typedef struct EE_STACK_NODE NODE;
typedef struct EE_STACK_NODE* STACK;
STACK push(double,STACK);
STACK pop(STACK);
double peek(STACK);
void push(double,STACK*);
void pop(STACK*);
double peek(STACK*);
int isNum(const char*);
int arrity(const char);
@@ -29,7 +29,7 @@ double evaluate(STACK*, char);
double* operands(STACK*, int);
double fact(int n);
void report();
void report(STACK*);