在C中使用3个数组组织数据#
问题描述:
我在制作一个自动化数据消息的程序。在C中使用3个数组组织数据#
我有一个Excel工作表,它有一个名称列,类型列和值列。
它看起来像这样:
Name Type Value
Sam A 32
Ben B 65
Sam B 213
max B 23
max C 24
max C 12
Ben C 45
这个数据是不是真实的,但它是类似于我一起工作。
注意:一些名称没有某些类型。
我已经在3个数组arrName[]
,arrType[]
加载的数据,并arrValue[]
我想使数据看起来像这样与3D阵列arrPro[name, type, value]
。所有相同类型的值应该属于相同的名称,并且所有值应该相加在一起以形成总值。
答
将excel数据直接载入到DataSet是最简单的方法。
如果您确实需要3D阵列,我建议将 Tuple<string, string, int>
作为每行的数据保存器。
和 List<Tuple<string, string, int>>
将保存您的所有数据。
答
这种结构可能保存数据
public class MyTypes
{
public string TypeName;
public List<NamesValues> Names;
}
public class NamesValues
{
public string Name;
public List<int> Values;
}
答
你应该使用字典。
public Enum EType
{
A,
B,
C
}
public class PatientData
{
public EType Type { get; set; }
public int Value {get; set; }
}
public class PatientManager
{
private readonly Dictionary<String, List<PatientData>> _patients = new Dictionary<String, List<PatientData>>();
public void AddPatientData(string name, EType type, int value)
{
var patientData = new PatientData
{
Type = type,
Value = value
};
List<PatientData> patientDatas;
if (!dictionary.TryGetValue(name, out patientDatas))
{
patientDatas = new List<PatientData>();
dictionary.Add(key, patientDatas);
}
_patientDatas.Add(patientData);
}
public void LoadData(string[] names, EType[] types, int[] values)
{
var iMax = Math.Min(names.Length, Math.Min(type.Length, values.Length));
for (var i = 0; i < iMax; i++)
{
AddPatientData(names[i], types[i], values[i]);
}
}
}
答
这不是simplier和清洁来存储你的数据在某些List<YourObject>
例如:
public class YourObject {
public string Name { get; set; }
public string Type{ get; set; }
public int Value { get; set; }
}
再算上你想使用LINQ(更小)的内容:
List<YourObject> yourObject = "your logic goes here (with whole data)";
List<YourObject> result = from s in yourObject
group s by new { s.Name, s. Type) into r
select new YourObject { Name = r.Name, Type = r.Type, Value = r.Sum(s => s.Value) };
我不确定这正是你想要的(分组类型)。
加载数据表/数据集中的数据。如果您不知道如何将Excel转移到DataSet,我可以为您提供代码。 – mybirthname 2014-10-10 08:37:45
我已经将数据加载到3个不同的数组中。 我想要的数据看起来像 马克斯 - A 144 - B 344 - Visual C 223 萨姆 - 一个343 - B 23 – Matty 2014-10-10 08:41:43
好男人你的来电,祝你好运。可能有人会回答你。 – mybirthname 2014-10-10 08:52:46