【问题标题】:I have a calculator but need to modify it我有一个计算器,但需要修改它
【发布时间】:2016-05-30 09:44:33
【问题描述】:

我有一个计算器。它工作正常示例在 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)
        {

        }

        }

【问题讨论】:

  • 请仔细阅读how to ask questions at SO site。主要思想 - 您应该提供您的代码并描述您的问题(意外结果或错误)。目前这个问题只描述了你的任务,但没有说明你的具体问题,也没有显示你有问题的任何代码。
  • 您在寻找这样的东西吗? stackoverflow.com/questions/21950093/string-calculator
  • 没有其他问题。 string 转换为 int 计算结果。
  • 感谢我使用 new DataTable().Compute(math, null).ToString();来自 stackoverflow.com/questions/21950093/string-calculator 的方法。非常感谢

标签: c# visual-studio-2012 calculator


【解决方案1】:

您很可能希望创建一个方法,该方法可以接收与输入一样多的十进制值。比如我下面的例子,你可以将输入的数字传递给方法。然后在方法里面调用根据补上的计算方法。我的观点是,您不希望在 UI 中处理业务或数据逻辑,并且由于您有检查、清除和分配给文本框的常见任务,请选择一种可以为您处理的方法。Multiple input method

【讨论】:

    【解决方案2】:

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

    一个好的解析器是ANTLR,它还有一个sample grammar for a calculator。

    【讨论】:

    • 有例子可以看代码吗?我找不到。只有简单的计算
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2019-08-10
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多