zoukankan      html  css  js  c++  java
  • 数据结构-栈(三)-c++代码--判断括号是否匹配

    本文目的:

    利用栈机制实现字符串内括号是否匹配的判定。

    算法如下:

    定义栈1存字符串中读取的符号;
    定义栈2存放预期望的右括号符号;
    for(整个字符串)
    {
    	第一个元素是否是预期期望的右括号?
    	不是:
    	{
    		是否为左括号:
    		是:
    		{
    			判断是什么类型的左括号,然后存入栈1;将该类型对应的右括号存入栈2,将预期望符号改为该右括号;
    		}		
    		不是
    		{
    			判定字符串不匹配!
    			程序结束
    		}
    	}
    	是预期望右括号:
    	{
    		将栈1和栈2的首元素出栈;
    		将预期望符号改为栈2的新的首个字符。	
    	}
    }
    循环结束后判定栈1是否为空
    是
    {
    	字符串匹配!
    }
    不是
    {
    	字符串不匹配
    }
    

    代码如下,附上栈模板的实现

    //栈模板.h
    #pragma once
    #include<iostream>
    #include<string>
    using namespace std;
    template<class T>
    class MyStack
    {
    public:
    	MyStack(int size);//构造
    	~MyStack();//析构
    	bool s_empty();//判空
    	bool s_full();//判满
    	void s_in(T t);//进栈
    	T s_out();//出栈
    	void s_traverse(bool n);//遍历
    	void s_clear();//清空
    	int s_length();//长度
    
    private:
    	T* sta;
    	int s_num;
    	int s_size;
    };
    template<class T>
    MyStack<T>::MyStack(int size)
    {
    	s_size = size;
    	s_num = 0;
    	sta = new T[size];
    }
    template<class T>
    MyStack<T>::~MyStack()
    {
    	delete []sta;
    	sta = NULL;
    }
    template<class T>
    bool MyStack<T>::s_empty()//判空
    {
    	return s_num == 0 ? true : false;
    }
    template<class T>
    bool MyStack<T>::s_full()//判满
    {
    	return s_num == s_size ? true : false;
    }
    template<class T>
    void MyStack<T>::s_in(T t)//进栈
    {
    	if (!s_full())
    	{
    		sta[s_num] = t;
    		s_num++;
    	}
    	else
    	{
    		cout << "Can not add any more ingredients!" << endl;
    	}	
    }
    
    template<class T>
    T MyStack<T>::s_out()//出栈
    {
    	if (!s_empty())
    	{
    		s_num--;
    		//cout << sta[s_num] << endl;
    		return sta[s_num];
    	}
    	else
    	{
    		cout << "There is no ingredient!" << endl;
    	}
    }
    
    template<class T>
    void MyStack<T>::s_traverse(bool n)
    {
    	for (int i = 0; i < s_num; i++)
    	{
    		if (n)
    		{
    			cout << sta[(s_num-i-1)];
    		}
    		else
    		{
    			cout << sta[i];
    		}
    		
    	}
    }//遍历
    
    template<class T>
    void MyStack<T>::s_clear()
    {
    	s_num = 0;
    }//清空
    
    template<class T>
    int MyStack<T>::s_length()
    {
    	return s_num;
    }//长度
    
    //main.cpp
    #include"MyStack.h"
    //# define __debug__
    
    int main(int argv, char* argc[])
    {
    	MyStack<char> *p1 = new MyStack<char>(20);
    	MyStack<char> *p2 = new MyStack<char>(20);
    	char c_need = 0;
    	char test[] = "[[[()]";
    #ifdef __debug__
    	cout << strlen(test) << endl;
    #endif
    	for (int i = 0; i < strlen(test); i++)
    	{
    		if (test[i] != c_need)
    		{
    			p1->s_in(test[i]);
    			switch (test[i])
    			{ 
    				case '(':
    				{
    					p2->s_in(')');
    					c_need = ')';
    					break;
    				}					
    				case '[':
    				{
    					p2->s_in(']');
    					c_need = ']';
    					break;
    				}					
    				default:
    					cout << "括号不匹配哟!" << endl;
    					return 0;
    			}
    		}
    		else
    		{
    			char mm;
    			mm=p1->s_out();
    			mm = p2->s_out();
    			if (p2->s_empty())
    			{
    				c_need = 0;
    			}
    			else
    			{
    				c_need = p2->s_out();
    				p2->s_in(c_need);
    			}
    		}
    	}
    	if (!p1->s_empty())
    	{
    		cout << "括号不匹配!" << endl;
    	}
    	else
    	{
    		cout << "括号匹配" << endl;
    	}
    	return 0;
    }
    
    /*
    输出结果:
    括号不匹配!
    */
    
    Higher you climb, more view you will see.
  • 相关阅读:
    UI 简单练习(联动实例)
    软件工程与计算机科学
    中文编程
    自我介绍
    曾经的梦想
    即时通讯研究学习
    即时通讯研究学习
    创业
    2015-08-12-火影
    看<后海不是海>的随想
  • 原文地址:https://www.cnblogs.com/yyfighting/p/12500617.html
Copyright © 2011-2022 走看看