From f1c974dcc2f7c6cabf11ca6267e21e25aed59bfd Mon Sep 17 00:00:00 2001 From: iridiumR Date: Fri, 12 Nov 2021 17:06:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A7=E5=8F=AF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ex7/ex7.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++++++---- ex7/stack.cpp | 27 -------------------- ex7/stack.h | 42 ------------------------------- ex7/stack.hpp | 62 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 127 insertions(+), 74 deletions(-) delete mode 100644 ex7/stack.cpp delete mode 100644 ex7/stack.h create mode 100644 ex7/stack.hpp diff --git a/ex7/ex7.cpp b/ex7/ex7.cpp index 2907e1c..62b1fc8 100644 --- a/ex7/ex7.cpp +++ b/ex7/ex7.cpp @@ -1,8 +1,68 @@ #include -#include "stack.h" - +#include +#include +#include "stack.hpp" int main() { - printf("g") - -} \ No newline at end of file + Stack stack; + int op = 0; + while (1) + { + op = 0; + printf("选择操作:1入栈2出栈3深度4是否空5栈顶6退出\n"); + scanf("%d", &op); + switch (op) + { + case 1: + printf("输入\n"); + scanf("%d", &op); + stack.push(op); + break; + case 2: + printf("出%d \n", stack.pop()); + break; + case 3: + printf("深度%d \n", stack.depth()); + break; + case 4: + printf("%d \n", stack.empty()); + break; + case 5: + printf("%d \n", stack.top()); + break; + case 6: + goto Out; + default: + break; + } + } +Out: + Stack S; + std::string exp; + bool flag = true; + printf("输入算式\n"); + std::cin >> exp; + for (int i = 0; i < sizeof(exp); i++) + { + switch (exp[i]) + { + case '(': + case '[': + case '{': + S.push(exp[i]); + break; + case ')': + '(' != S.pop() ? flag = false : 0; + break; + case ']': + '[' != S.pop() ? flag = false : 0; + break; + case '}': + '}' != S.pop() ? flag = false : 0; + break; + default: + break; + } + } + (flag==true&&S.empty()) ? printf("匹配成功\n") : printf("匹配失败\n"); +} diff --git a/ex7/stack.cpp b/ex7/stack.cpp deleted file mode 100644 index 9b642c5..0000000 --- a/ex7/stack.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "stack.h" - -// template -// void Stack::push (T const& elem) -// { -// _elems.add(elem); -// } - -// template -// T Stack::pop () -// { -// if (_elems.getused()) { -// return NULL; -// } -// // 删除最后一个元素 -// return _elems.remove_back() -// } - -// template -// T Stack::top () const -// { -// if (_elems.getused()) { -// return NULL; -// } -// // 返回最后一个元素的副本 -// return _elems[_elems.getused()-1]; -// } \ No newline at end of file diff --git a/ex7/stack.h b/ex7/stack.h deleted file mode 100644 index 710cb66..0000000 --- a/ex7/stack.h +++ /dev/null @@ -1,42 +0,0 @@ -template -class Stack { - private: - T _v[10]; - int _len=10; - int used=0; - public: - void push(T const&); - T pop(); - T top() const; - bool empty() const; - - protected: -// void expand() -// { - -// _len = _len * 2; -// T *p = new T[_len]; - -// for (int i = 0; i <= _used; i++) -// p[i] = _v[i]; - -// delete[] _v; -// _v = p; -// } - -// template -// void Vec::shrink() -// { -// if (((double)_used / (double)_len) >= 0.25) -// return; - -// _len = _len >> 1; -// T *p = new T[_len]; - -// for (int i = 0; i <= _used; i++) -// p[i] = _v[i]; - -// delete[] _v; -// _v = p; -// } -}; \ No newline at end of file diff --git a/ex7/stack.hpp b/ex7/stack.hpp new file mode 100644 index 0000000..0aa9f7a --- /dev/null +++ b/ex7/stack.hpp @@ -0,0 +1,62 @@ +#ifndef _GLIBCXX_IOSTREAM +#include +#endif + +template +class Stack +{ +private: + T *_v; + int _len = 10; + int _used = 0; + +public: + Stack() + { + _v = new T[_len]; + } + + void push(T const &elem) + { + _v[_used++] = elem; + expand(); + } + + T pop() { return !_used ? NULL : _v[--_used]; } + + T top() const { return !_used ? NULL : _v[_used - 1]; } + + bool empty() { return _used ? false : true; } + + int depth() { return _used; }; + +protected: + void expand() + { + if (((double)_used / (double)_len) <= 0.75) + return; + _len = _len * 2; + T *p = new T[_len]; + + for (int i = 0; i <= _used; i++) + p[i] = _v[i]; + + delete[] _v; + _v = p; + } + + void shrink() + { + if (((double)_used / (double)_len) >= 0.25) + return; + + _len = _len >> 1; + T *p = new T[_len]; + + for (int i = 0; i <= _used; i++) + p[i] = _v[i]; + + delete[] _v; + _v = p; + } +};