Stack correlation algorithm

Source: Internet
Author: User
Tags empty expression final int size

-Bracket Matching

Match Int (char * cs, int size);

1. Make an empty stack. Read the characters until the end of the file.

2. To judge the characters that are read,

2.1 If the character is a left parenthesis, then into the stack;

2.2 If the character is a closing parenthesis, if the stack empty or pop-up left parenthesis does not match, then the match fails;

2.3 Input end, if the stack is not empty, the match fails, otherwise the match succeeds.

-evaluates the value of the suffix expression (assuming the suffix expression is correct)

int Postfixvalue (char * expression, int size);

1. Make an empty stack, read the characters until the end of the file.

2. To judge the characters that are read,

2.1 If the number, then into the stack;

2.2 If it is an operator, it pops up two digits and the results are counted into the stack.

3. After the completion of the calculation, the final pop-up value is the final calculation results.

-infix expression suffix expression (assuming infix expression is correct)

void Convertexpression (char * expression, int size);

1. Make an empty stack, read the characters until the end of the file.

2. To judge the characters that are read,

2.1 If it is the operand, direct output;

2.2 If the operator (+-*/)

2.2.1 If the stack is not empty and the top of the stack has precedence over the current operator precedence, all the precedence in the output stack is greater than the current element's operator;

2.2.2 The current element into the stack;

2.2.3 The above four operators have precedence greater than ' (' precedence '.

2.3 If the operator ' (', then the stack.

2.4 If the operator ' is ', then the stack all ' (' before the stack element, ' (' Out of stack, ' but not added to the final expression.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.