01204212/expr

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
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

Example

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;
    }
}