コード例 #1
0
ファイル: MainWindow.xaml.cs プロジェクト: Jacka03/Calculate
 public MainWindow()
 {
     InitializeComponent();
     exp       = new Expressions();
     text.Text = "a * sin ( b * x + log(2,sin(a)+3x*y) ^ log(2,sin(a))) + abs(3+x) * cos( cos(g)+(b-4ac) )";//
 }
コード例 #2
0
ファイル: MainWindow.xaml.cs プロジェクト: Jacka03/Calculate
        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);
        }