queria hacer un programa que desarrolle una clase que tenga las siguientes responsabilidades (clase Formula):
- Ingresar una fórmula que contenga paréntesis, corchetes y llaves.
- Validar que los ( ) [] y {} estén correctamente balanceados.
La cosa es que hay cosas que no entiendo en el codigo.
Asi se ve en Form1.cs [Diseno]*:
Asi se ve en Pila.cs (hay que hacer una nueva clase):
Código:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Formula
{
class Pila
{
class Nodo
{
public char simbolo;
public Nodo sig;
}
private Nodo raiz;
public Pila()
{
raiz = null;
}
public void Insertar(char x)
{
Nodo nuevo;
nuevo = new Nodo();
nuevo.simbolo = x;
if (raiz == null)
{
nuevo.sig = null;
raiz = nuevo;
}
else
{
nuevo.sig = raiz;
raiz = nuevo;
}
}
public char Extraer()
{
if (raiz != null)
{
char informacion = raiz.simbolo;
raiz = raiz.sig;
return informacion;
}
else
{
return char.MaxValue;
}
}
public bool Vacia()
{
if (raiz == null)
{
return true;
}
else
{
return false;
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Formula
{
class Pila
{
class Nodo
{
public char simbolo;
public Nodo sig;
}
private Nodo raiz;
public Pila()
{
raiz = null;
}
public void Insertar(char x)
{
Nodo nuevo;
nuevo = new Nodo();
nuevo.simbolo = x;
if (raiz == null)
{
nuevo.sig = null;
raiz = nuevo;
}
else
{
nuevo.sig = raiz;
raiz = nuevo;
}
}
public char Extraer()
{
if (raiz != null)
{
char informacion = raiz.simbolo;
raiz = raiz.sig;
return informacion;
}
else
{
return char.MaxValue;
}
}
public bool Vacia()
{
if (raiz == null)
{
return true;
}
else
{
return false;
}
}
}
}
Y asi se ve en Form1.cs:
Código:
using Formula;
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 Formula
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Pila pila1;
pila1 = new Pila();
string cadena = textBox1.Text;
for (int f = 0; f < cadena.Length; f++)
{
if (cadena.ElementAt(f) == '(' || cadena.ElementAt(f) == '[' || cadena.ElementAt(f) == '{')
{
pila1.Insertar(cadena.ElementAt(f));
}
else
{
if (cadena.ElementAt(f) == ')')
{
if (pila1.Extraer() != '(')
{
Text = "Incorrecta";
return;
}
}
else
{
if (cadena.ElementAt(f) == ']')
{
if (pila1.Extraer() != '[')
{
Text = "Incorrecta";
return;
}
}
else
{
if (cadena.ElementAt(f) == '}')
{
if (pila1.Extraer() != '{')
{
Text = "Incorrecta";
return;
}
}
}
}
}
}
if (pila1.Vacia())
{
Text = "Correcta";
}
else
{
Text = "Incorrecta";
}
}
}
}
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 Formula
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Pila pila1;
pila1 = new Pila();
string cadena = textBox1.Text;
for (int f = 0; f < cadena.Length; f++)
{
if (cadena.ElementAt(f) == '(' || cadena.ElementAt(f) == '[' || cadena.ElementAt(f) == '{')
{
pila1.Insertar(cadena.ElementAt(f));
}
else
{
if (cadena.ElementAt(f) == ')')
{
if (pila1.Extraer() != '(')
{
Text = "Incorrecta";
return;
}
}
else
{
if (cadena.ElementAt(f) == ']')
{
if (pila1.Extraer() != '[')
{
Text = "Incorrecta";
return;
}
}
else
{
if (cadena.ElementAt(f) == '}')
{
if (pila1.Extraer() != '{')
{
Text = "Incorrecta";
return;
}
}
}
}
}
}
if (pila1.Vacia())
{
Text = "Correcta";
}
else
{
Text = "Incorrecta";
}
}
}
}
La cosa que no entiendo esta en Form1.cs. Aqui:
Código:
if (cadena.ElementAt(f) == ')')
{
if (pila1.Extraer() != '(')
{
Text = "Incorrecta";
return;
}
}
{
if (pila1.Extraer() != '(')
{
Text = "Incorrecta";
return;
}
}
Dijimos si en alguna parte hay un ) (if (cadena.ElementAt(f) == ')')) entonces if (pila1.Extraer() != '('). Pero porque? Porque hace falta preguntar si no hay un ( si ya preguntamos si no hay un )?
Gracias y saludos