public MainWindow()
        {
            InitializeComponent();
            TextBox1.Focus();

            _parser.OperatorList.Add("isnot");
            _parser.OperatorList.Add("and");
            _parser.OperatorList.Add("or");

            _parser.OperatorAction.Add("isnot", (x, y) => (x != y) ? 1 : 0);
            _parser.OperatorAction.Add("and", (x, y) => (x == 1 && y == 1) ? 1 : 0);
            _parser.OperatorAction.Add("or", (x, y) => (x == 1 || y == 1) ? 1 : 0);

            _parser.LocalFunctions.Add("IsPrime", x =>
            {
                if (x[0] % 1 == 0) // check if it's an integer
                {
                    return Check.IsPrime((long)x[0]) ? 1 : 0;
                }

                throw new ArgumentException("The input is not an integer");
            });

            _parser.LocalFunctions.Add("IsOdd", x =>
            {
                if (x[0] % 1 == 0) // check if it's an integer
                {
                    return Check.IsOdd((long)x[0]) ? 1 : 0;
                }

                throw new ArgumentException("The input is not an integer");
            });

            _parser.LocalFunctions.Add("IsEven", x =>
            {
                if (x[0] % 1 == 0) // check if it's an integer
                {
                    return Check.IsEven((long)x[0]) ? 1 : 0;
                }

                throw new ArgumentException("The input is not an integer");
            });

            _parser.LocalFunctions.Add("IsCoprime", x =>
            {
                if (x[0] % 1 == 0) // check if it's an integer
                {
                    return Check.IsCoprime((long)x[0],(long)x[1]) ? 1 : 0;
                }
                
                throw new ArgumentException("The input is not an integer");
            });

            _parser.LocalFunctions.Add("gdc", x =>
            {
                if (x[0] % 1 == 0) // check if it's an integer
                {
                    return Get.Gdc((long)x[0], (long)x[1]);
                }
                
                throw new ArgumentException("The input is not an integer");
            });

            _parser.LocalFunctions.Add("lcm", x =>
            {
                if (x[0] % 1 == 0) // check if it's an integer
                {
                    return Get.Lcm((long)x[0], (long)x[1]);
                }
            
                throw new ArgumentException("The input is not an integer");
            });

            _parser.LocalFunctions.Add("mod", x =>
            {
                if (x[0] % 1 == 0) // check if it's an integer
                {
                    return Get.Gdc((long)x[0], (long)x[1]);
                }
                
                throw new ArgumentException("The input is not an integer");
            });

            _parser.LocalFunctions.Add("mean", x =>
            {
                var result = x.Sum();

                return result / x.Length;
            });


            //tables
            //one main problem, we cannot store a the variable x in a decimal
            var isDone = false;

            _parser.LocalFunctions.Add("table", x =>
            {
                // remake this method, so that the the parser.LocalVariables["x"] < parser.LocalVariables["table_max"]
                // is in front of everything. then, we can reuse  the newWindow check code
                GC.Collect();

                if (isDone == false)
                {
                    _parser.LocalVariables["x"] = _parser.LocalVariables["table_min"];
                    if (_parser.LocalVariables["newWindow"] == 1)
                    {
                        _form = new FrmTable();
                    }
                    else
                    {
                        ListView1.Items.Add("x  |  y");
                        ListView1.Items.Add("-----------");
                    }
                    isDone = true;
                }

                if (_parser.LocalVariables["x"] <= _parser.LocalVariables["table_max"])
                {
                    if (_parser.LocalVariables["newWindow"] == 1)
                    {
                        _form.Funcs.Add(_parser.LocalVariables["x"], x[0]);
                    }
                    else
                    {
                        ListView1.Items.Add(_parser.LocalVariables["x"] + "  :  " + x[0]);
                    }
                    _parser.LocalVariables["x"] += _parser.LocalVariables["table_step"];
                    _parser.Parse(TextBox1.Text);
                    _parser.LocalVariables["x"] += _parser.LocalVariables["table_step"];
                    
                }
                else
                {
                    if (_parser.LocalVariables["newWindow"] == 1)
                    {
                        _form.ShowDialog();
                    }
                    _parser.LocalVariables["x"] = 0;
                    isDone = false;
                }

                return 1;
            });
            
            _parser.LocalVariables.Add("table_min", 0);
            _parser.LocalVariables.Add("table_max", 100);
            _parser.LocalVariables.Add("table_step", 1);
            _parser.LocalVariables.Add("newWindow",1);
            _parser.LocalVariables.Add("x", 0);

            // conditional operators
            _parser.LocalFunctions.Add("if", x =>
            {
                if (x[0] == 1)
                {
                    return x[1];
                }
                
                return x.Length == 3 ? x[2] : 0;
            });

            DataGrid1.ItemsSource = _parser.LocalVariables;
        }
