Esempio n. 1
0
        public static Expr ToCoord(string coord)
        {
            int  number;
            bool result = LogicSharp.IsInt(coord, out number);

            if (result)
            {
                if (number < 0)
                {
                    var abs = Math.Abs(number);
                    return(new CompositeExpr(WellKnownSym.minus,
                                             new Expr[] { new IntegerNumber(abs) }));
                }
                return(new IntegerNumber(coord));
            }

            double dNumber;

            result = LogicSharp.IsDouble(coord, out dNumber);

            if (result)
            {
                if (number < 0)
                {
                    var abs = Math.Abs(number);
                    return(new CompositeExpr(WellKnownSym.minus,
                                             new Expr[] { new DoubleNumber(abs) }));
                }
                return(new DoubleNumber(dNumber));
            }



            return(new WordSym(coord));
        }
Esempio n. 2
0
        public static Expr Generate(object obj)
        {
            var boolValue = obj as bool?;

            if (boolValue != null)
            {
                return(new WordSym(boolValue.ToString()));
            }

            var ss = obj as ShapeSymbol;

            if (ss != null)
            {
                return(Generate(ss));
            }

            var eqGoal = obj as EqGoal;

            if (eqGoal != null)
            {
                return(Generate(eqGoal));
            }

            var term = obj as Term;

            if (term != null)
            {
                return(Generate(term));
            }

            var equation = obj as Equation;

            if (equation != null)
            {
                return(Generate(equation));
            }

            var variable = obj as Var;

            if (variable != null)
            {
                return(new WordSym(variable.Token.ToString()));
            }

            int integer;

            if (LogicSharp.IsInt(obj, out integer))
            {
                var integerExpr = new IntegerNumber(integer.ToString());
                if (integer < 0)
                {
                    return(new CompositeExpr(WellKnownSym.times, new Expr[] { integerExpr }));
                }
                else
                {
                    return(integerExpr);
                }
            }

            double dNumber;

            if (LogicSharp.IsDouble(obj, out dNumber))
            {
                var doubleExpr = new WordSym(dNumber.ToString());
                //var doubleExpr = new DoubleNumber(dNumber);

                if (dNumber < 0)
                {
                    return(new CompositeExpr(WellKnownSym.times, new Expr[] { doubleExpr }));
                }
                return(doubleExpr);
            }

            return(null);
        }
