利用栈可以进行平衡符号的检测

摘要:当我们输入(,),{,},[,],等符号时,经常要检查它们的正确性。
(1)利用栈可以完成这个问题,当遇到一个左括号(,{,[,入栈,遇到右符号,出栈,如果出栈的元素不满足匹配,则说明不平衡.
(2)尽管基本思路简单,但是考虑的如果要判断不平衡的原因(比如“(”多了),或者输入了一些错误的符号也要处理,以及像/* */等有两个字符的符号等细节问题,代码还是不容易写的太简洁.

// chap3_平衡符号的检验_stack.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "malloc.h"
#include "stdlib.h"
#define Empty -1
#define not_matched 0
#define matched 1
#define error -3
#define not_empty -4
typedef struct StackRecord *Stack;

struct StackRecord
{
    int Cpapcity;
    int TopStack;
    char *Array;

};
Stack CreateStack(int MaxElements)
{
    Stack S;
    S = (Stack)malloc(sizeof( StackRecord));
    S->Array = (char*)malloc(sizeof(char)*MaxElements);
    S->TopStack = -1;
    S->Cpapcity = MaxElements;
    return S;
}
void Push(Stack S,char X)
{
    if(S->TopStack >= S->Cpapcity-1)
    {
        puts("cannot push an elements into a full stack");
        return;
    }
    else
        S->Array[++S->TopStack] = X;
}
char Pop(Stack S)
{
    if (S->TopStack == -1)
    {
        puts("cannot Pop an empty stack");
        exit(-1);
    }
    else
        return(S->Array[S->TopStack --]);
}
int match(Stack S,char A)
{
    char TOP;
    if (S->TopStack == -1)
        //stack为空
        return Empty;

    TOP = S->Array[S->TopStack];
    //下面进行匹配
         switch (A)
         {
         case ')':
             if(TOP == '(')
                 return 1;
             else
                 return not_matched;
              break;
         case ']':
             if(TOP == '[')
                 return 1;
             else
                 return not_matched;
              break;
         case '}':
             if(TOP == '{')
                 return 1;
             else
                 return not_matched; 
             break;
         case '*':
               if(TOP == '*')
                return 2;
               else
                  return 0;
             break;
         }
}

void Test(char *A,Stack S)
{
    int i = 0;
    int Error;
    while(A[i]!='\0'){
        switch(A[i])
        {
            case '/':
                if(i == 99 || A[i+1]!='*')
                    //处理错误符号
                    goto end;
                else
                    //压入/*
                    Push(S,A[i]);
                    Push(S,A[i+1]);
                    i+=2;
                break;
            case '(':
                Push(S,A[i++]);
                break;
            case '[':
                Push(S,A[i++]);
                break;
            case '{':
                Push(S,A[i++]);
                break;
            case '*':
                if (i==99||A[i+1]!='/'){
                    //处理错误符号
                    Error = error;
                    goto end;
                }
                else
                    if ((Error = match(S,A[i]))>0){
                    Pop(S);
                    Pop(S);
                    i = i+2;
                    }
                    else{
                        Error = match(S,A[i]);
                        goto end;
                    }
                break;
            case ')':
                if (( Error = match(S,A[i]))>0){
                    Pop(S);
                    i++;
                    }
                    else{
                        Error = match(S,A[i]);
                        goto end;
                    }
                break;
            case ']':
                if ( (Error = match(S,A[i]))>0){
                    Pop(S);
                    i++;
                    }
                    else
                        goto end;

                break;
            case '}':
            if ( (Error = match(S,A[i]))>0){
                    Pop(S);
                    i++;
                    }
                else
                        goto end;

                break;
        }
    }
    if (S->TopStack != -1){
        Error = not_empty;
        goto end;
    }
    else{
        puts("the symbol is balanced");
        return;
    }
end:
    switch(Error)//判断错误原因
    {
    case not_matched:
        printf("the symbol:%c do not match %c",A[i],S->Array[S->TopStack]);
    break;
    case Empty:
        printf("the symbol:%c is more",A[i]);
    break;
    case error:
        printf("the symbol:%c is wrong",A[i]);
        break;
    case not_empty:
        printf("there are too many symbol such as %c",S->Array[S->TopStack]);
        break;

    }
}
int _tmain(int argc, _TCHAR* argv[])
{
    Stack S;
    int x;
    S = CreateStack(30);
    char A[100] = {0};
    puts("Please input the expression");
    scanf("%s:",A);
    Test(A,S);
    return 0;
}

这里写图片描述

这里写图片描述

这里写图片描述

你可能感兴趣的:(利用栈可以进行平衡符号的检测)