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);
    }
  • 相关阅读:
    Java HttpClient使用小结
    【剑指offer】Q18:树的子结构
    poj3041-Asteroids , 二分图的最小顶点覆盖数 = 最大匹配数
    jquery.validate+jquery.form提交的三种方式
    &quot;undefined reference to&quot; 问题解决方法
    [Oracle]
    Effective_java之二:慎用重载函数
    C99规范
    迭代、递归替代循环
    1)Linux程序设计入门--基础知识
  • 原文地址:https://www.cnblogs.com/wwww2/p/11741932.html
Copyright © 2011-2022 走看看