diff --git a/thu_dsa/chp4/stackApp.cpp b/thu_dsa/chp4/stackApp.cpp new file mode 100644 index 0000000..c892cea --- /dev/null +++ b/thu_dsa/chp4/stackApp.cpp @@ -0,0 +1,55 @@ +#include "../chp2/Fib.h" +#include "stackApp.h" + +/* +* @brief : convert a decimal digit n to any base(2 <= base <= 36) +* @args : +* @return: return the result in the form of char* +*/ +char* convert(__int64 n, int base){ + Stack charStack; + while(n != 0){ + charStack.push(digits[n % base]); + n /= base; + } + char* res = new char[charStack.getSize() + 1]; + int pos = 0; + while (!charStack.empty()) res[pos++] = charStack.pop(); + res[pos] = '\0'; + return res; +} + +/* +* @brief : to judge if an expression of matches +* @args : exp +* @return: return true if matches +*/ +bool paren(const char exp[]){ + Stack parenStack; + char currParen, popped; + for(int ix = 0; (currParen = exp[ix]) != '\0'; ++ix){ + switch(currParen){ + case '(': + case '[': + case '{': + parenStack.push(currParen); + break; + + case ')': + if (parenStack.empty()) return false; + if (parenStack.pop() != '(') return false; + break; + + case ']': + if (parenStack.empty()) return false; + if (parenStack.pop() != '[') return false; + break; + + case '}': + if (parenStack.empty()) return false; + if (parenStack.pop() != '{') return false; + break; + } + } + return parenStack.empty()? true : false; +} diff --git a/thu_dsa/chp4/stackApp.h b/thu_dsa/chp4/stackApp.h new file mode 100644 index 0000000..427e644 --- /dev/null +++ b/thu_dsa/chp4/stackApp.h @@ -0,0 +1,24 @@ +#ifndef STACKAPP_H_ +#define STACKAPP_H_ + +#include "Stack.h" + +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' }; +/* + * @brief : convert a decimal digit n to any base(2 <= base <= 36) + * @args : + * @return: return the result in the form of char* + */ +char* convert(__int64 n, int base); + +/* +* @brief : to judge if an expression of parens matches +* @args : exp +* @return: return true if matches +*/ +bool paren(const char exp[]); + +#endif diff --git a/thu_dsa/chp4/testStack.cpp b/thu_dsa/chp4/testStack.cpp index 6f70756..ffd3f9e 100644 --- a/thu_dsa/chp4/testStack.cpp +++ b/thu_dsa/chp4/testStack.cpp @@ -9,7 +9,7 @@ void test_basic(); void test_capacity(); -int main(){ +int stack_test_main(){ test_basic(); test_capacity(); diff --git a/thu_dsa/chp4/testStackApp.cpp b/thu_dsa/chp4/testStackApp.cpp new file mode 100644 index 0000000..55b8228 --- /dev/null +++ b/thu_dsa/chp4/testStackApp.cpp @@ -0,0 +1,33 @@ +#include "../chp2/Fib.h" +#include "stackApp.h" +#include +#include + +using std::cout; +using std::endl; + +void test_convert(); +void test_paren(); + +int main(){ + cout << "Running tests......" << endl; + test_convert(); + test_paren(); + + cout << "All tests passed." << endl; + system("pause"); + return 0; +} + +void test_convert(){ + assert(strcmp(convert(1314, 16), "522") == 0); + assert(strcmp(convert(520, 2), "1000001000") == 0); + assert(strcmp(convert(520, 16), "208") == 0); +} + +void test_paren(){ + assert(paren("(())") == true); + assert(paren("()()") == true); + assert(paren("[(])") == false); + assert(paren("{[()]{[({})]}}") == true); +}