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
I could elaborate more on the "not answering anything" part, which would be that nothing ?
– LP. Gonçalves