Talk C Chestnut Bar (21st: C-language instance-expression evaluation)

Source: Internet
Author: User


Ladies and gentlemen, hello, crossing, we said the principle of the stack in the first few, and gave the actual example to explain, this time we say the example

The child is: an expression evaluates. The expression evaluates the same as the parentheses in the previous one, using the principle of the stack, as you can get from the example

Look out, so we put them together. Gossip Hugh, words return to the positive. Let's talk C chestnuts together!


Crossing, the expression we're talking about here is the arithmetic expression that contains the addition, subtraction, and multiplication. For example: 1+2*3-4/5 is a four

The calculation expression. In this expression, the operator is in the middle of the number, so we call it infix expression, which is a kind of table that fits our thinking.

form, however, the computer does not understand infix expression, because it has no way to do the same as we do multiplication operations before

Plus and minus operations, we need to convert infix expressions into suffix expressions that the computer can understand. "What is a suffix expression", this time

There are crossing under the table, crossing, the so-called suffix expression is the operator behind the number. For example: "1+2*3-6/3" this infix

An expression can be a suffix expression such as "123*+63/-".


There are two major steps to evaluating an expression:

    • Infix expressions are converted to suffix expressions.
    • Evaluates the suffix expression.

There are a few small steps in these two big steps, and we'll talk about these small steps in detail next. Before I say it, I first explain a concept: excellent

First class. Priority stands for order, a daily example of life: when queuing up to buy something, the person in front of the queue is more than

The person behind the queue has the right to buy things first, so we can say that the person in front of the queue has a high priority in buying things. Priority is expressed in an expression

The process of calculation is reflected in the precedence of multiplication and division as higher precedence than addition and subtraction. Which is what we usually say first multiplication and then add and subtract, this inside

Let me not say more, we have all learned in elementary school mathematics. We convert infix expressions to suffix expressions during the evaluation of expressions

First-class, because postfix expressions can take precedence out of operators. Without a priority, the computer can understand the suffix expression and

The associated operation.


The steps for converting infix expressions to Postfix expressions are as follows:

1. Iterates through infix expressions from beginning to end, reading one character at a time from an expression;

Span style= "FONT-FAMILY:SIMSUN; Font-size:18px ">2. Determine the character read in step 1, if the number is saved to array A, if the operator is +*, see the next step;

3. The stack that holds the operator is stacked, comparing the operators in step 2 with the operators that were just out of the stack;

4. If the operator in step 2 has a high precedence, then both of the operators participating in the comparison are put into the stack. Otherwise look at the next step;

5. If the operator in step 2 has a low precedence, let the operators in the stack continue out of the stack, And the operator of the stack is stored in array A,

6. Repeat steps 4 and 5,

7 until the precedence of the out-of-stack operator is higher than the precedence of the operator in step 2; Repeat steps 1 through 6 until you have traversed the infix expression;

8. Determine if there are operators in the stack, if any, All the operators are stacked and the operator of the stack is stored in array a.


The steps to evaluate a suffix expression are as follows:

1. Iterate through the suffix expression from beginning to end, reading one character from the expression at a time;

2. Judge the character read in step 1, if the number is in the stack, if it is +* operator, see the next step;

3. The stack holding the number two times out of the stack, according to the type of operator in step 2, the two numbers out of the stack operation;

4. The result of the operation in step 3 is put into the stack, so that the results can be saved to the stack of stored numbers;

5. Repeat steps 1 through 4 until the suffix expression has been traversed;

6. The last element in the stack is the result of the expression's operation.


Crossing, the text does not write code, the detailed code put in my resources, you can click the link to download the use. From the code, you can

To see, we used two times the stack, one time is the infix expression into a suffix expression process, the stack is used to store operators. The other One

The second is the value of the suffix expression in the process, the stack is used to hold the numbers involved in the operation.


Crossing, here's an example of matching brackets. I want to know what the following example, and listen to tell.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Talk C Chestnut Bar (21st: C-language instance-expression evaluation)

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.