Example #1
0
        public static void DoMath(TextBox input, TextBox output, char action, ref bool error, ref bool read, ref string last)
        {
            string _in  = input.Text;
            string _out = output.Text;

            if (_in.EndsWith(","))
            {
                _in        = _in.Replace(",", "");
                input.Text = _in;
            }
            if (action != '=')
            {
                if (_out.Length == 0)
                {
                    output.Text = _in + ' ' + action;
                    read        = false;
                }
                else
                {
                    if ((_out.EndsWith("+") || _out.EndsWith("/") || _out.EndsWith("-") || _out.EndsWith("*")) && read == false)
                    {
                        output.Text = _out.Remove(_out.Length - 1, 1) + action;
                    }
                    else if (_out.EndsWith("="))
                    {
                        output.Text = _in + ' ' + action;
                    }
                    else
                    {
                        output.Text = _out + ' ' + _in + ' ' + action;
                        input.Text  = Logic.Calculate(_out + ' ' + _in, ref error);
                        read        = false;
                    }
                }
            }
            else
            {
                if (_out.Length == 0)
                {
                    output.Text = _in + " =";
                }
                else
                {
                    if (_out.Last() == '=')
                    {
                        output.Text = _in + last + " =";
                        input.Text  = Logic.Calculate(_in + last, ref error);
                    }
                    else
                    {
                        last        = ' ' + _out.Last().ToString() + ' ' + _in;
                        input.Text  = Logic.Calculate(_out + ' ' + _in, ref error);
                        output.Text = _out + ' ' + _in + " =";
                    }
                }
                read = false;
            }
            output.Select(output.Text.Length, 0);
            output.Focus();
        }