ผลต่างระหว่างรุ่นของ "01204212/expr"

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
 
(ไม่แสดง 1 รุ่นระหว่างกลางโดยผู้ใช้คนเดียวกัน)
แถว 15: แถว 15:
  
 
== Example ==
 
== Example ==
 +
'''Input'''
 +
 +
<pre>
 +
3
 +
(2*(2+3))
 +
((2*3)+(3-15))
 +
((3+5)*((1-2)+(4*10)))
 +
</pre>
 +
 +
'''Output'''
 +
 +
<pre>
 +
10
 +
-6
 +
312
 +
</pre>
  
 
== Test data ==
 
== Test data ==

รุ่นแก้ไขปัจจุบันเมื่อ 00:25, 10 กันยายน 2559

from 01204212

Given an expression with operators +, -, and *, you want to evaluate its value. The expression that you get has been completely parenthesed. For example, you will not get an expression (4+5)*2, but ((4+5)*2). The correct evaluation produces 18.

Input/Output

Input

  • The first line: an integer T, the number of test cases.
  • The next T lines: each line contains a completely parenthesed expression.

Output

For each test case, output an integer which is an evaluation of the input expression.

Example

Input

3
(2*(2+3))
((2*3)+(3-15))
((3+5)*((1-2)+(4*10)))

Output

10
-6
312

Test data

Code

Term.java

public class Term {
    enum Type {
        NUMBER, PAREN, OPERATOR
    }
    
    private Type termType;
    private String termStr;
    private int val;
    
    public Term(String st) {
        termStr = st;
        if(st.equals("(") || st.equals(")")) {
            termType = Type.PAREN;
        } else if(st.equals("+") || st.equals("-") || st.equals("*")) {
            termType = Type.OPERATOR;
        } else {
            termType = Type.NUMBER;
            val = Integer.parseInt(termStr);
        }
    }

    public Term(int val) {
        termType = Type.NUMBER;
        this.val = val;
    }

    public Type getTermType() {
        return termType;
    }

    public int getVal() {
        return val;
    }
    
    public boolean isOpenParen() {
        return (termType == Type.PAREN) && (termStr.equals("("));
    }
    
    public boolean isCloseParen() {
        return (termType == Type.PAREN) && (termStr.equals(")"));
    }
    
    public boolean isOperator() {
        return termType == Type.OPERATOR; 
    }

    public int performOperator(int o1, int o2) {
        switch(termStr.charAt(0)) {
        case '+':
            return o1 + o2;
        case '-':
            return o1 - o2;
        case '*':
            return o1 * o2;
        }
        return 0;
    }
    
    public String toString() {
        return termStr;
    }
}

Main.java

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws Exception {
        Main m = new Main();
        m.process();
    }

    private void process() throws Exception {
        BufferedReader reader = new BufferedReader(
                   new InputStreamReader(System.in) );

        int t = Integer.parseInt(reader.readLine());
        
        for(int i=0; i<t; i++) {
            String st = reader.readLine(); 
            LinkedList<Term> terms = parseTerms(st);
            System.out.println(evaluate(terms));
        }
    }

    private int evaluate(LinkedList<Term> terms) {
        return 0;
    }

    private LinkedList<Term> parseTerms(String st) {
        LinkedList<Term> output = new LinkedList<Term>();
        int i=0;
        while(i<st.length()) {
            char ch = st.charAt(i);
            if((ch >= '0') && (ch <= '9')) {
                int j = i;
                while((st.charAt(j) >= '0') && (st.charAt(j) <= '9')) {
                    j++;
                }

                String termStr = st.substring(i, j);
                output.add(new Term(termStr));
                
                i = j;
            } else {
                output.add(new Term("" + ch));
                i++;
            }
        }
        return output;
    }
}