package com.steering.mes.signature;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 平均值误差处理
 * @author Administrator
 */
public class MeanValueCalculation {
    /**
     * 去除平均值误差,返回LIST<double[]> list里五个double数组,数组里两个数据,第一个支数,第二个单支重量
     * @param wgt 重量
     * @param num 支数
     * @return LIST<double[]>
     */
    public static List<double[]> MeanCalculation(double wgt, int num) {
        List<double[]> list = new ArrayList<double[]>();
        double[] num1 = new double[2];
        double[] num2 = new double[2];
        double[] num3 = new double[2];
        double[] num4 = new double[2];
        double[] numSurplus = new double[2];
        DecimalFormat df = new DecimalFormat("######0.000");
        String meanValue = df.format(wgt / num);
        String error = df.format(wgt - Double.parseDouble(meanValue) * num);
        int index = error.indexOf(".");
        if (num < 10) {
            num1[0] = 1;
            num1[1] = Double.parseDouble(df.format(Double.parseDouble(error)
                    + Double.parseDouble(meanValue)));
            num2[0] = 0;
            num2[1] = 0;
            num3[0] = 0;
            num3[1] = 0;
            num4[0] = 0;
            num4[1] = 0;
            numSurplus[0] = num - num1[0];
            numSurplus[1] = Double.parseDouble(meanValue);
            if (numSurplus[0] <= 0) {
                numSurplus[0] = 0;
                numSurplus[1] = 0;
            }
        }
        if (num >= 10 && num < 100) {
            double a = Double
                    .parseDouble(error.substring(index + 3, index + 4)) / 1000;
            if (error.startsWith("-")) {
                a = -a;
            }
            num1[0] = 1;
            num1[1] = Double.parseDouble(df.format(a
                    + Double.parseDouble(meanValue)));
            num2[0] = 10;
            if (num == 10) {
                num2[0] = 9;
            }
            num2[1] = Double
                    .parseDouble(df.format((Double.parseDouble(error) - a)
                            / num2[0] + Double.parseDouble(meanValue)));
            num3[0] = 0;
            num3[1] = 0;
            num4[0] = 0;
            num4[1] = 0;
            numSurplus[0] = num - num1[0] - num2[0];
            numSurplus[1] = Double.parseDouble(meanValue);
            if (numSurplus[0] <= 0) {
                numSurplus[0] = 0;
                numSurplus[1] = 0;
            }
        }
        if (num >= 100 && num < 1000) {
            double a = Double
                    .parseDouble(error.substring(index + 3, index + 4)) / 1000;
            if (error.startsWith("-")) {
                a = -a;
            }
            num1[0] = 1;
            num1[1] = a + Double.parseDouble(meanValue);
            double b = Double.parseDouble(df.format(Double.parseDouble(error
                    .substring(index + 2, index + 3)) / 100));
            if (error.startsWith("-")) {
                b = -b;
            }
            num2[0] = 10;
            num2[1] = Double.parseDouble(df.format(b / num2[0]
                    + Double.parseDouble(meanValue)));
            num3[0] = 100;
            if (num >= 100 && num <= 110) {
                num3[0] = num - 11;
            }
            num3[1] = Double.parseDouble(df.format((Double.parseDouble(error)
                    - a - b)
                    / num3[0] + Double.parseDouble(meanValue)));
            num4[0] = 0;
            num4[1] = 0;
            numSurplus[0] = num - num1[0] - num2[0] - num3[0];
            numSurplus[1] = Double.parseDouble(meanValue);
            if (numSurplus[0] <= 0) {
                numSurplus[0] = 0;
                numSurplus[1] = 0;
            }
        }
        if (num >= 1000 && num < 10000) {
            double a = Double
                    .parseDouble(error.substring(index + 3, index + 4)) / 1000;
            if (error.startsWith("-")) {
                a = -a;
            }
            num1[0] = 1;
            num1[1] = a + Double.parseDouble(meanValue);
            double b = Double.parseDouble(df.format(Double.parseDouble(error
                    .substring(index + 2, index + 3)) / 100));
            if (error.startsWith("-")) {
                b = -b;
            }
            num2[0] = 10;
            num2[1] = b / num2[0] + Double.parseDouble(meanValue);
            double c = Double.parseDouble(df.format(Double.parseDouble(error
                    .substring(index + 1, index + 2)) / 10));
            if (error.startsWith("-")) {
                c = -c;
            }
            num3[0] = 100;
            num3[1] = Double.parseDouble(df.format(c / 100
                    + Double.parseDouble(meanValue)));
            num4[0] = 1000;
            if (num >= 1000 && num <= 1110) {
                num4[0] = 1110-num;
            }
            num4[1] = (Double.parseDouble(error) - a - b - c) / num4[0]
                    + Double.parseDouble(meanValue);
            numSurplus[0] = num - num1[0] - num2[0] - num3[0] - num4[0];
            numSurplus[1] = Double.parseDouble(meanValue);
            if (numSurplus[0] <= 0) {
                numSurplus[0] = 0;
                numSurplus[1] = 0;
            }
        }
        if (num >= 10000) {
            num1[0] = 0;
            num1[1] = 0;
            num2[0] = 0;
            num2[1] = 0;
            num3[0] = 0;
            num3[1] = 0;
            num4[0] = 0;
            num4[1] = 0;
            numSurplus[0] = 0;
            numSurplus[1] = 0;
        }
        list.add(num1);
        list.add(num2);
        list.add(num3);
        list.add(num4);
        list.add(numSurplus);
        return list;
    }
    
