add toPostfix function(in stackApp.cpp). Create Queue.h, and passed all tests.
This commit is contained in:
15
thu_dsa/chp4/Queue.h
Normal file
15
thu_dsa/chp4/Queue.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#ifndef QUEUE_H_
|
||||
#define QUEUE_H_
|
||||
|
||||
#include "../chp3/List.h"
|
||||
|
||||
template <typename T>
|
||||
class Queue: public List<T>{
|
||||
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
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "../chp2/Fib.h"
|
||||
#include "stackApp.h"
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
|
||||
/*-----------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<char> 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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
61
thu_dsa/chp4/testQueue.cpp
Normal file
61
thu_dsa/chp4/testQueue.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
#include "Queue.h"
|
||||
#include <time.h>
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
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<int> q1;
|
||||
Queue<char> q2;
|
||||
Queue<double> q3;
|
||||
Queue < Queue<int>> q4;
|
||||
assert(q1.getSize() == 0);
|
||||
assert(q2.getSize() == 0);
|
||||
assert(q3.getSize() == 0);
|
||||
assert(q4.getSize() == 0);
|
||||
}
|
||||
|
||||
void test_enqueue(){
|
||||
Queue<int> 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<int> 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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user