second degree equation c#

Asked

Viewed 1,808 times

1

I made this code to calculate bhaskara, but it is not answering anything for the values a=1, b=8 and c=16.

I cannot understand why it is not working specifically for these values.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SegundoGrau
{
    public partial class Bhaskara : Form
    {
        public Bhaskara()
        {
            InitializeComponent();
        }

        private void btSair_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btLimpar_Click(object sender, EventArgs e)
        {
            tbA.Clear();
            tbB.Clear();
            tbC.Clear();
            labelConcavidade.Text = "";
            labelRaizes.Text = "";
            labelVertice.Text = "";
            tbA.Focus();
        }

        private void btCalcular_Click(object sender, EventArgs e)
        {
            double a = 0, b = 0, c = 0,
                vx = 0, vy = 0, 
                delta = 0,
                x1 = 0, x2 = 0;
            if (tbA.Text == "" || tbB.Text == "" || tbC.Text == "")
            {
                MessageBox.Show("Insira Valores para A, B e C");
                tbA.Focus();
            }
            else if (tbA.Text == "0")
            {
                MessageBox.Show("O valor de A deve ser diferente de 0 para que seja uma equação do 2º Grau.");
                tbA.Clear();
                tbA.Focus();
            }

            else
            {
                a = Convert.ToDouble(tbA.Text);
                b = Convert.ToDouble(tbB.Text);
                c = Convert.ToDouble(tbC.Text);
                delta = b * b - 4 * a * c;
            }
            if (delta < 0)
            {
                MessageBox.Show("Esta equação não possui raizes reais");
                btLimpar.Focus();
            }
            else if (delta == 0)
            {
                x1 = -b / (2 * a);
                vx = x1;
            }
            else
            {
                x1 = (-b + Math.Sqrt(delta)) / (2 * a);
                x2 = (-b - Math.Sqrt(delta)) / (2 * a);

                vx = -b / (2 * a);
                vy = a * vx * vx + b * vx + c;

                if (a < 0) labelConcavidade.Text = "Para baixo";
                else labelConcavidade.Text = "Para cima";

                labelRaizes.Text = string.Format("{0:N}", x1) + ", " + string.Format("{0:N}", x2);
                labelVertice.Text = string.Format("{0:N}", vx) + ", " + string.Format("{0:N}", vy);
            }


        }
    }
}

form image

Imagem do form

  • 1

    I could elaborate more on the "not answering anything" part, which would be that nothing ?

2 answers

4


With those values you put on, your delta will be = 0.

Look at this passage:

    if (delta < 0)
    {
        MessageBox.Show("Esta equação não possui raizes reais");
        btLimpar.Focus();
    }
    else if (delta == 0)
    {
        x1 = -b / (2 * a);
        vx = x1;
    }
    else
    {
        x1 = (-b + Math.Sqrt(delta)) / (2 * a);
        x2 = (-b - Math.Sqrt(delta)) / (2 * a);

        vx = -b / (2 * a);
        vy = a * vx * vx + b * vx + c;

        if (a < 0) labelConcavidade.Text = "Para baixo";
        else labelConcavidade.Text = "Para cima";

        labelRaizes.Text = string.Format("{0:N}", x1) + ", " + string.Format("{0:N}", x2);
        labelVertice.Text = string.Format("{0:N}", vx) + ", " + string.Format("{0:N}", vy);
    }

he will fall in the else if (delta == 0), and will not show anything, because the show code is in else

Put those two lines:

labelRaizes.Text = string.Format("{0:N}", x1) + ", " + string.Format("{0:N}", x2);
labelVertice.Text = string.Format("{0:N}", vx) + ", " + string.Format("{0:N}", vy);

out of the else

3

because with these values the delta gets zeroed and enters your if that has no output.

            else if (delta == 0)
            {

                //Entrou nesse if e não faz mais nada
                x1 = -b / (2 * a);
                vx = x1;

                //alguma saída ? 
            }
            else
            {
                x1 = (-b + Math.Sqrt(delta)) / (2 * a);
                x2 = (-b - Math.Sqrt(delta)) / (2 * a);

                vx = -b / (2 * a);
                vy = a * vx * vx + b * vx + c;

                if (a < 0) labelConcavidade.Text = "Para baixo";
                else labelConcavidade.Text = "Para cima";

                labelRaizes.Text = string.Format("{0:N}", x1) + ", " + string.Format("{0:N}", x2);
                labelVertice.Text = string.Format("{0:N}", vx) + ", " + string.Format("{0:N}", vy);
            }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.