第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在C#中計(jì)算簡(jiǎn)單移動(dòng)平均線?

如何在C#中計(jì)算簡(jiǎn)單移動(dòng)平均線?

C#
元芳怎么了 2022-08-20 15:29:01
我只是迷路了,似乎不知道該怎么計(jì)算簡(jiǎn)單的移動(dòng)平均線?這是文件中用于計(jì)算簡(jiǎn)單移動(dòng)平均線的方法之一。        public async Task<decimal?> UpdateAsync(decimal? value, CancellationToken cancelToken)        {            await Task.Run(() =>            {                var av = default(decimal?);                if (_av.Count - 1 >= _p)                {                }                else                {                    av = value;                }                _av.Add(av);                return av;            }, cancelToken).ConfigureAwait(false);            throw new Exception("major issue");        }    }
查看完整描述

2 回答

?
慕勒3428872

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超6個(gè)贊

讓我們從問(wèn)題開(kāi)始:


我只是迷路了,似乎不知道該怎么計(jì)算簡(jiǎn)單的移動(dòng)平均線?


鑒于


public static class Extensions

{

   public static IEnumerable<decimal> MovingAvg(this IEnumerable<decimal> source, int period)

   {

      var buffer = new Queue<decimal>();


      foreach (var value in source)

      {

         buffer.Enqueue(value);


         // sume the buffer for the average at any given time

         yield return buffer.Sum() / buffer.Count;


         // Dequeue when needed 

         if (buffer.Count == period)

            buffer.Dequeue();

      }

   }

}

用法


static  void Main(string[] args)

{

   var input = new decimal[] { 1, 2, 2, 4, 5, 6, 6, 6, 9, 10, 11, 12, 13, 14, 15 };

   var result = input.MovingAvg(2);

   Console.WriteLine(string.Join(", ",result));

}

輸出


1, 1.5, 2, 3, 4.5, 5.5, 6, 6, 7.5, 9.5, 10.5, 11.5, 12.5, 13.5, 14.5


查看完整回答
反對(duì) 回復(fù) 2022-08-20
?
慕慕森

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超17個(gè)贊

所以首先什么是移動(dòng)平均線


簡(jiǎn)單移動(dòng)平均線是一種計(jì)算數(shù)字流平均值的方法,它僅對(duì)流中的最后一個(gè) P 數(shù)字求平均值,其中 P 稱為周期。


接下來(lái)是代碼,就像我想象我的移動(dòng)平均線是如何實(shí)現(xiàn)的??梢允褂茫窃谖覀兲砑訑?shù)字后的每一步,我都希望我的平均值得到更新。LINQ


我的代碼被大量注釋,所以請(qǐng)閱讀注釋。


public class MovingAverageCalculator {


    /// <summary>

    /// Maximum number of numbers this moving average calculator can hold.

    /// </summary>

    private readonly int maxElementCount;


    /// <summary>

    /// Numbers which will be used for calculating moving average.

    /// </summary>

    private readonly int[] currentElements;


    /// <summary>

    /// At which index the next number will be added.

    /// </summary>

    private int currentIndex;


    /// <summary>

    /// How many elements are there currently in this moving average calculator.

    /// </summary>

    private int currentElementCount;


    /// <summary>

    /// Current total of all the numbers that are being managed by this moving average calculator

    /// </summary>

    private double currentTotal;


    /// <summary>

    /// Current Average of all the numbers.

    /// </summary>

    private double currentAvg;


    /// <summary>

    /// An object which can calcauclate moving average of given numbers.

    /// </summary>

    /// <param name="period">Maximum number elements</param>

    public MovingAverageCalculator(int period) {

        maxElementCount = period;

        currentElements = new int[maxElementCount];

        currentIndex = 0;

        currentTotal = 0;

        currentAvg = 0;

    }


    /// <summary>

    /// Adds an item to the moving average series then updates the average.

    /// </summary>

    /// <param name="number">Number to add.</param>

    public void AddElement(int number){

        // You can only have at most maximum elements in your moving average series.

        currentElementCount = Math.Min(++currentElementCount, maxElementCount);

        // IF your current index reached the maximum allowable number then you must reset

        if (currentIndex == maxElementCount){

            currentIndex = 0;

        }

        // Copy the current index number to the local variable

        var temp = currentElements[currentIndex];

        // Substract the value from the current total because it will be replaced by the added number.

        currentTotal -= temp;

        // Add the number to the current index

        currentElements[currentIndex] = number;

        // Increase the total by the added number.

        currentTotal += number;

        // Increase index

        currentIndex++;

        // Calculate average

        currentAvg = (double)currentTotal / currentElementCount;

    }


    /// <summary>

    /// Gets the current average

    /// </summary>

    /// <returns>Average</returns>

    public double GetAverage() => currentAvg;

}

最后,我檢查了你的代碼,但它對(duì)我來(lái)說(shuō)沒(méi)有意義


查看完整回答
反對(duì) 回復(fù) 2022-08-20
  • 2 回答
  • 0 關(guān)注
  • 147 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)