Version:0.9 StartHTML:0000000105 EndHTML:0000011659 StartFragment:0000000152 EndFragment:0000011625
/******************************************************/
/*************** Isidro Pastor Jorda ******************/
/******************************************************/
/****************** Telematica ************************/
/******************************************************/
/************* Practica 8 Ejercicio 2 *****************/
/******************************************************/


#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>

const int MAX = 10;
typedef int Matriz[MAX][MAX];

void MostrarMatriz(Matriz M, int filas, int columnas);
void InicializaMatriz(Matriz M, int reset);

int main()
{
    int i,j;
    int filas,columnas;


    int opcion;
    Matriz m;
    InicializaMatriz(m, 0);

    ifstream fentrada;
    ofstream fsalida;

    cout << "     Practica 8 Ejercicio 1 \n";
    cout << "     Lectura de una matriz de un fichero y grabar nuevo fichero \n";
    cout << "     Elige lo que que quieres hacer: \n";
    cout << " 1.- Leer matriz de un fichero y mostrarla\n";
    cout << " 2.- Introducir la matriz y grabarla en el fichero\n";
    cout << "     Opcion elegida -> ";
    cout << endl;
    cin >> opcion;

    switch (opcion)
    {
        case 1:
            fentrada.open("matriz1.bin");
            if (!fentrada)
                cout << "\n Error abriendo el fichero matriz1.dat";
            else
            {
                fentrada.read( (char *)(& filas), sizeof(filas) ); // leemos las filas
                fentrada.read( (char *)(& columnas), sizeof(columnas) ); // leemos las columnas

                for (i = 0; i < filas; i++)
                    for (j = 0; j < columnas; j++)
                        fentrada.read( (char *)(& m[i][j]), sizeof(m[i][j]));

     //           fentrada.read( (char *)(& m[0][0]), sizeof(m[0][0])*(filas*columnas)); // leemos toda la matriz de golpe

                fentrada.close();
                MostrarMatriz(m, filas, columnas);
          

            } // else
            break;
        case 2:

            do
            {
                cout << "\n Introduce el numero de filas (Maximo 10)-> ";
                cin >> filas;
            }
            while( filas < 0 || filas > 10);


            do
            {
                cout << "\n Introduce el numero de columnas (Maximo 10)-> ";
                cin >> columnas;
            }
            while( columnas < 0 || columnas > 10);


            for (i = 0; i < filas; i++)
            {
                for (j = 0; j < columnas; j++)
                {
                    cout << "\n Introduce la posicion " << i << " " << j << " -> ";
                    cin >> m[i][j];
                    system("CLS");
                    MostrarMatriz(m, filas, columnas);
                }
            }
            fsalida.open("matriz1.bin");
            if (!fsalida)
                cout << " Error abriendo el fichero matriz1.dat";
            else
            {
                fsalida.write( (char *)(& filas), sizeof(filas) );
                fsalida.write( (char *)(& columnas), sizeof(columnas) );

                for (i = 0; i < filas; i++)
                    for (j = 0; j < columnas; j++)
                        fsalida.write( (char *)(& m[i][j]), sizeof(m[i][j]));


                fsalida.close();
            } // else
            break;


    }// switch
    



    system("PAUSE");
    return 0;
}


/******************************************************************
* Funcion: MostrarMatriz
* Descripcion: Funcion para mostrar por pantalla la matriz tabulada
*
*
* Parametros:
*
* Nombre      E/S       Descripcion
* ------     -----      -----------
*   M         E          Matriz a mostrar por pantalla
*
*
* Valor devuelto:
*       void, la funcion solo muestra por pantalla la matriz
*******************************************************************/

void MostrarMatriz(Matriz M, int filas, int columnas)
{
    cout << endl << endl;
    for (int i = 0; i < filas; i++ )
    {    for (int j = 0; j< columnas; j++)
         {
             if (M[i][j] < 10) // Si el numero es menos que 10 introducimos 3 espacios para que quede bien la salida por pantalla
                cout << "   " << M[i][j];  
             else
                 if (M[i][j] < 100) // Si es mas pequeño que 100 introduciremos el numero con solo 2 espacios de separacion
                     cout << "  " << M[i][j];
                 else
                     cout << " " << M[i][j]; // Y si fuera mayor que 100 entonces un espacio simple para separar las columnas
         }
        cout << endl;
    }
    cout << endl;
}


/******************************************************************
* Funcion: InicializaMatriz
* Descripcion: Funcion que inicializa una matriz al valor de reset
*
*
* Parametros:
*
* Nombre      E/S       Descripcion
* ------     -----      -----------
*   M         E/S        Matriz a inicializar
* reset       E          Valor usado para inicializar la matriz
*
* Valor devuelto:
*       void, la matriz no devuelve nada pero modifica el vector
*******************************************************************/

void InicializaMatriz(Matriz M, int reset)
{

    for (int i = 0; i < MAX; i++ )
        for (int j = 0; j< MAX; j++)
            M[i][j] = reset;

    return;
}