Advertisement
Guest User

Matriz.h

a guest
Aug 26th, 2010
971
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.01 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. #include <cstdlib>
  6. #include <iomanip>
  7. #include <cassert>
  8.  
  9. class Matriz {
  10.  
  11.     private:
  12.  
  13.         double A[10][10];
  14.  
  15.         int rows, cols;
  16.  
  17.     public:
  18.  
  19.  
  20.          Matriz(int n, int m);  // Constructor de la clase
  21.         ~Matriz();  // Destructor de la clase
  22.  
  23.         void initMatriz(double **x);
  24.         void showMatriz();
  25.  
  26.         friend ostream& operator << (ostream &salida, Matriz &z); //sobrecarga del operador <<
  27.         friend Matriz operator *(Matriz x, Matriz y); //sobrecarga al operador * para multiplicar matrices
  28.  
  29. };
  30.  
  31. Matriz::Matriz(int n, int m) {// Para que imprima ceros para todos los elementos de cualquier matriz no inicializada
  32.  
  33.     rows = n;
  34.     cols = m;
  35.  
  36.     int i, j;
  37.        
  38.     for(i= 0; i < rows; i++){
  39.                        
  40.         for(j= 0; j < cols; j++){
  41.                    
  42.             A[i][j] = 0;
  43.                    
  44.         }
  45.    
  46.     }  
  47.                            
  48. }
  49.  
  50. Matriz::~Matriz(){}
  51.  
  52. void Matriz::initMatriz(double **x){
  53.  
  54.     int i,j;
  55.  
  56.     for(i=0; i < rows; i++){
  57.  
  58.             for(j=0; j < cols; j++){
  59.  
  60.                 A[i][j] = x[i][j];
  61.        
  62.         }
  63.     }
  64.    
  65.  
  66. }
  67.  
  68. void Matriz::showMatriz(){
  69.  
  70.     cout.setf(ios::fixed);
  71.     cout.precision(5); 
  72.  
  73.  
  74.     int i,j;
  75.  
  76.     for(i=0; i < rows; i++){
  77.  
  78.             for(j=0; j < cols; j++){
  79.  
  80.                 cout << setw(10) << A[i][j];
  81.        
  82.         }
  83.  
  84.         cout << endl;
  85.  
  86.     }
  87.  
  88. }
  89.  
  90. ostream &operator << (ostream &salida, Matriz &z){//sobrecarga del operador <<
  91.  
  92.     salida.setf(ios::fixed);
  93.     salida.precision(5);   
  94.  
  95.     int i, j;
  96.  
  97.     for (i = 0; i < z.rows; i++) {
  98.  
  99.         for (j = 0; j < z.cols; j++) {
  100.  
  101.             salida << setw(10) << z.A[i][j];
  102.  
  103.         }
  104.  
  105.         salida << "\n";
  106.      
  107.     }
  108.      
  109.     return salida;
  110.  
  111. }
  112.  
  113. Matriz operator *(Matriz x, Matriz y){//sobrecarga al operador * para multiplicar matrices
  114.  
  115.     assert(x.cols == y.rows);
  116.  
  117.     Matriz tempo(x.rows, y.cols);
  118.  
  119.     int i,j,k;
  120.  
  121.     for(i=0; i < x.rows; i++){
  122.  
  123.         for(j=0; j < x.cols; j++){
  124.  
  125.             tempo.A[i][j] = 0;
  126.  
  127.             for(k=0; k < x.cols; k++){
  128.  
  129.             tempo.A[i][j]= tempo.A[i][j]+ x.A[i][k] * y.A[k][j];
  130.            
  131.             }
  132.        
  133.         }
  134.     }
  135.    
  136.     return tempo;
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement