c#做的计算器怎么避免小数点以及计算符号被连续输入

c#做的计算器怎么避免小数点以及计算符号被连续输入!!
代码如下

using System;
using System.IO;
using System.Windows.Forms;
//using static System.Windows.Forms.VisualStyles.VisualStyleElement;


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

        public Form1()
        {
            InitializeComponent();
        }
        private double currentResult; //临时结果
        private string lastOperator;//上一次的操作


        private void btn1_Click(object sender, EventArgs e)
        {
            textBox.Text += 1;
        }

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

        private void btn2_Click(object sender, EventArgs e)
        {
            textBox.Text += 2;
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            textBox.Text += 3;
        }

        private void btn4_Click(object sender, EventArgs e)
        {
            textBox.Text += 4;
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            textBox.Text += 5;
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            textBox.Text += 6;
        }

        private void btn7_Click(object sender, EventArgs e)
        {
            textBox.Text += 7;
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            textBox.Text += 8;
        }

        private void btn9_Click(object sender, EventArgs e)
        {
            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();
        }

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

            textBox.Text = textBox.Text + " " + btn.Text + " ";

        }

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

            textBox.Text = textBox.Text + " " + btn.Text + " ";
        }

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

            textBox.Text = textBox.Text + " " + btn.Text + " ";
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void btn_Point_Click(object sender, EventArgs e)
        {
            textBox.Text += '.';



        }

        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)
        {
            System.Windows.Forms.Button btn = (System.Windows.Forms.Button)sender;

            textBox.Text = textBox.Text + " " + btn.Text + " ";
            {
                string add = "";
               
            }
        }

        private void textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            //if (!(Char.IsNumber(e.KeyChar) || (e.KeyChar == '.' && (!((TextBox)sender).Text.Trim().Equals(string.Empty)) && ((TextBox)sender).Text.Trim().IndexOf('.') < 1)))
            //{
            //    e.Handled = true;
            //}


        }


    }

        }
    



private void btn_Point_Click(object sender, EventArgs e)
        {
if (!textBox.Contains("."))
            textBox.Text += '.';
        }

看样子你整个算式是放在一个文本框里的,那判断是否已存在小数点就不行了,因为可以输入两个小数;
而且正常都是输入完成开始计算后再进行算式检验,检验有误则报错,让用户修改或重新输入;
所以你应该先把算式的数值与符号拆分,判断数值是否有效,是否是 数值与符号 交替出现;

【以下回答由 GPT 生成】

要避免在C#计算器中连续输入小数点和运算符号,可以进行以下修改:

  1. 添加一个bool类型的变量isDecimalEntered,用于标记是否已经输入了小数点。
  2. 添加一个bool类型的变量isOperatorEntered,用于标记是否已经输入了运算符号。
  3. 在每个数字按钮的点击事件处理方法中,在追加数字到文本框之前,先检查isOperatorEntered的值。如果为true,则将文本框内容清空,再追加数字。
  4. 在小数点按钮的点击事件处理方法中,先判断isDecimalEntered的值。如果为false,则追加小数点并将isDecimalEntered设置为true,否则不操作。
  5. 在运算符号按钮的点击事件处理方法中,先将isOperatorEntered设置为true,再将isDecimalEntered设置为false

以下是修改后的代码:

private bool isDecimalEntered = false; // 标记是否输入了小数点
private bool isOperatorEntered = false; // 标记是否输入了运算符号

// ...

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

// ...

private void btnDecimal_Click(object sender, EventArgs e)
{
    if (!isDecimalEntered)
    {
        textBox.Text += ".";
        isDecimalEntered = true;
    }
}

// ...

private void btnOperator_Click(object sender, EventArgs e)
{
    isOperatorEntered = true;
    isDecimalEntered = false;
    // 获取运算符号,并进行相应的计算
}

// ...

这样修改之后,连续输入小数点和运算符号的问题应该得到解决了。



【相关推荐】



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