Ejercicios resueltos en C# : Control de alcoholemia


La Dirección General de Tráfico necesita un programa que, dado el tipo de vehículo que conduce un conductor, su índice de alcohol y si ha sido medido en sangre o en aire, determine si da positivo en el control de alcoholemia o no. La tasa máxima de alcohol tanto en sangre como en aire según el tipo de vehículo se resume en la siguiente tabla:

Vehiculo Indicador Tasa maxima en sangre Tasa maxima en aire
Camión C 0.3 0.15
Autobús A 0.3 0.15
Turismo T 0.5 0.25
Motocicleta M 0.3 0.15


Como realizar este ejercicio en C#?


1ro. Crear un formulario con dos textboxes, el que contendra el Tipo de vehiculo se llamara txttipovehiculo y que contendra el indice de alcohol txtindicealcohol asi como dos radio buttons llamados rbtsangre y rbtaire.

2do. El botón "Determinar" hará el trabajo, por lo que este es el codigo que hará que el mismo funcione:


try
            {
                double indecealcohol;
                indecealcohol = Convert.ToDouble(txtindicealcohol.Text);

                if (txttipovehiculo.Text != "C" && txttipovehiculo.Text != "A" && txttipovehiculo.Text != "T" && txttipovehiculo.Text != "M")
                {

                    MessageBox.Show("Indicador no válido!");

                    txttipovehiculo.SelectionStart = 0; //Sombrear el texto
                    txttipovehiculo.SelectionLength = txttipovehiculo.Text.Length; //Sombrear el texto
                    txttipovehiculo.Focus(); 

                } 
                else
                {

                    if (txttipovehiculo.Text == "C")
                    {

                        if (rbtsangre.Checked)
                        {
                            if (indecealcohol >= 0.3)
                            {
                                lblresultado.Text = "Positivo".ToString();
                            }
                            else
                            {
                                lblresultado.Text = "Negativo".ToString();
                            }
                        }
                        else
                        {

                            if (rbtaire.Checked == true)
                            {
                                if (indecealcohol >= 0.15)
                                {
                                    lblresultado.Text = "Positivo".ToString();
                                }
                                else
                                {
                                    lblresultado.Text = "Negativo".ToString();
                                }

                            }
                        }
                    }
                    else
                    {

                        if (txttipovehiculo.Text == "A")
                        {

                            if (rbtsangre.Checked)
                            {
                                if (indecealcohol >= 0.3)
                                {
                                    lblresultado.Text = "Positivo".ToString();
                                }
                                else
                                {
                                    lblresultado.Text = "Negativo".ToString();
                                }
                            }
                            else
                            {
                                if (rbtaire.Checked)
                                {
                                    if (indecealcohol >= 0.15)
                                    {
                                        lblresultado.Text = "Positivo".ToString();
                                    }
                                    else
                                    {
                                        lblresultado.Text = "Negativo".ToString();
                                    }

                                }
                            }

                        }
                        else
                        {
                            if (txttipovehiculo.Text == "T")
                            {

                                if (rbtsangre.Checked)
                                {
                                    if (indecealcohol >= 0.5)
                                    {
                                        lblresultado.Text = "Positivo".ToString();
                                    }
                                    else
                                    {
                                        lblresultado.Text = "Negativo".ToString();
                                    }
                                }
                                else
                                {
                                    if (rbtaire.Checked)
                                    {
                                        if (indecealcohol >= 0.25)
                                        {
                                            lblresultado.Text = "Positivo".ToString();
                                        }
                                        else
                                        {
                                            lblresultado.Text = "Negativo".ToString();
                                        }

                                    }
                                }

                            }
                            else
                            {
                                if (txttipovehiculo.Text == "M")
                                {

                                    if (rbtsangre.Checked)
                                    {
                                        if (indecealcohol >= 0.3)
                                        {
                                            lblresultado.Text = "Positivo".ToString();
                                        }
                                        else
                                        {
                                            lblresultado.Text = "Negativo".ToString();
                                        }
                                    }
                                    else
                                    {
                                        if (rbtaire.Checked)
                                        {
                                            if (indecealcohol >= 0.15)
                                            {
                                                lblresultado.Text = "Positivo".ToString();
                                            }
                                            else
                                            {
                                                lblresultado.Text = "Negativo".ToString();
                                            }

                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                

            }
            catch (Exception msg)
            {

                MessageBox.Show(msg.Message);
            }


3ro. Adicionalmente el botón "Limpiar":

            txtindicealcohol.Clear();
            txttipovehiculo.Clear();
            lblresultado.Text = "";
            txttipovehiculo.Focus();


y el botón "Salir":

Close();

Publicar un comentario