(C++) 分隔符匹配

匹配(),{},[],如果遇到/*,要跳过/*....*/之间的所有字符,如果没有*/,不匹配。


#include <iostream>
#include <string>


using namespace std;


template <class T>
class Stack{
private:
    int maxSize;
    int top;
    T * st;
public:
    Stack(int size){
        maxSize = size;
        top = -1;
        st = new T[maxSize];
    }


    bool isEmpty()const;
    bool isFull()const;
    void push(const T & t);
    void pop();
    T & Top();
    int Size()const;
    ~Stack(){}
};




template<class T>
bool Stack<T>::isEmpty()const{
    return top==-1;
}
template<class T>
bool Stack<T>::isFull() const{
    return top == maxSize-1;
}


template<class T>
void Stack<T>::push(const T&t){
    if(!isFull()){
        st[++top] = t;
    }
    else
        cout<<"The stack is full\n";
}


template<class T>
void Stack<T>::pop(){
    if(!isEmpty()){
        top--;
    }
    else cout<<"Stack is empty\n";
}


template<class T>
T& Stack<T>::Top(){
    if(!isEmpty()){
        T el = st[top];
        return el;
    }
    else{
        char a;
        a = 'm';
        return a;
    }
}


template<class T>
int Stack<T>::Size()const{
    return top+1;
}


bool delimiterMatching(string str){
    int len = str.length();
    Stack<char> s(len);
    char ch;
    int i =0;
    while(i<len){
        ch = str[i];
        if(ch == '('||ch=='['||ch=='{'){
            s.push(ch);
            i++;
        }
        else if(ch ==')'){
            if(s.Top()=='('){
                s.pop();
               }
            else{
                cout<<"missing (,not match\n";
                return 0;
            }
            i++;
        }
        else if(ch==']'){
            if(s.Top()=='['){
                s.pop();
            }
            else{
                cout<<"missing [,not match\n";
                return 0;
            }
            i++;
        }
        else if(ch=='}'){
            if(s.Top()=='{'){
                s.pop();
            }
            else{
                cout<<"missing {,not match\n";
                return 0;
            }
            i++;
        }
        else if(ch=='/'){
          if(str[i+1] == '*'){
                i++;
                i++;
                s.push('/');
                s.push('*');
                while(i<len-2){
                    if(str[i]=='*'&&str[i+1]=='/'){
                            s.pop();
                            s.pop();
                            i++;
                            i++;
                            break;
                        }
                        else{
                            i++;
                        }
                }
          }
          else{
            i++;
          }
        }
        else{
            i++;
        }
    }




    if(s.isEmpty()){
        return 1;
    }
    else{
        if(s.Top() == '(')
        {
            cout <<"missing ')'"<<endl;
            s.pop();
        }
        else if(s.Top() == '{')
        {
            cout <<"missing '}'"<<endl;
            s.pop();
        }
        else if(s.Top() == '[')
        {
            cout <<"missing ']'"<<endl;
            s.pop();
        }
        else if(s.Top() == '*')
        {
            cout <<"missing '*/'"<<endl;
            s.pop();
        }
        return 0;
    }


}
int main()
{
    string str1 = "[s{=/*33212*/2}3]";
    if(delimiterMatching(str1))
        cout <<str1<<" is matching!\n";
    else
        cout<<str1 <<" is not matching\n";
    string str2 = "[s{=/*332122}3]";
    if(delimiterMatching(str2))
        cout <<str2<<" is matching!\n";
    else
        cout<<str2<<" is not matching\n";
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值