Esempio n. 3
0
        /// <summary>
        /// The purpose of parse string is to re-format the str
        /// x, 2x, -2x, ax, -ax,2ax, 3y,34y, y25
        /// </summary>
        /// <param name="str"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static object TransformString(string str)
        {
            char[] charArr = str.ToCharArray();
            if (charArr.Length == 1)
            {
                return(new Var(str));
            }

            bool   isNeg = false;
            string parseStr;

            if (charArr[0].Equals('-'))
            {
                isNeg    = true;
                parseStr = str.Substring(1, str.Length - 1);
            }
            else
            {
                parseStr = str;
            }

            //TODO tackle decimal number
            string[] strs = Regex.Split(parseStr, "(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
            //string[] strs = Regex.Split(parseStr, "(?<=\\D)(?=\\d)|(?<=(\\d+\\.\\d+))(?=\\D)");
            var lst = new List <object>();

            for (var i = 0; i < strs.Length; i++)
            {
                if (strs[i].Equals("."))
                {
                    throw new Exception("Cannot be decimal input");
                }

                if (i == 0)
                {
                    if (LogicSharp.IsNumeric(strs[i]))
                    {
                        int  iNum;
                        bool result000 = LogicSharp.IsInt(strs[i], out iNum);
                        if (result000)
                        {
                            iNum = isNeg ? iNum * -1 : iNum;
                            lst.Add(iNum);
                        }
                        else
                        {
                            double dNum;
                            LogicSharp.IsDouble(strs[i], out dNum);
                            dNum = isNeg ? dNum * -1 : dNum;
                            lst.Add(dNum);
                        }
                    }
                    else
                    {
                        char[] tempArr = strs[i].ToCharArray();
                        if (isNeg)
                        {
                            lst.Add(-1);
                        }
                        lst.AddRange(tempArr.Select(c => new Var(c)).Cast <object>());
                    }
                }
                else
                {
                    if (LogicSharp.IsNumeric(strs[i]))
                    {
                        int  iNum;
                        bool result000 = LogicSharp.IsInt(strs[i], out iNum);
                        if (result000)
                        {
                            lst.Add(iNum);
                        }
                        else
                        {
                            double dNum;
                            LogicSharp.IsDouble(strs[i], out dNum);
                            lst.Add(dNum);
                        }
                    }
                    else
                    {
                        char[] tempArr = strs[i].ToCharArray();
                        lst.AddRange(tempArr.Select(c => new Var(c)).Cast <object>());
                    }
                }
            }
            if (lst.Count == 1)
            {
                return(lst[0]);
            }
            return(new Term(Expression.Multiply, lst));
        }
        public static LineSymbol GenerateLine(Point pt1, Point pt2)
        {
            if (pt1.Equals(pt2))
            {
                return(null);
            }

            Debug.Assert(pt1.Concrete);
            Debug.Assert(pt2.Concrete);

            /* if (pt1.XCoordinate.Equals(pt2.XCoordinate))
             * {
             *   double d;
             *   LogicSharp.IsDouble(pt1.XCoordinate, out d);
             *   var line1 = new Line(1, null, -1 * d);
             *   return new LineSymbol(line1);
             * }
             *
             * if (pt1.YCoordinate.Equals(pt2.YCoordinate))
             * {
             *   double d;
             *   LogicSharp.IsDouble(pt1.YCoordinate, out d);
             *   var line2 = new Line(null, 1, -1 * d);
             *   return new LineSymbol(line2);
             * }*/

            //Strategy: y = mx+b, find slope m and intercept b
            //step 1: calc slope

            double p1x, p1y, p2x, p2y;

            LogicSharp.IsDouble(pt1.XCoordinate, out p1x);
            LogicSharp.IsDouble(pt1.YCoordinate, out p1y);
            LogicSharp.IsDouble(pt2.XCoordinate, out p2x);
            LogicSharp.IsDouble(pt2.YCoordinate, out p2y);


            var a = p1y - p2y;
            var b = p2x - p1x;
            var c = (p1x - p2x) * p1y + (p2y - p1y) * p1x;

            int  intB;
            int  intC;
            bool cond1 = LogicSharp.IsInt(b / a, out intB);
            bool cond2 = LogicSharp.IsInt(c / a, out intC);

            if (cond1 && cond2)
            {
                a = 1;
                b = intB;
                c = intC;
            }

            if (a < 0.0d)
            {
                a = -1 * a;
                b = -1 * b;
                c = -1 * c;
            }

            var line = new Line(a, b, c)
            {
                InputType = LineType.Relation
            };

            return(new LineSymbol(line));
        }
Esempio n. 5
0
        public static object TransformString2(string str)
        {
            char[] charArr = str.ToCharArray();
            if (charArr.Length == 1)
            {
                return(new Var(str));
            }
            bool isNeg = charArr[0].Equals('-');

            var lst = new List <object>();
            int j   = -1;

            for (int i = charArr.Count() - 1; i >= 0; i--)
            {
                if (Char.IsLetter(charArr[i]))
                {
                    lst.Insert(0, new Var(charArr[i]));
                }
                else
                {
                    j = i;
                    break;
                }
            }
            if (isNeg)
            {
                if (j == 0)
                {
                    var term1 = new Term(Expression.Multiply, new List <object>()
                    {
                        -1, lst[0]
                    });
                    lst[0] = term1;

                    if (lst.Count == 1)
                    {
                        return(lst[0]);
                    }
                    return(new Term(Expression.Multiply, lst));
                }
                var  subStr = str.Substring(1, j);
                bool result = LogicSharp.IsNumeric(subStr);
                if (!result)
                {
                    return(null);
                }

                int  iNum;
                bool result000 = LogicSharp.IsInt(subStr, out iNum);
                if (result000)
                {
                    lst.Insert(0, -1 * iNum);
                }
                else
                {
                    double dNum;
                    LogicSharp.IsDouble(subStr, out dNum);
                    lst.Insert(0, -1 * dNum);
                }
            }
            else
            {
                if (j == -1)
                {
                    if (lst.Count == 1)
                    {
                        return(lst[0]);
                    }
                    return(new Term(Expression.Multiply, lst));
                }
                var subStr = str.Substring(0, j + 1);

                bool result = LogicSharp.IsNumeric(subStr);
                if (!result)
                {
                    return(null);
                }

                int  iNum;
                bool result000 = LogicSharp.IsInt(subStr, out iNum);
                if (result000)
                {
                    lst.Insert(0, iNum);
                }
                else
                {
                    double dNum;
                    LogicSharp.IsDouble(subStr, out dNum);
                    lst.Insert(0, dNum);
                }
            }
            return(new Term(Expression.Multiply, lst));
        }