Trying to come up with control break logic for a SUM program

こ雲淡風輕ζ 提交于 2019-12-12 03:36:15

问题


I am trying to add up sums that are associated with an account. So my variable vControl is going to be my account variable, and when that variable changes I need to break from the loop and print out the SUM results and the account name. I could manage the printing out part and the SUM logic easily, but I am struggling to come up with the proper loop logic that iterates through the array and breaks anytime the account changes, and when the account changes to go back into the SUM loop again. I am reading a CSV file, by the way.

    static void Main(string[] args)
    {
        string[] parsedLine;
        string temp;


        String path = @"C:\Users\jhochbau\documents\visual studio 2015\Projects\CsvReader\CsvReader\Position_2016_02_25.0415.csv";
        //var accounts = new Dictionary<string, Positions>();
        //Adding lines read into a string[];
        string[] lines = File.ReadAllLines(path);
        foreach (string line in lines)
        {
            parsedLine = line.Split(',');
            string vControl = parsedLine[0];


            //need loop here to add these sums and break when vControl changes accounts.
            //int vSettleMMSum += parsedLine[10];
            //int vOpenSum += parsedLine[6];
            //int vBuySum += parsedLine[7];
            //int vSellSum+= parsedLine[8];

        //After each Break need to print out Account name and sums from above.





        }
    }

Class File:

      public class Positions
    {
        public string account;
        public string symbol;
        public string prevClose;
        public string curPrx;
        public string settlePX;
        public string Mult;
        public double open;
        public int buy;
        public int sell;
        public string netMM;
        public double settleMM;
        public string settleDay;
        public string underlying;


    }

回答1:


foreach loop based algorithms like the one from @gmiley answer do not work because they are missing the last group. In order to be made correct, they need to duplicate some code after the loop.

That's why I prefer using an explicit enumerator and two nested loops like this:

static void Main(string[] args)
{
    var path = @"C:\Users\jhochbau\documents\visual studio 2015\Projects\CsvReader\CsvReader\Position_2016_02_25.0415.csv";
    using (var parsedLines = File.ReadLines(path).Select(line => line.Split(',')).GetEnumerator())
    {
        bool more = parsedLines.MoveNext();
        while (more)
        {
            // Initialize
            var account = parsedLines.Current[0];
            int vSettleMMSum = 0;
            int vOpenSum = 0;
            int vBuySum = 0;
            int vSellSum = 0;
            do
            {
                // Aggregate
                vSettleMMSum += int.Parse(parsedLines.Current[10]);
                vOpenSum += int.Parse(parsedLines.Current[6]);
                vBuySum += int.Parse(parsedLines.Current[7]);
                vSellSum += int.Parse(parsedLines.Current[8]);
                more = parsedLines.MoveNext();
            }
            while (more && account == parsedLines.Current[0]);
            // Consume
            // Here you have the account and sums, do whatever you like (print etc.)
        }
    }
}



回答2:


You can do what you want without breaking out of the loop, have a look at the following:

    static void Main(string[] args)
    {
        string[] parsedLine;
        string temp;


        String path = @"C:\Users\jhochbau\documents\visual studio 2015\Projects\CsvReader\CsvReader\Position_2016_02_25.0415.csv";
        //var accounts = new Dictionary<string, Positions>();
        //Adding lines read into a string[];
        string[] lines = System.IO.File.ReadAllLines(path);
        string prevControl = string.Empty;
        string currControl = string.Empty;
        int vSettleMMSum = 0;
        int vOpenSum = 0;
        int vBuySum = 0;
        int vSellSum = 0;
        foreach (string line in lines)
        {
            parsedLine = line.Split(',');
            prevControl = currControl;
            currControl = parsedLine[0];


            //need loop here to add these sums and break when vControl changes accounts.
            vSettleMMSum += int.Parse(parsedLine[10]);
            vOpenSum += int.Parse(parsedLine[6]);
            vBuySum += int.Parse(parsedLine[7]);
            vSellSum += int.Parse(parsedLine[8]);

            if (!string.IsNullOrWhiteSpace(prevControl) && !string.Equals(prevControl, currControl))
            {
                //After each Break need to print out Account name and sums from above.
                // Do printing here as part of the loop, at the very end of the loop code block.

                // After printing, reset your values.
                vSettleMMSum = 0;
                vOpenSum = 0;
                vBuySum = 0;
                vSellSum = 0;
            }
        }
    }

I changed your vControl to instead use prevControl and currControl and check that they are not different.




回答3:


Change declaration of your variable. You need property not field. So place next code in your class (Program)

 private string _vControl;
        public string vControl {get
            {
                return _vControl;
            }
            set
            {
                _vControl = value;
                // print account name or what you want
            }
        }


来源:https://stackoverflow.com/questions/35725392/trying-to-come-up-with-control-break-logic-for-a-sum-program

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!