Algoritmos

13
INTEGRANTES: ALVARADO MORENO, Víctor. CABRERA CARRASCO, Moisés. RODRIGUEZ SANDOVAL, Rosalba. VELASQUEZ DIAZ, Elena. ZAMORA GONZALES, Stefani. Diseño del formulario Ventana de Propiedades OBJETO PROPIEDAD VALOR Label1 Label2 Text Text Lista de Elementos Espacios Libres

description

muy divertida

Transcript of Algoritmos

INTEGRANTES:

• ALVARADO MORENO, Víctor.

• CABRERA CARRASCO, Moisés.

• RODRIGUEZ SANDOVAL, Rosalba.

• VELASQUEZ DIAZ, Elena.

• ZAMORA GONZALES, Stefani.

Diseño del formulario

Ventana de Propiedades

OBJETO PROPIEDAD VALOR

Label1

Label2

Text

Text

Lista de Elementos

Espacios Libres

Label3

Label4

Label5

ListBox1

textBox1

textBox2

comboBox1

buttom1

buttom2

buttom3

buttom4

buttom5

Text

Text

Text

Name

Text

Name

Text

Name

Text

Name

Text

Name

Text

Name

Text

Name

Text

Name

Text

Name

Cabeza de Lista

Cabeza de Listas

Nuevo Elemento

LstDatos

<En Blanco>

TxtElemento

<En Blanco>

TxtFinal

Cabeza de Lista:

CmbCabezas

Crear &Lista

BtnCrear

&Agregar

BtnAgregar

&Mostrar

BtnMostrar

&Buscar

BtnBuscar

E&ditar

BtnEditar

buttom6

buttom7

buttom8

buttom9

buttom10

buttom11

buttom12

Text

Name

Text

Name

Text

Name

Text

Name

Text

Name

Text

Name

Text

Name

&Eliminar

BtnEliminar

N° Ele&mentos

BtnNumeroElementos

&Ocurrencias

BtnOcurrencias

&Salir

BtnSalir

O&rdenar

BtnOrdenar

A&gregar luego de

ocurrencias

BtnOcuAgre

Es& pacios Libres

BtnLibres

Creación de la clase estructura: EstructuraLista

#pragma once class EstructuraLista { public: int dat; int ptr; EstructuraLista(void) { } };

Creación de la clase proceso: OperaLista

#pragma once

#include "EstructuraLista.h"; class OperaLista { public: int CAB[10]; int t; int ALib; int BLib; EstructuraLista LisA[50]; EstructuraLista LisB[50]; public: void Inicializar(EstructuraLista[], EstructuraLista[], int [], int&, int&); // A B void CrearLista(EstructuraLista[], int[], int&, int, int); void AgregarElemento(EstructuraLista[], int, int&, int); void Editar(EstructuraLista[], int, int &, int, int);

void AgregarLuegoOcurrencia(EstructuraLista[], int, int&, int, int); void OrdenarLista(EstructuraLista[], int); void Eliminar(EstructuraLista[], int, int&, int);

int Buscar(EstructuraLista[], int, int, int, int); int CuentaElementos(EstructuraLista[]);

int EspaciosLibres(EstructuraLista[]); int CuentaCabezas(int[]);

int CompruebaCabeza(int[], int); int Ocurrencias(EstructuraLista[], int, int); bool VerificarListaOrdenada(EstructuraLista[], int);

OperaLista(void) { } }; void OperaLista::Inicializar(EstructuraLista A[], EstructuraLista B[], int cab[], int &libA, int &libB){ for (int i = 0; i < 10; i++){ cab[i]=99; } libA=0; libB=0; for (int i = 0; i < 50; i++){ A[i].dat=-11; A[i].ptr=i+1; B[i].dat=-11; B[i].ptr=i+1; } A[49].ptr=99; A[49].ptr=99; } void OperaLista::CrearLista(EstructuraLista L[], int cab[], int &lib, int t, int xdat){ L[lib].dat=xdat; cab[t]=lib; lib=L[lib].ptr; L[cab[t]].ptr=99; }

