Demonstrate calculator in Stack

Asked

Viewed 435 times

2

I have a problem here and I need a help, I have to do a job for college, and a calculator, so far so good I’ve even made the calculator, only the problem and the next, I need to demonstrate the numbers in the pile, someone knows how to do it, by which I understood stack and last that goes in and first that comes out, only I have no idea how to do that in my code.

Follow the code of the calculator.

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));
        }
    }
}
}
  • "demonstrate the stack numbers" explain this statement better. Do you want to implement the calculations of a calculator based on a Stack-type data structure? display the result visually as if it were a stack ?

  • That’s right Isac, I need to demonstrate the calculations as a pile structure base that’s right...

1 answer

4


It is common for calculators to use batteries, there are even calculators that work with RPN, but in your case I believe that it is not necessary RPN. What you should do to work with stack is to remember that, stack is nothing more than a Chained List with the LIFO rule (last one in, first one 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 basis:

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#

  • Even, since you are working with Forms, how about making a perfumery for the teacher? show your stack in real time on the screen.

  • I believe stack refers to LIFO structure (Last in, First Out). A queue is more appropriate to FIFO.

  • 1

    @Vinícius Really, I got confused, I’ve adjusted, thank you!

  • Thank you so much for your example, helped me a lot here vlw.

  • @Juniorguerreiro I am happy, if you can evaluate the answer please :D

  • 1

    +1 for the explanation and the well exemplified code.

  • why not use Stack<> ? https://msdn.microsoft.com/pt-br/library/system.collections.stack(v=vs.110). aspx

  • @Rovannlinhalis is a good one too, but particularly I don’t know.

  • 1

    is just the stack class, same as the list... just use Push and Pop all ready rs

  • @Nice rovannlinhalis, it’s interesting.. but because it is an academic work, and the main goal is to learn, I think using ready-made things are not always the best option, but anyway, it’s good to know that there is ready, thank you :D

  • 2

    Yes, I thought about this possibility... even in college I implemented the list, queue and stack, but as it was used the List<>, which is already ready, it is almost equivalent to use the Stack<> rsrs. Ps. It’s just a comment, vlw

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.