(C++)数据结构之括号匹配的问题?

<pre name="code" class="cpp">//-------------------------------------------【夜雨蝉鸣】----------------------------------------------
#include <iostream>
#include <stack>
#include <iterator>
using namespace std;
void main()
{
    stack<char> mystackda; //大括号
    stack<char> mystackxiao; //小括号
    stack<char> mystackjian; //尖括号
    
    char s[30]="ABCD{()PPPL<>()}";
    
    char *p=&s[0];

    while( (*p)!='\0' ) //遍历字符串s
    {
        switch(*p)
        {
		case '{':
			mystackda.push('{');
			break;
			case '}':
			if (mystackda.empty())
			{
			goto over;
			}
			mystackda.pop();
			break;
		
		  case '<':
			mystackjian.push('<');
			break;
			case '>':
			
			if (mystackjian.empty())
			{
			goto over;
			}
			mystackjian.pop();
			break;
		case '(':
			mystackxiao.push('(');
			
			break;
			case ')':
			
			if (mystackxiao.empty())
			{
			goto over;
			}
			mystackxiao.pop();
			break;
		
	}

	 p++;

     }


        if ( mystackda.empty() && mystackjian.empty() && mystackxiao.empty() )
        {
		 cout<<"\n字符匹配"<<endl;
        }
        else
        {
		     over:    cout<<"\n字符不匹配"<<endl;
        }
        

        system("pause");

}
    原文作者:括号匹配问题
    原文地址: https://blog.csdn.net/forider/article/details/47298331
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