题目描述:
思路:
代码实现
#ifndef STACK
#define STACK
typedef struct {
char str[200];
int top;
}*stack, Stack;
#endif
#define max 20
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
void init(stack s) {
s->top = -1;
memset(s->str, '\0', 200);
}
char pop(stack s) {
if (s->top == -1) {
return ' ';
}
char result = s->str[s->top--];
return result;
}
char push(stack s, char ch) {
if (s->top == max - 1) {
return ' ';
}
s->str[++s->top] = ch;
return ch;
}
int main(void)
{
char str[20];
gets_s(str, 20);
stack s = (stack)malloc(sizeof(Stack));
init(s);
for (int i = 0; i < strlen(str); i++) {
if (str[i] == '(' || str[i] == '[') {
push(s, str[i]);
}
else {
if (str[i] == ')') {
if (s->str[s->top] == '(') {
pop(s);
}
}
else if (str[i] == ']') {
if (s->str[s->top] == '[') {
pop(s);
}
}
}
}
if (s->top == -1) {
puts("匹配");
}
else {
puts("不匹配");
}
return 0;
}