How to restructure this bit of code?

I am designing a simple calculator using Visual C #, but I run into an annoying runtime error (which is ridiculous for a statically typed language).

First let me show you a partial code:

private float get_input()
{
    try
    {
        return float.Parse(textBox1.Text);
    }
    catch
    {
        textBox2.Clear();
        textBox2.AppendText("Invalid Input");
        return 0;
    }

}

private void button1_Click(object sender, EventArgs e)
{ 
    textBox2.Clear();
    float v = get_input();
    textBox2.AppendText((Math.Sin(v)).ToString());
}

The problem is that when I run the program and, for example, say that I entered "a" in the input field, my program handles the exception by displaying "Invalid input" in the output field. However, then it computes the values sinor cos(etc.) of the float type by default. Thus, the answer in the output field will look like this: "invalid input 1" or "invalid input signal0". I provided a screenshot:

, , , get_input(), , . 7 , - .

+4
6

TryParse try... catch

private void button1_Click(object sender, EventArgs e)
{ 
    textBox2.Clear();
    float result;
    if (float.TryParse(textBox1.Text, out result))
    {
         textBox2.AppendText(Math.Sin(result).ToString());
    }
    else
    {
        textBox2.Text = "Invalid Input";
    }
}
+9

, , .

.

  • , nullable float (float?) null :

    private float? get_input()
    {
        try
        {
            return float.Parse(textBox1.Text);
        }
        catch
        {
            textBox2.Clear();
            textBox2.AppendText("Invalid Input");
            return null;
        }
    }
    

    :

    private void button1_Click(object sender, EventArgs e)
    { 
        textBox2.Clear();
        float? v = get_input();
        if (v != null) textBox2.AppendText((Math.Sin(v)).ToString());
    }
    
  • . get_input :

    private void button1_Click(object sender, EventArgs e)
    { 
        textBox2.Clear();
        try
        {
            float v = float.Parse(textBox1.Text);
            textBox2.AppendText((Math.Sin(v)).ToString());
        }
        catch
        {
            textBox2.Clear();
            textBox2.AppendText("Invalid Input");
        }
    }
    

, - .

+2

? .

private void button1_Click(object sender, EventArgs e)
{ 
    textBox2.Clear();
    try
    {
        float v = float.Parse(textBox1.Text);
        textBox2.AppendText(Math.Sin(v).ToString());
    }
    catch
    {
        textBox2.AppendText("Invalid Input");
    }
}

, :

private float? try_get_input()
{
    try
    {
        return float.Parse(textBox1.Text);
    }
    catch
    {
        return null;
    }
}

private void button1_Click(object sender, EventArgs e)
{ 
    textBox2.Clear();
    float? v = try_get_input();
    if (v != null)
    {
        textBox2.AppendText(Math.Sin(v.Value).ToString());
    }
    else
    {
        textBox2.AppendText("Invalid Input");
    }
}
+1

, get_input , 0. button1_Click , .

+1

- , ?

private bool try_get_input(out float val)
{
    val = 0;

    try
    {
        val = float.Parse(textBox1.Text);
        return true;
    }
    catch
    {
        textBox2.Clear();
        textBox2.AppendText("Invalid Input");
    }

    return false;
}

private void button1_Click(object sender, EventArgs e)
{ 
    textBox2.Clear();

    float v = 0;

    if (try_get_input(out v))
    {
        textBox2.AppendText((Math.Sin(v)).ToString());
    }
}
+1

, get_input, - . :

  • NaN. NaN Not-a-Number. , , , arcsin 2.0. a NaN . button1_Click:

    private float get_input() {
        try {
            return float.Parse(textBox1.Text);
        }
        catch {
            textBox2.Clear();
            textBox2.AppendText("Invalid Input");
            return float.NaN;
        }
    }
    
    private void button1_Click(object sender, EventArgs e) { 
        textBox2.Clear();
        float v = get_input();
        if(v != float.NaN) {
            textBox2.AppendText((Math.Sin(v)).ToString());
        }
    }
    
  • , , : , :

    private void calculateResult (Func<float,float> f) {
        textBox2.Clear();
        try {
            float v = float.Parse(textBox1.Text);
             textBox2.AppendText((f(v)).ToString());
        }
        catch {
            textBox2.AppendText("Invalid Input");
            return float.NaN;
        }
    }
    

    , Math.Sin :

    private void button1_Click(object sender, EventArgs e) { 
        calculateResult(x => (float) Math.Sin(x));
    }
    

    , , , :

    private void button1_Click(object sender, EventArgs e) { 
        calculateResult(x => (float) Math.Sin(x));
    }
    
    private void button2_Click(object sender, EventArgs e) { 
        calculateResult(x => (float) Math.Cos(x));
    }
    
    private void button3_Click(object sender, EventArgs e) { 
        calculateResult(x => (float) Math.Tan(x));
    }
    

    . .

0
source

Source: https://habr.com/ru/post/1624679/


All Articles