Descarga la aplicación para disfrutar aún más
Vista previa del material en texto
UNIVERSIDAD DE GUADALAJARA Centro Universitario de Ciencias Exactas e Ingenierías Estructura de datos I Actividad de Aprendizaje 03. La Lista, implementación estática Alumno: Sandoval Padilla Fernando Cesar Docente: Gutiérrez Hernández Alfredo Código: 215685409 Sección: D12 7 de Septiembre de 2019 Resumen personal: En lo personal la realización de esta actividad de implementar una lista en un programa para una situación o contexto cotidiano “real” fue bastante complicada debido al hecho de implementación de clases, puesto que en cada una se manejaron los datos y funciones por separado, además de que pese a ser un programa sencillo, tan solo tener que implementar las operaciones aunque no se fueran a utilizar en el programa fue realmente lo mas laborioso pues en base a un ejemplo echo en clase se implementaron dichas operaciones, además de que también vi necesario investigar algunas cosas que aun no entendía de forma adecuada. Código fuente: main.cpp 1 #include <windows.h> 2 #include <iostream> 3 #include <cstdlib> 4 #include "Lista.h" 5 6 using namespace std; 7 int main() 8 { 9 setlocale(LC_ALL,"spanish"); 10 Musicbox pistas; 11 char op, op2; 12 int Intpos; 13 string ncancion, nautor, nraking; 14 cout << "Programa, lista de canciones..." << endl ; 15 Sleep(500); 16 do 17 { 18 system("cls"); 19 if(pistas.getlast() != -1) 20 { 21 for(int x=0; x <= pistas.getlast(); x++) 22 { 23 cout<<pistas.seeAll(x)<<endl<<"Posicion: "<<x<<endl<<endl<<endl; 24 } 25 } 26 if(pistas.getlast() == -1) 27 { 28 cout<<"No se encuentra ninguna cancion en la lista"<<endl<<endl<<endl; 29 } 30 cout << "----------Menu----------" << endl ; 31 cout << "1) Añadir Cancion" << endl; 32 cout << "2) Eliminar Cancion" << endl; 33 cout << "3) Otras opciones" << endl; 34 cout << "4) Salir" << endl; 35 cin >> op; 36 37 op = toupper(op); 38 39 switch (op) { 40 case '1': 41 cin.ignore(); 42 cout << "Ingresa el nombre de la cancion >> "; 43 getline(cin,ncancion); 44 cout << "Ingresa el nombre del Autor >> "; 45 getline(cin,nautor); 46 cout << "Ingresa la posicion del ranking >> "; 47 cin >> nraking; 48 try { 49 pistas.insertMusic(ncancion,nautor,nraking); 50 } catch(Musicbox::Exception ex) { 51 cout << ex.what() << endl; 52 } 53 cin.ignore(); 54 break; 55 case '2': 56 cout << "Ingresa la posicion de la cancion a borrar : "; 57 cin>>Intpos; 58 try { 59 pistas.deleteMusic(Intpos); 60 } catch(Musicbox::Exception ex) { 61 cout << ex.what() << endl; 62 } 63 break; 64 case '3': 65 int other; 66 cout<<"1.-Primer elemento: "<<endl; 67 cout<<"2.-Ultimo elemento: "<<endl; 68 cout<<"3.-Elemento anterior: "<<endl; 69 cout<<"4.-Siguiente elemento: "<<endl; 70 cout<<"5.-Eliminar todo: "<<endl; 71 cout<<"6.-Recuperar elemento: "<<endl; 72 cin>>other; 73 if (other==1) 74 { 75 try { 76 cout << pistas.firstMusic() << endl << endl; 77 } catch(Musicbox::Exception ex) { 78 cout << ex.what() << endl; 79 } 80 system("pause"); 81 } 82 else if (other==2) 83 { 84 try { 85 cout<<pistas.lastMusic() << endl << endl; 86 } catch(Musicbox::Exception ex) { 87 cout << ex.what() << endl; 88 } 89 system("pause"); 90 } 91 else if (other==3) 92 { 93 cout << "Ingresa la posicion para ver el anterior a esta: "; 94 cin>>Intpos; 95 try { 96 cout << pistas.prevMusic(Intpos) << endl << endl; 97 } catch(Musicbox::Exception ex) { 98 cout << ex. what() << endl << endl << endl; 99 } 100 system("pause"); 101 } 102 else if (other==4) 103 { 104 cout << "Ingresa la posicion para ver la siguiente a esta: "; 105 cin>>Intpos; 106 try { 107 cout << pistas.nextMusic(Intpos) << endl << endl; 108 } catch(Musicbox::Exception ex) { 109 cout << ex. what() << endl << endl << endl; 110 } 111 system("pause"); 112 } 113 else if (other==5) 114 { 115 do { 116 cout<< "Estas seguro? [S/N] >> "; 117 cin>>op2; 118 cin.ignore(); 119 op2 = toupper(op2); 120 } while(op2 != 'S' and op2!= 'N'); 121 if (op2 == 'S') 122 pistas.deleteAll(); 123 system("pause"); 124 } 125 else if (other==6) 126 { 127 cout << "Proporciona el nombre de la cancion a recuperar: "; 128 getline(cin,ncancion); 129 try { 130 cout << pistas.recoverMusic(ncancion); 131 } catch(Musicbox::Exception ex) { 132 cout << ex.what() << endl; 133 } 134 system("pause"); 135 } 136 else{ 137 cout<<"Opcion inexistente"<<endl; 138 system("pause"); 139 } 140 break; 141 142 case '4': 143 cout << "Nos vemos..." << endl; 144 break; 145 146 default: 147 cout << "Error, opcion invalida o inexistente" << endl << endl ; 148 } 149 } while(op!='4'); 150 system("pause"); 151 return 0; 152 } Lista.h 1 #ifndef LISTA_H 2 #define LISTA_H 3 #include "Musica.h" 4 class Musicbox { 5 private: 6 int Last; 7 int Deleted; 8 Musica AllMusic[50]; 9 Musica DeletedMusic[50]; 10 public: 11 12 class Exception : public std::exception { 13 private: 14 std::string msg; 15 public: 16 explicit Exception(const char* message) : msg(message) { } 17 18 explicit Exception(const std::string& message) : msg(message) { } 19 20 virtual ~Exception() throw () { } 21 22 virtual const char* what() const throw() { 23 return msg.c_str(); 24 } 25 }; 26 int getlast(); 27 Musicbox(); 28 bool isFull(); 29 bool isEmpty(); 30 bool isValidPos(const int&); 31 void insertMusic(const std::string&,const std::string&,const std::string&); 32 void deleteMusic(const int&); 33 std::string recoverMusic(std::string&); 34 std::string firstMusic(); 35 std::string lastMusic(); 36 std::string prevMusic(const int&); 37 std::string nextMusic(const int&); 38 std::string seeAll(const int&); 39 void deleteAll(); 40 }; 41 42 #endif // LISTA_H Lista.cpp 1 #include "Lista.h" 2 Musicbox::Musicbox() { 3 Last=-1; 4 Deleted=-1; 5 } 6 7 bool Musicbox::isFull() { 8 return Last==49; 9 } 10 11 bool Musicbox::isEmpty() { 12 return Last==-1; 13 } 14 15 bool Musicbox::isValidPos(const int& Pos) { 16 return Pos>-1 and Pos<=Last+1; 17 } 18 19 void Musicbox::insertMusic(const std::string& Name, const std::string& Autor, const std::string& Rank) { 20 if(isFull()) { 21 throw Exception("Desbordamiento de datos, insertMusic"); 22 } 23 Last++; 24 AllMusic[Last].setSongName(Name); 25 AllMusic[Last].setSongAutor(Autor); 26 AllMusic[Last].setRanking(Rank); 27 } 28 29 void Musicbox::deleteMusic(const int& Pos) { 30 if(isEmpty()) { 31 throw Exception("Insuficiencia de datos,deleteMusic"); 32 } 33 if(!isValidPos(Pos)) { 34 throw Exception("Posicion invalida,deleteMusic"); 35 } 36 37 if(Deleted<50) { 38 Deleted++; 39 DeletedMusic[Deleted].setSongName(AllMusic[Pos].getSongName()); 40 DeletedMusic[Deleted].setSongAutor(AllMusic[Pos].getSongAutor()); 41 DeletedMusic[Deleted].setRanking(AllMusic[Pos].getRanking()); 42 } 43 int i = Pos; 44 while(i<=Last) { 45 AllMusic[i].setSongName(AllMusic[i+1].getSongName()); 46 AllMusic[i].setSongAutor(AllMusic[i+1].getSongAutor()); 47 AllMusic[i].setRanking(AllMusic[i+1].getRanking()); 48 i++; 49 } 50 Last--; 51 } 52 53 std::string Musicbox::recoverMusic(std::string& Name) { 54 if(Deleted==-1) { 55 throw Exception("Insuficiencia de datos,recoverMusic"); 56 } 57 int band; 58 for(int i=0; i<=Deleted; i++){ 59 if(DeletedMusic[i].getSongName() == Name){ 60 band=i; 61 } 62 } 63 return DeletedMusic[band].toString(); 64 } 65 66 std::string Musicbox::firstMusic() { 67 if(isEmpty()){ 68 throw Exception("Insuficiencia de datos,firstMusic"); 69 } 70 return AllMusic[0].toString(); 71 } 72 73 std::string Musicbox::lastMusic() { 74 if(isEmpty()){ 75 throw Exception ("Insuficiencia de datos,lastMusic"); 76 } 77 return AllMusic[Last].toString(); 78 } 79 80 std::string Musicbox::prevMusic(const int& Pos) { 81 if(isEmpty() or Last==0){ 82 throw Exception("Insuficiencia de datos,prevMusic"); 83 } 84 if(!isValidPos(Pos)){ 85 throw Exception("Posicion invalida,prevMusic"); 86 } 87 return AllMusic[Pos-1].toString(); 88 } 89 90 std::string Musicbox::nextMusic(const int& Pos) { 91 if(isEmpty()){ 92 throw Exception("Insuficiencia de datos,nextMusic"); 93 } 94 if(!isValidPos(Pos)){ 95 throw Exception("Posicion invalida,nextMusic"); 96 } 97 return AllMusic[Pos+1].toString(); 98 } 99 100 std::string Musicbox::seeAll(const int& Pos){101 return AllMusic[Pos].toString(); 102 } 103 104 void Musicbox::deleteAll() { 105 if(isEmpty()){ 106 throw Exception("Insuficiencia de datos,deleteAll"); 107 } 108 Last=-1; 109 Deleted=-1; 110 } 111 112 int Musicbox::getlast(){ 113 return Last; 114 } Musica.h 1 #ifndef MUSICA_H 2 #define MUSICA_H 3 #include <string> 4 5 class Musica { 6 private: 7 std::string SongName; 8 std::string SongAutor; 9 std::string Ranking; 10 public: 11 Musica(); 12 std::string getSongName(); 13 std::string getSongAutor(); 14 std::string getRanking(); 15 void setSongName(const std::string&); 16 void setSongAutor(const std::string&); 17 void setRanking(const std::string&);; 18 std::string toString(); 19 }; 20 #endif // MUSICA_H Musica.cpp 1 #include "Musica.h" 2 #include <cstdio> 3 4 Musica::Musica() { 5 SongAutor=""; 6 SongName=""; 7 Ranking=""; 8 } 9 10 std::string Musica::getSongName() { 11 return SongName; 12 } 13 14 std::string Musica::getSongAutor() { 15 return SongAutor; 16 } 17 18 std::string Musica::getRanking() { 19 return Ranking; 20 } 21 22 void Musica::setSongName(const std::string& Name) { 23 SongName = Name; 24 } 25 26 void Musica::setSongAutor(const std::string& Autor) { 27 SongAutor = Autor; 28 } 29 30 void Musica::setRanking(const std::string& Rank) { 31 Ranking = Rank; 32 } 33 34 std::string Musica::toString() { 35 std::string CompleteSong; 36 CompleteSong+="Nombre : "; 37 CompleteSong+=SongName; 38 CompleteSong+="\nAutor : "; 39 CompleteSong+=SongAutor; 40 CompleteSong+="\nRanking : "; 41 CompleteSong+=Ranking; 42 43 return CompleteSong; 44 } Capturas de pantalla: Menu Agregando datos Datos ya gregados en conjunto con el menú Eliminar canción Resultado de borrar Otras opciones Opción inexistente Opción Salir
Compartir