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);
    }
  • 相关阅读:
    一个IT工薪族的4年奋斗成果
    一个IT工薪族的4年奋斗成果
    【VBA】制作散点图及打标签VBA
    【VBA】合并多个excel文件
    【统计分析】ROC曲线
    【ARDUINO】HC-05蓝牙不配对问题
    【ARDUINO】串口无法打开问题
    【ARDUINO】蓝牙(HC-05)透传
    【Raspberry pi+python+mysql】红外传感器-发邮件-存数据库
    【Raspberry Pi】crontab 定时任务
  • 原文地址:https://www.cnblogs.com/wwww2/p/11741932.html
Copyright © 2011-2022 走看看