Last active
April 6, 2017 04:22
-
-
Save David256/936856bb0fd128fb0bea60fae235d4ee to your computer and use it in GitHub Desktop.
Generador aleatorio de números que serán evaluados usando funciones recursivas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Recurso usado: | |
* - http://blog.martincruz.me/2012/09/obtener-numeros-aleatorios-en-c-rand.html | |
* - http://robologs.net/2016/03/31/como-colorear-el-output-de-la-terminal-en-linux/ | |
* - http://stackoverflow.com/questions/2616906/how-do-i-output-coloured-text-to-a-linux-terminal | |
*/ | |
#include <iostream> | |
#include <time.h> | |
#include <string.h> | |
using namespace std; | |
void procesar_impar(int numero); | |
void procesar_par(int numero); | |
int inicial_random(void) | |
{ | |
cout << " " << "Generando un número inicial..." << endl; | |
int valor; | |
valor = 10 + rand() % (20+1 -10); | |
cout << "\033[94m" << "[ RUN ] " << "\033[0m" << valor << " es generado." << endl; | |
return valor; | |
} | |
void procesar_impar(int numero) | |
{ | |
if (numero <= 0) | |
{ | |
cout << "\033[35m" << "[FINAL] " << "\033[0m" << numero << " es el último." << endl; | |
return; | |
} | |
int resta = rand() % 3; | |
if (numero % 2 != 0) | |
{ | |
cout << "\033[32m" <<"[IMPAR] " << "\033[0m" << numero << " es un impar." << endl; | |
} | |
int nuevo = numero - resta; | |
if (nuevo % 2 == 0) | |
{ | |
procesar_par(nuevo); | |
} | |
else | |
{ | |
procesar_impar(nuevo); | |
} | |
} | |
void procesar_par(int numero) | |
{ | |
if (numero <= 0) | |
{ | |
cout << "\033[35m" << "[FINAL] " << "\033[0m" << " es el último." << endl; | |
return; | |
} | |
int resta = rand() % 3; | |
if (numero % 2 == 0) | |
{ | |
cout << "\033[92m" <<"[ PAR ] " << "\033[0m" << numero << " es un par." << endl; | |
} | |
int nuevo = numero - resta; | |
if (nuevo % 2 == 0) | |
{ | |
procesar_par(nuevo); | |
} | |
else | |
{ | |
procesar_impar(nuevo); | |
} | |
} | |
int main(int argc, char *argv[]) | |
{ | |
srand(time(NULL)); | |
int eleccion = inicial_random(); | |
if (eleccion % 2 == 0) | |
procesar_par(eleccion); | |
else | |
procesar_impar(eleccion); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment