C#计算器 怎么实现连续计算

C#计算器 怎么实现连续计算
例如:首先计算1+1,再点击-,会先输出2,在执行-的操作

已设计的代码如下:

using System;
using System.IO;
using System.Threading;
using System.Windows.Forms;




namespace 计算器demo2
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }
        private double currentResult; //临时结果
        private string lastOperator;//上一次的操作
        private bool isDecimalEntered = false; // 标记是否输入了小数点
        private bool isOperatorEntered = false; // 标记是否输入了运算符号


        private void btn1_Click(object sender, EventArgs e)
        {
            if (isOperatorEntered)//先检查isOperatorEntered的值。如果为true,则将文本框内容清空,再追加数字。0-9相同

            {
                textBox.Text = "";//清空文本框内容
                isOperatorEntered = false;
            }
            textBox.Text += "1";
        }

        private void btn0_Click(object sender, EventArgs e)
        {
            if (isOperatorEntered)

            {
                textBox.Text = "";//清空文本框内容
                isOperatorEntered = false;
            }
            textBox.Text += "0";
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            if (isOperatorEntered)

            {
                textBox.Text = "";//清空文本框内容
                isOperatorEntered = false;
            }
            textBox.Text += "2";
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            if (isOperatorEntered)

            {
                textBox.Text = "";//清空文本框内容
                isOperatorEntered = false;
            }
            textBox.Text += "3";
        }

        private void btn4_Click(object sender, EventArgs e)
        {
            if (isOperatorEntered)

            {
                textBox.Text = "";//清空文本框内容
                isOperatorEntered = false;
            }
            textBox.Text += "4";
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            if (isOperatorEntered)

            {
                textBox.Text = "";//清空文本框内容
                isOperatorEntered = false;
            }
            textBox.Text += "5";
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            if (isOperatorEntered)

            {
                textBox.Text = "";//清空文本框内容
                isOperatorEntered = false;
            }
            textBox.Text += "6";
        }

        private void btn7_Click(object sender, EventArgs e)
        {
            if (isOperatorEntered)

            {
                textBox.Text = "";//清空文本框内容
                isOperatorEntered = false;
            }
            textBox.Text += "7";
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            if (isOperatorEntered)

            {
                textBox.Text = "";//清空文本框内容
                isOperatorEntered = false;
            }
            textBox.Text += "8";
        }
        private void btn9_Click(object sender, EventArgs e)
        {
            if (isOperatorEntered)

            {
                textBox.Text = "";//清空文本框内容
                isOperatorEntered = false;
            }
            textBox.Text += "9";
        }

        private void btn_Clear_Click(object sender, EventArgs e)
        {
            textBox.Text = "";//修改显示为kong,实现清楚
            txtResult.Text = "";
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            textBox.Text = textBox.Text + " " + btn.Text + " ";
            {
                btnAdd.Enabled = false;
            }



        }

        private void btn_Compute_Click(object sender, EventArgs e)
        {
            Single r; //r用于保存结果值

            string t = textBox.Text; //t用于保存文本框中的算术表达式

            int space = t.IndexOf(" "); //用于搜索空格位置

            string s1 = t.Substring(0, space); //s1用于保存第一个运算数

            char op = Convert.ToChar(t.Substring(space + 1, 1)); //op用于保存运算符

            string s2 = t.Substring(space + 3); //s2用于保存第二个运算数

            Single arg1 = Convert.ToSingle(s1);

            Single arg2 = Convert.ToSingle(s2);//Convert.ToSingle方法将字符串或者其他可转换为数字的对象变量转换为float类型

            switch (op)

            {

                case '+':

                    r = arg1 + arg2;

                    break;

                case '-':

                    r = arg1 - arg2;

                    break;

                case '*':

                    r = arg1 * arg2;

                    break;

                case '/':

                    if (arg2 == 0)

                    {

                        MessageBox.Show("0不能作为除数!");
                        return;



                    }

                    else

                    {

                        r = arg1 / arg2;

                        break;

                    }



                default:

                    throw new ApplicationException();

            }



            txtResult.Text = r.ToString();
            isOperatorEntered = true;
            isDecimalEntered = false;

        }

        private void btn_Cut_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            if (!isDecimalEntered)
            {
                textBox.Text = textBox.Text + " " + btn.Text + " ";
                isDecimalEntered = true;
            }

        }


        private void btnMul_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            if (!isDecimalEntered)
            {
                textBox.Text = textBox.Text + " " + btn.Text + " ";
                isDecimalEntered = true;
            }
        }

        private void btnDiv_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            if (!isDecimalEntered)
            {
                textBox.Text = textBox.Text + " " + btn.Text + " ";
                isDecimalEntered = true;
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void btn_Point_Click(object sender, EventArgs e)
        {
            //textBox.Text += '.';
            if (textBox.Text.IndexOf(".") < 0)
            {
                textBox.Text = textBox.Text + ".";
            }
            //if (!isDecimalEntered)
            //{
            //    textBox.Text += ".";
            //    isDecimalEntered = true;
            //}





        }

        private void btn_Remove_Click(object sender, EventArgs e)
        {
            string old = textBox.Text;//取得当前的数据
            if (old.Length > 0)
            textBox.Text = old.Remove(old.Length - 1);//判断长度先,删掉最后一个字符
        }

        private void txtResult_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (MessageBox.Show("计算机Demo", "", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) ;
        }


        private void process1_Exited(object sender, EventArgs e)
        {

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dr = MessageBox.Show("确认退出吗", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (dr == DialogResult.Yes)
            {
                e.Cancel = false;
            }
            else
            {
                e.Cancel = true;
            }
        }

        private void textBox_TextChanged(object sender, EventArgs e)
        {
            //if (textBox.Text.StartsWith("."))
            //    textBox.Text = textBox.Text.Remove(0, 1);
            //else if (textBox.Text.IndexOf('.') != textBox.Text.LastIndexOf('.'))
            //    MessageBox.Show("你输入了两个点!!");

            //输入两个点提示(目前有解决可以输入两个点问题,此代码注释)

        }

        private void btn_Point_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            if (!isDecimalEntered)
            {
                textBox.Text = textBox.Text + " " + btn.Text + " ";
                isDecimalEntered = true;
            }
            {
                string add = "";

            }

            





        }

        
       
    }
}
    



