喵星人
发表于 2013-9-4 23:03:56
不明白啊,怎样才知道他们匹配呢?
bbyuner0220
发表于 2013-9-13 14:10:29
学习一下看看代码了
nan_nan121
发表于 2013-9-21 18:00:04
看一看啊。。。。。。
小睿webster
发表于 2013-10-22 09:33:16
学习一下!
岁月如歌
发表于 2013-11-26 20:21:55
楼主加油,鱼C加油!我们都看好你哦!
doublelight
发表于 2013-11-30 16:00:23
学习下小甲鱼老师的精神
沁蓝心
发表于 2013-12-3 10:58:31
希望有帮助,5鱼币还是很贵的
易数2
发表于 2013-12-4 12:14:07
楼主加油,鱼C加油!我们都看好你哦!
涛--
发表于 2013-12-16 23:36:25
做过了对答案来的
huge200890
发表于 2014-1-19 14:42:13
正在跟着视频学习!!! 赞!
柠“萌”圆
发表于 2014-2-8 10:41:27
#ifndef STACK_H_
#define STACK_H_
#include <new>
#include <initializer_list>
template <typename T>
class Stack
{
private:
T * base;
T * top;
unsigned int StackSize; //表示栈内剩余容量
public:
Stack(const unsigned int size, const T * begin, const T * end);
Stack(const unsigned int size, const T arr[], const int n);
Stack(const unsigned int size, const std::initializer_list<T> & l);
Stack(const unsigned int size = 100);
~Stack() {delete [] base;}
bool Push(const T & item);
bool Pop(T & e);
bool Clear();
bool Is_Empty()const {return base == top;}
unsigned int Remaining()const {return StackSize;}
unsigned int StackLen()const {return top - base;}
unsigned int Size()const {return StackSize + (top - base);}
};
template <typename T>
Stack<T>::Stack(const unsigned int size, const T * begin, const T * end) : StackSize(size)
{
if (StackSize < end - begin)
StackSize = end - begin;
try {
base = top = new T;
}
catch (std::bad_alloc & ba) {
base = top = nullptr;
StackSize = 0;
throw;
}
for (T * p = begin; p != end; p++)
Push(*p);
}
template <typename T>
Stack<T>::Stack(const unsigned int size, const T arr[], const int n) : StackSize(size)
{
if (StackSize < n)
StackSize = n + 1;
try {
base = top = new T;
}
catch (std::bad_alloc & ba) {
base = top = nullptr;
StackSize = 0;
throw;
}
for (int i = 0; i < n; i++)
Push(arr);
}
template <typename T>
Stack<T>::Stack(const unsigned int size, const std::initializer_list<T> & l) : StackSize(size)
{
int i = 0;
try{
base = top = new T;
}
catch (std::bad_alloc & ba) {
base = top = nullptr;
StackSize = 0;
throw;
}
for (auto p = l.begin(); p != l.end(); p++, i++)
Push(*p);
}
template <typename T>
Stack<T>::Stack(const unsigned int size) : StackSize(size)
{
try {
base = top = new T;
}
catch (std::bad_alloc & ba) {
base = top = nullptr;
StackSize = 0;
throw;
}
}
template <typename T>
bool Stack<T>::Push(const T & item)
{
if (StackSize == 0)
{
T * temp = base;
unsigned int size = top - base;
StackSize = size + 10;
try {
base = top = new T;
}
catch (std::bad_alloc & ba) {
StackSize = 0;
base = top = nullptr;
delete [] temp;
return false;
}
for (int i = 0; i < size; i++)
Push(temp);
Push(item);
delete [] temp;
return true;
}
*(top++) = item;
StackSize--;
return true;
}
template <typename T>
bool Stack<T>::Pop(T & e)
{
if (top && !Is_Empty())
{
e = *(--top);
StackSize++;
return true;
}
return false;
}
template <typename T>
bool Stack<T>::Clear()
{
if(top)
{
StackSize += top - base;
top = base;
return true;
}
return false;
}
#endif#include <iostream>
#include "Stack.h"
bool is_match (const char c1, const char c2);
int main()
{
using std::cout;
using std::endl;
using std::cin;
cout << "请输入一组括号,程序将判断是否匹配: ";
char ch;
Stack<char>par(5);
while (cin.get(ch) && ch != '\n')
{
if (ch == '(' || ch == '<'
|| ch == '{')
par.Push(ch);
else if (ch == ')' || ch == '>'
|| ch == '}')
{
char temp = ch;
par.Pop(ch);
if (!is_match(ch, temp))
{
cout << "您输入的括号不完全匹配.\n";
return 0;
}
}
}
cout << "您输入的括号完全匹配!\n";
return 0;
}
bool is_match(const char c1, const char c2)
{
switch (c1)
{
case '(': return c2 == ')';
break;
case '<': return c2 == '>';
break;
case '{': return c2 == '}';
break;
}
return false;
}
じO-联合
发表于 2014-2-16 17:35:43
真是难得给力的帖子啊。
jaywei518
发表于 2014-2-23 11:41:49
sdfsdfsdfsdfasdfs
zhaopengfei
发表于 2014-2-28 20:49:42
强烈支持楼主ing……
zhaopengfei
发表于 2014-2-28 21:45:59
我怎么感觉巨亏呢,这是个什么东西呀?
602968956
发表于 2014-3-23 18:35:52
真是难得给力的帖子啊。
a372187663
发表于 2014-3-27 22:15:14
我来虚席的!!!
未ぃ明ゞ
发表于 2014-3-29 00:47:38
膜拜小甲鱼{:7_181:}
有风声
发表于 2014-3-30 13:34:55
强烈支持楼主ing……
immature
发表于 2014-3-31 16:16:35
谢谢了这个来学习了
页:
1
[2]
3
4
5
6
7
8
9
10
11