Calculate square root in C #

2

I have a calculator made in WPF C # with the basic operations +, -, *, /,%. And now I wanted to try to improve my calculator.

namespace calculadora
 {
 public delegate float? dlgoperacao(float? a, float? b);

public partial class MainWindow : Window
{
    System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"[+-/*]", System.Text.RegularExpressions.RegexOptions.None);
    const int PESO = 10;
    int? divisor = null;
    bool flag = false;

    float? op1 = 0, op2 = null;
    float? rslt = null;
    dlgoperacao operacao = null;

    public MainWindow()
    {
        InitializeComponent();
        // img.Source = new BitmapImage(new Uri(@"imgs/2.jpg",UriKind.Relative));
    }//Fim Construtor

    protected float? calculaop(float? operori, ref  int? div, char ch)
    {
        float? rslt = null;
        if (ch == ',') { rslt = operori; div = 1; return rslt; }
        else
        {
            int digito = Convert.ToInt32(ch - 48);
            if (div == null) rslt = (operori == null) ? digito : (operori * PESO) + digito;
            else
            {
                div = div * 10;
                rslt = ((operori * div) + digito) / div;

            }
            return rslt;
        }

    }




    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Button bt = (Button)sender;
        char[] ch = bt.Content.ToString().ToCharArray();
        if (char.IsDigit(ch[0].ToString(), 0) || ch[0] == ',')
        {
            if (flag)
            {
                op2 = calculaop(op2, ref divisor, ch[0]);
                // int i = regex.Match(txt.Text).Index;
                //txt.Text = txt.Text.Substring(0, i +1) +  " " + op2.ToString();
                txt.Text = op2.ToString();
            }
            else
            {

                op1 = calculaop(op1, ref divisor, ch[0]);
                txt.Text = op1.ToString();
            }

        }//if ch[0] is digit
        else
        {
            divisor = null;
            switch (ch[0])
            {
                case 'c':
                    flag = false;
                    op1 = op2 = null;
                    operacao = null;
                    txt.Text = "";
                    break;
                case '+':
                    if (op2 == null) flag = !flag;
                    else if (operacao != null)
                    {
                        op1 = operacao(op1, op2);
                        op2 = null;
                        flag = true;

                    }
                    operacao = (a, b) => a + b;
                    txt.Text = op1.ToString() + " + ";
                    break;
                case '-':
                    if (op2 == null) flag = !flag;
                    else if (operacao != null)
                    {
                        op1 = operacao(op1, op2);
                        op2 = null;
                        flag = true;
                    }
                    operacao = (a, b) => a - b;
                    txt.Text = op1.ToString() + "-";
                    break;
                case '*':
                    if (op2 == null) flag = !flag;
                    else if (operacao != null)
                    {
                        op1 = operacao(op1, op2);
                        op2 = null;
                        flag = true;
                    }
                    operacao = (a, b) => a * b;
                    txt.Text = op1.ToString() + "*";
                    break;
                case '/':

                    try
                    {
                        if (op2 == null) flag = !flag;
                        else if (operacao != null)
                        {
                            op1 = operacao(op1, op2);
                            op2 = null;
                            flag = true;

                        }
                        operacao = (a, b) => a / b;
                        txt.Text = op1.ToString() + "/";
                    }
                    catch (Exception x)
                    {

                        txt.Text = x.Message;
                    }

                    break;

                case '√':



                    break;

                case '%':

                    try
                    {
                        if (op2 == null) flag = !flag;
                        else if (operacao != null)
                        {
                            op1 = operacao(op1, op2);
                            op2 = null;
                            flag = true;

                        }
                        operacao = (a, b) => a % b;
                        txt.Text = op1.ToString() + "%";
                    }
                    catch (Exception x)
                    {

                        txt.Text = x.Message;
                    }

                    break;


                case '=':
                    if (operacao != null)
                    {
                        if (op2 != null) rslt = operacao(op1, op2);
                        else rslt = op1;
                        txt.Text = "Resultado -> " + rslt.ToString();
                        flag = false;
                        operacao = null;
                        op1 = op2 = null;

                    }
                    break;

            }//switch

        }//else ch[0] -> not digit

    }
}

}

Now I want to put the square root. I do not know if I will use case to make it work or if I will use another way.

    
asked by anonymous 23.06.2016 / 13:29

1 answer

5

Just use the .Net function Sqrt() :

if (op2 == null) flag = !flag;
else if (operacao != null) {
    op1 = operacao(op1, op2);
    op2 = null;
    flag = true;
}
operacao = (a, b) => (float?)Convert.ToSingle(Math.Sqrt(a));
txt.Text = op1.ToString() + "√";
break;

I needed to conversion to float to match with the type used.

You may have to make an adaptation since the square root has only one operand.

Other operations can be added using proper formulas and existing functions in .Net in class Math .

I did not want to mess around, but the code is pretty confusing and I doubt you need all that to work. Actually see some problems and it does not even work right.

I believe the right exception there in other cases would be DivideByZeroException . Do not use Exception everywhere .

    
23.06.2016 / 13:52