状态机,后缀表达式。
虽然这样说你大概率不想看。但你要想最简单的完成这个最方便。
好了,我也不打算写代码。就直接说过程了,你明白就自己写。
需要使用的数据结构 Stack 栈

好了:先设置两个栈。一个操作符栈,一个操作数栈
我们按顺序讲

  1. 输入1,压1入操作数栈
  2. 输入+,压+入操作符栈
  3. 输入2,压2 入操作数栈
  4. 输入-,先尝试从栈顶获取前面的操作符+,同时压-入操作符栈
  5. 如果4获取到前面的操作符号+,调用Add方法
  6. add方法自己知道自己需要两个操作数,就取操作数栈弹出2个操作数a,b,同时执行a+b,同时把a+b的结果压入操作数栈

后续都是这样的顺序,我就不往下写了

你这代码槽点太多;
小数点这样处理的话1.1+1.1要怎么算?
连续计算的话在输入运算符时判断有运算符就计算一次结果再把运算符加上去就好


 private void btn_Cut_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            if(isDecimalEntered)
            {
                btn_Compute_Click(null,null)
                textBox.Text = textBox.Text + " " + btn.Text + " ";
            }
            if (!isDecimalEntered)
            {
                textBox.Text = textBox.Text + " " + btn.Text + " ";
                isDecimalEntered = true;
            }
 
        }

【以下回答由 GPT 生成】

问题解答:

首先,根据给出的代码,我看到你实现了一个简单的计算器窗体应用程序,用于进行简单的加减乘除操作。根据你的描述,你想要实现在计算过程中连续进行多个操作。我会对你的代码进行优化,以实现这个功能。

我会对以下几个方面进行优化:

**1. 提取公共逻辑**
你的代码中,针对每个数字按钮的点击事件处理函数都有相似的逻辑,可以提取成一个公共函数,以减少重复的代码。你可以创建一个名为"btnNumber_Click"的函数,并将TextView的值传递给这个函数。

```c#
private void btnNumber_Click(object sender, EventArgs e)
{
    Button button = (Button)sender;
    if (isOperatorEntered)
    {
        textBox.Text = ""; // 清空文本框内容
        isOperatorEntered = false;
    }
    textBox.Text += button.Text;
}

然后,在Form1的构造函数中,对每个数字按钮的Click事件绑定这个公共函数,例如:

btn1.Click += btnNumber_Click;
btn2.Click += btnNumber_Click;
...

2. 用一个变量来跟踪当前运算符 你的代码中,每次点击操作符按钮时都需要跟踪上一次的操作符,可以将这个操作符保存在一个变量中,不需要定义一个变量来保存上一次的操作符。在btnOperator_Click事件处理函数中,可以使用一个全局的operatorChar变量来保存当前的运算符。

private char operatorChar; // 用于保存当前运算符

private void btnOperator_Click(object sender, EventArgs e)
{
    Button operatorButton = (Button)sender;
    operatorChar = operatorButton.Text[0];
    isOperatorEntered = true;
    isDecimalEntered = false;

    // 计算结果
    switch (lastOperator)
    {
        case "+":
            currentResult += double.Parse(textBox.Text);
            break;
        case "-":
            currentResult -= double.Parse(textBox.Text);
            break;
        case "*":
            currentResult *= double.Parse(textBox.Text);
            break;
        case "/":
            currentResult /= double.Parse(textBox.Text);
            break;
        default:
            currentResult = double.Parse(textBox.Text);
            break;
    }

    textBox.Text = currentResult.ToString();

    lastOperator = operatorButton.Text;
}

需要注意的是,在首次点击运算符按钮时,我们需要将当前的textBox的文本解析为一个double类型,并赋值给currentResult变量。

3. 连续运算处理 你的代码中,每次按下一个数字按钮之后,都会清空textBox文本框中的内容,这样会使得用户无法进行连续计算。可以将文本框中的内容保存在一个临时变量中,然后进行连续运算,并在运算结束后将结果显示在文本框中。

private string currentText = ""; // 用于保存当前的文本框内容

private void btnNumber_Click(object sender, EventArgs e)
{
    Button button = (Button)sender;
    currentText += button.Text;
    textBox.Text = currentText;
}

然后,在btnOperator_Click事件处理函数中,将当前的文本框内容保存到currentText变量中,并在计算结束后将结果显示在文本框中:

private void btnOperator_Click(object sender, EventArgs e)
{
    // ...
    currentText = textBox.Text;
    // ...
    textBox.Text = currentResult.ToString();

    lastOperator = operatorButton.Text;
}

这样,用户就可以在连续进行多个操作时,保留之前的计算结果,并在结果文本框中显示。

这样,我对你的代码进行了一些优化,以实现在计算过程中连续进行多个操作。希望对你有帮助。如果你有任何问题,请随时与我联系。



【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^