Example #1
0
        private void Creat(List <Variable> variables) //动态生成未知数label
        {
            if (varlabels.Count != variables.Count)   //如果这个未知数没有创建Label
            {
                for (int i = varlabels.Count; i < variables.Count; i++)
                {
                    Creat_Vari_Label(variables[i].name, variables[i].value.ToString());
                }
            }
            else
            {
                if (textStr.Contains("="))  //变量Label已经存在的
                {
                    string[] str = textStr.Split('=');
                    int      n   = variables.IndexOf(new Variable(str[0]));

                    double t = Expressions.variables[n].value;
                    varSliders[n].Value = t;

                    varlabels[n].Content = variables[n].name + "=" + variables[n].value;

                    for (int i = 0; i < variables[n].expressions.Count; i++)
                    {
                        double result = exp.Cal(exp.AddSub(exp.Division(variables[n].expressions[i]))); //将表达式重新计算
                        int    m      = variables[n].expIndex[i];                                       //找到这个表达式在label上的索引
                        expLabels[m].Content = variables[n].expressions[i] + "=" + result.ToString();
                    }
                }
            }
        }
Example #2
0
        public double Func2()  //返回每个因子
        {
            double temp = 0;

            string[] str = { "([{", ")]}" };

            Expressions expressions = new Expressions();

            //MessageBox.Show(facList[0]);


            if (str[0].Contains(facList[0]))  //删除这一项外面的括号((x-y)*x) -> (x-y)*z
            {
                int count = 0;
                for (int i = 0; i < facList.Count; i++)
                {
                    if (str[0].Contains(facList[i]))
                    {
                        count++;
                    }
                    else if (str[1].Contains(facList[i]))
                    {
                        count--;
                    }

                    if (count == 0)
                    {
                        if (i == facList.Count - 1)
                        {
                            facList.RemoveAt(facList.Count - 1);
                            facList.RemoveAt(0);
                            break;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }

            if (facList.Count == 1)                        //如果只有一项,只能是变量或者常量
            {
                if (double.TryParse(facList[0], out temp)) //常量,直接将字符转化为double的数
                {
                    return(temp);
                }
                else if (Expressions.variables.Contains(new Variable(facList[0])))   //变量
                {
                    int n = Expressions.variables.IndexOf(new Variable(facList[0])); //找到这个变量的索引
                    temp = Expressions.variables[n].value;

                    return(temp);
                }
            }
            else  //表达式||函数
            {
                int    tpm = IsFunc(facList);
                double number1; //第一个参数
                double number2; //第二个参数(双目运算

                List <string> exp1, exp2;

                if (tpm == 1)                                        //函数
                {
                    if (Function.dou_Operators.Contains(facList[0])) //双目
                    {
                        //MessageBox.Show("双目");
                        int n     = 0;
                        int count = 0;
                        exp1 = new List <string>();  //^前
                        for (int i = 2; i < facList.Count; i++)
                        {
                            if (str[0].Contains(facList[i]))
                            {
                                count++;
                            }
                            else if (str[1].Contains(facList[i]))
                            {
                                count--;
                            }
                            exp1.Add(facList[i]);

                            if (count == 0 && facList[i + 1] == ",")
                            {
                                n = i;
                                break;
                            }
                        }

                        exp2 = new List <string>(); //^后
                        for (int i = n + 2; i < facList.Count - 1; i++)
                        {
                            exp2.Add(facList[i]);
                        }


                        number1 = expressions.Cal(exp1);
                        number2 = expressions.Cal(exp2);

                        Function factor = new Function(facList[0], number1, number2);
                        temp = factor.Func();
                        return(temp);
                    }
                    else if (Function.m_Operators.Contains(facList[0]))   //单目
                    {
                        //MessageBox.Show("单目");

                        exp1 = new List <string>();
                        string str2 = "";

                        for (int i = 2; i < facList.Count - 1; i++)
                        {
                            exp1.Add(facList[i]);
                            str2 += facList[i];
                        }


                        //MessageBox.Show(str2);

                        number1 = expressions.Cal(exp1);

                        Function factor = new Function(facList[0], number1);
                        temp = factor.Func();
                        return(temp);
                    }
                }
                else if (tpm == 2)//处理乘方 ^
                {
                    //MessageBox.Show("liang");

                    int count = 0;
                    int n     = 0;

                    //string stttt = "";
                    exp1 = new List <string>();  //^前
                    for (int i = 0; i < facList.Count; i++)
                    {
                        //stttt += facList[i];

                        if (str[0].Contains(facList[i]))
                        {
                            count++;
                        }
                        else if (str[1].Contains(facList[i]))
                        {
                            count--;
                        }
                        exp1.Add(facList[i]);
                        if (count == 0)
                        {
                            if (!Function.dou_Operators.Contains(facList[i]) && !Function.m_Operators.Contains(facList[i]))
                            {
                                n = i;
                                break;
                            }
                        }
                    }

                    //MessageBox.Show(stttt);
                    exp2 = new List <string>(); //^后
                    for (int i = n + 2; i < facList.Count; i++)
                    {
                        exp2.Add(facList[i]);
                        //stttt += facList[i];
                    }

                    number1 = expressions.Cal(exp1);
                    number2 = expressions.Cal(exp2);

                    Function factor = new Function("^", number1, number2);

                    temp = factor.Func();
                    return(temp);
                }
                else  //表达式
                {
                    //MessageBox.Show("falssss");

                    if (facList.Count > 0)
                    {
                        temp = expressions.Cal(facList);
                    }

                    return(temp);
                }
            }
            MessageBox.Show("error???");

            return(temp);
        }