How to generate a suffix expression and a suffix expression

Source: Internet
Author: User

How to generate a suffix expression and a suffix expression

If you calculate an expression, such4 + 5 + 6*2As calculators differ, a simple four-function calculator is 30. Many scientific calculators know that multiplication has a higher priority than addition, so the scientific answer is 21. The typical calculation sequence can be 4 + 5, save as a temporary variable a, then calculate 6*2, save as B, and finally calculate a + B to get the final result. The operation sequence is as follows:45 + 62 * +

This method is a suffix expression, and its evaluation process is the whole process described above. So how to generate a suffix expression, that is, to convert an infix expression to a suffix expression, you can use the stack to implement it. The entire process is as follows:

 

For example(1 + 2) * (3 + 2)-4/2The suffix expression and calculation result are as follows:

The Java sample code is as follows:

Import java. util. arrayList; import java. util. list; import java. util. stack;/*** MyComputer */public class MyComputer {public static int computer (String input) {List <String> cutList = cutInput (input ); list <String> afterList = getAfterList (cutList); return getResultFromAfterList (afterList);}/*** calculate the result based on the suffix expression */private static int getResultFromAfterList (List <String> afterList) {Stack <Integer> stack = New Stack <> (); for (String ele: afterList) {if (isFlag (ele. charAt (0) {int B = stack. pop (); int a = stack. pop (); stack. push (cal (a, B, ele. charAt (0);} else {stack. push (Integer. valueOf (ele);} if (stack. size ()! = 1) {throw new StackOverflowError ();} return stack. pop ();}/*** get the calculation result of two numbers */private static int cal (int a, int B, char flag) {int result = 0; switch (flag) {case '+': {result = a + B; break;} case '-': {result = a-B; break;} case '*': {result = a * B; break;} case '/': {result = a/B; break;} default: {break;} return result ;} /*** generate the suffix expression */private static List <String> getA FterList (List <String> cutList) {List <String> output = new ArrayList <> (); Stack <Character> stack = new Stack <> (); for (String ele: cutList) {char flag = ele. charAt (0); if (isFlag (ele. charAt (0) | (flag = '(') | (flag = ') {// calculate the operator into the stack if (stack. isEmpty () {stack. push (flag);} else {// If the operator to be added to the stack is greater than the operator on the top of the stack, the operator is directly added to the stack; otherwise, the stack is output until the stack is empty or the computing operator to be written into the stack is smaller than the top computing operator if (flag = '(') {stack. push (flag);} else if (flag = ') ') {While (stack. peek ()! = '(') {Output. add (String. valueOf (stack. pop ();} stack. pop ();} else if (isFlagSmaller (stack. peek (), flag) {stack. push (flag);} else if (stack. peek () = '(') {stack. push (flag);} else {do {if (stack. peek () = '(') {break;} output. add (String. valueOf (stack. pop ();} while (! Stack. isEmpty ()&&! IsFlagSmaller (stack. peek (), flag); stack. push (flag) ;}} else {// add the number to the output directly. add (ele) ;}} while (! Stack. isEmpty () {if (stack. peek ()! = '(') | (Stack. peek ()! = ') {Output. add (String. valueOf (stack. pop () ;}} return output;}/*** slices the String using the operator as the separator */private static List <String> cutInput (String input) {List <String> cutList = new ArrayList <> (); boolean running = true; while (input. length ()> 0) & running) {char c = input. charAt (0); if (isFlag (c) | (c = '(') | (c = ') {cutList. add (String. valueOf (c); input = input. substring (1);} else {for (int I = 0; I <input. length (); I ++) {char tmpC = input. charAt (I); if (isFlag (tnmpc) | (tnmpc = '(') | (tnmpc = ') {cutList. add (input. substring (0, I); cutList. add (String. valueOf (tmpC); input = input. substring (I + 1); break;} if (I = input. length ()-1) {cutList. add (input); running = false ;}}} return cutList;}/*** determines whether a character is an operator */private static boolean isFlag (char c) {return (c = '+' | c = '-' | c = '*' | c = '/');} /*** whether the priority of the First operator is smaller than that of the second operator */private static boolean isFlagSmaller (char a, char B) {boolean flag = true; switch () {case '+': case '-': {if (B = '+') | (B = '-') {flag = false ;} break;} case '*': case '/': {flag = false;} case '(': {flag = false;} default: {break ;}} return flag ;}}

 

Suffix expression and Expression Tree

The Expression Tree is constructed by a suffix expression, so how to construct it is also achieved through stack. The above code has implemented the transformation from an infix expression to a suffix expression. The entire process is roughly as follows:

 

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.