0

I have added code for plus button, but how i write code for other buttons? My code is

double total1 = 0;
        double total2 = 0;
        private void plus_Click(object sender, EventArgs e)
        {
            total1 = total1 + double.Parse(textBox1.Text);
            textBox1.Clear();
        }
        private void equal_Click(object sender, EventArgs e)
        {
            total2 = total1 + double.Parse(textBox1.Text);
            textBox1.Text = total2.ToString();
            total1 = 0;
        }
        private void minus_Click(object sender, EventArgs e)
        {
            total1 = double.Parse(textBox1.Text) - total1;
            textBox1.Clear();            
        }
        private void divide_Click(object sender, EventArgs e)
        {
            total1 = total1 + double.Parse(textBox1.Text);
            textBox1.Clear();
        }
        private void multiply_Click(object sender, EventArgs e)
        {
            total1 = total1 + double.Parse(textBox1.Text);
            textBox1.Clear();
        }
2
Contributors
1
Reply
11
Views
1 Year
Discussion Span
Last Post by Reverend Jim
0

Are you using postfix or infix notation? If you are using infix then I don't see how your plus code will work because you can't do the calculation until you know the second operand nd you won't know that until you press a second operator (or =). For example, most calculators work like this using infix

123                  push and display 123
+                    push +
17                   push and display 17
=                    display 123+17

or

123                  push and display 123
+                    push +
17                   push and display 17
-                    push - and display 123+17
9                    push and display 9
=                    display 140-9

You have to keep track of the last operator pressed so you know what calculation to perform when you click the next operator.

Edited by Reverend Jim

This article has been dead for over six months. Start a new discussion instead.
Be sure to adhere to our posting rules.