简体   繁体   中英

My C# Calculator will not calculate more than 2 consecutive numbers

I've just recently completed my first Windows Form Calculator which works fine, but my question is - how do i add a third, or even a fourth number to the calculation before hitting the equal sign? I've been trying to place the ThirdNumber variable in a number of different spots in my code like so --

  private void bequal_Click(object sender, EventArgs e)
    {
        double SecondNumber;
        double ThirdNumber;
        double Result;

        SecondNumber = Convert.ToDouble(textBox1.Text);
        ThirdNumber = Convert.ToDouble(textBox1.Text);

        if (Operation == "+")
        {
            Result = (FirstNumber + SecondNumber) + ThirdNumber; 
            textBox1.Text = Convert.ToString(Result);
            FirstNumber = Result;
        }

However, each time I am given the incorrect answer (eg. 6 + 6 + 8 = 22, when i know it should be 20). Could someone please point me in the right direction as to how to go about fixing this flaw and please keep in mind that i am still a beginner so any and all advice and information would be greatly appreciated

Thanks

The rest of my code for the App --

public partial class Form1 : Form
{
    double FirstNumber;
    string Operation;
    public Form1()
    {
        InitializeComponent();
    }

    private void n1_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "0" && textBox1.Text != null)  // If the textbox value is 0 or is not null then
        {                                                   // the number that corresponds to the button that is clicked
            textBox1.Text = "1";                            // will be subsituted in
        }
        else
        {                                                   // Else add the button value to the existing text box value 
            textBox1.Text = textBox1.Text + "1";
        }
    }

    private void n2_Click(object sender, EventArgs e)
    {
        if(textBox1.Text == "0" && textBox1.Text != null)
        {
            textBox1.Text = "2";
        }
        else
        {
            textBox1.Text = textBox1.Text + "2";
        }
    }

    private void n3_Click(object sender, EventArgs e)
    {
        if(textBox1.Text == "0" && textBox1.Text != null)
        {
            textBox1.Text = "3";
        }
        else
        {
            textBox1.Text = textBox1.Text + "3";
        }
    }

    private void n4_Click(object sender, EventArgs e)
    {
        if(textBox1.Text == "0" && textBox1.Text != null)
        {
            textBox1.Text = "4";
        }
        else
        {
            textBox1.Text = textBox1.Text + "4";
        }
    }

    private void n5_Click(object sender, EventArgs e)
    {
        if(textBox1.Text == "0" && textBox1.Text != null)
        {
            textBox1.Text = "5";
        }
        else
        {
            textBox1.Text = textBox1.Text + "5";
        }
    }

    private void n6_Click(object sender, EventArgs e)
    {
        if(textBox1.Text == "0" && textBox1.Text != null)
        {
            textBox1.Text = "6";
        }
        else
        {
            textBox1.Text = textBox1.Text + "6";
        }
    }

    private void n7_Click(object sender, EventArgs e)
    {
        if(textBox1.Text == "0" && textBox1.Text != null)
        {
            textBox1.Text = "7";
        }
        else
        {
            textBox1.Text = textBox1.Text + "7";
        }
    }

    private void n8_Click(object sender, EventArgs e)
    {
        if(textBox1.Text == "0" && textBox1.Text != null)
        {
            textBox1.Text = "8";
        }
        else
        {
            textBox1.Text = textBox1.Text + "8";
        }
    }

    private void n9_Click(object sender, EventArgs e)
    {
        if(textBox1.Text == "0" && textBox1.Text != null)
        {
            textBox1.Text = "9";
        }
        else
        {
            textBox1.Text = textBox1.Text + "9";
        }
    }

    private void n0_Click(object sender, EventArgs e)
    {
        textBox1.Text = textBox1.Text + "0";
    }

    private void bad_Click(object sender, EventArgs e)
    {
        FirstNumber = Convert.ToDouble(textBox1.Text);
        textBox1.Text = "0";
        Operation = "+";
    }

    private void sub_Click(object sender, EventArgs e)
    {
        FirstNumber = Convert.ToDouble(textBox1.Text);
        textBox1.Text = "0";
        Operation = "-";
    }

    private void bmul_Click(object sender, EventArgs e)
    {
        FirstNumber = Convert.ToDouble(textBox1.Text);
        textBox1.Text = "0";
        Operation = "*";
    }

    private void bdiv_Click(object sender, EventArgs e)
    {
        FirstNumber = Convert.ToDouble(textBox1.Text);
        textBox1.Text = "0";
        Operation = "/";
    }

    private void bc_Click(object sender, EventArgs e)
    {
        textBox1.Text = "0";
    }

    private void bdot_Click(object sender, EventArgs e)
    {
        textBox1.Text = textBox1.Text + ",";
    }

    private void bequal_Click(object sender, EventArgs e)
    {
        double SecondNumber;
        double ThirdNumber;
        double Result;

        SecondNumber = Convert.ToDouble(textBox1.Text);
        ThirdNumber = Convert.ToDouble(textBox1.Text);

        if (Operation == "+")
        {
            Result = (FirstNumber + SecondNumber) + ThirdNumber; 
            textBox1.Text = Convert.ToString(Result);
            FirstNumber = Result;
        }
        else if (Operation == "-")
        {
            Result = (FirstNumber - SecondNumber);
            textBox1.Text = Convert.ToString(Result);
            FirstNumber = Result;
        }
        else if (Operation == "*")
        {
            Result = (FirstNumber * SecondNumber);
            textBox1.Text = Convert.ToString(Result);
            FirstNumber = Result;
        }
        else if (Operation == "/")
        {
            if (SecondNumber == 0)
            {
                textBox1.Text = "Cannot divide by zero";
            }
            else
            {
                Result = (FirstNumber / SecondNumber);
                textBox1.Text = Convert.ToString(Result);
                FirstNumber = Result;
            }
        }
    }
}

