蓝桥杯-公式解析

/*

在某些应用中,为了支持灵活性,每每用到自定义的公式。

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

    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();

    }

}
相关文章
相关标签/搜索