void OperaLista::AgregarElemento(EstructuraLista L[], int cab, int &lib, int xdat){ L[lib].dat=xdat; int xptr=cab; while (L[xptr].ptr!=99){ xptr=L[xptr].ptr; } int xlib=lib; L[xptr].ptr=lib; lib=L[xlib].ptr; L[xlib].ptr=99; } void OperaLista::Editar(EstructuraLista L[], int cab, int &lib, int xdat, int ndat){ int xptr=Buscar(L,cab,lib,1,xdat); if (xptr!=99){ L[xptr].dat=ndat; } } void OperaLista:: AgregarLuegoOcurrencia (EstructuraLista L[], int cab, int &lib, int xdat, int ndat){ int xptr=cab; int xant=xptr; int rep=Ocurrencias(L,xptr,xdat); int aux=0; while (aux<rep){ if (L[xptr].dat==xdat){ aux++; } xptr=L[xptr].ptr; } int vec[30]; int cont=0; xant=xptr; int qwe=0; while (xptr!=99){ vec[cont]=L[xptr].dat; qwe=xptr; xptr=L[xptr].ptr; cont++; } int xlib=lib; L[qwe].ptr=xlib; L[lib].dat=vec[cont]; lib=L[lib].ptr; L[xlib].ptr=99; cont--; xptr=xant; L[xptr].dat=ndat; xptr=L[xant].ptr; int zep=0; while (zep<=cont){ L[xptr].dat=vec[zep]; xptr=L[xptr].ptr; zep++; } } void OperaLista::OrdenarLista(EstructuraLista L[], int cab){ int xptr=cab;

int xant=xptr; int vec[30]; int cont=0; while (xptr!=99){ vec[cont]=L[xptr].dat; xptr=L[xptr].ptr; cont++; } int aux=0; for (int i = 0; i < cont; i++){ if (vec[i]>=vec[i+1]) { aux=vec[i+1]; vec[i+1]=vec[i]; vec[i]=aux; } } cont=0; xptr=xant; while (xptr!=99){ L[xptr].dat=vec[cont]; xptr=L[xptr].ptr; cont++; } } void OperaLista::Eliminar(EstructuraLista L[], int cab, int &lib, int xdat){ int xptr=Buscar(L, cab, lib, 1, xdat); if (xptr!=99){ L[xptr].dat=-11; int xant=Buscar(L,cab,lib,0,xdat); if (xant==xptr){ cab=99; L[xptr].ptr=lib; lib=xptr; } } } int OperaLista::Buscar(EstructuraLista L[], int cab, int lib, int tipo, int xdat){ int xptr=cab; int xant=xptr; bool Existe=false; while (xptr!=99 && !Existe){ if (L[xptr].dat==xdat){ Existe=true; }else{ xant=xptr; xptr=L[xptr].ptr; } } if (Existe){ if (tipo==0){ return (xant); }else{ return (xptr); } }else{ return (99);

} } int OperaLista::CuentaElementos(EstructuraLista M[]){ int aux=0; while (M[aux].dat!=-11){ aux++; } return aux; } int OperaLista::EspaciosLibres(EstructuraLista L[]){ int aux=0; for (int i = 0; i < 50; i++){ if (L[i].dat==-11){ aux++; } } return (aux); } int OperaLista::CuentaCabezas(int cab[]){ int cont=0; while (cab[cont]!=99){ cont++; } return (cont); } int OperaLista::CompruebaCabeza(int cab[], int xcab){ int aux=0; while (cab[aux]!=xcab){ aux++; } return aux; } int OperaLista::Ocurrencias(EstructuraLista L[], int cab, int xdat){ int xptr=cab; int aux=0; while (xptr!=99){ if (L[xptr].dat==xdat){ aux++; } xptr=L[xptr].ptr; } return (aux); }

bool OperaLista::VerificarListaOrdenada(EstructuraLista L[], int cab){ int xptr=cab; int xante=xptr; bool xorde=true; while (xptr!=99 && xorde){ if (L[xante].dat>L[xptr].dat){ xorde=false; } xante=xptr; xptr=L[xptr].ptr; } return (xorde); }

Implementación del código en el formulario

En la parte superior del código

#pragma once #include "OperaLista.h" OperaLista Lista;

Los botones

#pragma endregion private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) { for (int i = 0; i < 10; i++){ Lista.CAB[i]=99; } Lista.ALib=0; Lista.BLib=0; Lista.Inicializar(Lista.LisA, Lista.LisB,Lista.CAB, Lista.ALib,Lista.BLib); }

private: System::Void BtnCrear_Click(System::Object^ sender, System::EventArgs^ e) { BtnAgregar->Enabled = true; BtnMostrar->Enabled = true; BtnNumeroElementos->Enabled=true; if (Convert::ToString(TxtElemento->Text)=="" || Convert::ToString(TxtElemento->Text)==" "){ MessageBox::Show("Ningún elemento entero ingresado"); }else{ LblElemento->Text="Elemento: "; int canT=Lista.CuentaCabezas(Lista.CAB); int mdat=Convert::ToInt32(TxtElemento->Text); if (canT<10){ if (Lista.CuentaElementos(Lista.LisA)==50) { Lista.CreaLista(Lista.LisB,Lista.CAB,Lista.BLib,canT,mdat); }else{ Lista.CreaLista(Lista.LisA,Lista.CAB,Lista.ALib,canT,mdat); } }else{

BtnCrear->Enabled=false; } CmbCabezas->Items->Add(mdat); } TxtElemento->Focus(); TxtElemento->Clear(); }

