Converted push/pop to pass by ref
This commit is contained in:
65
ee.c
65
ee.c
@@ -1,34 +1,37 @@
|
|||||||
#include "ee.h"
|
#include "ee.h"
|
||||||
int error = 0;
|
int error = 0;
|
||||||
|
void printStack(STACK);
|
||||||
int
|
int
|
||||||
main (int argc, const char* argv[]){
|
main (const int argc, const char* argv[]){
|
||||||
|
|
||||||
STACK expr = NULL;
|
STACK expr = NULL;
|
||||||
|
|
||||||
const char** endPtr = argv+argc,
|
const char** endPtr = argv+argc,
|
||||||
** arg = argv+1 ;
|
** arg = argv+1 ;
|
||||||
|
|
||||||
while ((arg < endPtr) && (!error)){
|
while ((arg < endPtr) && (!error)){
|
||||||
|
|
||||||
if (arrity(**arg))
|
if (arrity(**arg))
|
||||||
expr = push(evaluate(&expr, **arg), expr);
|
push(evaluate(&expr, **arg), &expr);
|
||||||
|
|
||||||
else if(isNum(*arg))
|
else if(isNum(*arg))
|
||||||
expr = push(strtod(*arg,NULL), expr);
|
push(strtod(*arg,NULL), &expr);
|
||||||
|
|
||||||
else
|
else
|
||||||
error |= ERR_INVALID_INPUT;
|
error |= ERR_INVALID_INPUT;
|
||||||
|
|
||||||
arg++;
|
|
||||||
|
arg += 1;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (expr == NULL)
|
if (expr == NULL)
|
||||||
error |= ERR_NS_OPERANDS;
|
error |= ERR_NS_OPERANDS;
|
||||||
|
|
||||||
else if (expr->tail!=NULL)
|
else if ((*expr).tail != NULL)
|
||||||
error |= ERR_NS_OPERATORS;
|
error |= ERR_NS_OPERATORS;
|
||||||
|
|
||||||
report(expr);
|
report(&expr);
|
||||||
|
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
@@ -131,8 +134,8 @@ operands(STACK* target_stack, int size){
|
|||||||
double * endPtr = _operands + size;
|
double * endPtr = _operands + size;
|
||||||
while ((tPtr < endPtr) && !error)
|
while ((tPtr < endPtr) && !error)
|
||||||
{
|
{
|
||||||
*tPtr = peek((*target_stack));
|
*tPtr = peek((target_stack));
|
||||||
(*target_stack) = pop(*target_stack);
|
pop(target_stack);
|
||||||
|
|
||||||
tPtr += 1;
|
tPtr += 1;
|
||||||
}
|
}
|
||||||
@@ -148,13 +151,14 @@ fact(int n){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
report(STACK expr){
|
report(STACK* expr){
|
||||||
/*
|
/*
|
||||||
* reportErrors() prints error strings according to the value of global
|
* reportErrors() prints error strings according to the value of global
|
||||||
* bitvector error if it is nonzero, or the result of evaluation if error
|
* bitvector error if it is nonzero, or the result of evaluation if error
|
||||||
* is zero.
|
* is zero.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
//printStack(expr);
|
||||||
if (error & ERR_INVALID_INPUT)
|
if (error & ERR_INVALID_INPUT)
|
||||||
printf("Error: Unexpected input.\n");
|
printf("Error: Unexpected input.\n");
|
||||||
|
|
||||||
@@ -171,38 +175,49 @@ report(STACK expr){
|
|||||||
printf("%g\n", peek(expr));
|
printf("%g\n", peek(expr));
|
||||||
}
|
}
|
||||||
|
|
||||||
STACK
|
void
|
||||||
push(double datum, STACK tail){
|
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),
|
/* Given a double value (datum), and a STACK (tail),
|
||||||
* push(datum,tail) is the STACK resulting from
|
* push(datum,tail) is the STACK resulting from
|
||||||
* prepending datum to the STACK tail.
|
* prepending datum to the STACK tail.
|
||||||
*/
|
*/
|
||||||
NODE* new_node = (NODE*)malloc(sizeof(NODE));
|
NODE* new_node = (NODE*)malloc(sizeof(NODE));
|
||||||
(*new_node).datum = datum;
|
(*new_node).datum = datum;
|
||||||
(*new_node).tail = tail;
|
(*new_node).tail = *tail;
|
||||||
return new_node;
|
(*tail) = new_node;
|
||||||
}
|
}
|
||||||
|
|
||||||
STACK
|
void
|
||||||
pop(STACK target_stack){
|
pop(STACK* target_stack){
|
||||||
/* Given a STACK (target_stack), pop(target_stack)
|
/* Given a STACK (target_stack), pop(target_stack)
|
||||||
* is the STACK resulting from the removal of the first
|
* is the STACK resulting from the removal of the first
|
||||||
* item in target_stack;
|
* item in target_stack;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
STACK tail = NULL;
|
STACK tail = NULL;
|
||||||
|
|
||||||
if (target_stack != NULL){
|
if ((*target_stack) != NULL){
|
||||||
tail = (*target_stack).tail;
|
tail = (*target_stack)->tail;
|
||||||
free(target_stack);
|
free(*target_stack);
|
||||||
}
|
}
|
||||||
return tail;
|
(*target_stack) = tail;
|
||||||
}
|
}
|
||||||
|
|
||||||
double
|
double
|
||||||
peek(STACK target_stack){
|
peek(STACK* target_stack){
|
||||||
/* Given a STACK (target_stack), peek(STACK)
|
/* Given a STACK (target_stack), peek(STACK)
|
||||||
* is the first element's datum.
|
* is the first element's datum.
|
||||||
*/
|
*/
|
||||||
return (target_stack)? (*target_stack).datum : 0;
|
return (*target_stack)? (*target_stack)->datum : 0;
|
||||||
}
|
}
|
||||||
|
|||||||
8
ee.h
8
ee.h
@@ -18,9 +18,9 @@ struct EE_STACK_NODE {
|
|||||||
typedef struct EE_STACK_NODE NODE;
|
typedef struct EE_STACK_NODE NODE;
|
||||||
typedef struct EE_STACK_NODE* STACK;
|
typedef struct EE_STACK_NODE* STACK;
|
||||||
|
|
||||||
STACK push(double,STACK);
|
void push(double,STACK*);
|
||||||
STACK pop(STACK);
|
void pop(STACK*);
|
||||||
double peek(STACK);
|
double peek(STACK*);
|
||||||
|
|
||||||
int isNum(const char*);
|
int isNum(const char*);
|
||||||
int arrity(const char);
|
int arrity(const char);
|
||||||
@@ -29,7 +29,7 @@ double evaluate(STACK*, char);
|
|||||||
double* operands(STACK*, int);
|
double* operands(STACK*, int);
|
||||||
double fact(int n);
|
double fact(int n);
|
||||||
|
|
||||||
void report();
|
void report(STACK*);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user