[C#] 纯文本查看 复制代码 /// <summary>
/// 获得一组数据中的个数,最大值,最小值,平均值,标准差
/// </summary>
/// <param name="sData">一组数据</param>
/// <returns>个数,最大值,最小值,平均值,标准差</returns>
public double[] GetStatisData(string sData)
{
double[] dResult = new double[5]; //个数,最大值,最小值,平均值,标准差
string[] sDataList = sData.Split('|');
double dMax =-9999;
double dMin = 9999;
double dSum =0;
double dAvg =0;
double dAvgSum =0;
double dStd =0;
for (int i = 0; i < sDataList.Length; i++)
{
double dTemp;
double.TryParse(sDataList[i],out dTemp);
if (dMax <=dTemp)
{
//最大值
dMax = dTemp;
}
if (dMin >= dTemp)
{
//最小值
dMin = dTemp;
}
dSum += dTemp;
}
//平均值
dAvg = dSum / sDataList.Length;
for (int i = 0; i < sDataList.Length; i++)
{
double dTemp;
double.TryParse(sDataList[i],out dTemp);
dAvgSum += (dTemp - dAvg) * (dTemp - dAvg);
}
dAvgSum = dAvgSum / (sDataList.Length - 1);
//方差
dStd = Math.Sqrt(dAvgSum);
//个数,最大值,最小值,平均值,标准差
dResult[0] = sDataList.Length;
dResult[1] = dMax;
dResult[2] = dMin;
dResult[3] = dAvg;
dResult[4] = dStd;
return dResult;
} |