Hello! Good day :) I want to make a program (in Visual C ++) that makes several operations (with overloaded operators) with two arrays read from two different txt files. So far I have done this.
typedef unsigned int uint;
class matriz {
private:
uint nFilas, nColumnas;
float **elementos;
public:
matriz(const char*);
void imprimir(const char*);
};
#include "matriz.h"
#include <iostream>
#include <fstream>
#include<iomanip>
matriz::matriz(const char*archivo) {
ifstream fcin(archivo, ios::in);
if (!fcin) {
cerr << "\nError: El archivo no se pudo abrir\n";
exit(EXIT_FAILURE);
}
fcin >> nFilas;
fcin >> nColumnas;
elementos = new float*[nFilas];
for (uint i = 0; i < nFilas; i++) {
elementos[i] = new float[nColumnas];
for (uint j = 0; j < nColumnas; j++)
fcin >> elementos[i][j];
}
fcin.close();
}
void matriz::imprimir(const char*archivo) {
ofstream fcout(archivo, ios::out);
if (!fcout) {
cerr << "\nError: El archivo no se pudo abrir\n";
exit(EXIT_FAILURE);
}
fcout << nFilas;
fcout << "\n";
fcout << nColumnas;
fcout << "\n";
for (uint i = 0; i < nFilas; i++) {
for (uint j = 0; j < nColumnas; j++)
fcout << setw(6) << elementos[i][j];
fcout << "\n";
}
fcout.close();
};
#include "matriz.h"
#include <cstdlib>
int main()
{
matriz A("matriz1.txt");
A.imprimir("matriz2.txt");
system("PAUSE");
return 0;
}
I started doing the program with randomly generated matrices, and I know how to perform the operations (addition, subtraction, multiplication) with the overloaded operators; and also the inverse operations and multiplication by a scalar. However, when working with arrays read from two different files, I compliment myself: \ and I have several doubts, starting with, what is the prototype for operator overload for matrices from txt files?
When doing the sum for randomly generated matrices, I had done this:
matriz* operator+ (const matriz&matriz2){
matriz*suma=new matriz(nFilas, nColumnas);
for (uint i=0; i<nFilas; i++){
for (uint j=0; j<nColumnas; j++){
suma->elementos[i][j]=elementos[i][j]+matriz2.elementos[i][j];
}
}
return suma;
}
And similarly he had performed the operations mentioned above and they worked well. But, I have no idea how to do the operations (with overload) with matrices obtained from two different text files. Can you guide me, please? Help me please, guide me :( Thank you very much! I hope my question has been clearly stated.