运算符是计算器计算字符串的一部分

时间:2013-11-28 07:31:57

标签: c#

我在c#中制作一个计算器。用户的等式显示在名为textBoxInput的文本框中,结果显示在labelResult中。当我点击'equals'按钮时,secondInt = int.Parse(textBoxInput.Text);发生System.FormatException异常,这对我来说意味着程序正在尝试将运算符转换为int以及用户在textBoxInput中输入的整数。我的问题是如何让程序只转换第一个和第二个,而不试图转换运算符?我希望将整个等式保存在textBoxInput中,同时结果显示在labelResult中。这是问题所在的计算器类的enitre代码。

public partial class Calculator : Form
    {

        int firstInt;
        int secondInt;
        int result;
        string Operator;
        Boolean Op;

        public Calculator()
        {
            InitializeComponent();
        }

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

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

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

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

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

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

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

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

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

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

        private void labelResult_Click(object sender, EventArgs e)
        {

        }

        private void buttonEquals_Click(object sender, EventArgs e)
        {
            Calculations application = new Calculations();
            secondInt = int.Parse(textBoxInput.Text);  

            switch (Operator)
            {
                case "/":
                    result = application.Division(firstInt, secondInt);
                    labelResult.Text = (firstInt + " / " + secondInt + "\n" + "= " + result);
                    break;

                case "+":
                    result = application.Addition(firstInt, secondInt);
                    labelResult.Text = (firstInt + " + " + secondInt + "\n" + "= " + result);
                    break;

                case "-":
                    result = application.Subtract(firstInt, secondInt);
                    labelResult.Text = (firstInt + " - " + secondInt + "\n" + "= " + result);
                    break;

                case "*":
                    result = application.Multiply(firstInt, secondInt);
                    labelResult.Text = (firstInt + " * " + secondInt + "\n" + "= " + result);
                    break;
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {


            firstInt = int.Parse(textBoxInput.Text);
            textBoxInput.Text += (" + ");
            Op = true;
            Operator = "+";

        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            textBoxInput.Text = "";
            labelResult.Text = "";
        }

        private void buttonMultiply_Click(object sender, EventArgs e)
        {
            firstInt = Convert.ToInt32(textBoxInput.Text);

            Operator = "*";
            textBoxInput.Text += (" * ");
        }

        private void buttonSubtract_Click(object sender, EventArgs e)
        {
            firstInt = Convert.ToInt32(textBoxInput.Text);

            Operator = "-";
            textBoxInput.Text += (" - ");
        }

        private void button12_Click(object sender, EventArgs e)
        {
            firstInt = Convert.ToInt32(textBoxInput.Text);
            Operator = "/";
            textBoxInput.Text += (" / ");
        }

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

3 个答案:

答案 0 :(得分:1)

基本上,您不能在此处单独使用int.Parse。您将需要对代码进行逐个字符的检查,并自己决定每个部分是否是值与运算符的一部分 - 基本上构建AST。然后使用计算器实现的优先级规则计算AST。

答案 1 :(得分:0)

尝试更改此行:

secondInt = int.Parse(textBoxInput.Text);

到此:

secondInt = int.Parse(textBoxInput.Text.Split('/', '+', '-', '*')[1].Trim());

看看它是否有帮助。

我试着简单地回答这个问题,深入研究制作一个可靠的计算器应用程序的细节,因为该主题过于宽泛。

答案 2 :(得分:-1)

您可以使用数学表达式Evaluator for .NET,例如:NCalc

Expression e = new Expression("2 + 3 * 5");
Debug.Assert(17 == e.Evaluate());

对于你的情况

Expression e = new Expression(textBoxInput.Text);
labelResult.Text = e.Evaluate().ToString();