Datos Personales

Nombre:Gabriel Alejandro Barrientos Medellin
Ciudad:Matamoros,Coahuila
Edad:20
Materia:Estructura de Datos

sábado, 26 de noviembre de 2011

matematicasRec

class Matematicas_recursivo

{

public int Factorial_r(int n)

{

int f = 1;

if (n == 0 || n == 1)

return f;

else

f = n * Factorial_r(n - 1);

return f;

}

public int sumatoria_r(int n1)

{

int s = 1;

if (n1 == 0)

return s;

else

s = n1 + sumatoria_r(n1 - 1);

return s;



}

//static public void Invertir_R(int n)

//{



// if (n >= 10)

// Invertir_R(n / 10);

//}


static public int Fibonacci_R(int n)

{

if (n <= 2)

return 1;

else

return Fibonacci_R(n - 1) + Fibonacci_R(n - 2);

}

}

}


class pila

class Pila

{

private int[] elementos = null;

private int tope;

public Pila()

{

elementos = new int[5];

tope = -1;

}

public Pila(int n)

{

if (n < 1)

n = 5;

elementos = new int[n];

tope = -1;

}

public int this[int index]

{

get { return elementos[index]; }

}

public int Maximo

{

get { return elementos.Length; }

}

public int Tope

{

get { return tope; }

}

public bool estaVacia()

{

return tope == -1;

}

public bool estaLlena()

{

return tope == elementos.Length - 1;

}

public bool Poner(int x)

{

if (estaLlena())

return false;



tope++;

elementos[tope] = x;

return true;

}



public bool Quitar(ref int x)

{

if (estaVacia())

return false;



x = elementos[tope];

tope--;

return true;

}


}


Notacion Polaca

enum Simbolo { OPERANDO, PIZQ, PDER, SUMARES, MULTDIV, POW };

class Not_Polaca

{

public StringBuilder Convertir_Pos(string Ei)

{

char[] Epos = new char[Ei.Length];

int tam = Ei.Length;

PilaPlantilla<int> stack = new PilaPlantilla<int>(tam);



int i, pos = 0;

for (i = 0; i < Epos.Length; i++)

{

char car = Ei[i];

Simbolo actual = Tipo_y_Presendecia(car);

switch (actual)

{

case Simbolo.OPERANDO: Epos[pos++] = car; break;

case Simbolo.SUMARES:

{

while (!stack.Empty && Tipo_y_Presendecia((char)stack.Topee())>= actual)

{

Epos[pos++] = (char)stack.Pop();

}

stack.Push(car);

}

break;

case Simbolo.MULTDIV:

{

while (!stack.Empty && Tipo_y_Presendecia((char)stack.Topee())>= actual)

{

Epos[pos++] = (char)stack.Pop();

}

stack.Push(car);

}

break;

case Simbolo.POW:

{

while (!stack.Empty && Tipo_y_Presendecia((char)stack.Topee())>= actual)

{

Epos[pos++] = (char)stack.Pop();

}

stack.Push(car);

}

break;

case Simbolo.PIZQ: stack.Push(car);break;

case Simbolo.PDER:

{

char x = (char)stack.Pop();

while(Tipo_y_Presendecia(x)!= Simbolo.PIZQ)

{

Epos[pos++] = x;

x=(char)stack.Pop();

}

}

break;

}

}

while(!stack.Empty)

{

if(pos< Epos.Length)

Epos[pos++] = (char)stack.Pop();

else

break;

}

StringBuilder regresa = new StringBuilder(Ei);



for(int r=0; r<Epos.Length; r++)

regresa[r] = Epos[r];

return regresa;

}

public Simbolo Tipo_y_Presendecia(char s)

{

Simbolo simbolo;

switch (s)

{

case '+': simbolo = Simbolo.SUMARES; break;

case '-': simbolo = Simbolo.SUMARES; break;

case '*': simbolo = Simbolo.MULTDIV; break;

case '/': simbolo = Simbolo.MULTDIV; break;

case '(': simbolo = Simbolo.PIZQ; break;

case ')': simbolo = Simbolo.PDER; break;

case '^': simbolo = Simbolo.POW; break;

default: simbolo = Simbolo.OPERANDO; break;

}

return simbolo;

}

}



