-2

这个计算器非常基础,所以我一次只使用两个值来计算。

我只计算两个值val1val2。这些值可以是任何大小,包括小数点。我有数字 0-9、小数点、加、减、除、乘和等于的按钮。

我希望 val1、val2 和operator(ex. +,-,/,*)我选择计算这些值listBox在我点击=按钮后出现在我的列表中。我进行的每个计算都希望将其添加到上一个计算下方的列表框中。当 listBox 增长到一定数量的计算(10-15ish)时,我希望从底部删除最旧的计算,并将最新的计算添加到列表的顶部。

public partial class Form1 : Form
{
    // global variables
    bool plus = false;
    bool minus = false;
    bool multiply = false;
    bool divide = false;
    bool equal = false;

    public Form1()
    {
        InitializeComponent();
    } // **MUST HAVE**

    private void button0_Click(object sender, EventArgs e)
    {
        CheckIfEqual();
        textBox1.Text = textBox1.Text + "0";
    } // makes number click usable

    private void CheckIfEqual()
    {
        if (equal) // checks if equal is true
        {
            textBox1.Text = ""; // clear textBox
            equal = false; // tells program there is new calculation
        }
    } 

    private void button1_Click(object sender, EventArgs e)
    {
        CheckIfEqual();
        textBox1.Text = textBox1.Text + "1";
    } // makes number click usable

    private void button2_Click(object sender, EventArgs e)
    {
        CheckIfEqual();
        textBox1.Text = textBox1.Text + "2";
    } // makes number click usable

    private void button3_Click(object sender, EventArgs e)
    {
        CheckIfEqual();
        textBox1.Text = textBox1.Text + "3";
    } // makes number click usable

    private void button4_Click(object sender, EventArgs e)
    {
        CheckIfEqual();
        textBox1.Text = textBox1.Text + "4";
    } // makes number click usable

    private void button5_Click(object sender, EventArgs e)
    {
        CheckIfEqual();
        textBox1.Text = textBox1.Text + "5";
    } // makes number click usable

    private void button6_Click(object sender, EventArgs e)
    {
        CheckIfEqual();
        textBox1.Text = textBox1.Text + "6";
    } // makes number click usable

    private void button7_Click(object sender, EventArgs e)
    {
        CheckIfEqual();
        textBox1.Text = textBox1.Text + "7";
    } // makes number click usable

    private void button8_Click(object sender, EventArgs e)
    {
        CheckIfEqual();
        textBox1.Text = textBox1.Text + "8";
    } // makes number click usable

    private void button9_Click(object sender, EventArgs e)
    {
        CheckIfEqual();
        textBox1.Text = textBox1.Text + "9";
    } // makes number click usable


    private void buttonDecimal_Click(object sender, EventArgs e)
    {
        CheckIfEqual();
        if (textBox1.Text.Contains("."))
        {
            return;
        } // if it contains a decimal execute 
        else
        {
            textBox1.Text = textBox1.Text + ".";
        } // adds decimal
      } // end decimal

    private void buttonPlusMinus_Click(object sender, EventArgs e)
    {
        if (textBox1.Text.Contains("-"))
        {
            textBox1.Text = textBox1.Text.Remove(0, 1);
        } // if it contains the - character remove it
        else
        {
            textBox1.Text = "-" + textBox1.Text;
        } //
    } // adds/removes -

