Modulate a program that contains a class?

0

The fact is that I have to make a program. This contains 2 simple functions and one class GeneradorAleatorioEnteros. The problem is that I have to divide the .cpp of the program in a .cpp for the main, and another for the functions and the class, to the rest of the corresponding .h.

If it were not because the class is used by one of the functions, it could be included in the same main ... How do I divide the program ??, I leave the code:

/// La función leerá el número usando
/// una cadena, comprobará que todos los caracteres son dígitos y la 
/// convertirá a un entero. 
/// Si  es correcto, lo devolverá; si no lo es, volverá a pedirlo.
/// 
/// Otra función se encarga de pedir un rango y da un número en dicho rango.
/// 
/// Este programa incluye una class "GeneradorAleatorioEnteros".
////////////////////////////////////////////////////////////////////////////////

#include <iostream>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <ctime>
#include <cstdlib>
#include <cctype>
#include <random>  
#include <chrono>  

using namespace std;

////////////////////////////////////////////////////////////////////////////////
class GeneradorAleatorioEnteros
{  
private:

   mt19937 generador_mersenne;    // Mersenne twister
   uniform_int_distribution<int>  distribucion_uniforme;

   /************************************************************************/
   long long Nanosec(){
      return (chrono::high_resolution_clock::now().
              time_since_epoch().count());
   }
   /************************************************************************/ 

public:

   /************************************************************************/   
   GeneradorAleatorioEnteros()
      :GeneradorAleatorioEnteros(0, 1){
   }

   /************************************************************************/  
   GeneradorAleatorioEnteros(int min, int max) {
      const int A_DESCARTAR = 70000;
      // ACM TOMS Volume 32 Issue 1, March 2006

      auto semilla = Nanosec();
      generador_mersenne.seed(semilla);
      generador_mersenne.discard(A_DESCARTAR);
      distribucion_uniforme = uniform_int_distribution<int> (min, max);
   }

   /************************************************************************/
   int Siguiente(){
      return (distribucion_uniforme(generador_mersenne));
   }
   /************************************************************************/
};
////////////////////////////////////////////////////////////////////////////////


//Funcion que acepta solamente una cadena clásica de dígitos.

int leeEntero(char * cadena){

    bool Esdigito = true;   
    char * p = cadena;


    //  Bucle en el que avanzo valores mientras sean dígitos.

    while ( *p && Esdigito) { 

        if (isdigit (*p)){  
            p++;
        }

        else{
            Esdigito = false;
        }
    }

    int i = atoi(cadena);

    if (Esdigito)
        return (i);

    else{
        return (0);
    }
}

//Funcion con un dato predeterminado que acepta 1 o 2 argumentos y genera un
//número aleatorio comprendido entre los límites del intervalo aportado.
int leeEntero(int limite_superior, int limite_inferior = 0){

    GeneradorAleatorioEnteros generador_aleatorio(limite_inferior, limite_superior);
    return generador_aleatorio.Siguiente();

}

int main (){

    const int TOPE = 100;
    char cadena[TOPE];
    int * p_enteros;
    int rango_inferior, rango_superior; 
    int entero_comprendido;

    cout << "\tInserte una cadena de digitos: "<< endl;
    cin.getline (cadena, TOPE);


//Pedimos una cadena mientras contenga elementos que no sean dígitos.
    while (leeEntero(cadena) == 0){

        cout << "\tNo ha insertado digitos." << endl;
        cin.getline (cadena, TOPE);

    }

        cout << "\t" << leeEntero(cadena) << endl;

    cout << "\tInserte un rango para recibir un numero ";
    cout << "en el mismo ('0' si no inserta minimo):  " << endl;

    cin >> rango_inferior;
    cin >> rango_superior;

    if (rango_inferior == 0)    
        entero_comprendido = leeEntero(rango_superior);

    else
        entero_comprendido = leeEntero(rango_superior, rango_inferior);

    cout << "\t" << entero_comprendido << endl;



}
    
asked by Master cabesa 16.03.2018 в 13:19
source

1 answer

0

You have to create a project, you are adding the files you need (3 I understand, the class you save as .h, the other two as .cpp, one for the main and another for the functions). In the header of the .h you define it like this:

And at the end you put #endif

Then in the .cpp that you are going to use the class you pass this .h in the following way:

And I would be, the only thing left for you to do is the main that if you are going to use the class of the .h you have to call it back:

As in the .cpp of the functions. This program is not the one you've been through but this is always the same. It is rare that you know how to do classes and not program in several xD files.

    
answered by 16.03.2018 в 14:07