Stack Application - Bracket Matching and Reverse Polish Expressions

See the previous stack statement: https://blog.csdn.net/hanzheng6602/article/details/80053607

Bracket matching
matches strings like "(()()abc{[]})"

Idea: A pointer traverses the string. If the left bracket is pushed onto the stack, the right bracket is popped off the stack and compared with the current pointer to see if it matches. Then the pointer points to \0, and the matching mode is only when the stack is empty

//匹配成功返回1
int match_brackets(const char * string)
{
    if (NULL == string) {
        return 1;
    }
    Stack s;
    init(&s);
    //考虑左括号多,右括号多,和括号类型顺序不同三种情况
    while (*string) {
        if (*string == '(' || *string == '[' || *string == '{') {
            pushBack(&s, *string);
            string++;
            continue;
        }
        else if (*string == ')' || *string == '}' || *string == ']') {
            char tmp = top(&s);
            pop(&s);
            if ((*string == ')'&& tmp == '(') ||
                (*string == '}'&& tmp == '{') ||
                (*string == ']'&& tmp == '[') ) {
                string++;
                continue;
            }
            else {
                //顺序不匹配
                return 0;
            }
        }
        else {
            string++;
        }
    }
    //只有当字符串扫描完了,并且栈中括号都匹配完了,才是正确匹配
    if (isEmpty(&s)) {
        return 1;
    }
    return 0;
}

Reverse Polish expression Put
operands before and after operators
For example " 12 3 4 + * 6 - 8 2 / + " = 12*(3+4) - 6 + 8/2

Idea: The pointer traverses the string. If it is a number, it is pushed onto the stack. The operator takes out two stack elements for operation, and then pushes it into the stack. When the pointer points to an empty value, it returns the top value of the stack.

//错误串返回-1,其他返回表达式值
int exp(const char* str)
{
    if(NULL == str){
        return 0;
    }
    char *low = str;
    char *fast = str;
    Stack s;
    init(&s);
    //排除掉串前多个空格
    while (*low && *low == ' ') {
        low++;
    }
    fast = low;
    //开始处理
    while (*fast) {
        if (isdigit(*low)) {
            //读取一个数字
            int sum = 0;
            while (*fast && isdigit(*fast)) {
                sum = sum * 10 + (*fast - '0');
                fast++;
            }
            pushBack(&s, sum);
        }
        else if(*low=='+' || *low == '-' || *low == '*' || *low == '/'){
            //是操作符
            int tmp = 0;
            switch (*low) {
            case '+':
                tmp = top(&s); pop(&s); 
                tmp += top(&s); pop(&s);
                pushBack(&s, tmp);
                break;
            case '-':
                tmp = top(&s); pop(&s);
                tmp = top(&s) - tmp; pop(&s);
                pushBack(&s, tmp);
                break;
            case '*':
                tmp = top(&s); pop(&s);
                tmp *= top(&s); pop(&s);
                pushBack(&s, tmp);
                break;
            case '/':
                tmp = top(&s); pop(&s);
                tmp = top(&s) / tmp; pop(&s);
                pushBack(&s, tmp);
                break;
            default:break;
            }
            fast++;
        }
        else {
            return  -1;
        }
        //low和fast指向下一个字符开头

        while (*fast && *fast == ' ') {
            fast++;
        }
        low = fast;
    }

    return top(&s);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325904853&siteId=291194637