在C#中创建计算器,需要帮助实现数学逻辑/其他

时间:2012-06-29 16:35:55

标签: c# c#-4.0

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        bool plus;
        bool minus;
        bool multiply;
        bool divide;

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

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

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

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

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

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

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

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

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

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

        private void buttonPlus_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
                return;
            else
            {
                plus = true;
                textBox1.Text = textBox1.Text + " + ";
            }
        }

        private void buttonDivide_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
                return;
            else
            {
                divide = true;
                textBox1.Text = textBox1.Text + " / ";
            }
        }

        private void buttonMinus_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
                return;
            else
            {
                minus = true;
                textBox1.Text = textBox1.Text + " - ";
            }
        }

        private void buttonMultiply_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
                return;
            else
            {
                multiply = true;
                textBox1.Text = textBox1.Text + " * ";
            }
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            textBox1.Text = string.Empty;
        }

        private void buttonEquals_Click(object sender, EventArgs e)
        {
            if (plus)
            {

            }

            if (minus)
            {

            }

            if (multiply)
            {

            }

            if (divide)
            {

            }
        }


    }
}

我被困住的部分是在我按下数学按钮(+, - ,/,*)之后。我只想在文本框中显示一个字符,除非我按下另一个数字。

示例:现在,这是发生的事情: 87 + + + + + + + + +

我想要87 + 如果用户添加另一个号码,我想在那里添加另一个号码,如87 + 87 +等。

另外,在添加数学逻辑时我需要一点方向。

我在考虑以某种方式在变量中的数学符号之前存储字符串中的内容,然后对继续数学符号的数字执行相同的操作。那很好还是有一种更容易/更简单的方法来实现这个目标?

3 个答案:

答案 0 :(得分:3)

我认为对计算器采用一种好的方法,而不是测试字符串等,就是使用states来确定计算器的“时刻”。 让我们考虑一些州:

  • NotInitialized :仅接受数字。当给出第一个数字时,计算器将转到接收
  • 接收:将连接更多数字。任何操作员都会将计算器带到 NextNumber
  • NextNumber :只接受数字。当给出第一个数字时,计算器将转到接收

然后就行了。试着找出描述计算器操作的状态。

答案 1 :(得分:0)

对于+ - * \情境..我建议使用像lastCharIsSymbol这样的布尔属性。无论何时单击数字设置,它都等于false,当您单击符号集时,它等于true。然后,您可以在符号单击事件中添加if语句,仅添加符号IF lastCharIsSymbol = false。

public Boolean lastCharIsSymbol {get;set;}

private void button9_Click(object sender, EventArgs e)
{
    textBox1.Text = textBox1.Text + "9";
    lastCharIsSymbol = false;
}

private void buttonPlus_Click(object sender, EventArgs e)
{
    if (textBox1.Text == "" || lastCharIsSymbol)
        return;
    else
    {
        plus = true;
        textBox1.Text = textBox1.Text + " + ";
        lastCharIsSymbol = true;
    }
}

如果您不想这样做,您还可以更改if语句,只需获取文本框中任何内容的子字符串,并找出最后一个字符是什么。

答案 2 :(得分:0)

虽然您可以简单地写一个字符串,然后在用户按下equals时从那里解析它,但创建一个他们输入的内容可能更有益。然后,当用户输入内容时,您可以确保他们不会将2个号码放在彼此旁边,或者将2个号码放在彼此旁边。例如,即使你只做一个简单的计算器,你也可以处理负数和多位数。我可以输入数字,按下操作符后,它会将数字和操作符提交到列表中。如果运算符是等号键,则迭代列表并执行计算。

一个例子是当前的Windows计算器。你会注意到有2个文本框。一个给出当前的工作数,而另一个给出整体方程。

这是一个例子。有更好的方法可以解决这个问题,但为了简单起见,我们指出了一个不错的方向,这应该有效。

    List<object> calculatorChain = new List<object>();

    /// <summary>
    /// Handles the button press for the minus key
    /// </summary>
    /// <param name="sender">The originating object</param>
    /// <param name="e">Event arguments</param>
    private void buttonMinus_Click(object sender, EventArgs e)
    {
        CommitOperand(textBox1.Text, "-");
        textBox1.Text = string.Empty;
    }

    /// <summary>
    /// Commits the current working number to the calculator chain with a given operator
    /// </summary>
    /// <param name="operand">The current working number to add</param>
    /// <param name="operation">The operation to perform</param>
    private void CommitOperand(string operand, string operation)
    {
        if (string.IsNullOrWhiteSpace(operand) || string.IsNullOrWhiteSpace(operation))
        {
            return;
        }
        else
        {
            decimal number;
            if (decimal.TryParse(operand, out number))
            {
                calculatorChain.Add(number);
                calculatorChain.Add(operation);
            }
        }
    }
相关问题