You get 6 + 6 + 8 = 22 because what you basically do is:

  • input 6, press +
  • 6 is stored as FirstNumber
  • input another 6, press +
  • the FirstNumber is overwritten with a new 6.
  • input 8, press =
  • retrieve the FirstNumber - which is 6 - and add 8 to it twice .

Another approach could be to make a type that stores both the number and the related operation.

So the workflow would be like:

  • input 6, press + .
  • information like { op: multiply, value: 6 } is stored in a collection of 'events'.
  • keep adding.
  • on pressing = iterate through the collection of these 'events' and calculate the result.

Rather than thinking in terms of first, second and third numbers, I assume you're wanting to mimic a real calculator.

For a simple calculator, what you really want is an accumulator . That's a single variable/register where we store the last result we computed. 1

When any of the "operation" buttons is pressed, you then need to consider the current value in the accumulator, the current contents of your textbox and whatever the previous operation button was that was pressed. You then perform that operation between the accumulator and the number in the textbox, update the accumulator and textbox with that new value, and record the new operation that you're starting to perform. You should be able to put most of that logic into a single function that all of your "operation" buttons call.

The equals buttons then becomes very similar to the other operation buttons. There are some nuances here that you may want to consider where someone presses the "=" button and then immediately afterwards presses another operation button - you'll probably find that you want at least two "operation" values that distinguish between "I'm just displaying the last result because someone pressed the = key - they can start another operation at this point" and "I was displaying a result, but they've now started typing something else so we want to overwrite the accumulator with this new input when the press an operation key".


1 Once you've got this working, then add a second variable, this one called "Memory". You can then implement the "M", "M+", "M-", "MC", "MR", etc buttons found on most calculators.

private void bdiv_Click(object sender, EventArgs e)
{
    FirstNumber = Convert.ToDouble(textBox1.Text);
    textBox1.Text = "0";
    Operation = "/";
}

Everytime you press '+' button, first number will be reset and old value of it will be lost.

    SecondNumber = Convert.ToDouble(textBox1.Text);
    ThirdNumber = Convert.ToDouble(textBox1.Text);

Second and third number are always same.

6+6+8 makes 22 in your calculator because it actually calculates 6+8+8. One of the 6's is lost in the second add operation. And 8 is duplicated because both second and third number will be equal to textBox1.Text.

Solution :

Use += operation

private void bdiv_Click(object sender, EventArgs e)
{
   FirstNumber += Convert.ToDouble(textBox1.Text);
   textBox1.Text = "0";
   Operation = "/";
}

And Remove SecondNumber variable.

SecondNumber = Convert.ToDouble(textBox1.Text); // remove this line
ThirdNumber = Convert.ToDouble(textBox1.Text);

Second number is not needed because it is inside FirstNumber variable now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM