Evaluate the four arithmetic expressions (stack Application)

Source: Internet
Author: User

1. Conversion of forward/middle/suffix expressions (first, you need to understand the conversion between the three)

Converting a natural expression to a forward/suffix expression is actually very simple. First, construct the binary tree corresponding to the expression according to the priority order of the natural expression, and then traverse the binary tree with the forward/middle/suffix to obtain the forward/middle/suffix expression. For example, convert a natural expression to a binary tree: A × (B + C)-D ① based on the priority order of the expression, first calculate (B + C ), form a binary tree ② followed by a × (B + C). Pay attention to the relationship between left and right locations during writing.
③ Add-D to the right and then traverse the most constructed binary tree. The three traversal sequences are as follows: ① forward traversal: Root-left-right ② middle traversal: left-root-Right
③ Post-order traversal: Left-right-Root
Based on the preceding example, the prefix expression-* A + BCD infix expression: a * B + c-d suffix expression can be obtained based on the final binary tree: ABC + * D-2. infix expression to suffix expression (stack application) infix expression "9 + (3-1) * 3 + 10/2 "is converted into a suffix expression" 9 3 1-3 * + 10 2/+ ".

Rule: traverse each number and symbol of the infix expression from left to right. If it is a number, it is output, which is a part of the suffix expression. If it is a symbol, it determines its priority with the symbol on the top of the stack, if it is a right brace or a symbol with a lower priority than the top of the stack (multiplication, division, plus or minus), the top elements of the stack are output in sequence, and the current symbol is added to the stack until the suffix expression is output.

A. Initialize an empty stack to use the symbol to access the stack.

B. the first character is the number 9, and the output 9 is followed by the symbol "+.

C. The third character is "(", which is still a symbol. Because it is only left parenthesis and has not been paired, It is pushed to the stack.

D. The fourth character is the number 3, the output, the total expression is 9 3, followed by "-", into the stack.

E. next is the number 1, output, the total expression is 9 3 1, followed by the symbol ")", at this time, we need to match the previous "(", so the top of the stack in turn out of the stack, and output until "(" goes out of the stack. At this time, there is only "-" above the left bracket, so "-" is output. The total expression is 9
3 1 -.

F. followed by the number 3, output, the total expression is 9 3 1-3. the symbol "*" is followed, because the top symbol of the stack is "+" and the priority is lower than "*", so no output is made, and "*" is added to the stack.

G. the symbol "+" is followed. At this time, the "*" element on the top of the current stack has a higher priority than the "+" symbol, therefore, the elements in the stack are output and output (there is no lower priority than "+", so all are output). The total output expression is 9 3 1-3 *
+. Then, the current symbol "+" is added to the stack.

H. After the number is 10, the total output expression is 9 3 1-3 * + 10. The symbol "/" is followed, so "/" is added to the stack.

I. output the last number 2. The total expression is 9 3 1-3 * + 10 2.

J. Because it has reached the end, all the symbols in the stack are output to the stack. The final output suffix expression is 9 3 1-3 * + 10 2/+.

3. suffix expression calculation result (stack Application)

Suffix expression: 9 3 1-3 * + 10 2/+

The rule is: traverse each number and symbol of the expression from left to right. When a number is used, it is pushed to the stack. When a number is used as a symbol, the two digits at the top of the stack are pushed out of the stack for calculation, the calculation result is added to the stack until the final result is obtained.

A. Initialize an empty stack. This stack is used to access and use numbers to be computed.

B. In the suffix expression, the first three are numbers, so 9 3 1 goes into the stack.

C. The next step is "-". Therefore, the first-out stack in the stack is used as the reduction, the third-out stack is used as the reduction, and 3-1 is calculated to get 2, and then 2 is added to the stack.

D. Add the number 3 to the stack.

E. It is followed by "*", which means 3 and 2 in the stack are output stacks, 2 is multiplied by 3, get 6, and put 6 into the stack.

F. The following is "+", so the stack 6 and 9 are added to the stack, 9 and 6, get 15, and add 15 to the stack.

G. Then the numbers 10 and 2 are pushed to the stack.

H. The following is the symbol "/". Therefore, 2 and 10 at the top of the stack are split from 2, and 5 is obtained.

I. The last one is the symbol "+". Therefore, 15 and 5 go out of the stack and add them together to get 20. Let's talk about 20 go into the stack.

J. The result is 20 outputs, and the stack becomes empty.

 

 

// The following code only supports some simple integer addition, subtraction, multiplication, division operations, and does not support floating-point numbers, negative numbers, or numeric operations greater than 9, just // write your own code, perform a simple verification of this process. If you need to solve complicated computing problems, you can search for information online! # Include <iostream> # include <cstdio> # include <string> # include <stack> using namespace STD; stack <char> S; stack <int> SS; int main () {int len1, len2, Len, I, j; string str1, str2; // str1 is the infix expression, str2 is the suffix expression while (1) {// The infix expression is converted to the suffix expression Getline (CIN, str1); len1 = str1.length (); str2.clear (); for (I = 0; I <len1; I ++) {If (str1 [I]> = '0' & str1 [I] <= '9') str2.push _ back (str1 [I]); else {If (S. size () = 0 | Str 1 [I] = '(') s. push (str1 [I]); else {char tmp1 = S. top (); If (str1 [I] = ') {Len = S. size (); While (LEN) {char TMP = S. top (); S. pop (); If (TMP = '(') break; else str2.push _ back (TMP); Len --;}} else {If (tmp1 = '*' | tmp1 = '/') {If (str1 [I] = '*' | str1 [I] = '/') s. push (str1 [I]); else {Len = S. size (); While (LEN) {char TMP = S. top (); str2.push _ back (TMP); S. pop (); Len --;} s. push (str1 [I]);} Else {S. Push (str1 [I]) ;}}} if (S. Size ()! = 0) {Len = S. size (); While (LEN) {char TMP = S. top (); str2.push _ back (TMP); S. pop (); Len -- ;}}cout <str2 <Endl; // result calculated by the suffix expression int temp1, temp2, temp3; len2 = str2.length (); for (I = 0; I <len2; I ++) {If (str2 [I]> = '0' & str2 [I] <= '9 ') {int T = str2 [I]-48; SS. push (t);} else {temp1 = ss. top (); SS. pop (); temp2 = ss. top (); SS. pop (); If (str2 [I] = '+') {temp3 = temp2 + temp1;} else if (str2 [I] = '-') {temp3 = temp2-temp1;} else if (str2 [I] = '*') {temp3 = temp2 * temp1 ;} else if (str2 [I] = '/') {temp3 = temp2/temp1;} ss. push (temp3) ;}}cout <ss. top () <Endl;} system ("pause ");}

 

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.