Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- #include <cstdlib>
- #include <iomanip>
- #include <cassert>
- class Matriz {
- private:
- double A[10][10];
- int rows, cols;
- public:
- Matriz(int n, int m); // Constructor de la clase
- ~Matriz(); // Destructor de la clase
- void initMatriz(double **x);
- void showMatriz();
- friend ostream& operator << (ostream &salida, Matriz &z); //sobrecarga del operador <<
- friend Matriz operator *(Matriz x, Matriz y); //sobrecarga al operador * para multiplicar matrices
- };
- Matriz::Matriz(int n, int m) {// Para que imprima ceros para todos los elementos de cualquier matriz no inicializada
- rows = n;
- cols = m;
- int i, j;
- for(i= 0; i < rows; i++){
- for(j= 0; j < cols; j++){
- A[i][j] = 0;
- }
- }
- }
- Matriz::~Matriz(){}
- void Matriz::initMatriz(double **x){
- int i,j;
- for(i=0; i < rows; i++){
- for(j=0; j < cols; j++){
- A[i][j] = x[i][j];
- }
- }
- }
- void Matriz::showMatriz(){
- cout.setf(ios::fixed);
- cout.precision(5);
- int i,j;
- for(i=0; i < rows; i++){
- for(j=0; j < cols; j++){
- cout << setw(10) << A[i][j];
- }
- cout << endl;
- }
- }
- ostream &operator << (ostream &salida, Matriz &z){//sobrecarga del operador <<
- salida.setf(ios::fixed);
- salida.precision(5);
- int i, j;
- for (i = 0; i < z.rows; i++) {
- for (j = 0; j < z.cols; j++) {
- salida << setw(10) << z.A[i][j];
- }
- salida << "\n";
- }
- return salida;
- }
- Matriz operator *(Matriz x, Matriz y){//sobrecarga al operador * para multiplicar matrices
- assert(x.cols == y.rows);
- Matriz tempo(x.rows, y.cols);
- int i,j,k;
- for(i=0; i < x.rows; i++){
- for(j=0; j < x.cols; j++){
- tempo.A[i][j] = 0;
- for(k=0; k < x.cols; k++){
- tempo.A[i][j]= tempo.A[i][j]+ x.A[i][k] * y.A[k][j];
- }
- }
- }
- return tempo;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement