En este programa se incluyen todos los metodos de la clase matematicas usando recursividad pero ademas incluye otro metodo, que es la serie de fabinucci. que consiste en una sumatoria de numeros e incluye calcular el numero de combinaciones tomando n datos.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Recursividad
{
class Matematicas
{
//factorial normal
public int factorialNormal(int num)
{
int fact = 1;
if (num == 0 || num == 1)
return fact;
for (int i = 1; i <= num; i++)
{
fact = fact * i;
}
return fact;
}
//factorial recursivo
public int factorial_Recursivo(int num)
{
int f = 1;
if (num == 0 || num == 1)
return f;
else
f = num * factorial_Recursivo(num - 1);
return f;
}
//invertir un numero entero de x digitos
public string invertir_R(int num)
{
if (num < 10)
return num.ToString();
else
return (num % 10).ToString() + invertir_R(num / 10);
}
//Division mediante resta recursiva
public int Division_R(int a, int b)
{
//int r=0;
if (b > a)
return 0;//return r;
else
return Division_R(a - b, b) + 1;//r = Division(a - b, b) + 1;
}
//sumatoria
public int Sumatoria(int n)
{
if (n == 0 || n == 1)
return n;
else
return n % 10 + Sumatoria(n / 10);
}
//fabinucci iterativo
public int fibonacci_iter(int n)
{
int suma = 0, fib = 1, fib2 = 0;
for (int i = n - 1; i > 0; i--)
{
suma = fib + fib2;
fib2 = fib;
fib = suma;
}
return suma;
}
public int Fabinucci_Rec(int num)
{
if (num == 1 | num == 2)
return 1;
else
return Fabinucci_Rec(num - 1) + Fabinucci_Rec(num - 2);
}
//NUMERO DE COMBINACIONES
public int Combinaciones(int n, int r)
{
int res = 0;
if (n == 1 || r == 0 || n == r)
{
return 1;
}
else
{
//return Combinaciones(n-1,r)+combinaciones(n-1,r-1);
res = Combinaciones(n - 1, r) + Combinaciones(n - 1, r - 1);
}
return res;
}
}
}
============================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Recursividad
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
groupBox2.Visible = false;
groupBox3.Visible = false;
butLimpiar.Visible = false;
pictureBox1.Visible = false;
//factorial
radioFactIter.Visible = false;
radioFactRec.Visible = false;
textNumFact.Visible = false;
butFactorial.Visible = false;
textResultFact.Visible = false;
/*iterativo*/
texNumFactNorm.Visible = false;
textResulFacNor.Visible = false;
butFactorialNormal.Visible = false;
//INVERTIR
textResultInvertir.Visible = false;
textNumInvertir.Visible = false;
butInvertir.Visible = false;
//DIVISION
butDivision.Visible = false;
textDiviA.Visible = false;
textDiviB.Visible = false;
textResultDivi.Visible = false;
//SUMATORIA
textNumSuma.Visible = false;
textResltSuma.Visible = false;
butSumatoria.Visible = false;
//FABINUCCI
textNumFabi.Visible = false;
textResultFabi.Visible = false;
butFabinucci.Visible = false;
//COMBINACIONES//
textNumN.Visible = false;
textNumR.Visible = false;
textResultCombi.Visible = false;
butCombin.Visible = false;
label1.Visible = false;
label2.Visible = false;
}
Matematicas mat = new Matematicas();
//CALCULAR EL FACTORIAL RECURSIVO
private void butFactorial_Click(object sender, EventArgs e)
{
try
{
int num = Convert.ToInt32(textNumFact.Text);
textResultFact.Text = Convert.ToString(mat.factorial_Recursivo(num));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//CALCULAR FACTORIAL RECURSIVO
private void butFactorialNormal_Click(object sender, EventArgs e)
{
try
{
int num = Convert.ToInt32(texNumFactNorm.Text);
textResulFacNor.Text = Convert.ToString(mat.factorialNormal(num));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//FACTORIAL ITERATIVO
private void radioFactIter_CheckedChanged(object sender, EventArgs e)
{
texNumFactNorm.Visible = true;
textResulFacNor.Visible = true;
butFactorialNormal.Visible = true;
//para deshabilitar la otra opcion y borrar
textNumFact.Visible = false;
butFactorial.Visible = false;
textResultFact.Visible = false;
textNumFact.Text = "";
textResultFact.Text = "";
butLimpiar.Visible = true;
}
//FACTORIAL RECURSIVO
private void radioFactRec_CheckedChanged(object sender, EventArgs e)
{
textNumFact.Visible = true;
butFactorial.Visible = true;
textResultFact.Visible = true;
texNumFactNorm.Visible = false;
textResulFacNor.Visible = false;
butFactorialNormal.Visible = false;
texNumFactNorm.Text = "";
textResulFacNor.Text = "";
butLimpiar.Visible = true;
}
//CALCULAR LA DIVISION
private void butDivision_Click(object sender, EventArgs e)
{
try
{
int A = Convert.ToInt32(textDiviA.Text);
int B = Convert.ToInt32(textDiviB.Text);
textResultDivi.Text = Convert.ToString(mat.Division_R(A, B));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//CALCULAR SUMATORIA DE LOS DIGITOS DE UN ENTERO
private void butSumatoria_Click(object sender, EventArgs e)
{
try
{
int numSuma = Convert.ToInt32(textNumSuma.Text);
textResltSuma.Text = Convert.ToString(mat.Sumatoria(numSuma));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//BOTON INVERTIR
private void butInvertir_Click(object sender, EventArgs e)
{
try
{
int num = Convert.ToInt32(textNumInvertir.Text);
Matematicas inv = new Matematicas();
//inv.invertir_R(num);
textResultInvertir.Text = inv.invertir_R(num).ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//BOTON FABINUCCI
private void butFabinucci_Click(object sender, EventArgs e)
{
int fabi = Convert.ToInt32(textNumFabi.Text);
textResultFabi.Text = Convert.ToString(mat.fibonacci_iter(fabi));
}
//BOTON COMBINACIONES
private void butCombin_Click(object sender, EventArgs e)
{
int N = Convert.ToInt32(textNumN.Text);
int R = Convert.ToInt32(textNumR.Text);
textResultCombi.Text = Convert.ToString(mat.Combinaciones(N, R));
}
//BOTON OK
private void butOK_Click(object sender, EventArgs e)
{
try
{
if (radioFactorial.Checked)
{
radioFactIter.Visible = true;
radioFactRec.Visible = true;
groupBox3.Visible = true;
}
else
if (radioInvertir.Checked)
{
textResultInvertir.Visible = true;
textNumInvertir.Visible = true;
butInvertir.Visible = true;
groupBox2.Visible = true;
butLimpiar.Visible = true;
}
else
if (radioDivision.Checked)
{
butDivision.Visible = true;
textDiviA.Visible = true;
textDiviB.Visible = true;
textResultDivi.Visible = true;
groupBox2.Visible = true;
butLimpiar.Visible = true;
pictureBox1.Visible = true;
}
else
if (radioSuatoria.Checked)
{
textNumSuma.Visible = true;
textResltSuma.Visible = true;
butSumatoria.Visible = true;
groupBox2.Visible = true;
butLimpiar.Visible = true;
}
else
if (radioFabinucci.Checked)
{
textNumFabi.Visible = true;
textResultFabi.Visible = true;
butFabinucci.Visible = true;
groupBox2.Visible = true;
butLimpiar.Visible = true;
}
else
if (radioCombinaciones.Checked)
{
groupBox2.Visible = true;
textNumN.Visible = true;
textNumR.Visible = true;
textResultCombi.Visible = true;
butCombin.Visible = true;
label1.Visible = true;
label2.Visible = true;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void radioInvertir_CheckedChanged(object sender, EventArgs e)
{
//factorial
radioFactIter.Visible = false;
radioFactRec.Visible = false;
textNumFact.Visible = false;
butFactorial.Visible = false;
textResultFact.Visible = false;
/*iterativo*/
texNumFactNorm.Visible = false;
textResulFacNor.Visible = false;
butFactorialNormal.Visible = false;
//INVERTIR
textResultInvertir.Visible = false;
textNumInvertir.Visible = false;
butInvertir.Visible = false;
//DIVISION
butDivision.Visible = false;
textDiviA.Visible = false;
textDiviB.Visible = false;
textResultDivi.Visible = false;
//SUMATORIA
textNumSuma.Visible = false;
textResltSuma.Visible = false;
butSumatoria.Visible = false;
//FABINUCCI
textNumFabi.Visible = false;
textResultFabi.Visible = false;
butFabinucci.Visible = false;
//COMBINACIONES//
textNumN.Visible = false;
textNumR.Visible = false;
textResultCombi.Visible = false;
butCombin.Visible = false;
label1.Visible = false;
label2.Visible = false;
groupBox3.Visible = false;
groupBox2.Visible = false;
butLimpiar.Visible = false;
pictureBox1.Visible = false;
}
private void radioFactorial_CheckedChanged(object sender, EventArgs e)
{
//factorial
radioFactIter.Visible = false;
radioFactRec.Visible = false;
textNumFact.Visible = false;
butFactorial.Visible = false;
textResultFact.Visible = false;
/*iterativo*/
texNumFactNorm.Visible = false;
textResulFacNor.Visible = false;
butFactorialNormal.Visible = false;
//INVERTIR
textResultInvertir.Visible = false;
textNumInvertir.Visible = false;
butInvertir.Visible = false;
//DIVISION
butDivision.Visible = false;
textDiviA.Visible = false;
textDiviB.Visible = false;
textResultDivi.Visible = false;
//SUMATORIA
textNumSuma.Visible = false;
textResltSuma.Visible = false;
butSumatoria.Visible = false;
//FABINUCCI
textNumFabi.Visible = false;
textResultFabi.Visible = false;
butFabinucci.Visible = false;
//COMBINACIONES//
textNumN.Visible = false;
textNumR.Visible = false;
textResultCombi.Visible = false;
butCombin.Visible = false;
label1.Visible = false;
label2.Visible = false;
groupBox3.Visible = false;
groupBox2.Visible = false;
butLimpiar.Visible = false;
pictureBox1.Visible = false;
}
private void radioDivision_CheckedChanged(object sender, EventArgs e)
{
//factorial
radioFactIter.Visible = false;
radioFactRec.Visible = false;
textNumFact.Visible = false;
butFactorial.Visible = false;
textResultFact.Visible = false;
/*iterativo*/
texNumFactNorm.Visible = false;
textResulFacNor.Visible = false;
butFactorialNormal.Visible = false;
//INVERTIR
textResultInvertir.Visible = false;
textNumInvertir.Visible = false;
butInvertir.Visible = false;
//DIVISION
butDivision.Visible = false;
textDiviA.Visible = false;
textDiviB.Visible = false;
textResultDivi.Visible = false;
//SUMATORIA
textNumSuma.Visible = false;
textResltSuma.Visible = false;
butSumatoria.Visible = false;
//FABINUCCI
textNumFabi.Visible = false;
textResultFabi.Visible = false;
butFabinucci.Visible = false;
//COMBINACIONES//
textNumN.Visible = false;
textNumR.Visible = false;
textResultCombi.Visible = false;
butCombin.Visible = false;
label1.Visible = false;
label2.Visible = false;
groupBox3.Visible = false;
groupBox2.Visible = false;
butLimpiar.Visible = false;
pictureBox1.Visible = false;
}
private void radioSuatoria_CheckedChanged(object sender, EventArgs e)
{
//factorial
radioFactIter.Visible = false;
radioFactRec.Visible = false;
textNumFact.Visible = false;
butFactorial.Visible = false;
textResultFact.Visible = false;
/*iterativo*/
texNumFactNorm.Visible = false;
textResulFacNor.Visible = false;
butFactorialNormal.Visible = false;
//INVERTIR
textResultInvertir.Visible = false;
textNumInvertir.Visible = false;
butInvertir.Visible = false;
//DIVISION
butDivision.Visible = false;
textDiviA.Visible = false;
textDiviB.Visible = false;
textResultDivi.Visible = false;
//SUMATORIA
textNumSuma.Visible = false;
textResltSuma.Visible = false;
butSumatoria.Visible = false;
//FABINUCCI
textNumFabi.Visible = false;
textResultFabi.Visible = false;
butFabinucci.Visible = false;
//COMBINACIONES//
textNumN.Visible = false;
textNumR.Visible = false;
textResultCombi.Visible = false;
butCombin.Visible = false;
label1.Visible = false;
label2.Visible = false;
groupBox3.Visible = false;
groupBox2.Visible = false;
butLimpiar.Visible = false;
pictureBox1.Visible = false;
}
private void radioFabinucci_CheckedChanged(object sender, EventArgs e)
{
//factorial
radioFactIter.Visible = false;
radioFactRec.Visible = false;
textNumFact.Visible = false;
butFactorial.Visible = false;
textResultFact.Visible = false;
/*iterativo*/
texNumFactNorm.Visible = false;
textResulFacNor.Visible = false;
butFactorialNormal.Visible = false;
//INVERTIR
textResultInvertir.Visible = false;
textNumInvertir.Visible = false;
butInvertir.Visible = false;
//DIVISION
butDivision.Visible = false;
textDiviA.Visible = false;
textDiviB.Visible = false;
textResultDivi.Visible = false;
//SUMATORIA
textNumSuma.Visible = false;
textResltSuma.Visible = false;
butSumatoria.Visible = false;
//FABINUCCI
textNumFabi.Visible = false;
textResultFabi.Visible = false;
butFabinucci.Visible = false;
//COMBINACIONES//
textNumN.Visible = false;
textNumR.Visible = false;
textResultCombi.Visible = false;
butCombin.Visible = false;
label1.Visible = false;
label2.Visible = false;
groupBox3.Visible = false;
groupBox2.Visible = false;
butLimpiar.Visible = false;
pictureBox1.Visible = false;
}
private void radioCombinaciones_CheckedChanged(object sender, EventArgs e)
{
//factorial
radioFactIter.Visible = false;
radioFactRec.Visible = false;
textNumFact.Visible = false;
butFactorial.Visible = false;
textResultFact.Visible = false;
/*iterativo*/
texNumFactNorm.Visible = false;
textResulFacNor.Visible = false;
butFactorialNormal.Visible = false;
//INVERTIR
textResultInvertir.Visible = false;
textNumInvertir.Visible = false;
butInvertir.Visible = false;
//DIVISION
butDivision.Visible = false;
textDiviA.Visible = false;
textDiviB.Visible = false;
textResultDivi.Visible = false;
//SUMATORIA
textNumSuma.Visible = false;
textResltSuma.Visible = false;
butSumatoria.Visible = false;
//FABINUCCI
textNumFabi.Visible = false;
textResultFabi.Visible = false;
butFabinucci.Visible = false;
//COMBINACIONES//
textNumN.Visible = false;
textNumR.Visible = false;
textResultCombi.Visible = false;
butCombin.Visible = false;
label1.Visible = false;
label2.Visible = false;
groupBox3.Visible = false;
groupBox2.Visible = false;
butLimpiar.Visible = false;
pictureBox1.Visible = false;
}
private void butLimpiar_Click(object sender, EventArgs e)
{
textNumFact.Text = "";
textResultFact.Text = "";
texNumFactNorm.Text = "";
textResulFacNor.Text = "";
textResultInvertir.Text = "";
textNumInvertir.Text = "";
textDiviA.Text = "";
textDiviB.Text = "";
textResultDivi.Text = "";
textNumSuma.Text = "";
textResltSuma.Text = "";
textNumFabi.Text = "";
textResultFabi.Text = "";
//COMBINACIONES//
textNumN.Text = "";
textNumR.Text = "";
textResultCombi.Text = "";
}
private void butSalir_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
No hay comentarios:
Publicar un comentario