Post on 02-Dec-2015
description
ALGORITMOS Y ESTRUCTURAS DE DATOS
LABORATORIO N° 02
FORMULARIOS Y MÉTODOS RECURSIVOS
OPERACIONES A IMPLEMENTAR
a) Creación del formulario principal.b) Cálculo del Factorial de un número.c) Cálculo del número de Fibonacci de un número.
DETALLE DE LAS OPERACIONES
FACTORIAL DE UN NÚMERO
El Factorial de un número entero positivo, es otro número entero que se obtiene al multiplicar en forma sucesiva desde 1 hasta el mismo número; así:
Factorial(5) = 1*2*3*4*5 = 120
FIBONACCI DE UN NÚMERO
El Fibonacci de un número entero positivo, es otro número que se obtiene al ejecutar en forma sucesiva la suma de los elementos inmediatamente precedentes, considerando que el Fibonacci de los dos primeros números son 0 y 1. Veamos un ejemplo:
Fibonacci(1) = 0; Fibonacci(2) =1; Fibonacci(3) = 0+1=1; Fibonacci(4) = 1+1=2; Fibonacci(5) = 2+1; . . . .
En general; diremos: Si n=1; Fibonacci=0
Si n=2; Fibonacci=1
Si n>2; Fibonacci=Fibonacci(n-1) + Fibonacci(n-2)
DISEÑO DEL FORMULARIO
MG. LUIS BOY CHAVIL Página 1
ALGORITMOS Y ESTRUCTURAS DE DATOS
PROGRAMACIÓN DE LA CLASE OPERACIONES
#pragma once
ref class OPERACIONES{
public:int Numero;
public:long Factorial(int);long Fibonacci(int);
OPERACIONES(void){}
};
long OPERACIONES::Factorial(int n){
if(n==1)return(1);
elsereturn(n * Factorial(n-1));
}
long OPERACIONES::Fibonacci(int n){
if(n==1)return(0);
else if(n==2)return(1);
elsereturn(Fibonacci(n-1)+Fibonacci(n-2));
}
PROGRAMACIÓN DE LAS OPERACIONES DEL MENÚ EN EL FORMULARIO
Agregue al inicio del programa:
#include "OPERACIONES.h"
private: System::Void RbFactorial_CheckedChanged(System::Object^ sender, System::EventArgs^ e)
{OPERACIONES OP;OP.Numero=Convert::ToInt16(TxtNumero->Text);if(OP.Numero>0)
TxtResultado->Text=Convert::ToString(OP.Factorial(OP.Numero));else
TxtResultado->Text="Ingrese Número +"; }
MG. LUIS BOY CHAVIL Página 2
ALGORITMOS Y ESTRUCTURAS DE DATOS
private: System::Void RbFibonacci_CheckedChanged(System::Object^ sender, System::EventArgs^ e)
{OPERACIONES OP;OP.Numero=Convert::ToInt16(TxtNumero->Text);if(OP.Numero>0)
TxtResultado->Text=Convert::ToString(OP.Fibonacci(OP.Numero));else
TxtResultado->Text="Ingrese Número +"; }
MG. LUIS BOY CHAVIL Página 3