private void buttonEliminar_Click(object sender, EventArgs e)
        {
            Vertice vertice = listBoxlistCiudades.SelectedItem as Vertice;

            for (int i = 0; i < listaVuelos.Count; i++)
            {
                if (listaVuelos[i].getOrigen() == vertice.getOrigen())
                {
                    listaVuelos.RemoveAt(i);
                    --i;
                }

                else if (listaVuelos[i].getDestino() == vertice.getOrigen())
                {
                    listaVuelos.RemoveAt(i);
                    --i;
                }
            }

            grafoVuelos.Remove(vertice);

            List <char> listaCD = new List <char>();
            List <char> listaCO = new List <char>();

            foreach (Vuelo v in listaVuelos)
            {
                listaCO.Add(v.getOrigen());
                listaCD.Add(v.getDestino());
            }

            for (int i = 0; i < grafoVuelos.Count; i++)
            {
                for (int j = 0; j < grafoVuelos[i].getListAristas().Count; j++)
                {
                    if (!listaCD.Contains(grafoVuelos[i].getListAristas()[j].getDestino()))
                    {
                        grafoVuelos[i].getListAristas().RemoveAt(j);
                    }
                }

                if ((!listaCO.Contains(grafoVuelos[i].getOrigen())) && (!listaCD.Contains(grafoVuelos[i].getOrigen())))
                {
                    grafoVuelos.RemoveAt(i);
                }
            }


            actualizarListBox();
        }
示例#2
0
        private void buttonAgregar_Click(object sender, EventArgs e)
        {
            bool duplicado = false;
            bool origenExisteEnGrafo, destinoExisteEnGrafo;

            origenExisteEnGrafo = destinoExisteEnGrafo = false;

            Vuelo vuelo = new Vuelo(Convert.ToChar(textBoxOrigen.Text), Convert.ToChar(textBoxDestino.Text),
                                    Int32.Parse(textBoxTiempo.Text), Int32.Parse(textBoxCosto.Text), dateTimePickerFechaSalida.Value);

            Arista arista1;
            int    i, k;

            for (i = 0; i < listVuelos.Count; i++)
            {
                if (vuelo.getOrigen() == listVuelos[i].getOrigen() && vuelo.getDestino() == listVuelos[i].getDestino())
                {
                    duplicado = true;
                }
            }
            //**********
            if (!duplicado)
            {
                FormGrafo ventanaGrafo = new FormGrafo(grafoVuelos, true);

                Vertice vertice;

                for (i = 0; i < grafoVuelos.Count; i++)
                {
                    if (grafoVuelos[i].getOrigen() == Convert.ToChar(textBoxOrigen.Text))
                    {
                        origenExisteEnGrafo = true;
                        break;
                    }
                }

                for (k = 0; k < grafoVuelos.Count; k++)
                {
                    if (grafoVuelos[k].getOrigen() == Convert.ToChar(textBoxDestino.Text))
                    {
                        destinoExisteEnGrafo = true;
                        break;
                    }
                }

                if (origenExisteEnGrafo && destinoExisteEnGrafo)
                {
                    arista1 = new Arista(grafoVuelos[i].getOrigen(), grafoVuelos[k].getOrigen(), vuelo.getCosto(), grafoVuelos[k].X, grafoVuelos[k].Y);
                    grafoVuelos[i].getListAristas().Add(arista1);
                }

                else if (!origenExisteEnGrafo && destinoExisteEnGrafo)
                {
                    arista1 = new Arista(vuelo.getOrigen(), grafoVuelos[k].getOrigen(), vuelo.getCosto(), grafoVuelos[k].X, grafoVuelos[k].Y);
                    ventanaGrafo.ShowDialog();
                    vertice = new Vertice(Convert.ToChar(textBoxOrigen.Text), ventanaGrafo.getX(), ventanaGrafo.getY());
                    vertice.getListAristas().Add(arista1);
                    grafoVuelos.Add(vertice);
                }

                else if (origenExisteEnGrafo && !destinoExisteEnGrafo)
                {
                    ventanaGrafo.ShowDialog();
                    vertice = new Vertice(Convert.ToChar(textBoxDestino.Text), ventanaGrafo.getX(), ventanaGrafo.getY());
                    grafoVuelos.Add(vertice);
                    arista1 = new Arista(grafoVuelos[i].getOrigen(), vertice.getOrigen(), vuelo.getCosto(), vertice.X, vertice.Y);
                    grafoVuelos[i].getListAristas().Add(arista1);
                }

                else if (!origenExisteEnGrafo && !destinoExisteEnGrafo)
                {
                    Vertice vertice2;
                    ventanaGrafo.ShowDialog();
                    vertice = new Vertice(vuelo.getOrigen(), ventanaGrafo.getX(), ventanaGrafo.getY());
                    ventanaGrafo.ShowDialog();
                    vertice2 = new Vertice(vuelo.getDestino(), ventanaGrafo.getX(), ventanaGrafo.getY());

                    arista1 = new Arista(vertice.getOrigen(), vertice2.getOrigen(), vuelo.getCosto(), vertice2.X, vertice2.Y);
                    vertice.getListAristas().Add(arista1);
                    grafoVuelos.Add(vertice);
                    grafoVuelos.Add(vertice2);
                }
                listVuelos.Add(vuelo);
            }
            //*************

            actualizarListViewVuelos();
        }