Java Implementation Excel Formula topic (vii)--statistical function

Source: Internet
Author: User

Statistical functions are mainly implemented by more complex statistical functions such as COUNTIF, SUMIF, frequency, also, directly on the code
/** * Project Name: * File Description: * Main Features: File Description: Excel function Type: statistical function * Simple functions such as sum,average and so on are not implemented * Version number: 1.0 * Producer: Liu Morning * created: 2013-12-3 **/p Ackage Excel;import Games. Mathtools;import Javax.script.scriptengine;import Javax.script.scriptenginemanager;import javax.script.scriptexception;/** * @author LCX * */public class Countfunctions {/** * Statistics satisfies the number of conditions * @param range * @param cr Iteria * @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;} /** * Statistics meet the number of conditions * @param range * @param criteria * @return */public static int countif (int[][] range,string criteria) {SCR Iptenginemanager 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 ("Sort array and Tags");} /** * for simplified processing, here you need to ensure that the input is an array of ascending order * @param array * @param tags * @return */public static int[] Frequency (int [] array,int[] t AGS) {sort ();//Sort, where it is not implemented if (array==null| | tags==null| | array.length==0| | array.length==0) throw new IllegalArgumentException ("Array is empty"); 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 ++;}} The remaining 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) {/******************* test array related *****************************/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}));}}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Implementation Excel Formula topic (vii)--statistical function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.