java achieve expression evaluation (20 points) ------- non-recursive version

Dr.Kong design robots Caddo mastered addition and subtraction operations in the future, by the recently learned some simple function evaluation. For example, it knows the value of the function min (20, 23) is 20, the value of add (10, 98) is 108 and the like. After training, Dr.Kong design robots Caddo even in a nested calculation of more complex expressions.

Suppose expression can be simply defined as: 1, a positive number x is a decimal expression. 2, if x and y are an expression, the function min (x, y) is an expression whose value is x, y in the minimum number. 3, if x and y are an expression, the function max (x, y) is an expression whose value is x, y is the maximum number. 4, if x and y are an expression, the Add the function (x, y) is an expression whose value is x, y sum. 5, if x and y are an expression, the function Sub (x, y) is an expression whose value is x, y is the difference. For example, the expression max (add (1,2), 7) a value of 7.

Please write programs for a given set of expressions to help Dr.Kong calculate the correct answer to proofread Caddo calculation of right and wrong.

Input formats:

The first line: N represents the number of expressions (1≤N≤10) to be calculated next has N rows, each row is a string representing the expression to be evaluated. (No expression extra spaces per line no more than 300 characters, the number of decimal expression does not appear in more than 1000)

Output formats:

Output is N rows, each row corresponding to the value of an expression.

Sample input:

Here we are given a set of inputs. E.g:

3
add(1,2)
sub(1,999)
add(min(1,1000),add(100,99))

Sample output:

Given here corresponding output. E.g:3

-998
200


Code:
import java.util.*;
class NUM{
    private int left;
    private int right;
    public int getLeft() {
        return left;
    }
    public void setLeft(int left) {
        this.left = left;
    }
    public int getRight() {
        return right;
    }
    public void setRight(int right) {
        this.right = right;
    }

}
public  class Main {
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();sc.nextLine();
        Stack<String> stk = new Stack<String>();
        Stack<Integer> stkNum = new Stack<Integer>();
        int k=0;
        for(int i =0 ;i< N;i++){
            String line = sc.nextLine();
            if(line.length() == 1){
                int num = Integer.parseInt(line);
                System.out.println(num);
            }else{
                int index = 0;
                int p=0;
                while(true){
                    index = line.indexOf("(",index+1);
                    if(index == -1) break;
                    p = line.indexOf(",",p+1);
                    if(p == -1) break;

                    NUM num = getNum(p,line);
                    if(num.getLeft()!=-999){
                        stkNum.push(num.getLeft());
                    }
                    if(num.getRight()!=-999) {
                        stkNum.push(num.getRight());
                    }
                    String sub = line.substring(index-3, index);
                    stk.push(sub);
                }
                while(stk.size()!=0){
                    String opt = stk.pop();
                    if(opt.equals("min")){
                        int d = min(stkNum.pop(),stkNum.pop());
                        stkNum.push(d);
                    }else if(opt.equals("max")){
                        int d = max(stkNum.pop(),stkNum.pop());
                        stkNum.push(d);
                    }else if(opt.equals("add")){
                        int d = add(stkNum.pop(),stkNum.pop());
                        stkNum.push(d);
                    }else if(opt.equals("sub")){
                        int d = sub(stkNum.pop(),stkNum.pop());
                        stkNum.push(d);
                    }
                }
                System.out.println(stkNum.pop());
                k=0;
                
            }
            
            
        }
        
        sc.close();
    }
    private static int sub(int i, int j) {
        return j-i;
    }
    private static int add(int i, int j) {
        return i+j;
    }
    private static int max(int i, int j) {
        return i>=j?i:j;
    }
    private static int min(int i, int j) {
        return i>=j?j:i;
    }
    private static NUM getNum(int p,String line){
        int a=p,b=p;
        while(!line.substring(a, a+1).equals("(") && !line.substring(a, a+1).equals(")")){
            a--;
        }
        while(!line.substring(b, b+1).equals(")")&& !line.substring(b, b+1).equals("m")&& !line.substring(b, b+1).equals("a") && !line.substring(b, b+1).equals("s")){
            b++;
        }
        String left = line.substring(a+1, p);
        String right = line.substring(p+1, b);
        Num num = new NUM ();
        num.setleft ( -999 );
        num.setRight(-999);
        if(left.length()>=1){
            int lNum = Integer.parseInt(left);
            num.setLeft(lNum);
        }
        if(right.length() >= 1) {
            int rNum = Integer.parseInt(right);
            num.setRight (rnum);
        }
        return num;
    }

}

 

 

Guess you like

Origin www.cnblogs.com/HoweZhan/p/11946377.html