本文介绍了谁能帮我在C#中获得输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
PALINK-ID PANELS
1.1 10
20
************************************
1.2 10
20
-----------------------------
2.1 10
20
30
--------------------------------------
2.2 10
20
30
我希望它在分裂.并计算面板中的元素并返回
输出:
1个具有2个面板
2具有3个面板(基于不同的值)
i want it to split at . and count the elements in panel and return it
output :
1 has 2 panels
2 has 3 panels(based distinct values)
推荐答案
public IEnumerable<Pair> CountThat(string data)
{
var result = new List<Pair>();
var lines = new List<string>(data.Split(new [] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries));
lines = lines.Where(x => !x.Contains(''-'') && !x.Contains(''*'')).ToList();
double id = 0;
foreach (var line in lines)
{
var item = line.Replace("\t", " "); // not sure if you''re using tab''s or spaces
var sections = new List<string>(item.Split(new []{'' ''}, StringSplitOptions.RemoveEmptyEntries));
if (sections.Count > 1)
{
if (!double.TryParse(sections[0], out id))
id = 0;
int panel;
if (!int.TryParse(sections[1], out panel))
panel = 0;
if ((id != 0) && (panel != 0))
{
var pair = new Pair {Key = (int) id, Value = panel};
if(!result.Contains(pair))
result.Add(pair);
}
}
else
{
int panel;
if ((int.TryParse(sections[0], out panel)) &&
(0 != id) )
{
var pair = new Pair {Key = (int) id, Value = panel};
if (!result.Contains(pair))
result.Add(pair);
}
}
}
return result;
}
然后查看结果,如:
then go over the result like:
foreach (var key in result.Select(x => x.Key).Distinct())
{
int closure = key;
int count = result.Count(x => x.Key == closure);
Console.WriteLine(closure + " has " + count + " panels");
}
我以某种方式感到自己正在做作业....
如果这对您有用,请投票,接受答案并感到高兴.
此致AT
I somehow get the feeling that I''m doing your homework....
If this works for you, upvote it, accept the answer and be happy.
Regards, AT
这篇关于谁能帮我在C#中获得输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!