cpp
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
using namespace std;
int FactorialRec( int N ) { return N == 0 ? 1 : FactorialRec( N-1 ) * N; }
int FactorialIte( int N )
{
int fact = 1;
while ( N > 0 ){
fact *= N;
N--;
}
return fact;
}
int FibonacciRec( int N ) { return N==0 || N==1 ? N : FibonacciRec( N - 1 ) + FibonacciRec( N - 2 ); }
int FibonacciIte( int N )
{
if ( ( N == 0) || ( N == 1) ) return N;
int fibo, a = 0, b = 1, i = 2;
while ( i <= N ) {
fibo = b + a;
a = b;
b = fibo;
i++;
}
return fibo;
}
void HanoiRec( int N, char Orig, char Dest, char Aux )
{
if ( N == 1 ) cout << "Mover un disco de " << Orig << " a "<< Dest<< endl;
Hanoi( N - 1, Orig, Aux, Dest);
cout << "Mover un disco de " << Orig << " a "<< Dest<< endl;
Hanoi( N - 1, Aux, Dest, Orig);
}
void HanoiIte( int N, char Orig, char Dest, char Aux ){
}
int EuclidesRec( int M, int N ){ return N == 0 ? M : EuclidesRec( N, M % N); }
int EuclidesIte( int M, int N ){
while ( N != 0 ){
int x = N;
N = M % N;
M = x;
}
return M;
}
void Ackermann( int M, int N ){
if ( M == 0 ){ return N + 1;}
else {
if ( N == 0 ) return Ackermann( M - 1, 1 );
return Ackermann( M - 1, Ackermann( M, N - 1));
}
}
void MenuPrincipal()
{
int Opc, Num;
char Frase[ 20 ];
char Orig, Dest, Aux;
int N, M;
do {
system("cls");
cout << "Ejemplos utilizando algoritmos Recursivos" << endl;
cout << "1. Factorial Recursivo." << endl;
cout << "2. Factorial Iterativo." << endl;
cout << "3. Fibonacci Recursivo." << endl;
cout << "4. Fibonacci Iterativo." << endl;
cout << "5. Euclides Recursivo." << endl;
cout << "6. Euclides Iterativo." << endl;
cout << "7. Invierte Cadena." << endl;
cout << "8. Palindromo." << endl;
cout << "9. Ackermann." << endl;
cout << "10. Torres de Hanoi Recursivo." << endl;
cout << "11. Torres de Hanoi Iterativo." << endl;
cout << "0. Salir." << endl;
cout << "Opcion: "; cin >> Opc;
switch (Opc) {
case 1: // Numero factorial
system("cls");
cout << "Dame un numero: ";
cin >> Num;
cout << "El factorial de " << Num << " es = " << FactorialRec(Num) << endl;
system("Pause");
break;
case 2: // Serie fibonacci
system("cls");
cout << "Dame un numero: ";
cin >> Num;
cout << "La serie Fibonacci de " << Num << " es = " << FibonacciRec(Num) << endl;
system("Pause");
break;
case 3: // Algoritmo de Euclides o MCD
case 4: // Invierte Cadena
system("cls");
cout << "Escriba una frase: ";
cin >> Frase;
//cout << Invierte( Frase, 0, strlen( Frase )-1 );
system("PAUSE");
break;
case 5: // Palindromo
case 6: // Algoritmo de Ackermann
case 7: // Torres de Hanoi
Orig= 'A';
Aux= 'B';
Dest= 'C';
cout << "Numero de discos: ";
cin >> N;
cout << "\n Los movimientos a realizar son: \n";
Hanoi( N, Orig, Dest, Aux);
system("Pause");
break;
};
} while (Opc!=0);
}
int main (){
MenuPrincipal();
getchar();
return 0;
}
Top comments (0)