我有一个数据集,我通过以下方式输出到列表中:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
class Program
{
static void Main(string[] args)
{
List<object> myList = new List<object>();
int i = 1;
using (StreamReader reader = new StreamReader("file.dat"))
{
string line;
var locations = new Dictionary<string, int[]>() {
{"210", new [] {405, 4, 128, 12, 141, 12, 247, 15, 121, 3}},
{"310", new [] {321, 4, 112, 12, 125, 12, 230, 15, 105, 3}},
{"410", new [] {477, 4, 112, 12, 125, 12, 360, 15, 105, 3}}
};
while ((line = reader.ReadLine()) != null)
{
var lineStart = line.Substring(0, 3);
if (lineStart == "210" || lineStart == "310" || lineStart == "410")
{
var currentLocations = locations[lineStart];
var letters = line.Substring(currentLocations[0], currentLocations[1]);
var tvolume =
int.Parse(line.Substring(currentLocations[2], currentLocations[3])) +
int.Parse(line.Substring(currentLocations[4], currentLocations[5]));
var tprice = long.Parse(line.Substring(currentLocations[6], currentLocations[7]));
var mvolume = tprice * tvolume * 0.01 * 0.0000001;
var currency = line.Substring(currentLocations[8], currentLocations[9]);
myList.Add(i);
myList.Add(lineStart);
myList.Add(letters);
myList.Add(tvolume);
myList.Add(tprice);
myList.Add(mvolume);
myList.Add(currency);
// double total = myList.
Console.WriteLine(i);
Console.WriteLine(lineStart);
Console.WriteLine(letters);
Console.WriteLine(tvolume * 0.01);
Console.WriteLine(tprice * 0.0000001);
Console.WriteLine("{0:N}", mvolume);
Console.WriteLine(currency + "\n");
i = i + 1;
}
}
int index = 0;
Dictionary<string, int> tvolumeDictionary = new Dictionary<string, int>();
Dictionary<string, long> mvolumeDictionary = new Dictionary<string, long>();
string listLetters;
foreach (object item in myList)
{
switch (index % 7)
{
case 2:
listLetters = item.ToString();
if (!tvolumeDictionary.Keys.Contains(listLetters))
{
tvolumeDictionary.Add(listLetters, 0);
mvolumeDictionary.Add(listLetters, 0);
}
break;
case 3:
listLetters = myList[index - 1].ToString();
tvolumeDictionary[listLetters] = tvolumeDictionary[listLetters] + (int)item;
break;
case 5:
listLetters = myList[index - 3].ToString();
mvolumeDictionary[listLetters] = mvolumeDictionary[listLetters] + long.Parse(item.ToString());
break;
}
index++;
}
// Console.WriteLine(myList.Count);
foreach (KeyValuePair<string, int> entry in tvolumeDictionary)
{
Console.WriteLine("{0} tvolume: {1} mVolume: {2}", entry.Key, entry.Value, mvolumeDictionary[entry.Key]);
}
}
}
}
File.dat 有数千行,其中将返回数百行。我希望为每个“字母”提供数字总数的细分(字母中只有大约 4 个类别)。AAAA、BBBB、CCCC 和 DDDD。如果这是 excel,我希望它对字母列中的每个更改执行小计功能,但这里的问题是它们不按顺序排列。
所需的输出是
AAAA: TOTAL TVOLUME: TOTAL MVOLUME
BBBB: TOTAL TVOLUME: TOTAL MVOLUME
CCCC: TOTAL TVOLUME: TOTAL MVOLUME
DDDD: TOTAL TVOLUME: TOTAL MVOLUME
(上述格式不是必需的)。