编写一个算法,检查一个程序中的花括号,方括号和圆括号是否配对,若能够全部配对则返回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;

}

运行结果:

利用栈实现字符串中三种括号的匹配问题c++语言实现的更多相关文章

  1. web.xml中三种通配符及匹配规则

    一.url-pattern的三种写法 1.精确匹配.以”/”开头,加上servlet名称:    /ad  ; 2.路径匹配.以”/”开头,加上通配符”*” :    /*  ; 3.扩展名匹配.以通 ...

  2. JavaScript中三种字符串连接方式及其性能比较

    参考地址: https://www.cnblogs.com/programs/p/5554742.html 工作中经常会碰到要把2个或多个字符串连接成一个字符串的问题,在JS中处理这类问题一般有三种方 ...

  3. 彻底了解构建 JSON 字符串的三种方式

    原创播客,如需转载请注明出处.原文地址:http://www.cnblogs.com/crawl/p/7701856.html 前言:JSON 是轻量级的数据交换格式,很常用,尤其是在使用 Ajax ...

  4. Spring中三种配置Bean的方式

    Spring中三种配置Bean的方式分别是: 基于XML的配置方式 基于注解的配置方式 基于Java类的配置方式 一.基于XML的配置 这个很简单,所以如何使用就略掉. 二.基于注解的配置 Sprin ...

  5. Android中三种超实用的滑屏方式汇总(转载)

    Android中三种超实用的滑屏方式汇总   现如今主流的Android应用中,都少不了左右滑动滚屏这项功能,(貌似现在好多人使用智能机都习惯性的有事没事的左右滑屏,也不知道在干什么...嘿嘿),由于 ...

  6. 转:VMware中三种网络连接的区别

    转自:http://www.cnblogs.com/rainman/archive/2013/05/06/3063925.html VMware中三种网络连接的区别   1.概述 2.bridged( ...

  7. iOS开发UI篇—iOS开发中三种简单的动画设置

    iOS开发UI篇—iOS开发中三种简单的动画设置 [在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView b ...

  8. C#中三种定时器对象的比较

    ·关于C#中timer类 在C#里关于定时器类就有3个1.定义在System.Windows.Forms里2.定义在System.Threading.Timer类里3.定义在System.Timers ...

  9. 转-Web Service中三种发送接受协议SOAP、http get、http post

    原文链接:web服务中三种发送接受协议SOAP/HTTP GET/HTTP POST 一.web服务中三种发送接受协议SOAP/HTTP GET/HTTP POST 在web服务中,有三种可供选择的发 ...

随机推荐

  1. php 连接sqlserver

    本地环境windows 10+phpstudy2016+ SQL Server 2008 R2 x86+php7.0查看自己sql server 多少位可以在新建查询里输入 select @@VERS ...

  2. javascript的一些有用函数记录,不断更新。。。

    addLoadEvent函数: 众所周知,html文档加载完后会立即执行一个onload函数.但是onload函数只能包含一个被执行的函数,也就是你需要在加载完文档后执行的一个自己的函数.在实际中ht ...

  3. go 拼接sql

    //原文链接:https://www.jianshu.com/p/a0569157c418 golang mysql拼接子查询 使用fmt.Sprintf拼接SQL 实例代码 func Select( ...

  4. 并不对劲的复健训练-p5212 SubString

    题目大意 有一个串\(s\),一开始只知道它的一个前缀.有\(q\)(\(q\leq 10^4\))个操作,操作有两种:1.给一个字符串,表示\(s\)(\(s\)总长\(\leq 6\times 1 ...

  5. django 支持跨域请求配置

    参考:https://www.jianshu.com/p/63fb55bee142 核心注意点:

  6. unity ugui image更换图片

    1:利用资源加载方式 using UnityEngine; using System.Collections; using UnityEngine.UI; public class ChangeIma ...

  7. c# 是如何对一个可遍历对象实现遍历的

    public class Persons:IEnumerable { public Persons(string[] people) { this.people = people; } public ...

  8. NSIS MUI 的内置向导页面

    MUI 的内置向导页面和安装程序有关的向导页面MUI_PAGE_WELCOME 该向导页面显示欢迎信息MUI_PAGE_LICENSE text/rtf_file 该向导页面显示软件授权申明MUI_P ...

  9. Centos7:MyCat安装,配置及使用

    解压缩 使用,默认端口8066 ./mycat start //启动 ./mycat stop //停止 ./mycat restart //重启

  10. CSS3总结五:弹性盒子(flex)、弹性盒子布局

    弹性盒子容器的属性与应用 display:flex/inline-flex flex-direction flex-wrap justify-content align-items align-con ...