Я хочу умножить 2 матрицы и умножить эскалар и матрицу.Тем не менее, перед этим я хочу показать матрицу для умножения, а затем умножения, но это не работает нормально.Я не знаю в чем проблема.Это код.Заранее спасибо.Здесь представлены различные конструкторы, деструкторы, операторы и функции.
Matriu::Matriu(){
m_fila = 0;
m_columna = 0;
m_valor = 0;
matriu = new float*[m_fila];
for(int i = 0; i < m_fila; i++){
matriu[i] = new float[m_columna];
}
}
Matriu::Matriu(int nFiles, int nColumnes){
m_fila = nFiles;
m_columna = nColumnes;
matriu = new float*[m_fila];
for(int i = 0; i < m_fila; i++){
matriu[i] = new float[m_columna];
}
}
Matriu::Matriu(const Matriu& m){
m_fila = m.m_fila;
m_columna = m.m_fila;
matriu = new float*[m_fila];
for(int i = 0; i < m_fila; i++){
matriu[i] = new float[m_columna];
}
for(int i = 0; i < m_fila;i++){
for(int j = 0; j < m_columna; j++){
matriu[i][j] = m.matriu[i][j];
}
}
}
Matriu::~Matriu(){
for(int i = 0; i < m_fila; i++){
delete[] matriu[i];
}
delete[] matriu;
}
void Matriu::setValor(float valor){
for(int i = 0; i < m_fila; i++){
for(int j = 0; j < m_columna; j++){
matriu[i][j] = valor;
}
}
}
Matriu& Matriu::operator=(const Matriu& m){
for(int i = 0; i < m_columna; i++){
delete[] matriu[i];
}
delete[] matriu;
m_fila = m.m_fila;
m_columna = m.m_columna;
matriu = new float*[m_fila];
for(int i = 0; i < m_fila; i++){
matriu[i] = new float[m_columna];
}
for(int i = 0; i < m_fila; i++){
for(int j = 0; j < m_columna;j++){
matriu[i][j] = m.matriu[i][j];
}
}
return *this;
}
void Matriu::init(int nFiles, int nColumnes){
if(!esBuida()){
for(int i = 0; i < m_columna; i++){
delete[] matriu[i];
}
}
delete[] matriu;
m_fila = nFiles;
m_columna = nColumnes;
matriu = new float*[m_fila];
for(int i = 0; i < m_fila; i++){
matriu[i] = new float[m_columna];
}
}
Matriu Matriu::operator*(const Matriu& m){
if(m_columna != m.m_fila){
for(int i = 0; i < m_fila; i++){
for(int j = 0; j < m_columna; j++){
matriu[i][j] = 0;
}
}
}
Matriu aux(m);
for (int i=0; i< m.m_fila; i++)
for (int j=0; j< m.m_columna; j++)
{
aux.matriu[i][j] = 0;
for(int x = 0; x < m.m_fila; x++){
aux.matriu[i][j] = aux.matriu[i][j] + matriu[i][x]*matriu[x][j];
}
}
return aux;
}
Matriu Matriu::operator*(float s){
for(int i = 0; i < m_fila; i++){
for(int j = 0; j < m_columna; j++){
matriu[i][j] = s * matriu[i][j];
}
}
return *this;
}
bool Matriu::esBuida() const{
if(matriu == NULL){
return true;
}else{
return false;
}
}
float& Matriu::operator()(int fila, int columna){
assert(fila >= 0 && fila < m_fila);
assert(columna >= 0 && columna < m_columna);
return matriu[fila][columna];
}
float& Matriu::operator()(int fila,int columna)const{
if(fila >= 0 && fila < m_fila){
if(columna >= 0 && columna < m_columna){
return matriu[fila][columna];
}
}
}
И это мой класс.
class Matriu
{
public:
Matriu();
Matriu(int nFiles, int nColumnes);
Matriu(const Matriu& m);
~Matriu();
Matriu& operator=(const Matriu& m);
void init(int nFiles, int nColumnes);
void setValor(float valor);
void setNFiles(int fila){m_fila = fila;}
void setNColumnes(int columna){m_columna = columna;}
Matriu operator*(const Matriu& m);
Matriu operator*(float s);
float getValor(){return m_valor;}
bool esBuida() const;
int getNFiles() const{return m_fila;}
int getNColumnes() const{return m_columna;}
float& operator()(int fila, int columna);
float& operator()(int fila,int columna)const;
private:
float** matriu;
int m_fila;
int m_columna;
int m_valor;
};
Для отображения матрицы перед умножением у меня есть 2 функции в основном (это правильно, я знаю, что это не проблема).Заранее спасибо.