matchbrackets.cpp

来自「关于栈的操作的小程序」· C++ 代码 · 共 103 行

CPP
103
字号
// MatchBrackets.cpp : implementation file
//

#include "stdafx.h"
#include "CStackOperation.h"
#include "MatchBrackets.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMatchBrackets dialog


CMatchBrackets::CMatchBrackets(CWnd* pParent /*=NULL*/)
	: CDialog(CMatchBrackets::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMatchBrackets)
	m_strExpr = _T("");
	m_strResult = _T("");
	//}}AFX_DATA_INIT
}


void CMatchBrackets::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMatchBrackets)
	DDX_Text(pDX, IDC_EXPRESSION, m_strExpr);
	DDX_Text(pDX, IDC_RESULT, m_strResult);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMatchBrackets, CDialog)
	//{{AFX_MSG_MAP(CMatchBrackets)
	ON_BN_CLICKED(IDC_JUDGE, OnJudge)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMatchBrackets message handlers

void CMatchBrackets::OnJudge() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	s.Clear();
	char x;
	CString str = m_strExpr;
	int len = str.GetLength();
	for(int i=0; i<len; i++)
	{
		char c = str.GetAt(i);
		if(c == '(' || c == '[' || c == '{')
		{
		s.Push(c);
		}
		else if(c==']')
		{
			x = s.Pop();
			if(x != '[')
			{
				m_strResult = "括号不匹配!";
				UpdateData(FALSE);
				return;
			}
		}
		else if(c==')')
		{
			x = s.Pop();
			if(x != '(')
			{
				m_strResult = "括号不匹配!";
				UpdateData(FALSE);
				return;
			}
		}
		else if(c=='}')
		{
			x = s.Pop();
			if(x != '{')
			{
				m_strResult = "括号不匹配!";
				UpdateData(FALSE);
				return;
			}
		}
	}
	if(s.IsEmpty())
	{
		m_strResult = "括号匹配!";
	}
	else
	{
		m_strResult = "括号不匹配!";
	}
	UpdateData(FALSE);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?