Exemplo n.º 1
0
        private void AddNumber(NumericTextBox textBox)
        {
            if (!numbers.Keys.Contains(textBox))
            {
                return;
            }

            int i = numbers[textBox];

            expression = expression.Insert(i + offset - 1, textBox.Text.Replace(",", "."));
            offset    += textBox.Text.Count();
            numbers.Remove(textBox);
        }
Exemplo n.º 2
0
        public InputFields(string e, Action <bool, InputFields> check)
        {
            Check = check;

            FontStyle  = FontStyles.Italic;
            FontSize   = 35;
            FontFamily = new FontFamily("Cambria Math");

            double d;

            Inlines.Add(input_fields_stack);
            string[] parts = e.Split('|');


            TextBlock equal_text = new TextBlock()
            {
                VerticalAlignment = VerticalAlignment.Center,
                Text   = "=",
                Margin = new Thickness(0, 0, 10, 0)
            };

            input_fields_stack.Children.Add(equal_text);


            for (int i = 0; i < parts.Count(); i++)
            {
                string part = parts[i];
                if (part == "box")
                {
                    Border border = new Border()
                    {
                        Padding = new Thickness(0, 0, 10, 0)
                    };

                    NumericTextBox textBoxTask = new NumericTextBox()
                    {
                        Width             = 80,
                        VerticalAlignment = VerticalAlignment.Center
                    };

                    border.Child = textBoxTask;

                    input_fields_stack.Children.Add(border);

                    numbers.Add(textBoxTask, expression.Length);
                }
                else if (operators.Contains(part) || double.TryParse(part, out d))
                {
                    expression += part.Replace(",", ".");

                    TextBlock text = new TextBlock()
                    {
                        VerticalAlignment = VerticalAlignment.Center,
                        Text   = part.Replace("*", "∙"),
                        Margin = new Thickness(0, 0, 10, 0)
                    };

                    input_fields_stack.Children.Add(text);
                }
                else if (part.StartsWith("sqrt"))
                {
                    expression += "sqrt";

                    TextBlock text = new TextBlock()
                    {
                        VerticalAlignment = VerticalAlignment.Center,
                        Text      = "√",
                        FontStyle = FontStyles.Normal,
                        Margin    = new Thickness(0, 0, 10, 0)
                    };

                    if (part.Length > 4)
                    {
                        text.FontSize = 100;
                        IsSqrt        = true;
                        text.Margin   = new Thickness(0, 5, 4, 0);
                    }

                    input_fields_stack.Children.Add(text);
                }
                else if (part.StartsWith("pow2"))
                {
                    expression += "pow2";

                    TextBlock text = Helpers.GetFormattedText("<sup>2</sup>", (int)FontSize);

                    text.VerticalAlignment = VerticalAlignment.Center;
                    text.Margin            = new Thickness(0, 0, 10, 0);

                    input_fields_stack.Children.Add(text);
                }
                else if (part == "(b" || part == ")b")
                {
                    expression += part.Substring(0, 1);

                    if (IsSqrt && part == "(b")
                    {
                        continue;
                    }
                    if (IsSqrt && part == ")b")
                    {
                        IsSqrt = false; continue;
                    }

                    /*TextBlock text = new TextBlock()
                     * {
                     *  VerticalAlignment = VerticalAlignment.Center,
                     *  Text = part.Substring(0, 1),
                     *  FontStyle = FontStyles.Normal,
                     *  FontSize = 75,
                     *  Margin = new Thickness(0, 0, 10, 0)
                     * };
                     *
                     * input_fields_stack.Children.Add(text);*/
                }
                else if (part.StartsWith("{"))
                {
                    Regex           regex1   = new Regex(@"[^{}]+");
                    MatchCollection matches1 = regex1.Matches(part);

                    Border border_fraction = new Border()
                    {
                        Padding = new Thickness(0, 0, 10, 0)
                    };

                    if (IsSqrt)
                    {
                        border_fraction.BorderBrush     = Brushes.Black;
                        border_fraction.BorderThickness = new Thickness(0, 6, 0, 0);
                    }

                    StackPanel fraction = new StackPanel()
                    {
                        Margin = new Thickness(0, 0, 10, 0)
                    };

                    expression += "(";
                    bool first = true;

                    foreach (Match m in matches1)
                    {
                        Console.WriteLine(m.Value);
                        StackPanel stack  = AddTextBoxes(m.Value);
                        Border     border = new Border()
                        {
                            BorderBrush     = Brushes.Black,
                            BorderThickness = new Thickness(0, 0, 0, 1)
                        };
                        border.Child = stack;
                        fraction.Children.Add(border);

                        if (first)
                        {
                            first       = false;
                            expression += ")/(";
                        }
                    }

                    (fraction.Children[1] as Border).BorderThickness = new Thickness(0, 1, 0, 0);
                    expression += ")";

                    border_fraction.Child = fraction;

                    input_fields_stack.Children.Add(border_fraction);
                }
            }

            equal = new Button()
            {
                VerticalAlignment = VerticalAlignment.Center,
                Content           = "=",
                Padding           = new Thickness(10, 0, 10, 0),
                Margin            = new Thickness(0, 0, 10, 0)
            };
            equal.Click += Calc;
            input_fields_stack.Children.Add(equal);

            input_fields_stack.Children.Add(text_result);
        }