栈的实现及括号匹配

#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();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值