Professional Documents
Culture Documents
LABORATORIO N 03
OBJETIVO:
1. Construir un compilador de una pasada de expresiones infija.
2. Analizar las construcciones de arboles.
Paso N 01: Disear el siguiente formulario, tal como se muestra a
continuacin
Paso N 02: Modificar las propiedades del objeto agregado, tal como se indica
a continuacin
Objeto
Form1
Label1
Label2
Label3
TextBox1
TextBox2
TextBox3
GroupBox1
DataGridView1
DataGridView2
Botn de Comando 1
Botn de Comando 1
Botn de Comando 1
Propiedad
Text
Text
Text
Text
Name
Name
ReadOny
Name
ReadOnly
Text
Name
allowUserToAddRows
Name
allowUserToAddRows
Name
Text
Name
Text
Name
Text
Valor
Compilador de una pasada Facultad de Ingeniera
Ingrese la expresin en INFIJA :
La expresin en POSTFIJA es :
Resultado es :
TxtPreg
TxtInter
True
TxtRespuesta
True
Detalle
DataGridView1
True
DataGridView2
True
BtnNuevo
&Nuevo
BtnCalcular
&Calcular
BtnSalir
&Salir
Pg. 1/6
System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;
namespace Compilador_de_una_pasada
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int pila_top;
string [] pila_items = new string [100];
public Boolean PilaVacia()
{
if (pila_top == -1)
return true;
else
return false;
}
public string QuitarElePila()
{
if (PilaVacia())
{
MessageBox.Show("Pila Vacia (UnderFlow)...!",
"Atencin",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
return "";
}
return pila_items[pila_top--];
}
public void AgregarElePila(string x)
{
if (pila_top == 100)
MessageBox.Show("Pila llena (OverFlow)...!",
"Atencin",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
else
{
pila_top++;
pila_items[pila_top] = x;
}
}
Pg. 2/6
Pg. 3/6
return false;
else
return true;
}
return false;
}
public string postfija(string Prefija)
{
int i=0,Indice1=0,Indice2=0;
string simbolo, perdido, conca="", posfijo="";
this.IniciarPila();
while (i < Prefija.Trim().Length)
{
simbolo = Prefija.Substring(i, 1);
i++;
if (!es_operador(simbolo))
{
posfijo += simbolo;
this.dataGridView2.Rows.Add();
this.dataGridView2.Rows[Indice2].Cells[0].Value=simbolo;
Indice2++;
}
else
{
while (!this.PilaVacia() && this.Pred(pila_topPila(), simbolo))
posfijo += QuitarElePila();
if (string.Compare(simbolo, ")") != 0)
{
this.AgregarElePila(simbolo);
this.dataGridView1.Rows.Add();
this.dataGridView1.Rows[Indice1].Cells[0].Value = simbolo;
Indice1++;
}
else
perdido = this.QuitarElePila();
}
}
while (!PilaVacia())
posfijo += this.QuitarElePila();
return posfijo;
}
public Boolean es_operador(string c)
{
string s;
int i=0;
s = "+-*^/()";
while (i <= s.Length-1)
{
if(c==s.Substring (i,1))
return true;
i++;
}
return false;
}
Pg. 4/6
Pg. 5/6
this.TxtRespuesta.Text = string.Empty;
this.TxtPreg.Focus();
}
private void BtnSalir_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Desea salir?",
"Facutad de Ingeniera",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)
this.Close();
}
private void TxtPreg_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
this.BtnCalcular.Enabled = true;
this.BtnCalcular.Focus();
}
}
}
}
TAREA:
Modificar el programa, para que permita realizar operaciones bsicas con numeros enteros . por
ejemplo: 10+25.
Pg. 6/6