利用栈实现字符串中三种括号的匹配问题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;
}
运行结果:


利用栈实现字符串中三种括号的匹配问题c++语言实现的更多相关文章
- web.xml中三种通配符及匹配规则
一.url-pattern的三种写法 1.精确匹配.以”/”开头,加上servlet名称: /ad ; 2.路径匹配.以”/”开头,加上通配符”*” : /* ; 3.扩展名匹配.以通 ...
- JavaScript中三种字符串连接方式及其性能比较
参考地址: https://www.cnblogs.com/programs/p/5554742.html 工作中经常会碰到要把2个或多个字符串连接成一个字符串的问题,在JS中处理这类问题一般有三种方 ...
- 彻底了解构建 JSON 字符串的三种方式
原创播客,如需转载请注明出处.原文地址:http://www.cnblogs.com/crawl/p/7701856.html 前言:JSON 是轻量级的数据交换格式,很常用,尤其是在使用 Ajax ...
- Spring中三种配置Bean的方式
Spring中三种配置Bean的方式分别是: 基于XML的配置方式 基于注解的配置方式 基于Java类的配置方式 一.基于XML的配置 这个很简单,所以如何使用就略掉. 二.基于注解的配置 Sprin ...
- Android中三种超实用的滑屏方式汇总(转载)
Android中三种超实用的滑屏方式汇总 现如今主流的Android应用中,都少不了左右滑动滚屏这项功能,(貌似现在好多人使用智能机都习惯性的有事没事的左右滑屏,也不知道在干什么...嘿嘿),由于 ...
- 转:VMware中三种网络连接的区别
转自:http://www.cnblogs.com/rainman/archive/2013/05/06/3063925.html VMware中三种网络连接的区别 1.概述 2.bridged( ...
- iOS开发UI篇—iOS开发中三种简单的动画设置
iOS开发UI篇—iOS开发中三种简单的动画设置 [在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView b ...
- C#中三种定时器对象的比较
·关于C#中timer类 在C#里关于定时器类就有3个1.定义在System.Windows.Forms里2.定义在System.Threading.Timer类里3.定义在System.Timers ...
- 转-Web Service中三种发送接受协议SOAP、http get、http post
原文链接:web服务中三种发送接受协议SOAP/HTTP GET/HTTP POST 一.web服务中三种发送接受协议SOAP/HTTP GET/HTTP POST 在web服务中,有三种可供选择的发 ...
随机推荐
- easyswoole报错:failed: Error during WebSocket handshake: Unexpected response code: 200
WebSocket connection to 'ws://www.xxxx.com/xxx/xx' failed: Error during WebSocket handshake: Unexpec ...
- react 深度 循环嵌套对象渲染问题 map
查了一些资料貌似react的循环渲染对象只有map,但map只支持数组对象. 接到后台数据如下 { "list": { "A": [{ "image& ...
- VirtualBox网络之仅主机(Host-Only)网络
https://blog.csdn.net/dkfajsldfsdfsd/article/details/79441874
- Jenkins常用插件介绍
摘要: 对于中小型运维团队,jenkins作为运维利器,可以解决很多工作中的痛点.基于UI的特性从而让使用者的入门成本很低,基于插件可以具备认证,记录,条件触发以及联动,让运维工程师可以将精力放在业务 ...
- 解决windows系统下ping,ipconfig不是内部或外部命令
一般情况下,都是误删了系统变量path的值.解决方法:右击我的电脑 → 选择属性 → 选择高级系统设置 → 环境变量 → 在系统变量列表中,找到“path”环境变量双击,打开.在变量值这一栏检测下是否 ...
- vue入门:(条件渲染)
v-if v-show v-else 一.v-if:生成或者移出一个元素 <div id="example"> <button v-on:click=" ...
- React中使用遍历
1.使用for(let item of items){} render(){ var itemList = [] for(let item of items){ itemList.push(<I ...
- Java学习笔记【十、泛型】
简介 Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型. 泛型的本质是参数化类型,也就是说所操作的数据 ...
- centos redis自启动
#!/bin/sh # chkconfig: 2345 90 10 # description: Redis is a persistent key-value database # Simple R ...
- 读书笔记《SpringBoot编程思想》
目录 一. springboot总览 1.springboot特性 2.准备运行环境 二.理解独立的spring应用 1.应用类型 2.@RestController 3.官网创建springboot ...