Meal WaitPerson and Chef

import java.util.Stack;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.Map;
import java.util.HashMap;
public class ArithmeticExpressionEvaluation {
    private static Stack theStack;
    private static char charKey=(char)97;
    private  static Map  hashMap=new HashMap();
    private static String result;
    public static void main(String[] args) {
        if(args.length<1) {
            System.out.println(" please enter the input arithmetic expression string  that needs to be evaluated."+
                               "the  legal operators include '+', '-','*' and '/' ,and you can also add '(' and ')' ."+
                               "all numbers must above zero, the program doesn't support negative values now "
                              );
            System.exit(0);
        }
        System.out.println("the original input is :"+args[0]);
        String internalStr=processBeforeEvaluation(args[0]);
        System.out.println("the internal expression is :"+internalStr);
        String postfixStr= translateToPostfixExpression(internalStr);
        System.out.println("the postfix expression is :"+postfixStr);
        double result=evaluatePostfixExpression(postfixStr);
        System.out.println("the final result is :"+result);
    }
    private static String processBeforeEvaluation(String input) {
        Matcher legal=Pattern.compile("[^0-9.*+/\\()-]").matcher(input);
        if(legal.find()) {
            System.out.println("Please enter legal arithmetic expression string.");
            System.exit(0);
        }
        StringBuffer sbuf = new StringBuffer();
        Matcher m=Pattern.compile("\\d+(\\.\\d+)?").matcher(input);
        while(m.find()) {
            String temp=m.group(0);
            hashMap.put(charKey,temp);
            m.appendReplacement(sbuf,charKey+"");
            charKey=(char)(charKey+1);
        }
        m.appendTail(sbuf);
        return sbuf.toString();
    }


    private static String translateToPostfixExpression(String input) {
        theStack=new Stack();
        result="";
        for(int j=0; j(); //   ,               
        try {
            for(int j=0; j=97) {  //j    double    
                    theStack.push(ch);
                } else {
                    str2 =hashMap.get(theStack.pop());
                    str1=hashMap.get(theStack.pop());
                    num2=Double.parseDouble(str2);
                    num1=Double.parseDouble(str1);
                    switch(ch) {
                    case '+':
                        interAns=num1+num2;
                        break;
                    case '-':
                        interAns=num1-num2;
                        break;
                    case '*':
                        interAns=num1*num2;
                        break;
                    case '/':
                        interAns=num1/num2;
                        break;
                    default :
                        interAns=0;
                        break;
                    } //end switch
                    hashMap.put(charKey,interAns+"");
                    theStack.push(charKey);
                    charKey=(char)(charKey+1);
                } //end else
            } //end for loop
            str1=hashMap.get(theStack.pop());
            interAns=Double.parseDouble(str1);
        } catch(Exception e) {
            System.out.println("please enter legal numbers!");
            e.printStackTrace();
        }
        return interAns;
    }





}

좋은 웹페이지 즐겨찾기