private: System::Void BtnSalir_Click(System::Object^ sender, System::EventArgs^ e) { Close(); }

private: System::Void BtnMostrar_Click(System::Object^ sender, System::EventArgs^ e) { BtnBuscar->Enabled=true; BtnEditar->Enabled=true; BtnEliminar->Enabled=true; BtnOrdenar->Enabled=true; TxtFinal->Enabled=true; LstDatos->Items->Clear(); LstDatos->Items->Add("Datos A"); String^ cad=" "; for (int i = 0; i < 10; i++){ if (Lista.CAB[i]!=99){ int xptr=Lista.CAB[i]; while (xptr!=99){ if (Lista.LisA[xptr].dat!=-11){ cad=" ("+Convert::ToString(xptr)+") "+Convert::ToString(Lista.LisA[xptr].dat)+" -"+Convert::ToString(Lista.LisA[xptr].ptr)+"-"; LstDatos->Items->Add(cad); } xptr=Lista.LisA[xptr].ptr; } } } }

private: System::Void BtnNumeroElementos_Click(System::Object^ sender, System::EventArgs^ e) { LstDatos->Items->Clear(); LstDatos->Items->Add("Datos A, "+Convert::ToString(Lista.CuentaElementos(Lista.LisA))+" elementos"); LstDatos->Items->Add("Datos B, "+Convert::ToString(Lista.CuentaElementos(Lista.LisB))+" elementos"); }

private: System::Void BtnAgregar_Click(System::Object^ sender, System::EventArgs^ e) { if (CmbCabezas->SelectedIndex > -1){ if (Convert::ToString(TxtElemento->Text)=="" || Convert::ToString(TxtElemento->Text)==" "){ MessageBox::Show("No se ha ingresado ningún elemento"); }else{ int elem=Convert::ToInt16(TxtElemento->Text); int pos=Convert::ToInt16(CmbCabezas->SelectedIndex); int aux=Lista.CAB[pos]; if (Lista.CuentaElementos(Lista.LisA)==50){ Lista.AgregarElemento(Lista.LisB,aux,Lista.BLib,elem); }else{ Lista.AgregarElemento(Lista.LisA,aux,Lista.ALib,elem); } } TxtElemento->Focus(); TxtElemento->Clear(); }else{ MessageBox::Show("No se ha seleccionado ninguna cabeza de lista"); } }

private: System::Void BtnBuscar_Click(System::Object^ sender, System::EventArgs^ e) { if (CmbCabezas->SelectedIndex > -1){ if (Convert::ToString(TxtElemento->Text)=="" || Convert::ToString(TxtElemento->Text)==" "){ MessageBox::Show("No se ha ingresado ningún elemento"); }else{ int elem=Convert::ToInt16(TxtElemento->Text); int pos=Convert::ToInt16(CmbCabezas->SelectedIndex); int aux=Lista.CAB[pos]; int xptr=Lista.Buscar(Lista.LisA,aux,Lista.ALib,1,elem); if (xptr!=99){ MessageBox::Show("El elemento sí se encuentra"); LblFinal->Text="Ubicación del elemento buscado"; TxtFinal->Text=" ("+Convert::ToString(xptr)+") "+Convert::ToString(elem); }else{ MessageBox::Show("El elemento no se encuentra en la lista"); } } TxtElemento->Focus(); TxtElemento->Clear(); }else{ MessageBox::Show("No se ha seleccionado ninguna cabeza de lista"); } }

private: System::Void BtnEliminar_Click(System::Object^ sender, System::EventArgs^ e) { if (CmbCabezas->SelectedIndex > -1){ if (Convert::ToString(TxtElemento->Text)=="" || Convert::ToString(TxtElemento->Text)==" "){ MessageBox::Show("No se ha ingresado ningún elemento"); }else{ int elem=Convert::ToInt16(TxtElemento->Text); int pos=Convert::ToInt16(CmbCabezas->SelectedIndex); int aux=Lista.CAB[pos]; int xptr=Lista.Buscar(Lista.LisA,aux,Lista.ALib,1,elem); int ante=Lista.Buscar(Lista.LisA,aux,Lista.ALib,0,elem); if (xptr!=99){ Lista.LisA[xptr].dato=-11; int sig=Lista.LisA[xptr].ptr; if (ante!=xptr){ Lista.LisA[ante].ptr=Lista.LisA[xptr].ptr; } else{ CmbCabezas->SelectedItem=Lista.LisA[sig].dato; Lista.CAB[pos]=Lista.LisA[aux].ptr; } Lista.ALib=xptr; }else{ MessageBox::Show("El elemento no se encuentra en la lista"); } } TxtElemento->Focus(); TxtElemento->Clear(); }else{ MessageBox::Show("No se ha seleccionado ninguna cabeza de lista"); } }

