小学拼图的表达树

时间:2015-10-27 18:44:09

标签: c# expression-trees

我正在尝试用C#学习表达式树。有人可以帮我解决数学逻辑谜题 Kurt Smith ISBN 0-8069-3864-1 使用表达式树link to First Printing Edition的C#代码?或者,可能是,这个拼图最适合不同的方法?我想把一只拖鞋放在大象脚上吗? enter image description here

这是我到目前为止的代码

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; }
}

}

1 个答案:

答案 0 :(得分:0)

这是F#中的暴力解决方案。不确定这是否真的是最好的方式 - 它可以在这里工作,因为名字/姓氏/重量/山的组合相对较小(大约1.7米排列) - 但至少它有效:)

作为一个要点发布,因为它是一个相当大的代码片段。

https://gist.github.com/isaacabraham/7a4c1a9beba5bb9fd9e0