0

所以我已经有一段时间了,仍然无法让我的计算器做最后一件事。

我知道了谢谢!

在我按下等号按钮或 Tan、Sin、Cos 或 Mod 按钮后,我希望我的计算器输入一个新数字。基本上,即使答案仍然存在,文本框中也没有任何内容。目前,所有号码都保留,新号码添加到末尾。我不希望这种情况发生。

计算后,如果我按下一个数字按钮,我希望它清除屏幕并重新添加新数字。以下是我到目前为止的代码。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    Double FirstNumber;
    string mathOperator = "";

    private void AddButton_Click(object sender, EventArgs e)
    {
        FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
        mathOperator = "+";

        DisplayTextBox.Text = "";
        DisplayTextBox.Focus();

    }
    private void EqualButton_Click(object sender, EventArgs e)
    {
        Double SecondNumber;
        SecondNumber = Convert.ToDouble(DisplayTextBox.Text);

        switch (mathOperator)
        {
            case "+":
                DisplayTextBox.Text = (FirstNumber + SecondNumber).ToString();
                break;
            case "-":
                DisplayTextBox.Text = (FirstNumber - SecondNumber).ToString();
                break;
            case "*":
                DisplayTextBox.Text = (FirstNumber * SecondNumber).ToString();
                break;
            case "/":
                DisplayTextBox.Text = (FirstNumber / SecondNumber).ToString();
                break;
            default:
                break;
        }
    }

    private void button12_Click(object sender, EventArgs e)
    {
        FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
        FirstNumber *= -1;
        DisplayTextBox.Text = FirstNumber.ToString();

    }

    private void ButtonMinus_Click(object sender, EventArgs e)
    {

        FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
        mathOperator = "-";

        DisplayTextBox.Text = "";
        DisplayTextBox.Focus();

    }

    private void ButtonMultiply_Click(object sender, EventArgs e)
    {

        FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
        mathOperator = "*";

        DisplayTextBox.Text = "";
        DisplayTextBox.Focus();

    }

    private void ButtonDivide_Click(object sender, EventArgs e)
    {

        FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
        mathOperator = "/";

        DisplayTextBox.Text = "";
        DisplayTextBox.Focus();

    }

    private void ButtonMod_Click(object sender, EventArgs e)
    {
        FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
        DisplayTextBox.Text = Math.Tan(FirstNumber).ToString();

    }

    private void Button1_Click(object sender, EventArgs e)
    {
        DisplayTextBox.Text = DisplayTextBox.Text + "1";
    }

    private void Button2_Click(object sender, EventArgs e)
    {
        DisplayTextBox.Text = DisplayTextBox.Text + "2";
    }

    private void Button3_Click(object sender, EventArgs e)
    {
        DisplayTextBox.Text = DisplayTextBox.Text + "3";
    }

    private void Button4_Click(object sender, EventArgs e)
    {
        DisplayTextBox.Text = DisplayTextBox.Text + "4";
    }

    private void Button5_Click(object sender, EventArgs e)
    {
        DisplayTextBox.Text = DisplayTextBox.Text + "5";
    }

    private void Button6_Click(object sender, EventArgs e)
    {
        DisplayTextBox.Text = DisplayTextBox.Text + "6";
    }

    private void Button7_Click(object sender, EventArgs e)
    {
        DisplayTextBox.Text = DisplayTextBox.Text + "7";
    }

    private void Button8_Click(object sender, EventArgs e)
    {
        DisplayTextBox.Text = DisplayTextBox.Text + "8";
    }

    private void Button9_Click(object sender, EventArgs e)
    {
        DisplayTextBox.Text = DisplayTextBox.Text + "9";
    }

    private void ButtonClear_Click(object sender, EventArgs e)
    {
        DisplayTextBox.Text = "";
        mathOperator = "";
    }

    private void ButtonOff_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

    private void ButtonTan_Click(object sender, EventArgs e)
    {
        FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
        DisplayTextBox.Text = Math.Tan(FirstNumber).ToString();
    }

    private void ButtonSin_Click(object sender, EventArgs e)
    {
        FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
        DisplayTextBox.Text = Math.Sin(FirstNumber).ToString();

    }

    private void ButtonCos_Click(object sender, EventArgs e)
    {
        FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
        DisplayTextBox.Text = Math.Cos(FirstNumber).ToString();

    }

    private void Button0_Click(object sender, EventArgs e)
    {
        if (DisplayTextBox.Text.Length >= 1)
        {
            DisplayTextBox.Text = DisplayTextBox.Text + "0";
        }
        else
        {
            return;
        }
    }

    private void DecimalButton_Click(object sender, EventArgs e)
    {
        if (DisplayTextBox.Text.Contains("."))
        {
            return;
        }
        else
        {
            DisplayTextBox.Text = DisplayTextBox.Text + ".";
        }
    }
}
4

5 回答 5

3

向您的类添加一个布尔变量,该变量表示操作是否刚刚完成的标志,如下所示:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    Double FirstNumber;
    string mathOperator = "";
    bool operationJustCompleted = false;

最初,该值为false,因为首次创建计算器时没有发生任何事情。

现在,在等号按钮事件处理程序的末尾,将标志设置为 true,如下所示:

private void EqualButton_Click(object sender, EventArgs e)
{
    // Logic for calculation

    operationJustCompleted = true;
}

最后,在数字按钮的事件处理程序中,检查operationJustCompleted标志是否为真,如果是则清除文本并将刚刚完成的操作标志重置回false;像这样:

private void Button1_Click(object sender, EventArgs e)
{
    if(operationJustCompleted)
    {
        DisplayTextBox.Text = String.Empty;
        operationJustCompleted = false;
    }
    DisplayTextBox.Text = DisplayTextBox.Text + "1";
}
于 2013-10-31T15:48:01.550 回答
0

就像 David Arno 说的,是这样的:

    private void EqualButton_Click(object sender, EventArgs e)
    {
        ClearDisplayBeforeNextTextEntry = true;
    }

    private void Button1_Click(object sender, EventArgs e)
    {
        // New code
        ClearText();
        // Old code
        DisplayTextBox.Text = DisplayTextBox.Text + "1";
    }

    // Same for all other number buttons as above

    private void ClearText()
    {
        if (ClearDisplayBeforeNextTextEntry)
        {
            DisplayTextBox.Text = "";
            ClearDisplayBeforeNextTextEntry = false;
        }
    }
于 2013-10-31T15:48:44.823 回答
0

向您的类添加一个状态变量。如果为 true,并且选择了一个数字,则在写入数字之前清除显示并将其设置为 false。

单击“=”时,将其设置为true。

于 2013-10-31T15:45:35.070 回答
0

根据简要回顾,您似乎需要引入一个变量来跟踪计算器的状态,然后在按下“等于”按钮时,将状态设置为计算值。然后,在数字按钮 (0-9) 中,您需要检查状态变量并附加或替换显示文本。

于 2013-10-31T15:45:39.920 回答
0

创建这个全局变量...

private bool equationComplete = false;

将此添加到“等于”按钮的末尾...

equationComplete = true;

将此添加到每个数字按钮单击的开头...

if (equationComplete) DisplayTextBox.Text = "";

将此添加到每个数字按钮单击事件处理程序的末尾...

equationComplete = false;
于 2013-10-31T15:47:15.717 回答