C language to solve the problem of bracket matching and inverse Polish expression evaluation

1. Bracket matching problem:

Solution:

void MatchBrackets (const char* str)
{
    char* per = NULL;
    int i = 0;
    Stack s;
    assert (str != NULL);
    InitStack (&s);
    per = (char*)str;

    for (i=0; per[i] != 0; i++)
    {
        if ('(' == per[i] || '[' == per[i] || '{' == per[i])
        {
            PushStack (&s, per[i]);//Push
        }
        else if (')' == per[i] || ']' == per[i] || '}' == per[i])
        {
            char ch ;
            if (IsEmpty (&s) == 1)//Judge whether the stack is empty (wait for one is empty, wait for zero is not empty)
            {
                printf ("More right brackets than left brackets!!!\n");
                return ;
            }
            ch = TopNumAndTop (&s);//Return the top element of the stack and exit the stack
            switch (ch)
            {
            case '(':
                if (per[i] != ')')
                {
                    printf ("Wrong matching of left and right bracket order!!!\n");
                    return ;
                }
                break;
            case '[':
                if (per[i] != ']')
                {
                    printf ("Brackets don't match!!!\n");
                    return ;
                }
                break;
            case '{':
                if (per[i] != '}')
                {
                    printf ("Brackets don't match!!!\n");
                    return ;
                }
                break;
            }
        }
    }
    if (IsEmpty (&s) == 1)  //Judge whether the stack is empty (wait 1 is empty, wait 0 is not empty)
    {
        printf ("Brackets match correctly!!!\n");
    }
    else
    {
        printf ("There are more left parentheses than right parentheses!!\n");
    }
}

Test code:

void test_Match_Brackets()  //Bracket matching problem
{
    char arr1[] = "(())abc{[()]}}";//More right parentheses than left parentheses
    char arr2[] = "(())abc{[(])}";//Left and right bracket order matching error
    char arr3[] = "(())abc{[]}";    //The left and right brackets match correctly
    char arr4[] = "(([]))abc{{([])}";   //More open parentheses than right parentheses
    MatchBrackets (arr1);
    MatchBrackets (arr2);
    MatchBrackets (arr3);
    MatchBrackets (arr4);
}

Operation result:

2. Evaluation of inverse Polish expression

What is an inverse Polish expression:

The inverse Polish notation is also called suffix notation, that is, the operands are placed after the operands, and the inverse Polish notation can identify the priority of operators without brackets. For example: 3 + 4 is an infix expression, which is converted to an inverse Polish expression of 34+

Train of thought:

int ReversePolishExpression (const char* str)   //Evaluation of inverse Polish expression
{
    Stack s;
    char* arr;
    int i = 0;
    assert (str != NULL);
    InitStack (&s);
    arr = (char*)str;

    while (*arr)
    {
        char* p1 = arr;
        while (*arr != ' ' && *arr != '+' && *arr != '-' && *arr != '*' && *arr != '/')
        {
            arr++;
        }
        if (*arr == ' ' && *p1 != ' ')
        {
            *arr = '\0';
            PushStack (&s, StrToA (p1));
        }
        if ((*arr == '+') || (*arr == '-') || (*arr == '*') || (*arr == '/'))
        {
            DataType a = TopNumAndTop (&s);
            DataType b = TopNumAndTop (&s);

            switch (*arr)
            {
            case '+':
                PushStack (&s, b+a);
                break;
            case '-':
                PushStack (&s, b-a);
                break;
            case '*':
                PushStack (&s, b*a);
                break;
            case '/':
                PushStack (&s, b/a);
                break;
            }
        }

        arr++;
    }
    return TopNumAndTop (&s);
}

Test code:

void test_ReversePolishExpression ()//Reverse Polish notation 
{
    char arr[] = "12 3 4 + * 6 - 8 2 / +";
    printf ("%d\n", ReversePolishExpression(arr));;
}

Operation result:

Note: for the operation of the stack in this blog, please refer to the previous blog "basic operation of C language" sequential stack "

Tags: C

Posted on Tue, 31 Dec 2019 01:01:36 -0500 by wiredweb