【发布时间】:2014-02-24 20:26:28
【问题描述】:
这里创建了一个列表,其中存储了一个盒子的长度、高度和深度。现在,我需要对长度、高度、深度和体积相等的盒子进行分组。我需要创建具有相同长度、高度、深度的盒子的组,除了 bno(盒子编号)。请帮我解决这个问题。 @serv 这是我的实际代码,您可以根据需要进行更改。
namespace ReadInputfromText
{
class Box
{
private string bno;
private double length;
private double height;
private double depth;
private double volume;
// Declare a number of box of type string:
public string bnumber
{
get
{
return bno;
}
set
{
bno = value;
}
}
// Declare properties of box of type double:
public double blength
{
get
{
return length;
}
set
{
length = value;
}
}
public double bheight
{
get
{
return height;
}
set
{
height = value;
}
}
public double bdepth
{
get
{
return depth;
}
set
{
depth = value;
}
}
public double bvolume
{
get
{
return volume;
}
set
{
volume = value;
}
}
public static void boxdetails(string[] args)
{
String line;
List<Box> listofboxes = new List<Box>();
try
{
using (StreamReader sr = new StreamReader("c:/containervalues.txt"))
while ((line = sr.ReadLine()) != null)
{
// create new instance of container for each line in file
Box box = new Box();
// List<Box> listofboxes = new List<Box>();
string[] Parts = line.Split(' ');
// set non-static properties of container
box.bno = Parts[0];
box.length = Convert.ToDouble(Parts[1]);
box.height = Convert.ToDouble(Parts[2]);
box.depth = Convert.ToDouble(Parts[3]);
box.volume = Convert.ToDouble(Parts[4]);
// add container to list of containers
listofboxes.Add(box);
}
listofboxes = listofboxes.OrderBy(x => x.volume).ToList();
var groupedBoxes = listofboxes.GroupBy(b => new { b.depth, b.height, b.length }).Dump();
}
catch (FileNotFoundException e)
{
// FileNotFoundExceptions are handled here.
}
}
}
}
【问题讨论】:
标签: c#