统计函数主要实现的是较为复杂的统计函数如countif、sumif、frequency,也是,直接上代码
/*** 项目名称:* 文件说明:* 主要特点:文件说明:EXCEL函数类型:统计函数* 简单的函数如sum,average等等就不实现了* 版本号:1.0* * 创建时间:-12-3**/package EXCEL;import games.MathTools;import javax.script.ScriptEngine;import javax.script.ScriptEngineManager;import javax.script.ScriptException;/*** @author lcx**/public class CountFunctions {/*** 统计满足条件的个数* @param range* @param criteria* @return*/public static int sumif(int[][] range,String criteria){ScriptEngineManager man=new ScriptEngineManager();ScriptEngine engine=man.getEngineByName("javascript");int sum=0;for(int i=0;i<range.length;i++)for(int j=0;j<range[0].length;j++){try {Boolean b=(Boolean) engine.eval(range[i][j]+criteria);if(b)sum+=range[i][j];} catch (ScriptException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return sum;}/*** 统计满足条件的个数* @param range* @param criteria* @return*/public static int countif(int[][] range,String criteria){ScriptEngineManager man=new ScriptEngineManager();ScriptEngine engine=man.getEngineByName("javascript");int count=0;for(int i=0;i<range.length;i++)for(int j=0;j<range[0].length;j++){try {Boolean b=(Boolean) engine.eval(range[i][j]+criteria);if(b)count++;} catch (ScriptException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return count;}public static void sort(){System.out.println("给array和tags排序");}/*** 为简化处理,在这里边需保证输入的是升序排列的数组* @param array* @param tags* @return*/public static int[] frequency(int [] array,int[] tags){sort();//排序,在这里就不实现了if(array==null||tags==null||array.length==0||array.length==0)throw new IllegalArgumentException("数组为空");int[] res=new int[tags.length+1];int count=0;for(int i=0;i<array.length;i++){if(count>=tags.length)break;if(array[i]<=tags[count])res[count]+=1;else {i--;count++;}}//剩余的int sum=0;for(int i=0;i<res.length-1;i++)sum+=res[i];res[res.length-1]=array.length-sum;return res;}public static void main(String[] args) {/*******************测试数组相关*****************************/int [][]a={{1,2},{3,4},{5,6}};//countif(a,"<=2");//System.out.println(rank(3,a,true));System.out.println(sumif(a,"<=2"));int[] b={15,25,35,45,55,65,75,85,95};MathTools.printArray(frequency(b,new int[]{10,90}));}}