⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 复件 kuohao.cpp

📁 括号匹配算法 数据结构 用c++或c都实现的
💻 CPP
字号:
#include <iostream>
using namespace std;
#define ERROR 0
#define OK 1
#define SIZE 8
#define OVERFLOW -1
#define CRMENT 4
typedef char SElemtype;
typedef char Elemtype;
typedef int status;
typedef struct{
	SElemtype *base;
	SElemtype *top;
	int stacksize;
}SqStack;
status InitStack(SqStack&S){
	S.base=(SElemtype*)malloc(SIZE*sizeof(Elemtype));
	if(!S.base) exit(OVERFLOW);
	S.top=S.base;S.stacksize =SIZE;
	return OK;
	
}
status GetTop(SqStack S,SElemtype &e){
	if(S.top==S.base) return ERROR;
	e=*(S.top -1);
	return OK;
}
status Push (SqStack &S,SElemtype e){
	if(S.top-S.base>=S.stacksize){
		S.base=(Elemtype*) realloc (S.base,(S.stacksize+CRMENT)*sizeof(Elemtype));
		if (!S.base) exit (OVERFLOW);
		S.top=S.base+S.stacksize;
		S.stacksize+=CRMENT;
	}
	*S.top++=e;
	return OK;
}
status Pop(SqStack &S,SElemtype &e){
	if(S.base==S.top) return ERROR;
	e=*--S.top;
	return OK;
}
char  proced(SElemtype op1,SElemtype op2 ){
	char f;
	if((op1=='[')&&(op2==']'))  return f='e';
	if((op1=='(')&&(op2==')'))  return f='e';
    if((op1=='{')&&(op2=='}'))  return f='e';

}
main(){
	cout<<"请输入一组括号('[',']','(',')','{','}')并以 q 结束 注意!!!第一次以#开始"<<endl;
	SqStack S;
	char f;
	SElemtype e,c;
	InitStack(S);
	while((getchar())!='p'){InitStack(S);
	c=getchar();
	Push(S,c);cout<<c<<"进栈";
	while((c=getchar())!='q'){
		GetTop(S,e);
		f=proced(e,c);
		if(f=='e') {Pop(S,e);cout<<e<<"出栈"<<endl;}
		else Push(S,c);cout<<c<<"进栈"<<endl;}
	if(S.top==S.base)  cout<<"恭喜!匹配成功!!!"<<endl;
	else cout<<"抱歉!!!匹配错误!!!"<<endl;
	}
}



	
	



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -