zoukankan      html  css  js  c++  java
  • 利用栈实现字符串中三种括号的匹配问题c++语言实现

    编写一个算法,检查一个程序中的花括号,方括号和圆括号是否配对,若能够全部配对则返回1,否则返回0

    Head.h:

    #ifndef HEAD_H_INCLUDED

    #define HEAD_H_INCLUDED

    #include<iostream>

    struct LinkedNode

    {

        int data;

        LinkedNode*next;

    };

    class LinkedStack//链式栈的类定义

    {

    public:

        LinkedStack();

        ~LinkedStack(){makeEmpty();};

        void Push(LinkedNode &);

        int Pop();

        bool getTop(LinkedNode&x)const;

        bool IsEmpty()const{return (top==NULL)?true:false;}

        int getSize()const;

        void makeEmpty();

        void print();

    private:

        LinkedNode *top;

    };

    #endif // HEAD_H_INCLUDED

    Methods.cpp:

    #include"head.h"

    #include<iostream>

    using namespace std;

    LinkedStack::LinkedStack(){top=NULL;}

    void  LinkedStack::makeEmpty()

    {

        LinkedNode*p;

        while(top!=NULL)

        {

            p=top;top=top->next;delete p;

        }

    }

    void LinkedStack::Push(LinkedNode &x)

    {

        LinkedNode *p=top;

        x.next=p;

        top=&x;

    }

    int LinkedStack::Pop()

    {int data=top->data;

        LinkedNode*p=top;

        top=top->next;

        delete p;

        return data;

    }

    bool LinkedStack::getTop(LinkedNode&x)const

    {

        if(IsEmpty()==true)return false;

        x=*top;

        return true;

    }

    int LinkedStack::getSize()const

    {

        int k=0;

        LinkedNode*p=top;

        while(p!=NULL){p=p->next;k++;}

        return k;

    }

    void LinkedStack::print()

    {

        cout<<"栈中元素个数="<<getSize()<<endl;

        LinkedNode*p=top;int i=0;

        while(p!=NULL)

        {

            cout<<++i<<":"<<p->data<<endl;

            p=p->next;

        }

    }

    Main.cpp:

    #include"head.h"

    #include <iostream>

    #include<string.h>

    using namespace std;

    //所要求的判断括号匹配的算法

    void PrintMatchedPairs(char *expression)

    {

        LinkedStack s1;LinkedStack s2;LinkedStack s3;

        int j,length=strlen(expression);

        cout<<"对于小括号()的情况:"<<endl;

        for(int i=1;i<=length;i++)

        {

            if(expression[i-1]=='('){LinkedNode t;t.data=i;s1.Push(t);}//左括号,位置进栈

            else if(expression[i-1]==')')

            {

                if(s1.IsEmpty()==false){j=s1.Pop();cout<<j<<""<<i<<"匹配"<<endl;}

                else cout<<"没有与第"<<i<<"个右括号匹配的左括号!"<<endl;

            }

        }

        while(s1.IsEmpty()==false)

        {

            j=s1.Pop();

            cout<<"没有与第"<<j<<"个括号相匹配的右括号!"<<endl;

        }

        cout<<"对于中括号[]的情况:"<<endl;

        for(int i=1;i<=length;i++)

        {

            if(expression[i-1]=='['){LinkedNode t;t.data=i;s2.Push(t);}//左括号,位置进栈

            else if(expression[i-1]==']')

            {

                if(s2.IsEmpty()==false){j=s2.Pop();cout<<j<<""<<i<<"匹配"<<endl;}

                else cout<<"没有与第"<<i<<"个右括号匹配的左括号!"<<endl;

            }

        }

        while(s2.IsEmpty()==false)

        {

            j=s2.Pop();

            cout<<"没有与第"<<j<<"个括号相匹配的右括号!"<<endl;

        }

        cout<<"对于大括号{}的情况:"<<endl;

        for(int i=1;i<=length;i++)

        {

            if(expression[i-1]=='{'){LinkedNode t;t.data=i;s3.Push(t);}//左括号,位置进栈

            else if(expression[i-1]=='}')

            {

                if(s3.IsEmpty()==false){j=s3.Pop();cout<<j<<""<<i<<"匹配"<<endl;}

                else cout<<"没有与第"<<i<<"个右括号匹配的左括号!"<<endl;

            }

        }

        while(s3.IsEmpty()==false)

        {

            j=s3.Pop();

            cout<<"没有与第"<<j<<"个括号相匹配的右括号!"<<endl;

        }

    }

    int main()

    {

        char s[100];

        cout<<"请输入要判断的带有三种括号的字符串"<<endl;

        cin>>s;

        PrintMatchedPairs(s);

        return 0;

    }

    运行结果:

  • 相关阅读:
    AngularJS之开发组件的一些思路
    [译]React Context
    前端好玩技术中转站
    《JavaScript高级程序设计》笔记整理
    React-Router学习整理
    AngularJS中的模板安全与作用域绑定
    React问题总结与归纳
    AngularJS中的按需加载ocLazyLoad
    AngularJS+Node的RESTful之基本实现
    AngularJS中的指令
  • 原文地址:https://www.cnblogs.com/linruier/p/9485215.html
Copyright © 2011-2022 走看看