    public static List<double[]> MeanCalculationPro(double wgt, int num) {
        List<double[]> list = new ArrayList<double[]>();
        double[] num1 = new double[2];
        double[] num2 = new double[2];
        double[] num3 = new double[2];
        double[] num4 = new double[2];
        double[] numSurplus = new double[2];
        DecimalFormat df = new DecimalFormat("######0.000");
        String meanValue = df.format(wgt / num);
        String error = df.format(wgt - Double.parseDouble(meanValue) * num);
        int index = error.indexOf(".");
        if (num < 10) {
            num1[0] = 1;
            num1[1] = Double.parseDouble(df.format(Double.parseDouble(error)
                    + Double.parseDouble(meanValue)));
            num2[0] = 0;
            num2[1] = 0;
            num3[0] = 0;
            num3[1] = 0;
            num4[0] = 0;
            num4[1] = 0;
            numSurplus[0] = num - num1[0];
            numSurplus[1] = Double.parseDouble(meanValue);
            if (numSurplus[0] <= 0) {
                numSurplus[0] = 0;
                numSurplus[1] = 0;
            }
        }
        if (num >= 10 && num < 100) {
            double a = Double
                    .parseDouble(error.substring(index + 3, index + 4)) / 1000;
            if (error.startsWith("-")) {
                a = -a;
            }
            num1[0] = 1;
            num1[1] = Double.parseDouble(df.format(a
                    + Double.parseDouble(meanValue)));
            num2[0] = 10;
            if (num == 10) {
                num2[0] = 9;
            }
            num2[1] = Double
                    .parseDouble(df.format((Double.parseDouble(error) - a)
                            / num2[0] + Double.parseDouble(meanValue)));
            num3[0] = 0;
            num3[1] = 0;
            num4[0] = 0;
            num4[1] = 0;
            numSurplus[0] = num - num1[0] - num2[0];
            numSurplus[1] = Double.parseDouble(meanValue);
            if (numSurplus[0] <= 0) {
                numSurplus[0] = 0;
                numSurplus[1] = 0;
            }
        }
        if (num >= 100 && num < 1000) {
            double a = Double
                    .parseDouble(error.substring(index + 3, index + 4)) / 1000;
            if (error.startsWith("-")) {
                a = -a;
            }
            num1[0] = 1;
            num1[1] = a + Double.parseDouble(meanValue);
            double b = Double.parseDouble(df.format(Double.parseDouble(error
                    .substring(index + 2, index + 3)) / 100));
            if (error.startsWith("-")) {
                b = -b;
            }
            num2[0] = 10;
            num2[1] = Double.parseDouble(df.format(b / num2[0]
                    + Double.parseDouble(meanValue)));
            num3[0] = 100;
            if (num >= 100 && num <= 110) {
                num3[0] = num - 11;
            }
            num3[1] = Double.parseDouble(df.format((Double.parseDouble(error)
                    - a - b)
                    / num3[0] + Double.parseDouble(meanValue)));
            num4[0] = 0;
            num4[1] = 0;
            numSurplus[0] = num - num1[0] - num2[0] - num3[0];
            numSurplus[1] = Double.parseDouble(meanValue);
            if (numSurplus[0] <= 0) {
                numSurplus[0] = 0;
                numSurplus[1] = 0;
            }
        }
        if (num >= 1000 && num < 10000) {
            double a = Double
                    .parseDouble(error.substring(index + 3, index + 4)) / 1000;
            if (error.startsWith("-")) {
                a = -a;
            }
            num1[0] = 1;
            num1[1] = a + Double.parseDouble(meanValue);
            double b = Double.parseDouble(df.format(Double.parseDouble(error
                    .substring(index + 2, index + 3)) / 100));
            if (error.startsWith("-")) {
                b = -b;
            }
            num2[0] = 10;
            num2[1] = b / num2[0] + Double.parseDouble(meanValue);
            double c = Double.parseDouble(df.format(Double.parseDouble(error
                    .substring(index + 1, index + 2)) / 10));
            if (error.startsWith("-")) {
                c = -c;
            }
            num3[0] = 100;
            num3[1] = Double.parseDouble(df.format(c / 100
                    + Double.parseDouble(meanValue)));
            num4[0] = 1000;
            if (num >= 1000 && num <= 1110) {
                num4[0] = 1110-num;
            }
            num4[1] = (Double.parseDouble(error) - a - b - c) / num4[0]
                    + Double.parseDouble(meanValue);
            numSurplus[0] = num - num1[0] - num2[0] - num3[0] - num4[0];
            numSurplus[1] = Double.parseDouble(meanValue);
            if (numSurplus[0] <= 0) {
                numSurplus[0] = 0;
                numSurplus[1] = 0;
            }
        }
        if (num >= 10000) {
            num1[0] = 0;
            num1[1] = 0;
            num2[0] = 0;
            num2[1] = 0;
            num3[0] = 0;
            num3[1] = 0;
            num4[0] = 0;
            num4[1] = 0;
            numSurplus[0] = 0;
            numSurplus[1] = 0;
        }
        list.add(num1);
        list.add(num2);
        list.add(num3);
        list.add(num4);
        list.add(numSurplus);
        return list;
    }
    
