Skip to content

Commit

Permalink
Remove manual stack code for vector
Browse files Browse the repository at this point in the history
 * found a new set of bugs along the way
  • Loading branch information
lefticus committed Nov 17, 2023
1 parent 136c224 commit ee2aea4
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 143 deletions.
125 changes: 63 additions & 62 deletions legacy/Evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,24 @@ int precedence(int op) {
}


void evaluateStacks(Stack * numbers, Stack * operators, int num) {
void evaluateStacks(Stack<RationalNumber> &numbers, Stack<int> &operators, int num) {
int eatOpenParan = FALSE;
int cont = TRUE;
RationalNumber * operand1;
RationalNumber * operand2;

int startPrec = precedence(*(int *)(operators->peek()));
int startPrec = precedence(*operators.peek());

if ( *(int *)(operators->peek()) == CLOSE_PARAN ) {
if ( *operators.peek() == CLOSE_PARAN ) {
eatOpenParan = TRUE;
delete operators->pop();
operators.pop();
}

while ( (operators.peek() != NULL) && cont) {


while ( (operators->peek() != NULL) && cont) {


switch( *(int *)(operators->peek()) ) {
switch( *operators.peek() ) {
case OPEN_PARAN:
if (eatOpenParan == TRUE) {
delete operators->pop();
operators.pop();
cont = FALSE;
}

Expand All @@ -64,43 +60,44 @@ void evaluateStacks(Stack * numbers, Stack * operators, int num) {

break;

case PLUS_SIGN:
delete operators->pop();
operand2 = (RationalNumber *)numbers->pop();
operand1 = (RationalNumber *)numbers->pop();

operand1->add(*operand2);
delete operand2;
numbers->push(operand1);
case PLUS_SIGN:{
operators.pop();
RationalNumber operand2 = numbers.pop();
RationalNumber operand1 = numbers.pop();

operand1.add(operand2);
numbers.push(operand1);
break;
}

case MINUS_SIGN:
delete operators->pop();
operand1 = (RationalNumber *)numbers->pop();
operand1->negate();
case MINUS_SIGN: {
operators.pop();
RationalNumber operand1 = numbers.pop();
operand1.negate();

numbers->push(operand1);
numbers.push(operand1);
break;
}

case MULTIPLY_SIGN:
delete operators->pop();
operand2 = (RationalNumber *)numbers->pop();
operand1 = (RationalNumber *)numbers->pop();
case MULTIPLY_SIGN: {
operators.pop();
RationalNumber operand2 = numbers.pop();
RationalNumber operand1 = numbers.pop();

operand1->multiply(*operand2);
delete operand2;
numbers->push(operand1);
operand1.multiply(operand2);
numbers.push(operand1);
break;
}

case DIVIDE_SIGN:
delete operators->pop();
operand2 = (RationalNumber *)numbers->pop();
operand1 = (RationalNumber *)numbers->pop();
case DIVIDE_SIGN: {
operators.pop();
RationalNumber operand2 = numbers.pop();
RationalNumber operand1 = numbers.pop();

operand1->divide(*operand2);
delete operand2;
numbers->push(operand1);
operand1.divide(operand2);
numbers.push(operand1);
break;
}
}

if (num == 1)
Expand All @@ -113,8 +110,8 @@ void evaluateStacks(Stack * numbers, Stack * operators, int num) {


RationalNumber evaluateExpression(StringTokenizer & st) {
Stack * operators = new Stack();
Stack * numbers = new Stack();
Stack<int> operators;
Stack<RationalNumber> numbers;

int negateNext = FALSE;

Expand All @@ -124,75 +121,79 @@ RationalNumber evaluateExpression(StringTokenizer & st) {

std::string next = st.nextToken();

int * value = new int;
int value = 0;

if (!next.empty()) {
switch(next[0]) {
case '+':
*value = PLUS_SIGN;
value = PLUS_SIGN;
op = TRUE;
break;
case '/':
*value = DIVIDE_SIGN;
value = DIVIDE_SIGN;
op = TRUE;
break;
case '-':
*value = MINUS_SIGN;
value = MINUS_SIGN;
op = TRUE;
break;
case '*':
*value = MULTIPLY_SIGN;
value = MULTIPLY_SIGN;
op = TRUE;
break;
case ')':
*value = CLOSE_PARAN;
value = CLOSE_PARAN;
op = TRUE;
break;
case '(':
*value = OPEN_PARAN;
value = OPEN_PARAN;
op = TRUE;
break;

default:
*value = atoi(next.c_str());
value = atoi(next.c_str());
op = FALSE;
numbers->push(new RationalNumber(*value, 1));
numbers.push(RationalNumber(value, 1));
break;
}

if (op) {

int *plus = new int;
*plus = PLUS_SIGN;
int plus = PLUS_SIGN;

switch (*value) {
switch (value) {
case OPEN_PARAN:
operators->push(value);
operators.push(value);
break;
case CLOSE_PARAN:
operators->push(value);
operators.push(value);
evaluateStacks(numbers, operators, 0);
break;
default:
if (operators->peek() != NULL) {
if ( precedence(*value) >= precedence(*(int *)(operators->peek())) ) {
operators->push(value);
if (operators.peek() != NULL) {
if ( precedence(value) >= precedence(*operators.peek()) ) {
operators.push(value);
}
if ( precedence(*value) < precedence(*(int *)(operators->peek())) ) {
if ( precedence(value) < precedence(*operators.peek()) ) {
evaluateStacks(numbers, operators, 0);
operators->push(value);
operators.push(value);
}
}
else
operators->push(value);
operators.push(value);
break;
}
}
}
}

if (operators->peek() != NULL)
if (operators.peek() != NULL)
evaluateStacks(numbers, operators, 0);
return *(RationalNumber *)numbers->peek();

if (numbers.peek() != NULL) {
return *numbers.peek();
} else {
return RationalNumber(0,0);
}
}

2 changes: 1 addition & 1 deletion legacy/Evaluator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

int precedence(int op);
RationalNumber evaluateExpression(StringTokenizer &st);
void evaluateStacks(Stack * numbers, Stack * operators, int num);
void evaluateStacks(Stack<RationalNumber> &numbers, Stack<int> &operators, int num);

#endif
58 changes: 0 additions & 58 deletions legacy/Stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,4 @@
#include "Stack.h"
#include <stdio.h>

Stack::Stack() {
head=NULL;
}

Stack::~Stack() {
deleteStack(head);
}

void Stack::deleteStack(stackElem * head) {
if (head!=NULL) {
if(head->next != NULL) {
deleteStack(head->next);
}
delete head->data;
delete head;
}
}

void * Stack::pop() {
void * toReturn = NULL;
stackElem * newhead;

if (head != NULL) {
toReturn = head->data;
newhead = head->next;
delete head;
head = newhead;
}

return toReturn;
}

int Stack::moreThanOne() {
if (head != NULL) {
if (head->next!=NULL) {
return 1;
}
}
else
return 0;

return 0;
}

void Stack::push(void * newElem) {
stackElem * newelem;

newelem = new stackElem;
newelem->data = newElem;
newelem->next = head;
head = newelem;
}

void * Stack::peek() {
if (head != NULL)
return head->data;
else
return NULL;
}
51 changes: 31 additions & 20 deletions legacy/Stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,44 @@

// Stack.h

#include <vector>
#include <cassert>

/**
* A Class that allows allows void * to be
* pushed onto an popped off of a stack.
*/
* A Class that allows allows void * to be
* pushed onto an popped off of a stack.
*/

template<typename Contained>
class Stack
{
public:
Stack();
~Stack();
void * pop();
void push(void * stackElem);
void * peek();
int moreThanOne();

private:

Stack(){}

struct stackElem {
void * data;
stackElem * next;
};

void deleteStack(stackElem * head);
stackElem * head;

};
Contained pop() {
assert(!data.empty());
Contained toReturn = data.back();
data.pop_back();
return toReturn;
}

bool moreThanOne() const {
return data.size() > 1;
}

void push(const Contained &newElem) {
data.push_back(newElem);
}

const Contained * peek() const {
if (data.empty())
return NULL;
else
return &data.back();
}
private:
std::vector<Contained> data;
};

#endif
Binary file modified legacy/infiz.ncb
Binary file not shown.
Binary file modified legacy/infiz.opt
Binary file not shown.
7 changes: 5 additions & 2 deletions legacy/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ int main() {
run_test("(3 / 2)", RationalNumber(3, 2));
run_test("(4 / 2)", RationalNumber(4, 2));
run_test("(4 / 2) * 5", RationalNumber(20, 2));
run_test("(1 / 2) / 3", RationalNumber(1, 6));
run_test("((1 + 2) + 3) + 4", RationalNumber(10, 1));

// we need to evaluate this for later, I don't think
// this is correct, but we're capturing the current state of things.

// these fail because we expect grouping into 2's
run_test("1 / 2 / 3", RationalNumber(3, 2));
run_test("1 + 2 + 3 + 4", RationalNumber(10, 1));
return 0;
}
Loading

0 comments on commit ee2aea4

Please sign in to comment.