An infix expression suffix expression java precise implementation

Source: Internet
Author: User
Package Cn.itcast.StackAndQuen;
Import java.util.ArrayList;
Import java.util.List;

Import Java.util.Scanner;
 /** * Created by Likailong on 2016/10/16. * infix expression to suffix expression */public class Mathcaculate {public static void main (String [] args) {Scanner san=new Scanne  R (system.in);//keyboard reads list<character> list=new arraylist<> ()//infix expression into suffix expression with array storage String input
         = San.next ()//Get input information char[] Shuju = Input.tochararray ()//input all information into char array precedence of int priority=0;//mark symbol Mystack<character>mystack=new mystack<> ()//symbol precedence stack mystack<integer>caculatestack=new M
         Ystack<> ();//compute suffix expression stack int special=0;
                     for (int i=0;i<shuju.length;i++) {//The priority of each data that has reached the keyboard input for each data if (GetPriority (Shuju[i])!=0) {//)
                         if (GetPriority (Shuju[i]) >=priority&&getpriority (Shuju[i])!=3) {//If you get an operator that is compared to the top of the stack Priority=getpriority (Shuju[i]); If the stack is greater than or equal to the top priority stack
                          Mystack.push (Shuju[i]); }else if ((GetPriority (shuju[i)) ==3&&priority!=3&&special!=3) | | (GetPriority (Shuju[i])!=3&&priority==3&&special!=3)) {special=getpriority (shuju[i]), if greater than or equal to the top of the stack, and then the stack Mystack.pus
                     H (Shuju[i]); else if (getpriority (Shuju[i]) ==3&&special==3) {//If the stack is greater than or equal to the top priority stack instead
                             Out Stack while (!mystack.isempty ()) {//out stack operation Char Num=mystack.pop ();
                             List.add (num);
                         System.out.print (num);
                              } else{while (!mystack.isempty ()) {//Out of stack operation
                              Char Num=mystack.pop ();
                              List.add (num);
    System.out.print (num);                      } mystack.push (Shuju[i]);
             }else{System.out.print (Shuju[i]);//If the digital direct output is received with an array list.add (Shuju[i]);
            } while (!mystack.isempty ()) {char fuhao=mystack.pop ();
            List.add (Fuhao);
        System.out.print (Fuhao);
        } System.out.println (); for (int i=0;i<list.size (); i++) {//Operation method to traverse the string nums=list.get (i) in the presence of the suffix expression in the array. toString ();//each element is converted to a string.
            Nteger.parseint (Nums);
            Char num=list.get (i); if (num==) (' | |
            num== ') {list.remove (i);
                } if (' 0 ' <=num&&num<= ' 9 ') {//string comparison should be so written int numss=integer.parseint (nums);//String spin number
                Caculatestack.push (NUMSS);//into stack}else {int shuju1=caculatestack.pop ();/out Stack
                int Shuju2=caculatestack.pop (); Char CACUlate=list.get (i);
                int numadd=0;
                if (caculate== ' + ') {//Judge the operators in the array to perform related operations numadd=shuju1+shuju2;
                }else if (caculate== '-') {numadd= shuju2-shuju1;
                }else if (caculate== ' * ') {numadd= shuju1*shuju2;
                }else if (caculate== '/') {numadd=shuju1/shuju2;
            } caculatestack.push (Numadd); } System.out.println (Caculatestack.pop ());//get stack top element and is answer} private static int getpriority (char sh
            Uju) {//The input of each character of the priority int priority=0;
            Char Num=shuju;
                    if (0<=num&&num<=9) {}else {switch (num) {case '-':
                    Case ' + ': return priority=1;
                    Case '/': Case ' * ': return priority=2;
  Case ' (':                  Case '] ': return priority=3;
                default:priority=0;
    } return priority;
 }

}

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.