diff --git a/legacy/Evaluator.cpp b/legacy/Evaluator.cpp index 0ee0541..acb758f 100644 --- a/legacy/Evaluator.cpp +++ b/legacy/Evaluator.cpp @@ -34,28 +34,24 @@ int precedence(int op) { } -void evaluateStacks(Stack * numbers, Stack * operators, int num) { +void evaluateStacks(Stack &numbers, Stack &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; } @@ -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) @@ -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 operators; + Stack numbers; int negateNext = FALSE; @@ -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); + } } diff --git a/legacy/Evaluator.hpp b/legacy/Evaluator.hpp index 5766bbf..c7986b2 100644 --- a/legacy/Evaluator.hpp +++ b/legacy/Evaluator.hpp @@ -7,6 +7,6 @@ int precedence(int op); RationalNumber evaluateExpression(StringTokenizer &st); -void evaluateStacks(Stack * numbers, Stack * operators, int num); +void evaluateStacks(Stack &numbers, Stack &operators, int num); #endif \ No newline at end of file diff --git a/legacy/Stack.cpp b/legacy/Stack.cpp index bd6e52c..bc981f5 100644 --- a/legacy/Stack.cpp +++ b/legacy/Stack.cpp @@ -3,62 +3,4 @@ #include "Stack.h" #include -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; -} \ No newline at end of file diff --git a/legacy/Stack.h b/legacy/Stack.h index 13cee08..6ae35c2 100644 --- a/legacy/Stack.h +++ b/legacy/Stack.h @@ -3,33 +3,44 @@ // Stack.h +#include +#include /** - * 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 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 data; +}; + #endif \ No newline at end of file diff --git a/legacy/infiz.ncb b/legacy/infiz.ncb index fd03c83..00267f3 100644 Binary files a/legacy/infiz.ncb and b/legacy/infiz.ncb differ diff --git a/legacy/infiz.opt b/legacy/infiz.opt index 1a4f65a..4a38684 100644 Binary files a/legacy/infiz.opt and b/legacy/infiz.opt differ diff --git a/legacy/tests.cpp b/legacy/tests.cpp index 4749c0d..dc27ad6 100644 --- a/legacy/tests.cpp +++ b/legacy/tests.cpp @@ -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; } \ No newline at end of file diff --git a/legacy/tests/tests.plg b/legacy/tests/tests.plg index 9cdf2d3..6ef6b92 100644 --- a/legacy/tests/tests.plg +++ b/legacy/tests/tests.plg @@ -3,9 +3,47 @@
 

Build Log

+--------------------Configuration: InfixEvaluator - Win32 Debug-------------------- +

+

Command Lines

+Creating temporary file "C:\Users\Jason\AppData\Local\Temp\RSPAC52.tmp" with contents +[ +/nologo /MLd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /Fp"Debug/InfixEvaluator.pch" /YX /Fo"Debug/" /Fd"Debug/" /FD /GZ /c +"C:\Users\Jason\Documents\GitHub\infiz\legacy\Evaluator.cpp" +"C:\Users\Jason\Documents\GitHub\infiz\legacy\RationalNumber.cpp" +"C:\Users\Jason\Documents\GitHub\infiz\legacy\Stack.cpp" +"C:\Users\Jason\Documents\GitHub\infiz\legacy\StringTokenizer.cpp" +] +Creating command line "cl.exe @C:\Users\Jason\AppData\Local\Temp\RSPAC52.tmp" +Creating command line "link.exe -lib /nologo /out:"Debug\InfixEvaluator.lib" .\Debug\Evaluator.obj .\Debug\RationalNumber.obj .\Debug\Stack.obj .\Debug\StringTokenizer.obj " +

Output Window

+Compiling... +Evaluator.cpp +RationalNumber.cpp +Stack.cpp +StringTokenizer.cpp +Creating library... +

--------------------Configuration: tests - Win32 Debug--------------------

Command Lines

+Creating temporary file "C:\Users\Jason\AppData\Local\Temp\RSPAE28.tmp" with contents +[ +/nologo /MLd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /Fp"Debug/tests.pch" /YX /Fo"Debug/" /Fd"Debug/" /FD /GZ /c +"C:\Users\Jason\Documents\GitHub\infiz\legacy\tests.cpp" +] +Creating command line "cl.exe @C:\Users\Jason\AppData\Local\Temp\RSPAE28.tmp" +Creating temporary file "C:\Users\Jason\AppData\Local\Temp\RSPAE29.tmp" with contents +[ +kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /incremental:yes /pdb:"Debug/tests.pdb" /debug /machine:I386 /out:"Debug/tests.exe" /pdbtype:sept +.\Debug\tests.obj +\Users\Jason\Documents\GitHub\infiz\legacy\InfixEvaluator\Debug\InfixEvaluator.lib +] +Creating command line "link.exe @C:\Users\Jason\AppData\Local\Temp\RSPAE29.tmp" +

Output Window

+Compiling... +tests.cpp +Linking...