    private void buttonPlus_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "")
        {
            return;
        } // if textbox is empty return
        else
        {
            plus = true;
            textBox1.Tag = textBox1.Text; // stores number 
            textBox1.Text = ""; // clears textbox
        } // tells program we are adding
    } // end plus

    private void buttonMinus_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "")
        {
            return;
        } // if textbox is empty return
        else
        {
            minus = true;
            textBox1.Tag = textBox1.Text; // stores number 
            textBox1.Text = ""; // clears textbox
        } // tells program we are subtracting

    }

    private void buttonMultiply_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "")
        {
            return;
        } // if textbox is empty return
        else
        {
            multiply = true;
            textBox1.Tag = textBox1.Text; // stores number 
            textBox1.Text = ""; // clears textbox
        } // tells program we are multiplying
    }

    private void buttonDivide_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "")
        {
            return;
        } // if textbox is empty return
        else
        {
            divide = true;
            textBox1.Tag = textBox1.Text; // stores number 
            textBox1.Text = ""; // clears textbox
        } // tells program we are dividing
    }

    private void buttonEquals_Click(object sender, EventArgs e)
    {
        equal = true; // tells program it made a calculation
        if (plus) // if true execute
        {
            decimal dec = Convert.ToDecimal(textBox1.Tag) + Convert.ToDecimal(textBox1.Text);
            textBox1.Text = dec.ToString(); // display value once add is finished
            plus = false;
        } // end if plus

        if (minus) // if true execute
        {
            decimal dec = Convert.ToDecimal(textBox1.Tag) - Convert.ToDecimal(textBox1.Text);
            textBox1.Text = dec.ToString(); // display value once add is finished
            minus = false;
        } // end if minus

        if (multiply) // if true execute
        {
            decimal dec = Convert.ToDecimal(textBox1.Tag) * Convert.ToDecimal(textBox1.Text);
            textBox1.Text = dec.ToString(); // display value once add is finished
            multiply = false;
        } // end if multiply

        try
        {
            if (divide) // if true execute
            {
                decimal dec = Convert.ToDecimal(textBox1.Tag) / Convert.ToDecimal(textBox1.Text);
                textBox1.Text = dec.ToString(); // display value once add is finished
                divide = false;
            } // end if divide
        } // try
        catch (DivideByZeroException ex)
        {
            MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK);
        } // catch
     } // end buttonEquals

    private void buttonClear_Click(object sender, EventArgs e)
    {
        plus = minus = multiply = divide = false; // makes all false regardless
        textBox1.Text = ""; // clear
        textBox1.Tag = ""; // clears
    }


    } // end partial class Form1
}// end namespace   
4

1 回答 1

2

重构会很好...

例如,您可以对所有“数字按钮单击”事件使用相同的方法。

代替

private void button1_Click(object sender, EventArgs e)
    {
        CheckIfEqual();
        textBox1.Text = textBox1.Text + "1";
    } // makes number click usable

...
private void button9_Click(object sender, EventArgs e)
    {
        CheckIfEqual();
        textBox1.Text = textBox1.Text + "9";
    } // makes number click usable

只使用一种方法。

button1.Click += NumericButtonClick;
...
button9.Click +=NumericButtonClick;


private void NumericButtonClick(object sender, EventArgs e) {
        CheckIfEqual();
        textBox1.Text = textBox1.Text + ((Button)sender).Text;
}

反正。

假设你有一个 ListBox historyListBox;

然后只需添加一个方法

private void PopulateListBox(string left, string right, string operator) {
  var newContent = string.Format("{0} {1} {2}", left, operator, right);
  if (historyListBox.Items.Count == 15)
    historyListBox.Items.RemoveAt(14);//

  historyListBox.Items.Insert(0, newContent);

}

然后您可以更改“equal_click 事件”(也应该对其进行重构,但这是另一个问题)

private void buttonEquals_Click(object sender, EventArgs e)
    {
        var left = textBox1.Tag;
        var right = textBox1.Text;
        var operator = string.Empty;
        equal = true; // tells program it made a calculation
        if (plus) // if true execute
        {
            operator = "+";
            decimal dec = Convert.ToDecimal(left) + Convert.ToDecimal(right);
            textBox1.Text = dec.ToString(); // display value once add is finished
            plus = false;
        } // end if plus

        if (minus) // if true execute
        {
            operator = "-";
            decimal dec = Convert.ToDecimal(left) - Convert.ToDecimal(right);
            textBox1.Text = dec.ToString(); // display value once add is finished
            minus = false;
        } // end if minus

        //...etc

       PopulateListBox(left, right, operator);
   }
于 2013-01-26T21:27:03.037 回答