Exemplo n.º 1
0
        // unaryOps

        private void btnSqrtOp_Click(object sender, EventArgs e)
        {
            calcOperator         = "sqrt";
            isCalcOperatorActive = true;

            if (resultValue != 0)
            {
                btnEq.PerformClick();
                resultValue = Double.Parse(textBoxRes.Text);
            }
            else
            {
                if (textBoxRes.Text.Equals("x") || textBoxRes.Text.Equals("y") || textBoxRes.Text.Equals("z"))
                {
                    using (var form = new CalculatorValueForVariables())
                    {
                        var result = form.ShowDialog();
                        if (result == DialogResult.OK)
                        {
                            resultValue = form.varValue;
                        }
                    }
                }
                else
                {
                    resultValue = Double.Parse(textBoxRes.Text);
                }

                labelCurrentExpr.Text = calcOperator + "(" + resultValue + ")";
            }
        }
Exemplo n.º 2
0
        private void btnClassicOp_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;

            calcOperator         = button.Text;
            isCalcOperatorActive = true;

            if (resultValue != 0)
            {
                btnEq.PerformClick();
                labelCurrentExpr.Text = resultValue + " " + calcOperator + " ";
            }
            else
            {
                if (textBoxRes.Text.Equals("x") || textBoxRes.Text.Equals("y") || textBoxRes.Text.Equals("z"))
                {
                    using (var form = new CalculatorValueForVariables())
                    {
                        var result = form.ShowDialog();
                        if (result == DialogResult.OK)
                        {
                            resultValue = form.varValue;
                        }
                    }
                    labelCurrentExpr.Text = textBoxRes.Text + " " + calcOperator + " ";
                }
                else
                {
                    resultValue           = Double.Parse(textBoxRes.Text);
                    labelCurrentExpr.Text = resultValue + " " + calcOperator + " ";
                }
            }
        }
Exemplo n.º 3
0
        // evaluator

        private void btnEq_Click(object sender, EventArgs e)
        {
            Expression expression;
            Double     sndArgument = new Double();


            if (textBoxRes.Text.Equals("x") || textBoxRes.Text.Equals("y") || textBoxRes.Text.Equals("z"))
            {
                using (var form = new CalculatorValueForVariables())
                {
                    var result = form.ShowDialog();
                    if (result == DialogResult.OK)
                    {
                        sndArgument = form.varValue;
                    }
                }
            }
            else
            {
                sndArgument = Double.Parse(textBoxRes.Text);
            }

            switch (calcOperator)
            {
            case "sqrt":
                if (!(resultValue < 0))
                {
                    expression      = new Sqrt(resultValue);
                    finalValue      = expression.Evaluate();
                    textBoxRes.Text = finalValue.ToString();
                }
                else
                {
                    textBoxRes.Text = "Can't calculate sqrt of a neg num!";
                }
                break;

            case "neg":
                expression      = new Opposite(resultValue);
                finalValue      = expression.Evaluate();
                textBoxRes.Text = finalValue.ToString();
                break;

            case "cos":
                expression      = new Cosinus(resultValue);
                finalValue      = expression.Evaluate();
                textBoxRes.Text = finalValue.ToString();
                break;

            case "sin":
                expression      = new Sinus(resultValue);
                finalValue      = expression.Evaluate();
                textBoxRes.Text = finalValue.ToString();
                break;

            case "exp":
                expression      = new Exponentation(resultValue);
                finalValue      = expression.Evaluate();
                textBoxRes.Text = finalValue.ToString();
                break;

            case "abs":
                expression      = new Abs(resultValue);
                finalValue      = expression.Evaluate();
                textBoxRes.Text = finalValue.ToString();
                break;

            case "log":
                if (resultValue > 0)
                {
                    expression      = new Log(resultValue);
                    finalValue      = expression.Evaluate();
                    textBoxRes.Text = finalValue.ToString();
                }
                else
                {
                    textBoxRes.Text = "Can't calculate log of a neg num!";
                }
                break;

            case "+":
                labelCurrentExpr.Text += sndArgument;
                expression             = new Addition(resultValue, sndArgument);
                finalValue             = expression.Evaluate();
                textBoxRes.Text        = finalValue.ToString();
                break;

            case "-":
                labelCurrentExpr.Text += sndArgument;
                expression             = new Subtraction(resultValue, sndArgument);
                finalValue             = expression.Evaluate();
                textBoxRes.Text        = finalValue.ToString();
                break;

            case "*":
                labelCurrentExpr.Text += sndArgument;
                expression             = new Multiplication(resultValue, sndArgument);
                finalValue             = expression.Evaluate();
                textBoxRes.Text        = finalValue.ToString();
                break;

            case "/":
                labelCurrentExpr.Text += sndArgument;
                if (!sndArgument.Equals(0))
                {
                    expression      = new Division(resultValue, sndArgument);
                    finalValue      = expression.Evaluate();
                    textBoxRes.Text = finalValue.ToString();
                }
                else
                {
                    textBoxRes.Text = "Can't divide by 0";
                }
                break;

            case "^":
                labelCurrentExpr.Text += sndArgument;
                expression             = new Power(resultValue, sndArgument);
                finalValue             = expression.Evaluate();
                textBoxRes.Text        = finalValue.ToString();
                break;

            case "%":
                labelCurrentExpr.Text += sndArgument;
                expression             = new Modulo(resultValue, sndArgument);
                finalValue             = expression.Evaluate();
                textBoxRes.Text        = finalValue.ToString();
                break;
            }
            resultValue           = 0;
            labelCurrentExpr.Text = "";
        }