zoukankan      html  css  js  c++  java
  • 括号匹配的检验

    头文件中

    #include<stdio.h>
    #include<stdlib.h>
    typedef char Elem;
    typedef int status;
    #define MAXSIZE 100
    #define ERROR 0
    #define OK 1
    
    typedef struct{
       Elem data[MAXSIZE];
       int top;
    }SqStack;
    //初始化
    void Initstack(SqStack &S)
    {
        if(!S.data) exit(-1);
        S.top = 0;
    }
    //入栈
    status Push(SqStack &S,Elem e)
    {
        if(S.top==MAXSIZE)
        {
            printf("栈满了
    ");
            return ERROR;
        }
        S.data[S.top++] = e;
        return OK;
    
    }
    //出栈
    status Pop(SqStack &S)
    {
        if(S.top==0) return ERROR;
        S.top--;
    }
    //获得栈顶元素
    status getTop(SqStack S,Elem &e)
    {
        if(S.top==0) return ERROR;
        e = S.data[S.top-1];
        return OK;
    }
    //括号匹配 status Match(SqStack
    &S) { printf("---------------输入想要匹配的括号----------------- "); printf(" 括号范围[]、{}、() "); char str[MAXSIZE]; gets(str); printf(" "); Elem pre = '#';//假如第一个括号是右括号,则与其匹配,当然肯定匹配错误 int i=0; while(str[i]!='') { if(str[i]=='{'||str[i]=='['||str[i]=='(') { Push(S,str[i]); i++; continue; } if(getTop(S,pre)) { if((pre=='{'&&str[i]=='}')||(pre=='['&&str[i]==']')||(pre=='('&&str[i]==')')) { Pop(S); i++; }else{ printf("匹配错误 "); printf(" "); printf("----------------------end----------------------"); return ERROR; } }else{ printf("栈空了或输入的第一个是左括号 "); printf("----------------------end-------------------------"); return ERROR; } } if(S.top==0){ printf("匹配成功 "); printf(" "); printf("-----------------------end-------------------------"); }else{ printf("匹配错误 "); printf(" "); printf("------------------------end------------------------"); } }

     主函数

    #include"StringMatch.h"
    int main()
    {
        SqStack S;
        Initstack(S);
        Match(S);
    }
  • 相关阅读:
    HTTP与HTTPS的区别
    Linux内核结构体--kfifo 环状缓冲区
    POSIX 线程详解
    linux的fork()函数-进程控制
    HDU 3435 A new Graph Game(最小费用最大流)&amp;HDU 3488
    Memcached安装使用和源代码调试
    结构-01. 有理数比較(10)
    Android:你不知道的 WebView 使用漏洞
    关于文件异步上传
    &lt;二代測序&gt; 批量下载 NCBI sra 文件
  • 原文地址:https://www.cnblogs.com/wwww2/p/11741932.html
Copyright © 2011-2022 走看看