I have a problem here and I need some help, I have to do a job for college, and a calculator, so far so good I've already done the calculator, but the problem and the next, I need to demonstrate the numbers in stack, someone knows how to do this, so I understood the stack and the last one that comes in and the first one that comes out, but I have no idea how to do it in my code.
Follow the calculator code.
public partial class Form1 : Form
{
string operador;
int a = 0;
bool validar = false;
public Form1()
{
InitializeComponent();
}
private void btnNumerador_Click(object sender, EventArgs e)
{
Button bt = (Button)sender;
txt_valor.Text = txt_valor.Text + bt.Text;
}
private void btn_cancelar_Click(object sender, EventArgs e)
{
txt_valor.Text = "";
lbl_numero.Text = "";
a = 0;
}
private void btn_somar_Click(object sender, EventArgs e)
{
if (validar == true)
{
a = a + Convert.ToInt32(txt_valor.Text);
lbl_numero.Text = Convert.ToString(a) + "+";
txt_valor.Text = "";
operador = "+";
}
else
{
lbl_numero.Text = txt_valor.Text + btn_somar.Text;
a = Convert.ToInt32(txt_valor.Text);
txt_valor.Text = "";
operador = "+";
validar = true;
}
}
private void btn_subtrair_Click(object sender, EventArgs e)
{
if (validar == true)
{
a = a - Convert.ToInt32(txt_valor.Text);
lbl_numero.Text = Convert.ToString(a) + "-";
txt_valor.Text = "";
operador = "-";
validar = false;
}
else
{
lbl_numero.Text = txt_valor.Text + btn_subtrair.Text;
a = Convert.ToInt32(txt_valor.Text);
txt_valor.Text = "";
operador = "-";
}
}
private void btn_multiplicar_Click(object sender, EventArgs e)
{
if (validar == true)
{
a = a * Convert.ToInt32(txt_valor.Text);
lbl_numero.Text = Convert.ToString(a) + "*";
txt_valor.Text = "";
operador = "*";
}
else
{
lbl_numero.Text = txt_valor.Text + btn_multiplicar.Text;
a = Convert.ToInt32(txt_valor.Text);
txt_valor.Text = "";
operador = "*";
validar = true;
}
}
private void btn_dividir_Click(object sender, EventArgs e)
{
if (validar == true)
{
a = a / Convert.ToInt32(txt_valor.Text);
lbl_numero.Text = Convert.ToString(a) + "/";
txt_valor.Text = "";
operador = "/";
validar = false;
}
else
{
lbl_numero.Text = txt_valor.Text + btn_dividir.Text;
a = Convert.ToInt32(txt_valor.Text);
txt_valor.Text = "";
operador = "/";
}
}
private void btn_igual_Click(object sender, EventArgs e)
{
if (operador == "+")
{
lbl_numero.Text = lbl_numero.Text + txt_valor.Text + "=";
txt_valor.Text = Convert.ToString(a + Convert.ToInt32(txt_valor.Text));
}
else if (operador == "-")
{
lbl_numero.Text = lbl_numero.Text + txt_valor.Text + "=";
txt_valor.Text = Convert.ToString(a - Convert.ToInt32(txt_valor.Text));
}
else if (operador == "*")
{
lbl_numero.Text = lbl_numero.Text + txt_valor.Text + "=";
txt_valor.Text = Convert.ToString(a * Convert.ToInt32(txt_valor.Text));
}
else if (operador == "/")
{
lbl_numero.Text = lbl_numero.Text + txt_valor.Text + "=";
txt_valor.Text = Convert.ToString(a / Convert.ToInt32(txt_valor.Text));
}
}
}
}