From a72ce4925bce4031334c7c4ad4ecad439ebef9db Mon Sep 17 00:00:00 2001 From: Shine wOng <1551885@tongji.edu.cn> Date: Thu, 23 May 2019 19:16:46 +0800 Subject: [PATCH] add toPostfix function(in stackApp.cpp). Create Queue.h, and passed all tests. --- thu_dsa/chp4/Queue.h | 15 +++++++++ thu_dsa/chp4/stackApp.cpp | 51 +++++++++++++++++++++++++++++ thu_dsa/chp4/stackApp.h | 14 ++++++-- thu_dsa/chp4/testQueue.cpp | 61 +++++++++++++++++++++++++++++++++++ thu_dsa/chp4/testStackApp.cpp | 8 ++++- 5 files changed, 145 insertions(+), 4 deletions(-) create mode 100644 thu_dsa/chp4/Queue.h create mode 100644 thu_dsa/chp4/testQueue.cpp diff --git a/thu_dsa/chp4/Queue.h b/thu_dsa/chp4/Queue.h new file mode 100644 index 0000000..5397bd3 --- /dev/null +++ b/thu_dsa/chp4/Queue.h @@ -0,0 +1,15 @@ +#ifndef QUEUE_H_ +#define QUEUE_H_ + +#include "../chp3/List.h" + +template +class Queue: public List{ +public: + void enqueue(T const &elem) { push_back(elem); } + T dequeue() { return pop_front()->val; } + T front() { return first()->val; } + T rear() { return last()->val; } +}; + +#endif diff --git a/thu_dsa/chp4/stackApp.cpp b/thu_dsa/chp4/stackApp.cpp index 1e4f669..61e3864 100644 --- a/thu_dsa/chp4/stackApp.cpp +++ b/thu_dsa/chp4/stackApp.cpp @@ -1,5 +1,6 @@ #include "../chp2/Fib.h" #include "stackApp.h" +#include #include /*-----------build-in functions----------*/ @@ -9,6 +10,7 @@ inline char orderBetween(char optr1, char optr2); int findOptr(char optr); double cal(char optr, double opnd); double cal(double opnd1, char optr, double opnd2); +void append(char* &des, char c); /* * @brief : convert a decimal digit n to any base(2 <= base <= 36) @@ -106,6 +108,42 @@ double evaluate(char* infixExpr){ return opnd.pop(); } +/* + * @brief : to convert an infix expression to a postfix one + * @args : infixExpr + * @return: return the converted postfix expression + * @others: only consider valid infix expression + */ +char* toPostfix(char* infixExpr){ + char* postfixExpr = ""; + Stack optr; + optr.push('\0'); + while(!optr.empty()){ + if (isdigit(*infixExpr)) append(postfixExpr, *infixExpr++); + else{ + if (*infixExpr == ' ' || *infixExpr == '\t') { // skip white spaces + ++infixExpr; + continue; + } + switch(orderBetween(optr.top(), *infixExpr)){ + case '>': + append(postfixExpr, optr.pop()); + break; + + case '<': + optr.push(*infixExpr++); + break; + + case '=': + optr.pop(); + ++infixExpr; + break; + } + } + } + return postfixExpr; +} + /*-----------build-in functions----------*/ int readNumber(char* &expr){ int res = 0; @@ -178,3 +216,16 @@ double cal(double opnd1, char optr, double opnd2) { } return res; } + +/* + * @brief : to append a char to an array of char, even if the array is already full + * @args : char* des, char c + * @return: void + * @others: the array can automatically expand itself when needed + */ +void append(char* &des, char c){ + int n = strlen(des); + char* tmp = des; + des = new char[n + 2]; + sprintf(des, "%s%c\0", tmp, c); +} diff --git a/thu_dsa/chp4/stackApp.h b/thu_dsa/chp4/stackApp.h index 5d44422..f81b88e 100644 --- a/thu_dsa/chp4/stackApp.h +++ b/thu_dsa/chp4/stackApp.h @@ -6,9 +6,9 @@ #define NOPTR 9 static char digits[] = { '0','1','2','3','4','5','6','7','8','9', - 'a','b','c','d','e','f','g','h','i','j', - 'k','l','m','n','o','p','q','r','s','t', - 'u','v','w','x','y','z' }; + 'a','b','c','d','e','f','g','h','i','j', + 'k','l','m','n','o','p','q','r','s','t', + 'u','v','w','x','y','z' }; /* * @brief : convert a decimal digit n to any base(2 <= base <= 36) * @args : @@ -44,4 +44,12 @@ const char pri[NOPTR][NOPTR] = { */ double evaluate(char* infixExpr); +/* + * @brief : to convert an infix expression to a postfix one + * @args : infixExpr + * @return: return the converted postfix expression + * @others: only consider valid infix expression + */ +char* toPostfix(char* infixExpr); + #endif diff --git a/thu_dsa/chp4/testQueue.cpp b/thu_dsa/chp4/testQueue.cpp new file mode 100644 index 0000000..67d2a77 --- /dev/null +++ b/thu_dsa/chp4/testQueue.cpp @@ -0,0 +1,61 @@ +#include "Queue.h" +#include +#include +#include +using std::cout; +using std::endl; + +void test_constructor(); +void test_enqueue(); +void test_dequeue(); + +int main(){ + clock_t begin = clock(); + cout << "Running tests." << endl; + + test_constructor(); + test_enqueue(); + test_dequeue(); + + clock_t end = clock(); + cout << "All tests passed." << "Running time: " << end - begin << "ticks." << endl; + + system("pause"); + return 0; +} + +void test_constructor(){ + Queue q1; + Queue q2; + Queue q3; + Queue < Queue> q4; + assert(q1.getSize() == 0); + assert(q2.getSize() == 0); + assert(q3.getSize() == 0); + assert(q4.getSize() == 0); +} + +void test_enqueue(){ + Queue q; + q.enqueue(2); + assert(q.getSize() == 1); + q.enqueue(5); + assert(q.getSize() == 2); + assert(q.front() == 2); + assert(q.rear() == 5); +} + +void test_dequeue(){ + Queue q; + q.enqueue(2); + q.enqueue(5); + q.enqueue(7); + assert(q.getSize() == 3); + assert(q.front() == 2); + assert(q.rear() == 7); + + assert(q.dequeue() == 2); + assert(q.dequeue() == 5); + assert(q.dequeue() == 7); + assert(q.getSize() == 0); +} diff --git a/thu_dsa/chp4/testStackApp.cpp b/thu_dsa/chp4/testStackApp.cpp index 1f8f9ee..c408dcd 100644 --- a/thu_dsa/chp4/testStackApp.cpp +++ b/thu_dsa/chp4/testStackApp.cpp @@ -9,12 +9,14 @@ using std::endl; void test_convert(); void test_paren(); void test_evaluate(); +void test_toPostfix(); -int main(){ +int stackApp_test_main(){ cout << "Running tests......" << endl; test_convert(); test_paren(); test_evaluate(); + test_toPostfix(); cout << "All tests passed." << endl; system("pause"); @@ -38,3 +40,7 @@ void test_evaluate(){ assert(evaluate("2*5+(3+4-2*7)/2") == 6.5); assert(evaluate("(0!+ 1) * 2 ^ (3!+ 4) - (5!- 67 - (8 + 9))") == 2012); } + +void test_toPostfix(){ + assert(strcmp(toPostfix("(0! + 1) ^ (2 * 3! + 4 - 5)"),"0!1+23!*4+5-^") == 0); +}