class PilaPlantilla<T>

{

T[] pila;

int tam;

int tope;

bool vacia;

bool llena;

public PilaPlantilla(int n)

{

tam = n;

pila = new T[tam];

tope = 0;

vacia = true;

llena = false;

}

public void Push(T Dato)

{

vacia = false;

pila[tope++] = Dato;

if (tope == tam)

llena = true;

}

public T Pop()

{

llena = false;

if (--tope == 0)

{

vacia = true;

}

return pila[tope];

}

public int Length

{

get { return pila.Length; }

}

public int Used

{

get { return tope; }

}

public bool Full

{

get { return tope == pila.Length; }

}

public bool Empty

{

get { return tope == 0; }

}

public T Topee ()

{

return pila[tope - 1];

}

}


Notacion Polaca

enum Simbolo { OPERANDO, PIZQ, PDER, SUMARES, MULTDIV, POW };

class Not_Polaca

{

public StringBuilder Convertir_Pos(string Ei)

{

char[] Epos = new char[Ei.Length];

int tam = Ei.Length;

PilaPlantilla<int> stack = new PilaPlantilla<int>(tam);



int i, pos = 0;

for (i = 0; i < Epos.Length; i++)

{

char car = Ei[i];

Simbolo actual = Tipo_y_Presendecia(car);

switch (actual)

{

case Simbolo.OPERANDO: Epos[pos++] = car; break;

case Simbolo.SUMARES:

{

while (!stack.Empty && Tipo_y_Presendecia((char)stack.Topee())>= actual)

{

Epos[pos++] = (char)stack.Pop();

}

stack.Push(car);

}

break;

case Simbolo.MULTDIV:

{

while (!stack.Empty && Tipo_y_Presendecia((char)stack.Topee())>= actual)

{

Epos[pos++] = (char)stack.Pop();

}

stack.Push(car);

}

break;

case Simbolo.POW:

{

while (!stack.Empty && Tipo_y_Presendecia((char)stack.Topee())>= actual)

{

Epos[pos++] = (char)stack.Pop();

}

stack.Push(car);

}

break;

case Simbolo.PIZQ: stack.Push(car);break;

case Simbolo.PDER:

{

char x = (char)stack.Pop();

while(Tipo_y_Presendecia(x)!= Simbolo.PIZQ)

{

Epos[pos++] = x;

x=(char)stack.Pop();

}

}

break;

}

}

while(!stack.Empty)

{

if(pos< Epos.Length)

Epos[pos++] = (char)stack.Pop();

else

break;

}

StringBuilder regresa = new StringBuilder(Ei);



for(int r=0; r<Epos.Length; r++)

regresa[r] = Epos[r];

return regresa;

}

public Simbolo Tipo_y_Presendecia(char s)

{

Simbolo simbolo;

switch (s)

{

case '+': simbolo = Simbolo.SUMARES; break;

case '-': simbolo = Simbolo.SUMARES; break;

case '*': simbolo = Simbolo.MULTDIV; break;

case '/': simbolo = Simbolo.MULTDIV; break;

case '(': simbolo = Simbolo.PIZQ; break;

case ')': simbolo = Simbolo.PDER; break;

case '^': simbolo = Simbolo.POW; break;

default: simbolo = Simbolo.OPERANDO; break;

}

return simbolo;

}

}



class PilaPlantilla<T>

{

T[] pila;

int tam;

int tope;

bool vacia;

bool llena;

public PilaPlantilla(int n)

{

tam = n;

pila = new T[tam];

tope = 0;

vacia = true;

llena = false;

}

public void Push(T Dato)

{

vacia = false;

pila[tope++] = Dato;

if (tope == tam)

llena = true;

}

public T Pop()

{

llena = false;

if (--tope == 0)

{

vacia = true;

}

return pila[tope];

}

public int Length

{

get { return pila.Length; }

}

public int Used

{

get { return tope; }

}

public bool Full

{

get { return tope == pila.Length; }

}

public bool Empty

{

get { return tope == 0; }

}

public T Topee ()

{

return pila[tope - 1];

}

}