判断括号是否闭合

思路

()[]{}匹配的赋值为相反数,使用栈进行判断,如果匹配(栈顶和当前元素的值为相反数)就出栈,否则入栈,最终看栈是否为空,如果为空,则是对应的,我考虑到的情况有 类似 ()[] , [()] , (){[]}

具体代码

/*给定一个只包括 '(',')','{','}','[',']' 的字符串,
判断是否有效。必须以顺序闭合,注意顺序,闭合。*/

#include<iostream>
using namespace std;
#include <string>

class mystack
{
	public:
		int _maxsize;//最大容量
		int _nowpoint;//栈顶指针
		int *_stack;
		mystack(int maxsize);//构造函数
		bool isempty();//目前栈是否为空
		bool push(int value);//入栈
		bool pop(int &value);//出栈
		bool getstacktop(int &value);//获取栈顶元素
		void showstack();
		~mystack();//析构函数
};

mystack::mystack(int maxsize)
{
	this->_maxsize = maxsize;
	this->_nowpoint = -1;
	this->_stack = new int[maxsize];
	cout << "mystack构造函数被调用" << endl;
}

bool mystack::push(int value)//入栈
{
	if (this->_nowpoint != this->_maxsize - 1)//stack还没满
	{
		this->_nowpoint++;
		this->_stack[_nowpoint] = value;
		return true;
	}
	else
	{
		cout << "栈满了,入栈失败";
		return false;
	}

}

bool mystack::pop(int &value)//出栈
{
	if (_nowpoint != -1)
	{
		value = _stack[_nowpoint];
		_nowpoint--;
		return true;
	}
	else
	{
		cout << "栈为空,出栈失败";
		return false;
	}
}

void mystack::showstack()
{
	cout << "当前栈的情况" << endl;
	for (int i = 0; i <= _nowpoint; i++)
	{
		cout << this->_stack[i] << "  ";
	}
}
bool mystack::isempty()//目前栈是否为空
{
	if (this->_nowpoint == -1)
		return true;
	else
		return false;
}

bool mystack::getstacktop(int &value)//获取栈顶元素
{
	if (this->_nowpoint!=-1)
	{
		value= _stack[_nowpoint];
		return true;
	}
	else
	{
		return false;
	}
}

mystack::~mystack()//析构函数
{
	cout << "mystack析构啦" << endl;
	delete[] this->_stack;
}

struct mychar
{
	char _ch;
	int _numvalue;
	mychar(char ch,int numvalue)
	{
		_ch = ch;
		_numvalue = numvalue;
	}
};

bool judgeBihe(const string s, mychar *chararray)//判断s是否闭合
{
	mystack mystack1(s.size());
	if (s.size() == 0)
	{
		cout << "字符串为空";
		return true;
	}
	if (s.size() == 1)
	{
		cout << "只有一个字符,不对称"<<endl;
		return false;
	}

		for (int i = 0; i < s.size(); i++)
		{
			int value=0;//随便赋值的,获取每个输入的每个字符对应的值
			for (int j = 0; j < 6; j++)//找出符号对应的值
			{
				if (s[i] == chararray[j]._ch)
				{
					value = chararray[j]._numvalue;
					cout << "值是:" << value<<endl;
					break;
				}
					
			}

			if (i == 0)//第一个字符
			{
				mystack1.push(value);
			}
			else//不是第一个字符的情况
			{
				int stacktop=0;
				mystack1.getstacktop(stacktop);
				if (value+stacktop==0)
				{
					mystack1.pop(value);
				}
				else
				{
					mystack1.push(value);
				}
			}

		}
	
		mystack1.showstack();
	return mystack1.isempty();
	
}

int main()
{
	mychar chararray[6] = { mychar('(',1),mychar(')',-1) ,mychar('[',2) ,mychar(']',-2) ,mychar('{',3) ,mychar('}',-3) };
	string s;
	cout << "输入符号串:";
	while (1)
	{
		getline(cin, s);
		int flag;
		for (int i = 0; i < s.size(); i++)//检查输入是否是合法的括号对
		{
			flag = 0;
			for (int j = 0; j < 6; j++)
			{
				if (s[i] == chararray[j]._ch)
					flag = 1;
			}
			if (flag == 0)
			{
				cout << "输入不合规,请重新输入:";
				break;
			}
		}
		if (flag == 1)//输入合规就可以退出循环
			break;
	}
	

    if (judgeBihe(s,chararray))
		cout << "括号闭合";
	else
		cout << "括号不闭合";

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值