循环文件以获得每人的总数

时间:2012-06-19 20:45:39

标签: c# loops totals

这是我必须使用的工资单文件中的示例,它列出了员工编号,不同类型的收入,以及检查日期。目标是获得每位员工的小计,其中列出了员工的总工资和其他收入。我无法理解如何做到这一点,有人可以提出一些建议吗?我想用C#做这个。有些员工可能只有一行,其他员工可能没有正常的工资类型,只有扣除/收入(奖金,保险等......)

240 TN  DP  800.00  1696.65 .00 .00 .00 .00 65  46.20   11/10/11
240 TN  DP  800.00  1696.65 .00 .00 .00 .00 66  124.83  11/10/11
240 TN  DP  800.00  1696.65 .00 .00 .00 .00 72  5.27    11/10/11
240 TN  DP  800.00  1696.65 .00 .00 .00 .00 ZC  1933.75 11/10/11
240 TN  DP  800.00  1643.55 .00 .00 .00 .00 65  46.20   11/25/11
240 TN  DP  800.00  1643.55 .00 .00 .00 .00 66  122.18  11/25/11
240 TN  DP  800.00  1643.55 .00 .00 .00 .00 72  5.27    11/25/11
240 TN  DP  800.00  1643.55 .00 .00 .00 .00 ZC  1893.87 11/25/11
240 TN  DP  .00 .00 .00 .00 400.00  .00 66  20.00   12/09/11
240 TN  DP  800.00  1595.25 .00 80.00   .00 .00 65  46.20   12/09/11
242 TN  DP  1538.46 .00 .00 .00 .00 .00 65  .00 07/08/11
242 TN  DP  1538.46 .00 .00 .00 .00 .00 67  .00 07/08/11
242 TN  DP  1538.46 .00 .00 .00 .00 .00 69  .00 07/08/11
242 TN  DP  1538.46 .00 .00 .00 .00 .00 72  .00 07/08/11
242 TN  DP  1538.46 .00 .00 .00 .00 .00 74  .00 07/08/11
242 TN  DP  1538.46 .00 .00 .00 .00 .00 ZC  1299.03 07/08/11
242 TN  DP  1538.46 .00 .00 .00 .00 .00 65  181.85  07/22/11
242 TN  DP  1538.46 .00 .00 .00 .00 .00 67  12.46   07/22/11
242 TN  DP  1538.46 .00 .00 .00 .00 .00 69  .00 07/22/11
242 TN  DP  1538.46 .00 .00 .00 .00 .00 72  22.15   07/22/11
242 TN  DP  1538.46 .00 .00 .00 .00 .00 74  .00 07/22/11
242 TN  DP  1538.46 .00 .00 .00 .00 .00 ZC  1124.70 07/22/11

编辑:在回答一些问题时,字段按从左到右的顺序排列:员工#,州,部门,正常工资,加班工资,假期工资,假期工资,病假工资,特殊工资,收入代码,赚取金额,检查日期。我需要为每个领域获得每位员工的总数,同时确保在每个检查日期不要多次添加一个人的工资。

1 个答案:

答案 0 :(得分:0)

如果您正在使用文本文件,则应阅读它然后详细说明数据。所以这就是我要做的事情:

string[] lines = File.ReadLines(path);

然后对于数组中的每个元素(行),我会按数据分割它:

 __Data1___ ______________Data2_______________D3_ __D4__ ___Data5___
|          |                                |    |      |           |
 240 TN  DP  800.00  1696.65 .00 .00 .00 .00 65  46.20   11/10/11
 240 TN  DP  800.00  1696.65 .00 .00 .00 .00 66  124.83  11/10/11
 240 TN  DP  800.00  1696.65 .00 .00 .00 .00 72  5.27    11/10/11
 240 TN  DP  800.00  1696.65 .00 .00 .00 .00 ZC  1933.75 11/10/11
 240 TN  DP  800.00  1643.55 .00 .00 .00 .00 65  46.20   11/25/11
 240 TN  DP  800.00  1643.55 .00 .00 .00 .00 66  122.18  11/25/11
 240 TN  DP  800.00  1643.55 .00 .00 .00 .00 72  5.27    11/25/11
 240 TN  DP  800.00  1643.55 .00 .00 .00 .00 ZC  1893.87 11/25/11
 240 TN  DP  .00 .00 .00 .00 400.00  .00 66  20.00   12/09/11
 240 TN  DP  800.00  1595.25 .00 80.00   .00 .00 65  46.20   12/09/11
 242 TN  DP  1538.46 .00 .00 .00 .00 .00 65  .00 07/08/11
 242 TN  DP  1538.46 .00 .00 .00 .00 .00 67  .00 07/08/11
 242 TN  DP  1538.46 .00 .00 .00 .00 .00 69  .00 07/08/11
 242 TN  DP  1538.46 .00 .00 .00 .00 .00 72  .00 07/08/11
 242 TN  DP  1538.46 .00 .00 .00 .00 .00 74  .00 07/08/11
 242 TN  DP  1538.46 .00 .00 .00 .00 .00 ZC  1299.03 07/08/11
 242 TN  DP  1538.46 .00 .00 .00 .00 .00 65  181.85  07/22/11
 242 TN  DP  1538.46 .00 .00 .00 .00 .00 67  12.46   07/22/11
 242 TN  DP  1538.46 .00 .00 .00 .00 .00 69  .00 07/22/11
 242 TN  DP  1538.46 .00 .00 .00 .00 .00 72  22.15   07/22/11
 242 TN  DP  1538.46 .00 .00 .00 .00 .00 74  .00 07/22/11
 242 TN  DP  1538.46 .00 .00 .00 .00 .00 ZC  1124.70 07/22/11

所以我创建了一个类并获取了我需要的数据:

class Data
{
    public string Data1;
    public string Data2;
    public string Data3;
    public string Data4;
    public string Data5;
}

List<Data> myList = new List<Data>();

//Split lines
foreach(string s in lines)
{
   string[] buffer = s.Split(' '); //split by space

   Data data = new Data();
   data.Data1 = string.Concat(buffer.Take(3).ToArray());
   data.Data2 = string.Concat(buffer.Skip(3).Take(6).ToArray());
   data.Data3 = string.Concat(buffer.Skip(9).Take(1).ToArray());
   data.Data4 = string.Concat(buffer.Skip(10).Take(1).ToArray());
   data.Data5 = string.Concat(buffer.Skip(11).Take().ToArray());

   myList.Add(data);
}