private: System::Void BtnEditar_Click(System::Object^ sender, System::EventArgs^ e) { if (CmbCabezas->SelectedIndex > -1){ if (Convert::ToString(TxtElemento->Text)=="" || Convert::ToString(TxtElemento->Text)==" "){ MessageBox::Show("No se ha ingresado ningún elemento"); }else{ int xdat=Convert::ToInt16(TxtElemento->Text); int ndat=Convert::ToInt16(TxtFinal->Text); int pos=Convert::ToInt16(CmbCabezas->SelectedIndex); int aux=Lista.CAB[pos]; int xptr=Lista.Buscar(Lista.LisA,aux,Lista.ALib,1,xdat); if (xptr!=99){ Lista.Editar(Lista.LisA,aux,Lista.ALib,xdat,ndat); }else{ MessageBox::Show("El elemento no se encuentra en la lista"); } }

TxtElemento->Focus(); TxtElemento->Clear(); }else{ MessageBox::Show("No se ha seleccionado ninguna cabeza de lista"); } }

private: System::Void BtnLibres_Click(System::Object^ sender, System::EventArgs^ e) { int Alib=Lista.EspaciosLibres(Lista.LisA); int Blib=Lista.EspaciosLibres(Lista.LisB); LblLibres->Text="A:"+Convert::ToString(Alib)+", B:"+Convert::ToString(Blib); }

private: System::Void BtnOcurrencias_Click(System::Object^ sender, System::EventArgs^ e) { if (CmbCabezas->SelectedIndex > -1){ if (Convert::ToString(TxtElemento->Text)=="" || Convert::ToString(TxtElemento->Text)==" ") { MessageBox::Show("No se ha ingresado ningún elemento"); }else{ int xdat=Convert::ToInt16(TxtElemento->Text); int pos=Convert::ToInt16(CmbCabezas->SelectedIndex); int aux=Lista.CAB[pos]; int xptr=Lista.Buscar(Lista.LisA,aux,Lista.ALib,1,xdat); if (xptr!=99){ int rep=Lista.Ocurrencias(Lista.LisA,aux,xdat); TxtFinal->Text=Convert::ToString(rep); BtnOcuAgre->Enabled= true; LblFinal->Text="Cantidad de ocurrencias"; TxtFinal->Enabled=false; }else{ MessageBox::Show("El elemento no se encuentra en la lista"); } } TxtElemento->Focus(); TxtElemento->Clear(); }else{ MessageBox::Show("No se ha seleccionado ninguna cabeza de lista"); } }

private: System::Void BtnOcuAgre_Click(System::Object^ sender, System::EventArgs^ e) { if (CmbCabezas->SelectedIndex > -1){ if (Convert::ToString(TxtElemento->Text)=="" || Convert::ToString(TxtElemento->Text)==" "){ MessageBox::Show("No se ha ingresado ningún elemento"); }else{ int ndat=Convert::ToInt16(TxtElemento->Text); int xdat=Convert::ToInt16(TxtFinal->Text); int pos=Convert::ToInt16(CmbCabezas->SelectedIndex); int aux=Lista.CAB[pos]; int xptr=Lista.Buscar(Lista.LisA,aux,Lista.ALib,1,xdat); if (xptr!=99){ Lista.OcurrenciaLuegoAgrega(Lista.LisA,aux,Lista.ALib,xdat,ndat); TxtFinal->Enabled=true; TxtFinal->Text=""; BtnOcuAgre->Enabled=false; }else{ MessageBox::Show("El elemento no se encuentra en la lista"); } } TxtElemento->Focus(); TxtElemento->Clear(); }else{ MessageBox::Show("No se ha seleccionado ninguna cabeza de lista"); } }

private: System::Void BtnOrdenar_Click(System::Object^ sender, System::EventArgs^ e) { if (CmbCabezas->SelectedIndex > -1){ int pos=Convert::ToInt16(CmbCabezas->SelectedIndex); int aux=Lista.CAB[pos]; if (Lista.VerificarListaOrdenada(Lista.LisA,aux)){ MessageBox::Show("La lista ya está ordenada"); }else{ MessageBox::Show("La lista no está ordenada"); Lista.OrdenarLista(Lista.LisA,aux); } TxtElemento->Focus(); TxtElemento->Clear(); }else{ MessageBox::Show("No se ha seleccionado ninguna cabeza de lista"); } } }; }