递归算法来处理依赖变量 [英] Recursive algorithm to handle depend variables

查看:239
本文介绍了递归算法来处理依赖变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一个关于如何处理由递归算法因变量的问题。

I'd like to ask a question about how to deal with dependent variables by recursive algorithm.

C1=MA(X,10)
C2=MA(C1,10)
C3=C2.Minus(C1)
C4=MA(C3,10)
Final=C4.Minus(C3)
//Dependence:C4--> C3---> C2--->C1

在这个例子中,我开始最后一行。我的递归算法中是逐行读取找到它的因变量是处于previous行。

In this example, I begin with last line. My recursive algo is to read line by line to find whether its dependent variables is in previous line.

但是,如果我有一个更复杂的情况下

But if I have a more complicated case

C1=MA(X,10) 
C2=MA(X,20)
C3=MA(C1,5)
C4=MA(C2,10) 
C5=MA(C3,15)
C6=MA(C4,10)
Final=C6.Minus(C5)
//Dependence: C6---->C4--->C2         C5--->C3--> C1

阅读每一行发现其因变量不work.My问题是,如果有更好的方法来找到它的因变量?

Reading each line to find its dependent variables doesn't work.My question is if there is a better way to find its dependent variables?

如果你有一些想法,可以和我一起分享,非常感谢。 PS.Detailed code是这里<一个href="http://stackoverflow.com/questions/32802966/dependent-variable-loop-c-sharp?noredirect=1#comment53443702_32802966">Dependent可变环C#。

If you have some ideas that can share with me, thanks a lot. PS.Detailed code is here Dependent Variable Loop C#.

推荐答案

下面是我会亲自实施,从我的头顶部的解决这个问题。

Here is how I would personally implement a solution to this problem from the top of my head.

这将是关于构建一个操作树,它包含了一个递归方法解决(),您可以调用。

It will be about constructing an "Operation tree" which contains a recursive method Solve() that you can call.

我已写的使用的接口用于任何硬值或参考到另一前pression

I have wrote it to use an interface to be used for either hard values or "reference" to another expression.

这假定所有的操作只需要2个输入。

This assumes all operations only have 2 inputs.

public interface IExpression {
    public int Solve();
}

// class that will contain an integer number value
public class ValueExpression : IExpression {
    public int Value { get; set; }
    public ValueExpression(int value) {
        Value = value;
    }
    public int Solve() {
        return Value;
    }
}


// class that will contain definition for expressions
public class OperationExpression : IExpression {
    public IExpression Left { get; set; }
    public IExpression Right { get; set; }
    public Operations Operation { get; set; }
    public OperationExpression() { }
    public OperationExpression(IExpression left, IExpression right, Operations operation) {
        Left = left;
        Right = right;
        Operation = operation;
    }
    public enum Operations {
        MA,
        Minus,
        Add
        // other
    }
    public int Solve() {
        switch (Operation) {
            case MA :
                // You will have to implement MA method
                return MA(left.Solve(), right.Solve());
            case Minus :
                return left.Solve().Minus(right.Solve());
            case Add :
                return left.Solve().Add(right.Solve());
        }
    }
}

现在,你只需要找到一个办法让你的输入EX pressions成以上格式,这将最终看起来像一棵树。一旦你调用解决()你的根,它会返回答案。

Now you just need to find a way to get your input expressions into above format, which will end up looking like a tree. Once you call Solve() on your root, it will return the answer.

您也可以注意到,叶子都会变成数字值,而分支将是操作。

You can also note that the leaves will end up being the numeric values while branches will be the operations.

当然,现在的挑战是:

  1. 创建一个办法让你的输入IEx中的pression树格式

  1. Creating a way to get your input in the IExpression tree format

错误处理(你会怎么做,如果一个前pression不能 解决了吗?除以0,超过最大整数值,如果左/右 为null,等等)

Error handling (what will you do if an expression cannot be resolved? Dividing by 0, exceeding max integer value, if left/right is null, etc)

下面是我会设置你的复杂情况例如与上面的:

Here is how I would setup your "complicated case" example with the above:

int X = 0; // Your parameter I guess?
OperationExpression C1 = new OperationExpression(new ValueExpression(X),
    new ValueExpression(10), OperationExpression.Operations.MA);

OperationExpression C2 = new OperationExpression(new ValueExpression(X),
    new ValueExpression(20), OperationExpression.Operations.MA);

OperationExpression C3 = new OperationExpression(C1,
    new ValueExpression(5), OperationExpression.Operations.MA);

OperationExpression C4 = new OperationExpression(C2,
    new ValueExpression(10), OperationExpression.Operations.MA);

OperationExpression C5 = new OperationExpression(C3,
    new ValueExpression(15), OperationExpression.Operations.MA);

OperationExpression C6 = new OperationExpression(C4
    new ValueExpression(10), OperationExpression.Operations.MA);

OperationExpression Final = new OperationExpression(C6,
    C5, OperationExpression.Operations.Minus);

var C1Result = C1.Solve();
var C2Result = C2.Solve();
var C3Result = C3.Solve();
// etc

这篇关于递归算法来处理依赖变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