作业7可用

This commit is contained in:
iridiumR 2021-11-12 17:06:39 +08:00
parent 9f20e81f4d
commit f1c974dcc2
4 changed files with 127 additions and 74 deletions

View file

@ -1,8 +1,68 @@
#include <stdio.h> #include <stdio.h>
#include "stack.h" #include <string>
#include <iostream>
#include "stack.hpp"
int main() int main()
{ {
printf("g") Stack<int> 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<char> 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");
}

View file

@ -1,27 +0,0 @@
#include "stack.h"
// template <class T>
// void Stack<T>::push (T const& elem)
// {
// _elems.add(elem);
// }
// template <class T>
// T Stack<T>::pop ()
// {
// if (_elems.getused()) {
// return NULL;
// }
// // 删除最后一个元素
// return _elems.remove_back()
// }
// template <class T>
// T Stack<T>::top () const
// {
// if (_elems.getused()) {
// return NULL;
// }
// // 返回最后一个元素的副本
// return _elems[_elems.getused()-1];
// }

View file

@ -1,42 +0,0 @@
template <class T>
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 <class T>
// void Vec<T>::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;
// }
};

62
ex7/stack.hpp Normal file
View file

@ -0,0 +1,62 @@
#ifndef _GLIBCXX_IOSTREAM
#include <iostream>
#endif
template <class T>
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;
}
};