operate arrays from two different txt files

1

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.

    
asked by somebodylikeyou 16.04.2017 в 23:02
source

1 answer

1

First of all, if you want to read two different matrices, the ideal is that they are different objects. To do this, the entry operator >> will be overloaded in the following way:

istream& operator >> (istream &ent, matriz &m)
{
    ent >> m.nFilas;
    ent >> m.nColumnas;
    m.elementos = new float*[m.nFilas];

    for (uint i = 0; i < m.nFilas; i++) {
        m.elementos[i] = new float[m.nColumnas];
        for (uint j = 0; j < m.nColumnas; j++)
            ent >> m.elementos[i][j];
    }

    return ent ;
}

Previously declaring your header as friend in matriz.h :

friend istream& operator >> (istream &i, matriz &m) ;

The rest of the pre-reading checks is more convenient to carry out in the main (file opening and others). As you will see below, everything is much more aesthetic now

ifstream fcin("matriz1.txt", ios::in);
if (!fcin) {
    cerr << "\nError: El archivo 1 no se pudo abrir\n";
    exit(EXIT_FAILURE);
}
matriz A ;
fcin >> A ;

Once you read the matrices, you start to operate with them. In your case, from what I see, you have already implemented the sum, by means of an overload.

Res = A + B ;

Finally, it is necessary to save the contents of the result matrix in a different file. For this, the output operator << is overloaded:

ostream &operator << (ostream &out,const matriz &m)
{
    out << m.nFilas;
    out << "\n";
    out << m.nColumnas;
    out << "\n";
    for (uint i = 0; i < m.nFilas; i++) {
        for (uint j = 0; j < m.nColumnas; j++)
            out << setw(6) << m.elementos[i][j];
        out << "\n";
    }
    return out ;
}

And, similarly to the previous one, the file is opened in the main and the results are written.

ofstream fcout("resultado.txt", ios::out);
if (!fcout) {
    cerr << "\nError: El archivo 3 no se pudo abrir\n";
    exit(EXIT_FAILURE);
}

matriz A, B, Res;
fcin >> A ;
fcin2 >> B ;
Res = A + B ;
fcout << Res ;

I hope that with this you already have idea of how to do the operations (with overload) with matrices obtained from two different text files. I leave you, in addition, the full code here matrix

Greetings.

    
answered by 23.04.2017 / 02:00
source