    /**
     * 去除平均值误差,返回LIST<double[]> list里五个double数组,数组里两个数据,第一个支数,第二个单支长度
     * @param wgt 长度
     * @param num 支数
     * @return LIST<double[]>
     */
   
    public static List<double[]> MeanCalculationLen(double wgt, int num) {
        List<double[]> list = new ArrayList<double[]>();
        double[] num1 = new double[2];
        double[] num2 = new double[2];
        double[] num3 = new double[2];
        double[] num4 = new double[2];
        double[] numSurplus = new double[2];
        DecimalFormat df = new DecimalFormat("######0.00");
        String meanValue = df.format(wgt / num);
        String error = df.format(wgt - Double.parseDouble(meanValue) * num);
        int index = error.indexOf(".");
        if (num < 10) {
            num1[0] = 1;
            num1[1] = Double.parseDouble(df.format(Double.parseDouble(error)
                    + Double.parseDouble(meanValue)));
            num2[0] = 0;
            num2[1] = 0;
            num3[0] = 0;
            num3[1] = 0;
            num4[0] = 0;
            num4[1] = 0;
            numSurplus[0] = num - num1[0];
            numSurplus[1] = Double.parseDouble(meanValue);
            if (numSurplus[0] <= 0) {
                numSurplus[0] = 0;
                numSurplus[1] = 0;
            }
        }
        if (num >= 10 && num < 100) {
            double a = Double
                    .parseDouble(error.substring(index + 2, index + 3)) / 100;
            if (error.startsWith("-")) {
                a = -a;
            }
            num1[0] = 1;
            num1[1] = Double.parseDouble(df.format(a
                    + Double.parseDouble(meanValue)));
            num2[0] = 10;
            if (num == 10) {
                num2[0] = 9;
            }
            num2[1] = Double
                    .parseDouble(df.format((Double.parseDouble(error) - a)
                            / num2[0] + Double.parseDouble(meanValue)));
            num3[0] = 0;
            num3[1] = 0;
            num4[0] = 0;
            num4[1] = 0;
            numSurplus[0] = num - num1[0] - num2[0];
            numSurplus[1] = Double.parseDouble(meanValue);
            if (numSurplus[0] <= 0) {
                numSurplus[0] = 0;
                numSurplus[1] = 0;
            }
        }
        if (num >= 100 && num < 1000) {
            double a = Double
                    .parseDouble(error.substring(index + 2, index + 3)) / 100;
            if (error.startsWith("-")) {
                a = -a;
            }
            num1[0] = 1;
            num1[1] = a + Double.parseDouble(meanValue);
            double b = Double.parseDouble(df.format(Double.parseDouble(error
                    .substring(index + 1, index + 2)) / 10));
            if (error.startsWith("-")) {
                b = -b;
            }
            num2[0] = 10;
            num2[1] = Double.parseDouble(df.format(b / num2[0]
                    + Double.parseDouble(meanValue)));
            num3[0] = 100;
            if (num >= 100 && num <= 110) {
                num3[0] = num - 11;
            }
            num3[1] = Double.parseDouble(df.format((Double.parseDouble(error)
                    - a - b)
                    / num3[0] + Double.parseDouble(meanValue)));
            num4[0] = 0;
            num4[1] = 0;
            numSurplus[0] = num - num1[0] - num2[0] - num3[0];
            numSurplus[1] = Double.parseDouble(meanValue);
            if (numSurplus[0] <= 0) {
                numSurplus[0] = 0;
                numSurplus[1] = 0;
            }
        }
        if (num >= 1000 && num < 10000) {
            double a = Double
                    .parseDouble(error.substring(index + 2, index + 3)) / 100;
            if (error.startsWith("-")) {
                a = -a;
            }
            num1[0] = 1;
            num1[1] = a + Double.parseDouble(meanValue);
            double b = Double.parseDouble(df.format(Double.parseDouble(error
                    .substring(index + 1, index + 2)) / 10));
            if (error.startsWith("-")) {
                b = -b;
            }
            num2[0] = 10;
            num2[1] = b / num2[0] + Double.parseDouble(meanValue);
            double c = Double.parseDouble(df.format(Double.parseDouble(error
                    .substring(index - 1, index)) ));
            if (error.startsWith("-")) {
                c = -c;
            }
            num3[0] = 100;
            num3[1] = Double.parseDouble(df.format(c /  num3[0]
                    + Double.parseDouble(meanValue)));
            num4[0] = 1000;
            if (num >= 1000 && num <= 1110) {
                num4[0] = 1110-num;
            }
            num4[1] = (Double.parseDouble(error) - a - b - c) / num4[0]
                    + Double.parseDouble(meanValue);
            numSurplus[0] = num - num1[0] - num2[0] - num3[0] - num4[0];
            numSurplus[1] = Double.parseDouble(meanValue);
            if (numSurplus[0] <= 0) {
                numSurplus[0] = 0;
                numSurplus[1] = 0;
            }
        }
        if (num >= 10000) {
            num1[0] = 0;
            num1[1] = 0;
            num2[0] = 0;
            num2[1] = 0;
            num3[0] = 0;
            num3[1] = 0;
            num4[0] = 0;
            num4[1] = 0;
            numSurplus[0] = 0;
            numSurplus[1] = 0;
        }
        list.add(num1);
        list.add(num2);
        list.add(num3);
        list.add(num4);
        list.add(numSurplus);
        return list;
    }
    
    public static void main(String[] args) {
        List<double[]> list = MeanValueCalculation.MeanCalculation(167.55,1555);
        System.out.print(list.get(0)[0] + "," + list.get(0)[1] + ";  "
                + list.get(1)[0] + "," + list.get(1)[1] + ";  "
                + list.get(2)[0] + "," + list.get(2)[1] + ";  "
                + list.get(3)[0] + "," + list.get(3)[1] + ";  "
                + list.get(4)[0] + "," + list.get(4)[1]);
    }
}
