Hola este no es un tutorial, solo es un programa en C++ que estoy haciendo para una clase donde tenemos que crear un tipo de dato abstracto Matriz, sobrecargar algunos operadores y métodos, usar algo de manejo de memoria dinámica y templates.
Dejo el código por si a alguien le sirve 🙂 …
Menu del programa
Mostrar las matrices
Implementacion
#include #include "Matriz.h" //#include //descomentar esta linea en windows using namespace std; int main(){ int opc = 0; Matriz a(1),b(1),c,d; a.llenaMatriz(); b.llenaMatriz(1); do { cout<<"[0] Sumar matrices"<<<"[1] Multiplicar matrices"<<<"[2] Mostrar matrices"<<<"[3] Transpuesta (los resultados de suma/multiplicacion)"<<<"[4] Las diagonales (los resultados de suma/multiplicacion)"<<<"[5] Numero menor de la matriz (los resultados de suma/multiplicacion)"<<<"[6] Salir del programa"<<<<"Elije una opcion: "; cin>>opc; switch(opc) { case 0: c=a+b; cout<<<<"Matriz A"<<<<"Matriz B"<<<<<<"El numero menor de la Matriz: "<<<<<"Es: "<<<<<<"Gracias por usar el programa"; break; default: cout<<"Opcion invalida, elije un numero del menu"; } }while(opc != 6); //getch(); //descomentar esta linea en windows return 0; }
Diseño
#include <iostream> #include <cstdlib> #include <stdlib.h> #include <time.h> using namespace std; const int ren = 5; const int col = 5; template <class T> class Matriz { public: //constructores Matriz(); Matriz(T); Matriz(Matriz<T> &); ~Matriz(); //metodos void operator = (const Matriz<T> &); T getDato(int, int); void setDato(int, int, T); void transpuesta (Matriz<T> &); void muestradiagonales(); void llenaMatriz(); void llenaMatriz(int); T menor(); private: //puntero a punteros T **datos; //T datos[ren][col]; }; template<class T>Matriz<T>::~Matriz() { //liberar localidades de memoria for (int i=0; i<ren; i++) { free (datos[i]); } //free (datos); delete datos; datos = 0; } template<class T>Matriz<T>::Matriz() { //datos = new T[ren*col]; datos = (T **)malloc (sizeof(int *)*col); /*Reservas tantos punteros como filas*/ for (int i=0; i<col; i++) /* En cada fila ... */ datos[i] = (T *)malloc (sizeof(int)*ren); /* ... reservas tantos int como columnas*/ float n = 0; for(int i = 0; i<ren; i++) { for(int j = 0; j<col; j++) { setDato(i,j,n); } } } template<class T>Matriz<T>::Matriz(T valorinicial) { //datos = new T[ren*col]; datos = (T **)malloc (sizeof(int *)*col); /*Reservas tantos punteros como filas*/ for (int i=0; i<col; i++) /* En cada fila ... */ datos[i] = (T *)malloc (sizeof(int)*ren); /* ... reservas tantos int como columnas*/ float n = 0; for(int i = 0; i<ren; i++) { for(int j = 0; j<col; j++) { setDato(i,j,valorinicial); } } } //Matriz con numeros aleatorios si se le manda un parametro template<class T> void Matriz<T>::llenaMatriz() { int n = 0; srand(time(NULL)); for(int i = 0; i<ren; i++) { for(int j = 0; j<col; j++) { if((n = rand() % 100) % 2 == 0) n = n*-1; this->setDato(i,j,n); } } } template<class T> void Matriz<T>::llenaMatriz(int sal) { int n = 0; srand(time(NULL)); for(int i = 0; i<ren; i++) { for(int j = 0; j<col; j++) { sal = rand() % 5; if((n = rand() % 100) % 2 == 0) n = n*-1; if(n+sal > 100) {setDato(i,j,100);}else{ setDato(i,j,n+sal);} } } } template<class T>Matriz<T>::Matriz( Matriz<T> &M) { //datos = new T[ren*col]; datos = (T **)malloc (sizeof(int *)*col); /*Reservas tantos punteros como filas*/ for (int i=0; i<col; i++) /* En cada fila ... */ datos[i] = (T *)malloc (sizeof(int)*ren); /* ... reservas tantos int como columnas*/ for(int i = 0; i<ren; i++) { for(int j = 0; j<col; j++) { setDato(i,j,M.getDato(i,j)); } } } template<class T>T Matriz<T>::getDato(int c, int r) { //c = columna //r = renglon return datos[c][r]; //return datos[col*c+r]; } template <class T>T Matriz<T>::menor() { int menor = this->getDato(0,0); for(int i = 0; i<ren; i++) { for(int j = 0; j<col; j++) { if(this->getDato(i,j) < menor) menor = this->getDato(i,j); } } return menor; } template<class T>void Matriz<T>::muestradiagonales() { int renglon = ren-1; cout<<endl; for(int i = 0; i<ren; i++) { cout<<"["<<this->getDato(i,i)<<"]"; } cout<<endl; for(int i = 0; i<ren;i++) { cout<<"["<<this->getDato(renglon,i)<<"]"; renglon = renglon - 1; } cout<<endl; } template <class T> void Matriz<T>::setDato(int c, int r, T dato) { datos[c][r] = dato; //datos[col*r+c] = dato; //datos[col*c+r] = dato; } template <class T> void Matriz<T>::transpuesta (Matriz<T> & M) { float tmp = 0; if(ren != col) { cout<<"Solo se aceptan matrices cuadradas"; } else { for(int i = 0; i<col; i++) { for(int j = 0; j<ren; j++) { tmp = M.getDato(i,j); this->setDato(i,j,M.getDato(j,i)); this->setDato(j,i,tmp); } } } } template <class T> void Matriz<T>::operator = (const Matriz<T> &igualar) { int i = 0, j = 0; for(i = 0; i<col; i++) { for(j = 0; j<ren; j++) { //this->setDato(i,j,igualar.datos[i*j]); //this->setDato(i,j,igualar.datos[i*j]); this->setDato(i,j,igualar.datos[i][j]); this->setDato(i,j,igualar.datos[i][j]); } } } template <class T> Matriz<T> operator + (Matriz<T> &uno, Matriz<T> &dos) { Matriz<T> temp; for(int i = 0; i<col; i++) { for(int j = 0; j<ren; j++) { temp.setDato(i,j,(uno.getDato(i,j)+dos.getDato(i,j))); } } return temp; } template <class T> Matriz<T> operator * (Matriz<T> &uno, Matriz<T> &dos) { int i = 0, j = 0, k = 0; Matriz<T> objTemp; for(i = 0; i<col; i++) { for(j = 0; j<ren; j++) { for(k = 0; k <col; k++) { //temporal.setDato(j,i,(T.getDato(j,i)+uno.getDato(j,k)*dos.getDato(k,i))); objTemp.setDato(j,i,(objTemp.getDato(j,i)+uno.getDato(j,k)*dos.getDato(k,i))); } } } return objTemp; } //Sobrecargando el operador de asignacion para que maneje nuestro nuevo objeto abstracto Matriz template <class T> std::ostream& operator<< (std::ostream &salida, Matriz<T> &objeto) { for(int i = 0; i<col; i++) { for(int j = 0; j<ren; j++) { cout<<"\t"<<objeto.getDato(i,j); } cout<<endl; } //salida<<a; return salida; }
Descargar los archivos
salu2
PD: El banner del post no tiene nada que ver xD, lo que pasa que en ese momento estaba jugando Super Meat Boy :p
Muchas gracias, ahora toca comprenderlo al 100 en mis ratos libres me interesa mucho aprender c++, de nuevo muchas gracias.