#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include<cstring>
#define max 50;
typedef char type;
typedef struct
{
type data[50];
int top;
}stack;
void initialstack(stack *s)
{
s->top = 0;
}
int push(stack *s, type x)
{
if (s->top == 50)
return -1;
s->data[s->top++] = x;
return 0;
}
type pop(stack *s)
{
if (s->top == 0)
return -2;
type x = s->data[--s->top];
return x;
}
void kuohaomatch1()
{
//基本思想扫描每一个字符,遇到花中圆的左括号进栈,
//遇到花中圆的右括号时检查是否为相应的左括号若不是则匹配失败
char s[50];
int i = 0;
char d = 'a';
scanf_s("%c", &d);
while (d != ' '&&i<50)
{
s[i++] = d;
scanf_s("%c", &d);
}
s[i] = '\0';
stack *s1 = (stack *)malloc(sizeof(stack));
stack *s2 = (stack *)malloc(sizeof(stack));
initialstack(s1);
initialstack(s2);
for (i = 0; s[i] != '\0'; i++)
switch (s[i])
{
case'{':push(s1, '}'); break;
case'(':push(s1, ')'); break;
case'[':push(s1, ']'); break;
default:
if (pop(s1) != s[i])
printf("不匹配");
else
printf("匹配");
}
printf("匹配");
getchar();
getchar();
}