Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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)

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

Thanks for your help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
129 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...