Beispiel #1
0
        void CheckDataType(string Name)
        {
            if (tdc.CheckName(Name))              //简单输出显示 变量的值
            {
                CalData datatemp;
                try {
                    datatemp = tdc.GetData(Name).GetCalData();
                } catch (AssignedError e) {
                    PushToShow(e.Message);
                    return;
                }

                switch (datatemp.CalType)
                {
                case DataType.BLOCK:
                {
                    BlockData bd = (BlockData)datatemp;
                    PushToShow(bd.ToString());
                }
                break;

                case DataType.EXP:
                {
                    ExpData ed = (ExpData)datatemp;
                    try {
                        PushToShow(ed.ToString());
                    } catch (ExpError e) {
                        PushToShow(e.Message);
                    }
                    break;
                }

                case DataType.MATRIX:
                {
                    Matrix matrix = (Matrix)datatemp;
                    PushToShow(matrix.ToString());
                }
                break;

                case DataType.FUNC:
                {
                    FuncData fd = (FuncData)datatemp;
                    PushToShow(fd.ToString());
                }
                break;
                }
            }
            else
            {
                var str = SimpleCal(InputData);
                if (str == null)
                {
                    PushToShow("No result");
                }
                else
                {
                    PushToShow(str);
                }
            }
        }
        public Matrix ConvertToMatrix()
        {
            string tempExpress = strExpress.Substring(1, strExpress.Length - 2);

            string[] rows     = tempExpress.Split(';');
            string[] firstRow = rows[0].Trim().Split(new char[] { ' ', ',' });
            int      col      = 0;

            for (int i = 0; i < firstRow.Length; i++)
            {
                if (string.IsNullOrEmpty(firstRow[i]))
                {
                }
                else
                {
                    col++;
                }
            }
            int row = rows.Length;

            double[,] data = new double[row, col];
            try {
                for (int i = 0; i < row; i++)
                {
                    string[] singlerow = rows[i].Trim().Split(new char[] { ' ', ',' });
                    int      column    = 0;
                    for (int j = 0; column < col; j++)
                    {
                        if (j < singlerow.Length)
                        {
                            if (string.IsNullOrEmpty(singlerow[j]))
                            {
                                continue;
                            }
                            string str = new ExpData(singlerow[j].Trim()).GetValueEx();
                            data[i, column] = double.Parse(str);
                        }
                        else
                        {
                            data[i, column] = 0;
                        }
                        column++;
                    }
                }
            } catch (ExpError e) {
                throw new AssignedError(e.Message + "\nAssignmentError::矩阵转换出错\n");
            }
            Matrix matrix = new Matrix(data);

            cd = matrix;
            return(matrix);
        }
        public BlockData ConverToBlock()
        {
            function.FuncString funstring = new function.FuncString(strExpress);
            int leftBracket  = funstring.FindOp(0, '(');
            int rightBracket = funstring.FindOp(0, ')');
            int commaSymbol  = funstring.FindOp(0, ',');

            string[] ab = strExpress.Split(',');
            double   a  = 0;
            double   b  = 0;

            try {
                string strA = new ExpData(ab[0].Substring(1)).GetValueEx();
                string strB = new ExpData(ab[1].Substring(0, ab[1].Length - 1)).GetValueEx();
                a = double.Parse(strA);
                b = double.Parse(strB);
            } catch (ExpError e) {
                throw new AssignedError(e.Message + "\nAssignmentError::区间转换出错" + "\n");
            }
            BlockData bd = new BlockData(a, b);
            bool      left;
            bool      right;

            if (leftBracket != -1 && leftBracket < commaSymbol)
            {
                left = false;
            }
            else
            {
                left = true;
            }
            if (rightBracket != -1 && rightBracket > commaSymbol)
            {
                right = false;
            }
            else
            {
                right = true;
            }
            bd.SetBlockBounderAvailable(left, right);
            cd = bd;
            return(bd);
        }
Beispiel #4
0
        //f(x,y)=x+y+t

        /// <summary>
        /// 替换已知变量,fstr是一个字符串表达式:x+y 不是f=x+y ,
        /// 没有变量是会原串返回的
        /// 里面找未知数时会调用底层 CalString 的函数,里面已经自动排除sin,cos,avg等 函数名了
        /// </summary>
        /// <param name="fstr">含参表达式</param>
        /// <param name="ps">排除变量</param>
        /// <returns>返回FuncExpressTemp为替换后的string
        /// 或含有未知变量返回null</returns>
        public static string ReplaceParamers(string fstr, string[] ps = null)
        {
            string[]   strarr = null;
            FuncString fs     = new FuncString(fstr);

//			fs.SetFuncExpressTemp(fstr);
            fs.SetFuncExpress(fstr);            //为向下兼容不出错


            FuncCal fc = new FuncCal();

            if (ps == null)
            {
                strarr = fs.FindUnKnown();
            }
            else
            {
                strarr = fs.FindUnKnown(ps);
            }

            fs.SetParamers(strarr);            //为向下兼容不出错,不明觉厉
            if (strarr.Length != 0)
            {
                PushToShow("含有未知数 尝试替换已知变量");
            }

            string[] paramersdata = new string[strarr.Length];
            fc.SetFuncString(fs);
            for (int i = 0; i < strarr.Length; i++)
            {
                if (tdc.CheckName(strarr[i]))
                {
                    CalData datatemp = tdc.GetData(strarr[i]).GetCalData();

                    switch (datatemp.CalType)
                    {
                    case DataType.BLOCK:
                    {
                        PushToShow("Type not match!");
                    }
                    break;

                    case DataType.EXP:
                    {
                        ExpData ed = (ExpData)datatemp;
                        paramersdata[i] = ed.GetValueEx();
                        PushToShow("提取" + strarr[i] + "的值:" + paramersdata[i]);
                        break;
                    }

                    case DataType.MATRIX:
                    {
                        Matrix matrix = (Matrix)datatemp;
                        PushToShow(matrix.ToString());
                    }
                    break;
                    }

//					if (datatemp.CalType == DataType.EXP) {
//						ExpData ed = (ExpData)datatemp;
//						paramersdata[i] = ed.GetValueEx();
//						PushToShow("提取" + strarr[i] + "的值:" + paramersdata[i]);
//					}
//
//					else
//						PushToShow("Type not match!");
                    continue;
                }

                PushToShow("未发现变量" + strarr[i] + "还未定义其值\n退出");
                return(null);
            }
            if (paramersdata.Length != 0)
            {
                fc.DealFuncstring(paramersdata);
            }
            else
            {
                fs.SetFuncExpressTemp(fstr);
            }
            return(fc.GetFuncString().GetFuncExpressTemp());
        }