Show Calculator on Stack

2

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));
        }
    }
}
}
    
asked by anonymous 19.10.2017 / 13:37

1 answer

4

It is common for calculators to use stacks, there are even calculators that work with RPN , but in your case I believe it is not required RPN. What you should do to work with stack is to remember that, stack is nothing more than a Chain List with the LIFO rule (last incoming, first out). From there, you will have to change all your code, unfortunately, to work with your stack.

EDIT: I made a pseudo code here for you to take as a base:

public class Calculadora
{
    public static void Main(string[] args)
    {
        List<int> Lista = new List<int>();
    Lista.Add(4);
    Lista.Add(5);

    Console.WriteLine(Somar(Lista));
    Console.ReadLine();
}

public static int Somar (List<int> Lista)
{
    if (Lista != null && Lista.Count >= 2)
    {
        int valorA = Lista.FirstOrDefault();
        Lista.Remove(Lista.FirstOrDefault());
        int valorB = Lista.FirstOrDefault();
        Lista.Remove(Lista.FirstOrDefault());

        return valorA + valorB;
    }
    return 0;
}

public static int Desempilha (List<int> Lista)
{
    if (Lista == null)
        return 0;

    int retorno = Lista.FirstOrDefault();
    Lista.Remove(Lista.FirstOrDefault());
    return retorno;
}

public static List<int> Empilha (List<int> Lista, int valor)
{
    if (Lista == null)
    {
        Lista = new List<int>();
        Lista.Add(valor);
    }
    else
        Lista.Add(valor);

    return Lista;
    }
}

I used the List itself because you are working with C #

    
19.10.2017 / 13:48