小学拼图的表达树
Expression Trees for Grade School puzzle
我正在尝试学习 C# 中的表达式树。谁能帮我解决 Math Logic Puzzles 中的这个难题,作者是 Kurt Smith ISBN 0-8069-3864-1 在 C# 代码中使用表达式树 link to First Printing Edition? Or, could it be, that this puzzle is best suited for a different approach? Am I trying to put a slipper on an Elephants foot?
这是我目前的代码
using System.Collections.Generic;
using System.Linq.Expressions;
namespace ExpTreesBasics1
{
class Program
{
List<Person> ListOfPersons = new List<Person>();
static void Main(string[] args)
{
Person _paul = new Person()
{
Mountain = new Mountain(),
Pack = new Pack() { WeightExpression = Expression.Constant(30) }
};
Person _daleDorsey = new Person() {Mountain = null, Pack = new Pack() {Weight = 0}};
Person _jimMcGee = new Person() {Mountain = null, Pack = new Pack() {Weight = 0}};
Person _geraldBrown = new Person()
{
Mountain = new Mountain() {Height = 124},
Pack = new Pack() { WeightExpression = Expression.Subtract(Expression.Constant(_daleDorsey.Pack.Weight), Expression.Constant(_jimMcGee.Pack.Weight)) }
};
Person _andyStiller = new Person()
{
Mountain = new Mountain()
{
HeightExpression = Expression.Add(Expression.Constant(865), Expression.Constant(_geraldBrown.Mountain.Height))
},
Pack = new Pack() { WeightExpression = Expression.Divide(.5, Expression.Lambda(Person))}
};
var result = Expression.Add(Expression.Constant(865), Expression.Constant(_geraldBrown.Mountain.Height));
}
}
public class Person
{
public Mountain Mountain { get; set; }
public Pack Pack { get; set; }
}
public class Mountain
{
public string Name { get; set; }
public int Height { get; set; }
public Expression HeightExpression { get; set; }
}
public class Pack
{
public int Weight { get; set; }
public Expression WeightExpression { get; set; }
}
}
这是 F# 中的蛮力解决方案。不确定这是否真的是最好的方法 - 它在这里有效,因为名字/姓氏/体重/山峰的组合集相对较小(大约 1.7m 排列) - 但至少它有效:)
作为要点发布,因为它是相当大的代码片段。
我正在尝试学习 C# 中的表达式树。谁能帮我解决 Math Logic Puzzles 中的这个难题,作者是 Kurt Smith ISBN 0-8069-3864-1 在 C# 代码中使用表达式树 link to First Printing Edition? Or, could it be, that this puzzle is best suited for a different approach? Am I trying to put a slipper on an Elephants foot?
这是我目前的代码
using System.Collections.Generic;
using System.Linq.Expressions;
namespace ExpTreesBasics1
{
class Program
{
List<Person> ListOfPersons = new List<Person>();
static void Main(string[] args)
{
Person _paul = new Person()
{
Mountain = new Mountain(),
Pack = new Pack() { WeightExpression = Expression.Constant(30) }
};
Person _daleDorsey = new Person() {Mountain = null, Pack = new Pack() {Weight = 0}};
Person _jimMcGee = new Person() {Mountain = null, Pack = new Pack() {Weight = 0}};
Person _geraldBrown = new Person()
{
Mountain = new Mountain() {Height = 124},
Pack = new Pack() { WeightExpression = Expression.Subtract(Expression.Constant(_daleDorsey.Pack.Weight), Expression.Constant(_jimMcGee.Pack.Weight)) }
};
Person _andyStiller = new Person()
{
Mountain = new Mountain()
{
HeightExpression = Expression.Add(Expression.Constant(865), Expression.Constant(_geraldBrown.Mountain.Height))
},
Pack = new Pack() { WeightExpression = Expression.Divide(.5, Expression.Lambda(Person))}
};
var result = Expression.Add(Expression.Constant(865), Expression.Constant(_geraldBrown.Mountain.Height));
}
}
public class Person
{
public Mountain Mountain { get; set; }
public Pack Pack { get; set; }
}
public class Mountain
{
public string Name { get; set; }
public int Height { get; set; }
public Expression HeightExpression { get; set; }
}
public class Pack
{
public int Weight { get; set; }
public Expression WeightExpression { get; set; }
}
}
这是 F# 中的蛮力解决方案。不确定这是否真的是最好的方法 - 它在这里有效,因为名字/姓氏/体重/山峰的组合集相对较小(大约 1.7m 排列) - 但至少它有效:)
作为要点发布,因为它是相当大的代码片段。