Datos Personales

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

sábado, 26 de noviembre de 2011

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];

}

}


No hay comentarios:

Publicar un comentario