试图为SUM程序提出控制中断逻辑 [英] Trying to come up with control break logic for a SUM program

查看:67
本文介绍了试图为SUM程序提出控制中断逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试总计与一个帐户相关联的款项.因此,我的变量vControl将成为我的帐户变量,并且当该变量发生更改时,我需要退出循环并打印出SUM结果和帐户名.我可以轻松地管理打印部分和SUM逻辑,但是我在努力提出适当的循环逻辑,该逻辑遍历数组并在帐户更改时随时中断,并在帐户更改时再次中断以返回到SUM循环中.顺便说一下,我正在读取CSV文件.

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.





        }
    }

类文件:

      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;


    }

推荐答案

foreach基于循环的算法(如@gmiley答案中的算法)不起作用,因为它们缺少最后一组.为了变得正确,他们需要在循环后重复一些代码.

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.)
        }
    }
}

这篇关于试图为SUM程序提出控制中断逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