Logo Studenta

Actividad de Aprendizaje 08 Apuntadores - Fernando Cesar Sandoval Padilla

¡Este material tiene más páginas!

Vista previa del material en texto

UNIVERSIDAD DE GUADALAJARA
Centro Universitario de Ciencias Exactas e Ingenierías
Estructura de datos I
Actividad de Aprendizaje 08. Apuntadores
Alumno: Sandoval Padilla Fernando Cesar
Docente: Gutiérrez Hernández Alfredo
Código: 215685409
Sección: D12
	
			 12 de Octubre de 2019
Resumen personal:
Para realizar esta actividad solo fue necesario adaptar el código y agregar una clase nodo, para implementar el arreglo de apuntadores, es decir, cambiando el arreglo de objetos por un arreglo de apuntadores, por lo tanto para adaptarlo no fue necesario invertirle tanto tiempo a la realización de la actividad, mientras que el funcionamiento del programa es el mismo.
Código fuente:
main.cpp
1 #include <iostream>
2 #include <cstdlib>
3 #include <windows.h>
4 #include "Lista.h"
5 #include "Musica.h"
6
7 using namespace std;
8 int main() {
9 setlocale(LC_ALL,"spanish");
10 Musicbox<Songs> mySongs;
11 Songs mySong;
12 char op, op2;
13 string myString;
14 int myInt;
15
16 cout << "Bienvenido al programa" << endl;
17 do {
18 if( mySongs.getLastPos() != -1 )
19 for( int i(0) ; i <= mySongs.getLastPos() ; i++ )
20 cout << mySongs.retrieve(i)<< endl;
21 cout << "----------------Menu----------------" << endl;
22 cout << "1) Añadir cancion" << endl;
23 cout << "2) Eliminar cancion" << endl;
24 cout << "3) Buscar cancion" <<endl;
25 cout << "4) Ordenar" << endl;
26 cout << "5) Otras opciones" << endl;
27 cout << "6) Salir" << endl;
28 cout << "Ingresa una opcion" << endl;
29 cin >> op;
30 op = toupper(op);
31 switch(op) {
32 case '1':
33 cout << "Menu" << endl;
34 cout << "1) Añadir cancion" << endl;
35 cout << "2) Añadir desde una posicion" << endl;
36 cout << "Ingrese una opcion:";
37 cin >> op;
38 op = toupper(op);
39 switch(op) {
40 case '1':
41 cin.ignore();
42 cout << "Ingrese el nombre de la cacion:";
43 getline(cin,myString);
44 mySong.setSongName(myString);
45 cout << "Ingrese el nombre del autor:";
46 getline(cin,myString);
47 mySong.setSongAutor(myString);
48 cout << "Ingrese la posicion en el ranking:";
49 getline(cin,myString);
50 mySong.setSongRanking(myString);
51 try {
52 mySongs.insertSong(mySongs.getLastPos(),mySong);
53 }
54 catch(Exception ex) {
55 cout << ex.what() << endl;
56 }
57 break;
58 case '2':
59 cin.ignore();
60 cout << "Proporcione el nombre de la cacion:";
61 getline(cin,myString);
62 mySong.setSongName(myString);
63 cout << "Proporcione el nombre del autor:";
64 getline(cin,myString);
65 mySong.setSongAutor(myString);
66 cout << "Proporcione la posicion en el ranking:";
67 getline(cin,myString);
68 mySong.setSongRanking(myString);
69 cout << "Proporcione la posicion en que desa añadir:";
70 cin >> myInt;
71 try {
72 mySongs.insertSong(myInt,mySong);
73 }
74 catch(Exception ex) {
75 cout << ex.what() << endl << endl;
76 }
77 break;
78 default:
79 cout <<"Opcion invalida" <<endl;
80 break;
81 }
82 break;
83 case '2':
84 cout << "Ingrese que cancion deseas eliminar >> ";
85 cin >> myInt;
86 try {
87 mySongs.deleteMusic(myInt);
88 }
89 catch (Exception ex) {
90 cout << ex.what() << endl << endl;
91 }
92 break;
93 case '3':
94 cout << "Menu" << endl;
95 cout << "1) Busqueda lineal" << endl;
96 cout << "2) Busqueda binaria" << endl;
97 cout << "Ingresa una opcion" << endl;
98 cin >> op;
99 op = toupper(op);
100 switch(op) {
101 case '1':
102 cin.ignore();
103 cout << "Ingresa la cancion a buscar >> ";
104 getline(cin,myString);
105 mySong.setSongAutor(myString);
106 mySong.setSongName(myString);
107 try {
108 cout << mySongs.retrieve(mySongs.FindDataL(mySong)) << endl;
109 }
110 catch( Exception ex ) {
111 cout << ex.what() << endl;
112 }
113 break;
114 case '2':
115 cin.ignore();
116 cout << "Ingresa la cancion a buscar >> ";
117 getline(cin,myString);
118 mySong.setSongAutor(myString);
119 mySong.setSongName(myString);
120 try {
121 cout << mySongs.retrieve(mySongs.FindDataB(mySong)) << endl;
122 }
123 catch( Exception ex ) {
124 cout << ex.what() << endl;
125 }
126 break;
127 }
128 system("pause");
129 break;
130 case '4':
131 cout << "Menu" << endl;
132 cout << "A) Por Cancion" << endl;
133 cout << "B) Por Autor" << endl;
134 cin >> op;
135 cin.ignore();
136 op = toupper(op);
137 op == 'A' ? op2 = 'A' : op2 = 'B';
138 cout << "Menu" << endl;
139 cout << "1) Metodo burbuja" << endl;
140 cout << "2) Metodo Shell" << endl;
141 cout << "3) Metodo Insercion" << endl;
142 cout << "4) Metodo Seleccion" << endl;
143 cin >> op;
144 cin.ignore();
145 op = toupper(op);
146 switch(op) {
147 case '1':
148 try {
149 mySongs.BubbleSortSongs(op2);
150 }
151 catch(Exception ex) {
152 cout << ex.what() << endl;
153 }
154 break;
155 case '2':
156 try {
157 mySongs.ShellSortSongs(op2);
158 }
159 catch(Exception ex) {
160 cout << ex.what() << endl;
161 }
162 break;
163 case '3':
164 try {
165 mySongs.InsertSortSongs(op2);
166 }
167 catch(Exception ex) {
168 cout << ex.what() << endl;
169 }
170 break;
171 case '4':
172 try {
173 mySongs.SelectSortSongs(op2);
174 }
175 catch(Exception ex) {
176 cout << ex.what() << endl;
177 }
178 break;
179 }
180 break;
181
182
183
184 case '5':
185 int other;
186 cout<<"1.-Primer elemento: "<<endl;
187 cout<<"2.-Ultimo elemento: "<<endl;
188 cout<<"3.-Elemento anterior: "<<endl;
189 cout<<"4.-Siguiente elemento: "<<endl;
190 cout<<"5.-Eliminar todo: "<<endl;
191 cin>>other;
192 if (other==1)
193 {
194 try {
195 cout << mySongs.retrieve(mySongs.FirstMusic());
196 }
197 catch(Exception ex) {
198 cout << ex.what() << endl;
199 }
200 system("pause");
201 }
202 else if (other==2)
203 {
204 try {
205 cout << mySongs.retrieve(mySongs.LastMusic());
206 }
207 catch(Exception ex) {
208 cout << ex.what() << endl;
209 }
210 system("pause");
211 }
212 else if (other==3)
213 {
214 cout << "Ingresa la posicion a buscar >> ";
215 cin >> myInt;
216 try {
217 cout << mySongs.retrieve(mySongs.PrevMusic(myInt)) << endl;
218 }
219 catch(Exception ex) {
220 cout << ex.what() << endl;
221 }
222 system("pause");
223
224 }
225 else if (other==4)
226 {
227 cout << "Ingresa la posicion a buscar >> ";
228 cin >> myInt;
229 try {
230 cout << mySongs.retrieve(mySongs.NextMusic(myInt)) << endl;
231 }
232 catch(Exception ex) {
233 cout << ex.what() << endl;
234 }
235 system("pause");
236
237 }
238 else if (other==5)
239 {
240 do {
241 cout << "Estas seguro? [S/N]" << endl;
242 cin >> op;
243 op = toupper(op);
244 if( op == 'S' ) {
245 try {
246 mySongs.DeleteAllMusic();
247 }
248 catch(Exception ex) {
249 cout << ex.what() << endl;
250 }
251 cout << "Se han eliminado satisfactoriamente" << endl;
252 }
253 }
254 while ( op != 'S' and op != 'N');
255 system("pause");
256 }
257 else{
258 cout<<"Opcion inexistente"<<endl;
259 system("pause");
260 }
261 break;
262 default:
263 cout << "Se ha ingresado una opcion incorrecta, intentelo de nuevo" << endl;
264 }
265 system("cls");
266 }
267 while(op!='6');
268 return 0;
269 }
Lista.h
1 #ifndef LISTA_H
2 #define LISTA_H
3
4 #include <string>
5 #include "Exception.h"
6 #include "node.h"
7
8 template <class T, int ARRAYSIZE = 1024>
9 class Musicbox {
10 public:
11 class Node {
12 private:
13 T data;
14 public:
15 Node(const T&);
16 T& getData();
17 void setData(const T&);
18 };
19 private:
20 Node* AllSongs[ARRAYSIZE];
21 int Last;
22 void copyAll(const Musicbox<T,ARRAYSIZE>&);
23 void swapSong(Node*,Node*);
24 public:
25 Musicbox();
26 bool isFull();
27 bool isEmpty();
28 bool isValidPos(const int&);
29 void insertSong(const int&, const T&);
30 void deleteMusic(const int&);
31 int FirstMusic();
32 int LastMusic();
33 int PrevMusic(const int&);
34 int NextMusic(const int&);
35 int FindDataL(const T&);
36 int FindDataB(const T&);
37 T& retrieve(const int&);
38 void BubbleSortSongs(const char&);
39 void ShellSortSongs(const char&);
40 void InsertSortSongs(const char&);
41 void SelectSortSongs(const char&);
42 std::string toString();
43 void DeleteAllMusic();
44 int getLastPos();
45 Musicbox<T,ARRAYSIZE>& operator = (const Musicbox<T,ARRAYSIZE>&);
46 };
47 template <class T, int ARRAYSIZE>
48 Musicbox<T,ARRAYSIZE>::Node::Node(const T& e) : data(e) { }49 template <class T, int ARRAYSIZE>
50 T& Musicbox<T,ARRAYSIZE>::Node::getData() {
51 return data;
52 }
53 template <class T, int ARRAYSIZE>
54 void Musicbox<T,ARRAYSIZE>::Node::setData(const T& e) {
55 data = e;
56 }
57 template <class T, int ARRAYSIZE>
58 void Musicbox<T,ARRAYSIZE>::copyAll(const Musicbox<T,ARRAYSIZE>& AllMusic) {
59 int i(0);
60 while ( i <= Last ) {
61 this->*AllSongs[i] = AllMusic.AllSongs[i];
62 }
63 }
64 template <class T, int ARRAYSIZE>
65 Musicbox<T,ARRAYSIZE>::Musicbox() {
66 Last=-1;
67 int i=0;
68 while(i<=1023) {
69 AllSongs[i] = nullptr;
70 i++;
71 }
72 }
73 template <class T, int ARRAYSIZE>
74 bool Musicbox<T,ARRAYSIZE>::isFull() {
75 return Last == ARRAYSIZE - 1;
76 }
77 template <class T, int ARRAYSIZE>
78 bool Musicbox<T,ARRAYSIZE>::isValidPos(const int& pos) {
79 return pos > -1 and pos <= Last;
80 }
81 template <class T, int ARRAYSIZE>
82 bool Musicbox<T,ARRAYSIZE>::isEmpty() {
83 return Last == -1;
84 }
85 template <class T, int ARRAYSIZE>
86 void Musicbox<T,ARRAYSIZE>::insertSong(const int& pos, const T& Song) {
87 if( isFull() ) {
88 throw Exception("Desbordamiento de datos, insertSong");
89 }
90 if( pos != -1 and !isValidPos(pos) ) {
91 throw Exception("Posicion invalida, insertSong");
92 }
93 Node* aux(new Node(Song));
94 int i(Last);
95 while( i > pos ) {
96 AllSongs[i] = AllSongs[i-1];
97 i--;
98 }
99 AllSongs[pos + 1] = aux;
100 Last++;
101 }
102 template <class T, int ARRAYSIZE>
103 void Musicbox<T,ARRAYSIZE>::deleteMusic(const int& pos) {
104 if(isEmpty()) {
105 throw Exception("Insuficiencia de datos, deleteMusic");
106 }
107 if(!isValidPos(pos)) {
108 throw Exception("Posicion invalida, deleteMusic");
109 }
110 int i = pos;
111 delete AllSongs[pos];
112 while( i < Last ) {
113 AllSongs[i] = AllSongs[i+1];
114 i++;
115 }
116 Last--;
117 }
118 template <class T, int ARRAYSIZE>
119 int Musicbox<T,ARRAYSIZE>::FirstMusic() {
120 if(isEmpty()) {
121 throw Exception("Insuficiencia de datos, FirstMusic");
122 }
123 return 0;
124 }
125 template <class T, int ARRAYSIZE>
126 int Musicbox<T,ARRAYSIZE>::LastMusic() {
127 if(isEmpty()) {
128 throw Exception("Insuficienca de datos, LastMusic");
129 }
130 return Last;
131 }
132 template <class T, int ARRAYSIZE>
133 int Musicbox<T,ARRAYSIZE>::PrevMusic(const int& pos) {
134 if( isEmpty() ) {
135 throw Exception("Insuficiencia de datos, PrevMusic");
136 }
137 if( !isValidPos(pos) or pos == 0) {
138 throw Exception("Posicion invalida, PrevMusic");
139 }
140 return pos - 1;
141 }
142 template <class T, int ARRAYSIZE>
143 int Musicbox<T,ARRAYSIZE>::NextMusic(const int& pos) {
144 if( isEmpty() ) {
145 throw Exception("Insuficiencia de datos");
146 }
147 if( !isValidPos(pos) or pos == Last ) {
148 throw Exception("Posicion invalida, NextPos");
149 }
150 return pos + 1;
151 }
152 template <class T, int ARRAYSIZE>
153 int Musicbox<T,ARRAYSIZE>::FindDataL(const T& Element ) {
154 if( isEmpty() ) {
155 throw Exception("Insuficiencia de datos, FindDataL");
156 }
157 int i(0);
158 while ( i <= Last ) {
159 if( AllSongs[i]->getData() == Element ) {
160 return i;
161 }
162 i++;
163 }
164 return -1;
165 }
166 template <class T, int ARRAYSIZE>
167 int Musicbox<T,ARRAYSIZE>::FindDataB(const T& Element) {
168 if( isEmpty() ) {
169 throw Exception("Insuficiencia de datos, FindDataB");
170 }
171 int i(0), j(Last), m;
172 while ( i <= j ) {
173 m = ( i + j ) / 2;
174 if( AllSongs[m]->getData() == Element ) {
175 return m;
176 }
177 if( Element < AllSongs[m]->getData() ) {
178 j = m - 1;
179 }
180 else {
181 i = m + 1;
182 }
183 }
184 return -1;
185 }
186 template <class T, int ARRAYSIZE>
187 T& Musicbox<T,ARRAYSIZE>::retrieve(const int& pos) {
188 if( isEmpty() ) {
189 throw Exception("Insuficiencia de datos, retrieveSong");
190 }
191 if( !isValidPos(pos) ) {
192 throw Exception("Insuficiencia de datos, retrieveSong");
193 }
194 return AllSongs[pos]->getData();
195 }
196 template <class T, int ARRAYSIZE>
197 void Musicbox<T,ARRAYSIZE>::swapSong(Node* a,Node* b) {
198 Node* aux(a);
199 a=b;
200 b=aux;
201 }
202 template <class T, int ARRAYSIZE>
203 void Musicbox<T,ARRAYSIZE>::BubbleSortSongs(const char& opt) {
204 int i(Last), j;
205 bool flag;
206 do {
207 flag = false;
208 j = 0;
209 while( j < i ) {
210 if( opt == 'A' ) {
211 if( AllSongs[j]->getData() > AllSongs[j+1]->getData() ) {
212 swapSong( AllSongs[j], AllSongs[i] );
213 flag = true;
214 }
215 }
216 if( opt == 'B' ) {
217 if( AllSongs[j]->getData() > AllSongs[j+1]->getData() ) {
218 swapSong( AllSongs[j], AllSongs[i] );
219 flag = true;
220 }
221 }
222 j++;
223 }
224 i--;
225 }
226 while(flag);
227 }
228 template <class T, int ARRAYSIZE>
229 void Musicbox<T,ARRAYSIZE>::ShellSortSongs(const char& opt) {
230 float fact ( 3.0 / 4.0) ;
231 int dif( (Last + 1) * fact), lim, i;
232 while(dif > 0) {
233 lim = Last - dif;
234 i=0;
235 while( i <= lim ) {
236 if(opt == 'A') {
237 if( AllSongs[i]->getData() > AllSongs[ i+ dif ]->getData() ) {
238 swapSong( AllSongs[i], AllSongs[i+dif] );
239 }
240 }
241 if(opt == 'B') {
242 if( AllSongs[i]->getData() > AllSongs[ i+ dif ]->getData() ) {
243 swapSong( AllSongs[i], AllSongs[i+dif] );
244 }
245 }
246 i++;
247 }
248 dif *= fact;
249 }
250 }
251 template <class T, int ARRAYSIZE>
252 void Musicbox<T,ARRAYSIZE>::InsertSortSongs(const char& opt) {
253 int i(1), j;
254 Node* aux;
255 if( opt == 'A') {
256 while( i <= Last ) {
257 aux = AllSongs[i];
258 j=i;
259 while ( j > 0 and aux->getData() < AllSongs[j-1]->getData() ) {
260 AllSongs[j] = AllSongs[j-1];
261 j--;
262 }
263 if( i != j ) {
264 AllSongs[j] = aux;
265 }
266 i++;
267 }
268 }
269 if(opt == 'B') {
270 while( i <= Last ) {
271 aux = AllSongs[i];
272 j=i;
273 while ( j > 0 and aux->getData() < AllSongs[j-1]->getData() ) {
274 AllSongs[j] = AllSongs[j-1];
275 j--;
276 }
277 if( i != j ) {
278 AllSongs[j] = aux;
279 }
280 i++;
281 }
282 }
283 }
284 template <class T, int ARRAYSIZE>
285 void Musicbox<T,ARRAYSIZE>::SelectSortSongs(const char& opt) {
286 int i(0), j, m;
287 while( i < Last ) {
288 m = i;
289 j = i + 1;
290 if( opt == 'A') {
291 while( j < Last ) {
292 if( AllSongs[j]->getData() < AllSongs[m]->getData() ) {
293 m = j;
294 }
295 j++;
296 }
297 }
298 if( opt == 'B') {
299 while( j < Last ) {
300 if( AllSongs[j]->getData() < AllSongs[m]->getData() ) {
301 m = j;
302 }
303 j++;
304 }
305 }
306 if( m!=i ) {
307 swapSong( AllSongs[i], AllSongs[m] );
308 }
309 i++;
310 }
311 }
312 template <class T, int ARRAYSIZE>
313 std::string Musicbox<T,ARRAYSIZE>::toString() {
314 std::string AllMusic;
315 for( int i(0) ; i <= Last ; i++ ) {
316 AllMusic += AllSongs[i]->toString() + "\n" ;
317 }
318 return AllMusic;
319 }
320 template <class T, int ARRAYSIZE>
321 void Musicbox<T,ARRAYSIZE>::DeleteAllMusic() {
322 int i(0);
323 while(i <= Last) {
324 delete AllSongs[i];
325 i++;
326 }
327 Last = -1;
328 }
329 template <class T, int ARRAYSIZE>
330 int Musicbox<T,ARRAYSIZE>::getLastPos() {
331 return Last;
332 }
333 template <class T, int ARRAYSIZE>
334 Musicbox<T,ARRAYSIZE>& Musicbox<T,ARRAYSIZE>::operator = (const Musicbox<T,ARRAYSIZE>& Song) {
335 copyAll(Song);
336 return *this;
337 }
338 #endif // LISTA_H
Musica.h
1 #ifndef MUSICA_H
2 #define MUSICA_H
3 #include <string>
4 #include <iostream>
5
6 class Songs {
7 private:
8 std::string SongName;
9 std::string SongAutor;
10 std::string SongRanking;
11 public:
12 void setSongName(const std::string&);
13 void setSongAutor(const std::string&);
14 void setSongRanking(const std::string&);
15 std::string toString() const;
16 std::string getSongName() const;
17 std::string getSongAutor() const; std::string getSongRanking() const;
18 bool operator == (const Songs&) const;
19 bool operator != (const Songs&) const;
20 bool operator >= (const Songs&) const;
21 bool operator > (const Songs&) const;
22 bool operator <= (const Songs&) const;
23 bool operator < (const Songs&) const;
24 friend std::ostream& operator << (std::ostream&, Songs&);
25 friend std::istream& operator >> (std::istream&, Songs&);
26 };
27 #endif // MUSICA_H
Musica.cpp
1 #include "Musica.h"2
3 void Songs::setSongName(const std::string& Name) {
4 SongName = Name;
5 }
6 void Songs::setSongAutor(const std::string& Autor) {
7 SongAutor = Autor;
8 }
9 void Songs::setSongRanking(const std::string& Ranking) {
10 SongRanking = Ranking;
11 }
12 std::string Songs::toString() const {
13 std::string AllSong; AllSong += SongName; AllSong += " | ";
14 AllSong += SongAutor; AllSong += " | ";
15 AllSong += SongRanking;
16 return AllSong;
17 }
18 std::string Songs::getSongName() const {
19 return SongName;
20 }
21 std::string Songs::getSongAutor() const {
22 return SongAutor;
23 }
24 std::string Songs::getSongRanking() const {
25 return SongRanking;
26 }
27 bool Songs::operator == (const Songs& Song) const {
28 return SongName == Song.SongName or SongAutor == Song.SongAutor;
29 }
30 bool Songs::operator != (const Songs& Song) const {
31 return SongName != Song.SongName or SongAutor != Song.SongAutor;
32 }
33 bool Songs::operator >= (const Songs& Song) const {
34 return SongName >= Song.SongName or SongAutor >= Song.SongAutor;
35 }
36 bool Songs::operator > (const Songs& Song) const {
37 return SongName > Song.SongName or SongAutor > Song.SongAutor;
38 }
39 bool Songs::operator <= (const Songs& Song) const {
40 return SongName <= Song.SongName or SongAutor <= Song.SongAutor;
41 }
42 bool Songs::operator < (const Songs& Song) const {
43 return SongName < Song.SongName or SongAutor < Song.SongAutor;
44 }
45 std::ostream& operator << (std::ostream& os, Songs& s){
46 std::string aux;
47 aux = s.SongName + " | " + s.SongAutor + " | " + s.SongRanking + "\n";
48 os << aux;
49 return os;
50 }
51 std::istream& operator >> (std::istream& is, Songs& s){ is >> s.SongAutor;
52 is >> s.SongAutor; is >> s.SongRanking; return is;
53 }
Exception.h
1 #ifndef EXCEPTION_H_INCLUDED
2 #define EXCEPTION_H_INCLUDED
3
4 #include <exception>
5 #include <string>
6
7 class Exception : public std::exception {
8 private:
9 std::string msg;
10 public:
11 explicit Exception(const char* message) : msg(message) { }
12
13 explicit Exception(const std::string& message) : msg(message) { }
14
15 virtual ~Exception() throw () { }
16
17 virtual const char* what() const throw() {
18 return msg.c_str();
19 }
20 };
21
22 #endif // EXCEPTION_H_INCLUDED
node.cpp
1 #include "node.h"
2
3 template <class T>
4 Node<T>::Node() : next(nullptr) { }
5
6 template <class T>
7 Node<T>::Node(const T& e) : data(e), next(nullptr) { }
8
9 template <class T>
10 T& Node<T>::getData()
11 {
12 return data;
13 }
14
15 template <class T>
16 void Node<T>::setData(const T& e)
17 {
18 data = e;
19 }
node.h
1 #ifndef NODE_H_INCLUDED
2 #define NODE_H_INCLUDED
3
4 template<class T>
5 class Node
6 {
7 private:
8 T data;
9 Node* next;
10
11 public:
12 Node();
13 Node(const T&);
14
15 T& getData();
16
17 void setData(const T&);
18 };
19
20 #endif // NODE_H_INCLUDED
Capturas de pantalla:
Menu
Añadir canción
Ordenamientos
Método burbuja
Método Shell
Método de inserción 
Método de selección 
Ordenando por autor

Otros materiales