数据结构第一次作业第4题(括号匹配)

括号匹配
在这里插入图片描述

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct stack
{
	char data[51];
	int top;
} Stack;

void push(Stack* S,char x);
char pop(Stack* S);

int main()
{
	char input[51];
	scanf("%s",input);
	int print[3]={1,1,1};
	int len=strlen(input);
	Stack* S=(Stack*)malloc(sizeof(Stack));
	S->top=-1;
	for(int i=0; i<len; i++)
	{
		if(input[i]=='{'||input[i]=='('||input[i]=='[')
		{
			if(input[i]=='{')
				print[2]=0;
			else if(input[i]=='(')
				print[0]=0;
			else if(input[i]=='[')
				print[1]=0; 
			push(S,input[i]);
		}
		else if(input[i]=='}'||input[i]==']'||input[i]==')')
		{
			char temp=pop(S);
			if(input[i]==')')
			{
				if(temp=='(')
					print[0]=1;
				else
				{
					print[0]=0;
					if(temp=='{')
						print[2]=0;
					else if(temp=='[')
						print[1]=0;
				}
			}
			else if(input[i]=='}')
			{
				if(temp=='{')
					print[2]=1;
				else
				{
					print[2]=0;
					if(temp=='[')
						print[1]=0;
					else if(temp=='(')
						print[0]=0;
				}
			}
			else if(input[i]==']')
			{
				if(temp=='[')
					print[1]=1;
				else
				{
					print[1]=0;
					if(temp=='(')
						print[0]=0;
					else if(temp=='{')
						print[2]=0;
				}
			}
		}
	}
	while(S->top!=-1)//不为空都弹出来 
	{
		char temp=pop(S);
		if(temp=='(')
			print[0]=0;
		else if(temp=='{')
			print[2]=0;
		else if(temp=='[')
			print[1]=0;
	}
	if(print[0]==1&&print[1]==1&&print[2]==1)
	{
		printf("0\n");
	}
	else
	{
		for(int i=0;i<3;i++)
		{
			if(print[i]!=1)
			{
				printf("%d,",i+1);
			} 
		}
	}
	return 0;
}

void push(Stack* S,char x)
{
	if(S->top<50)
	{
		S->data[++S->top]=x;
	}
	else
	{
		return;
	}
}

char pop(Stack* S)
{
	if(S->top==-1)
	{
		//printf("堆栈空\n");
		return 0;
	}
	else
	{
		return S->data[S->top--];
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值