-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sparse.cpp
71 lines (47 loc) · 1.81 KB
/
Sparse.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "Sparse.h"
#include "Definiciones.h"
#include "Elemento.h"
#include <iostream>
using namespace std;
Sparse::Sparse(TipoEntero _noZeros, TipoEntero _numFilas, TipoEntero _numColumnas) {
noZeros = _noZeros;
numFilas = _numFilas;
numColumnas = _numColumnas;
elemento = new Elemento[noZeros];
}
Sparse::~Sparse() {
delete [] elemento;
}
istream &operator>>(istream &is, Sparse &matriz) {
//cout << "Ingrese el número de elementos diferentes de 0: ";
cout << "Ingrese fila/columna/valor y presiones ENTER: \n";
for(TipoEntero i=0;i<matriz.noZeros;i++){
cin>>matriz.elemento[i].fila;
cin>>matriz.elemento[i].columna;
cin>>matriz.elemento[i].valor;
}
return is;
}
ostream &operator<<(ostream &os, Sparse &matriz) {
TipoEntero nz =0; //Cuenta los elementos NoZero impresos
for(TipoEntero i=0;i<matriz.numFilas;i++){
for(TipoEntero j=0;j<matriz.numColumnas;j++){
if(matriz.elemento[nz].fila==i && matriz.elemento[nz].columna==j){
cout << matriz.elemento[nz++].valor << " ";
}else{
cout << "0 ";
}
}
cout << endl;
}
return os;
}
Sparse Sparse::operator+(Sparse &matrizASumar) {
TipoEntero a =0; //Contador elementos diferentes de 0 de la matriz 1
TipoEntero b =0; //Contador elementos diferentes de 0 de la matriz 2
TipoEntero c = 0; //Contador elementos diferentes de 0 de la matriz resultado
if(numFilas != matrizASumar.numFilas || numColumnas != matrizASumar.numColumnas) //Se comprueba que se pueden sumar ambas matrices
return matrizASumar;
Sparse* matrizResultadoSuma = new Sparse(noZeros+matrizASumar.noZeros,numFilas,numColumnas); // Sumamos la cantidad de noZeros de ambas matrices
return *matrizResultadoSuma;
}