因变量循环C# [英] Dependent Variable Loop C#

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

问题描述

我想问一个有关如何处理因变量的问题.

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

C1=MA(X,10)
C2=MA(C1,10)
C3=C2.Minus(C1)
C4=MA(C3,10)
Final=C4.Minus(C3)

一个例子:Final = C4.Minus(C3),我保存了它的两个参数C4,C3.

An illustration:Final=C4.Minus(C3), I save its two parameters C4,C3.

  C4(MA ok and firstTime in MA)--> C3(MA NotOK,not fstTime)---> C2(MA ok)--->C1(MA ok)
                       0------------------->  10-------------->  10+10---->10+10+10=30

我首先创建了一个类,以获取每一行的所有信息

I created a class at first to get all the informtion of each line

    public class IndicatorInform
    {
        char[] parenthesis = new []{'(',')'};
        char[] equal = new char[] { '=' };
        char[] comma = new char[] { ';' };
        char[] all = new char [] { '.','<'};


        string text=null;
        string funcname=null;
        string[] args=null;

        public void IndicatorInform (string expression, out string text,out string funcName,out string [] args)
        {
           string [] parts= expression.Split(equal);
           text = parts[0];
           if( parts[1].Contains(";"))
           {
               string[] subparts = parts[1].Split(parenthesis);
               funcname = subparts[0];
               args = subparts[1].Split(comma);
               if(args.Count.equal(2))
               {
                   funcarg = args[0];
                   period = Convert.ToDouble(args[1]);
               }
           }
           else
           {
               if (parts[1].Contains("Plus"))
                   funcname = "Plus";
               if (parts[1].Contains("Minus"))
                   funcname = "Minus";
               if (parts[1].Contains("Multi"))
                   funcname = "Multi";
               if (parts[1].Contains("Div"))
                   funcname = "Div";
               parts[1] = parts[1].Replace(funcname, "");
               args=parts[1].Split(all);
           }
        }
        public double Shifts {get; set;}
        public double period { get; set; }
        public string Funcname { get; set; }
        public string text { get; set; }
        public string funcarg { get; set; }
    }

然后我创建了字典,并开始处理因变量

And then I created a Dictonary and begin to deal with dependant variables

   Dictionary<string,SubIndicator> Dico=new Dictionary<string,SubIndicator> ;
        foreach (var line in richTextBox1.Lines)
        {
            SubIndicator SubInc = new SubIndicator();
            Dico.Add(SubInc.text,SubInc);
        }
        int incre=0;
        double tempvalue=0;
        foreach( string element in Dico.Keys)
        {
            string[] tempo=null;
            if(Dico[element].text.Contains("Final"))
            {
                tempo=Dico[element].args;
            }
            else
            {
                if(tempo.Contains(Dico[element].text))
                {
                    if(Dico[element].Funcname.Contains("MA") )
                    {
                        if (incre.Equals(0))
                        {tempvalue=Dico[element].period;
                        incre++;}
                        else
                        {
                            Dico[element].Shifts=tempvalue+Dico[element].period;
                            tempvalue =Dico[element].Shifts;
                        }
                    }
                    else
                    {
                        Dico[element].Shifts=tempvalue;
                    }
                }
            }

我的算法适用于上述情况,但是如何处理更复杂的情况,例如

My algorithm works for the case above.But how to deal a more complicated case such as

  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)
 C6(fst Time)---->C4--->C2         C5(fst Time)--->C3--> C1
            0-->10+10-->10+10+20              0--> 15+5-->15+5+10

感谢您的帮助.

推荐答案

我认为我需要写一个更长的答案.数学函数需要具有带有元素/节点的二叉树,该树是包含左节点,右节点,值和数学运算符的类.元素通常是一个类对象,并且可以是字典中的值.叶子节点将包含值,而非叶子节点将包含带有左节点,右节点或左右节点的运算符.

I think I need to write a longer answer. Math functions need to have a binary tree with element/nodes which is a class that contains a left node, right node, value, and math operator. The element is usually a class object and can be the value in a dictionary. Leafs nodes will contain the values while non leafs nodes will have operators with either a left node, a right node, or both a left and right node.

数学函数是关联的,因此任何函数都可以分为左右两个部分,例如a = b +(c + d + e + f). b是左部分,(c + d + e + f)是右部分.右部分可以进一步分为左和右.

Math function are associated so any function can divided into a left and right component like a = b + (c + d + e + f). b is the left component and (c + d + e + f) is the right component. The right component can further by separated into a left and right.

您的代码需要两段.首先是将输入解析为元素.第二部分是计算结果.递归函数将执行计算.递归检查元素是否包含左节点和/或右节点,并继续调用递归函数,直到找到包含值的叶子为止.然后将其移回函数调用堆栈,将数学运算符替换为左右组件返回的值.

You code need two pieces. The first is to parse the input into elements. The second part is to calculate the results. The recursive function will perform the calculations. The recursive checks to see if an element contains a left, and or right nodes and keeps calling the recursive function until a leaf is found that contains a value. Then moves back up the stack of function calls replacing the math operators with value returned from left and right components.

这篇关于因变量循环C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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