藍橋杯-公式解析

/*

在某些應用中,爲了支持靈活性,每每用到自定義的公式。

好比,有以下的原始公式集合:

    int add(int x, int y):  返回x與y的和
    
    int add(int x, int y, int z):  返回x,y,z三個數的和
    
    int min(int x, int y):  返回x,y中較小的值
    
    int max(int x, int y):  返回x,y中較大的值

    int doubleMe(int x):  返回 x 的2倍

給出一個自定義公式串

add(min(5,3),max(2,8),add(1,doubleMe(1)))

經過手工計算能夠得出結果爲:14

本題的任務是:編寫一個解析程序,可以對由上述原始公式任意組合出來的公式計算其結果。也就是輸入一個自定義公式串,輸出它的計算結果(能夠不考慮輸入公式自己有語法錯誤的狀況)。

輸入的公式串中能夠含有多餘的空格,相似:

add( min(5, 3) , max(2 , 8) )  也是合法的公式。


程序輸入:公式串
程序輸出:該公式的計算值



*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Scanner;


public class 公式解析 {

    /**
     * @param args
     */
    private static String expression = null;
    private static ArrayList<String> operation = new ArrayList<String>();
    private static ArrayList<String> number = new ArrayList<String>();
    
    public static void input()throws Exception{
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        expression = in.readLine();
    }
    
    public static void process() throws Exception{
        StringReader reader = new StringReader(expression);
        StringBuilder strT = new StringBuilder();
        int n = reader.read();
        int type = 0;
        char c = ' ';
        while(n!=-1){
            c = (char)n;
            type = getType(c);
            switch(type){
            case 0:
                n = reader.read();
                break;
            case 1:
                strT.append(c);
                n = reader.read();
                while(n!=-1&&getType((char)n)==1){
                    strT.append((char)n);
                    n = reader.read();
                }
                number.add(strT.toString());
                strT.delete(0,strT.length());
                break;
            case 2:
                strT.append(c);
                n = reader.read();
                while(n!=-1&&getType((char)n)==2){
                    strT.append((char)n);
                    n = reader.read();
                }
                operation.add(strT.toString());
                strT.delete(0,strT.length());
                break;
            case 3:
                number.add(String.valueOf(c));
                n = reader.read();
                break;
            case 4:
                String str = null;
                ArrayList<String> nums = new ArrayList<String>();
                do{
                    str = number.remove(number.size()-1);
                    if(!str.equals("(")){
                        nums.add(str);
                    }else{
                        break;
                    }
                }while(true);
                String operate = operation.remove(operation.size()-1);
                String num = calculate(nums,operate);
                number.add(num);
                n = reader.read();
                break;
            case 5:
                n = reader.read();
                break;
            default:
                n = reader.read();
            }
        }
        System.out.println(number.get(0));
    }
    
    public static int getType(char c){
        if(c==' ')
            return 0;
        if(c>='0'&&c<='9'){
            return 1;
        }
        if(c>='A'&&c<='Z'||c>='a'&&c<='z'){
            return 2;
        }
        if(c=='('){
            return 3;
        }
        if(c==')'){
            return 4;
        }
        if(c==','){
            return 5;
        }
        return 6;
    }
    
    public static String calculate(ArrayList<String> nums,String operate){
        if(operate.equals("add")){
            int sum = 0;
            for(String str:nums){
                sum+=Integer.parseInt(str);
            }
            return String.valueOf(sum);
        }
        if(operate.equals("min")){
            int min = Integer.parseInt(nums.get(0));
            for(int i=1;i<nums.size();i++){
                if(min>Integer.parseInt(nums.get(i)))
                    min = Integer.parseInt(nums.get(i));
            }
            return String.valueOf(min);
        }
        if(operate.equals("max")){
            int max = Integer.parseInt(nums.get(0));
            for(int i=1;i<nums.size();i++){
                if(max<Integer.parseInt(nums.get(i)))
                    max = Integer.parseInt(nums.get(i));
            }
            return String.valueOf(max);
        }
        if(operate.equals("doubleMe")){
            int n = Integer.parseInt(nums.get(0));
            return String.valueOf(n*2);
        }
        return 0+"";
    }
    
    public static void main(String[] args) throws Exception{
        // TODO Auto-generated method stub
        input();
        process();

    }

}
相關文章
相關標籤/搜索