Example #2
0
        public MainWindow()
        {
            InitializeComponent();
            TextBox1.Focus();

            _parser.OperatorList.Add("isnot");
            _parser.OperatorList.Add("and");
            _parser.OperatorList.Add("or");

            _parser.OperatorAction.Add("isnot", (x, y) => (x != y) ? 1 : 0);
            _parser.OperatorAction.Add("and", (x, y) => (x == 1 && y == 1) ? 1 : 0);
            _parser.OperatorAction.Add("or", (x, y) => (x == 1 || y == 1) ? 1 : 0);

            _parser.LocalFunctions.Add("IsPrime", x =>
            {
                if (x[0] % 1 == 0) // check if it's an integer
                {
                    return(Check.IsPrime((long)x[0]) ? 1 : 0);
                }

                throw new ArgumentException("The input is not an integer");
            });

            _parser.LocalFunctions.Add("IsOdd", x =>
            {
                if (x[0] % 1 == 0) // check if it's an integer
                {
                    return(Check.IsOdd((long)x[0]) ? 1 : 0);
                }

                throw new ArgumentException("The input is not an integer");
            });

            _parser.LocalFunctions.Add("IsEven", x =>
            {
                if (x[0] % 1 == 0) // check if it's an integer
                {
                    return(Check.IsEven((long)x[0]) ? 1 : 0);
                }

                throw new ArgumentException("The input is not an integer");
            });

            _parser.LocalFunctions.Add("IsCoprime", x =>
            {
                if (x[0] % 1 == 0) // check if it's an integer
                {
                    return(Check.IsCoprime((long)x[0], (long)x[1]) ? 1 : 0);
                }

                throw new ArgumentException("The input is not an integer");
            });

            _parser.LocalFunctions.Add("gdc", x =>
            {
                if (x[0] % 1 == 0) // check if it's an integer
                {
                    return(Get.Gdc((long)x[0], (long)x[1]));
                }

                throw new ArgumentException("The input is not an integer");
            });

            _parser.LocalFunctions.Add("lcm", x =>
            {
                if (x[0] % 1 == 0) // check if it's an integer
                {
                    return(Get.Lcm((long)x[0], (long)x[1]));
                }

                throw new ArgumentException("The input is not an integer");
            });

            _parser.LocalFunctions.Add("mod", x =>
            {
                if (x[0] % 1 == 0) // check if it's an integer
                {
                    return(Get.Gdc((long)x[0], (long)x[1]));
                }

                throw new ArgumentException("The input is not an integer");
            });

            _parser.LocalFunctions.Add("mean", x =>
            {
                var result = x.Sum();

                return(result / x.Length);
            });


            //tables
            //one main problem, we cannot store a the variable x in a decimal
            var isDone = false;

            _parser.LocalFunctions.Add("table", x =>
            {
                // remake this method, so that the the parser.LocalVariables["x"] < parser.LocalVariables["table_max"]
                // is in front of everything. then, we can reuse  the newWindow check code
                GC.Collect();

                if (isDone == false)
                {
                    _parser.LocalVariables["x"] = _parser.LocalVariables["table_min"];
                    if (_parser.LocalVariables["newWindow"] == 1)
                    {
                        _form = new FrmTable();
                    }
                    else
                    {
                        ListView1.Items.Add("x  |  y");
                        ListView1.Items.Add("-----------");
                    }
                    isDone = true;
                }

                if (_parser.LocalVariables["x"] <= _parser.LocalVariables["table_max"])
                {
                    if (_parser.LocalVariables["newWindow"] == 1)
                    {
                        _form.Funcs.Add(_parser.LocalVariables["x"], x[0]);
                    }
                    else
                    {
                        ListView1.Items.Add(_parser.LocalVariables["x"] + "  :  " + x[0]);
                    }
                    _parser.LocalVariables["x"] += _parser.LocalVariables["table_step"];
                    _parser.Parse(TextBox1.Text);
                    _parser.LocalVariables["x"] += _parser.LocalVariables["table_step"];
                }
                else
                {
                    if (_parser.LocalVariables["newWindow"] == 1)
                    {
                        _form.ShowDialog();
                    }
                    _parser.LocalVariables["x"] = 0;
                    isDone = false;
                }

                return(1);
            });

            _parser.LocalVariables.Add("table_min", 0);
            _parser.LocalVariables.Add("table_max", 100);
            _parser.LocalVariables.Add("table_step", 1);
            _parser.LocalVariables.Add("newWindow", 1);
            _parser.LocalVariables.Add("x", 0);

            // conditional operators
            _parser.LocalFunctions.Add("if", x =>
            {
                if (x[0] == 1)
                {
                    return(x[1]);
                }

                return(x.Length == 3 ? x[2] : 0);
            });

            DataGrid1.ItemsSource = _parser.LocalVariables;
        }