Java implementation of infix expression-to-suffix expression

Source: Internet
Author: User

This should be a point of knowledge that trained programmers have learned when they learn discrete mathematics.

It is easy for you to write the suffix expression yourself with a pen, but it is not possible to do so with code, only to know how to use the stack.

Public class caculator {private int priority (char c) {if (c ==  ' * '   ||  c==  '/') return 2;else if (c ==  ' + '  | |  c==  '-') return 1;elsereturn 0;} Private boolean leftpirorityisnotless (CHARACTER&NBSP;C1,CHAR&NBSP;C2) {if (c1 == null) return  false;return priority (C1) >=priority (C2);} Private boolean isnumber (char c) {if (' 0 '  <= c && c<= ' 9 ') return  true;return false;} Private boolean isleftbracket (char c) {if (c ==  ' (') return true;return false;} Private boolean isrightbracket (char c) {if (c ==  ') ') return true;return false ;} Private boolean isoperator (char c) {if (c ==  ' + '  | |  c ==  '-'  | |  c ==  ' * '  | | c ==  '/') Return true;return false;} Public caculator () {}public sTring parse (string str) {class stack<t>{private linkedlist<t> list =  new LinkedList<T> ();p Ublic void push (t t) {list.addlast (T);} Public t pop () {return list.removelast ();} Public t top () {return list.peeklast ();} Public boolean isempty () {return list.isempty ();}} Stack<character> stack = new stack<> (); Stringbuilder sb = new stringbuilder (); Char[] cs = str.tochararray (); for ( Int i=0; i<cs.length; ++i) {char c = cs[i];if ( isNumber (c)) {sb.append (c);} Else if ( isleftbracket (c)) {Stack.push (c);} Else if ( isoperator (c)) {        //sb.append ("'");// Separate left and right two number while ( leftpirorityisnotless (Stack.top (),  c)) {Sb.append ( stack.pop ());} Stack.push (c);} Else if ( isrightbracket (c)) {while ( !isleftbracket (&NBSP;STACK.top ())) {Sb.append (Stack.pop ()));} Stack.pop ();}} while ( !stack.isempty ()) {Sb.append (Stack.pop ());} Return sb.tostring ();} Public static void main (String[] args)  {String str =  "8-7* ((5+5)-8)/2+ 7 "; Caculator ca = new caculator (); SYSTEM.OUT.PRINTLN ( ca.parse (str));}}

From the point of view of the code to write your own code to run the process can probably understand that the stack here specific role.

The above code can also be improved, because the number of bits is a lot of bits when you need to distinguish between the decomposition of two of the number of pasted together. The code commented above is a method, but not perfect.

Java implementation of infix expression-to-suffix 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.