我有一个计算器但需要修改它

时间:2016-02-18 09:44:23

标签: c# visual-studio-2012 calculator

enter image description here

我有一个钙化器。它的工作正常例子在textbox1中输入10-5,在textbox2上显示结果。但我想计算更多。例如8-5 * 3-1这样。或7-2 + 3等。
怎么会这样? 这是我的代码

public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        float ricxvi1, pasuxi;
        int datvla;

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Clear();

           textBox1.Text = textBox1.Text + 7;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox1.Text = textBox1.Text + 0;
            textBox2.Clear();
            textBox2.Text = textBox2.Text + 0;
            datvla = 0; 
        }
        private void button6_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Clear();
            textBox1.Text = textBox1.Text + 4;
        }

        private void button17_Click(object sender, EventArgs e)
        {
                ricxvi1 =  ricxvi1 = float.Parse(textBox1.Text);
                datvla = 1;
                textBox1.Text += "-"; 
        }
        private void button10_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Clear();
            textBox1.Text = textBox1.Text + 1;
        }
        private void button11_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Clear();

            textBox1.Text = textBox1.Text + 2;
        }
        private void button12_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Clear();
            textBox1.Text = textBox1.Text + 3;  
        }

        private void mimateba_Click_1(object sender, EventArgs e)
        {
             ricxvi1 = float.Parse(textBox1.Text);

           datvla = 2;
           textBox1.Text += "+";
        }
        private void button7_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Clear();
            textBox1.Text = textBox1.Text + 5;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Clear();
            textBox1.Text = textBox1.Text + 6;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            ricxvi1 = float.Parse(textBox1.Text);

            datvla = 3;
            textBox1.Text += "*";

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Clear();
             textBox1.Text = textBox1.Text + 8;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
                textBox1.Clear();
             textBox1.Text = textBox1.Text + 9;
        }
        private void button9_Click(object sender, EventArgs e)
        {
            ricxvi1 = float.Parse(textBox1.Text);
            datvla = 4;
            textBox1.Text += "/";
        }

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

        private void button16_Click_1(object sender, EventArgs e)
        {

            switch (datvla)
            {
                case 1:

                    pasuxi = ricxvi1 - float.Parse(textBox1.Text.Substring(textBox1.Text.Length - 1));

                    textBox2.Text = pasuxi.ToString();
                    break;
                case 2:
                    pasuxi = ricxvi1 + float.Parse(textBox1.Text.Substring(textBox1.Text.Length - 1));
                    textBox2.Text = pasuxi.ToString();
                    break;
                case 3:
                    pasuxi = ricxvi1 * float.Parse(textBox1.Text.Substring(textBox1.Text.Length - 1));
                    textBox2.Text = pasuxi.ToString();
                    break;
                case 4:
                    pasuxi = ricxvi1 / float.Parse(textBox1.Text.Substring(textBox1.Text.Length - 1));
                    textBox2.Text = pasuxi.ToString();
                    break;
                default:
                    break;
            }
        }

        private void button15_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")

                textBox1.Clear();
            textBox1.Text = textBox1.Text + ".";
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        }

2 个答案:

答案 0 :(得分:0)

您必须构建一个解析器来理解完成的计算。正则表达式或手动解析非常困难,可能比使用解析器更难。

一个好的解析器是ANTLR,它也有一个ample grammar for a calculator

答案 1 :(得分:0)

您最有可能想要创建一个方法,该方法接收输入的十进制值。比如下面的例子,你可以将输入的数字传递给方法。然后在方法调用的基础上调用计算方法。我的观点是,您不希望在UI中处理业务或数据逻辑,并且您有一个检查,清除和分配到文本框的常见任务,请找一个能够为您处理的方法。{ {3}}