下面一段代码是用来求均值的(简单平均和加权平均),其中类CStockPrice是从大智慧存盘数据中读到的股价链表,你可以参考一下,里面的内容有一些统计方差的计算,去掉就行了。
void CExtraWindowThread::FillVarianceAnalysis(bool IsWeightedMean, CListCtrl& ListCtrl, int nPeriod, int nIndex)
{
#define MAX_PERCENT 900
#define MIN_PERCENT 100
CStockPrice *pFirst = pHead;
CStockPrice *pCurPos = pHead;
int nAverage = 0;
UINT nPercent[MAX_PERCENT];
double nTotal = 0;
double nAverageDeviate = 0;
double nAbsDeviate = 0;
double nMaxDeviate = 0;
double nVariance = 0;
UINT j = 0;
UINT nPositiveSum = 0, nNegativeSum = 0;
double nCurrentDeviate = 0;
int nDenominator = 0;
if (IsWeightedMean == true)
{
for (int i = 1; i <= nPeriod; i ++)
nDenominator += i;
}
else
{
nDenominator = nPeriod;
}
memset(nPercent, 0, sizeof(nPercent));
while (pCurPos != NULL)
{
if (IsWeightedMean == true)
{
CStockPrice *pTemp = pFirst;
nTotal += nPeriod * pCurPos->m_nEndPrice;
while (pTemp != pCurPos)
{
nTotal -= pTemp->m_nEndPrice;
pTemp = pTemp->m_pNext;
}
}
else
{
nTotal += pCurPos->m_nEndPrice;
}
j ++;
if (j == nPeriod)
{
nAverage = (UINT)(nTotal / nDenominator);
double nTemp = (pCurPos->m_nEndPrice - nAverage) * 100 / nAverage;
if (nTemp >= MAX_PERCENT)
{
nPercent[MAX_PERCENT + MIN_PERCENT - 1] ++;
}
else
{
nPercent[(int)nTemp + MIN_PERCENT] ++;
}
nTotal -= pFirst->m_nEndPrice;
nCurrentDeviate = ((int)pCurPos->m_nEndPrice - (int)nAverage)/1000.0;
nAverageDeviate += nCurrentDeviate;
nAbsDeviate += abs(nCurrentDeviate);
nVariance += pow(nAbsDeviate, 2);
if (nMaxDeviate < abs(nCurrentDeviate))
nMaxDeviate = abs(nCurrentDeviate);
pFirst = pFirst->m_pNext;
j --;
if (nTemp > 0)
{
nPositiveSum ++;
}
else
{
nNegativeSum ++;
}
}
pCurPos = pCurPos->m_pNext;
}
......
}