C#计算器逻辑

本文关键字:计算器 | 更新日期: 2023-09-27 18:19:42

当我按9,然后按次时,显示屏显示9,当我再次按9时,它会将其添加到第一个数字(9)中,而不是清除第一个9。所以它认为它是9*99。如何在操作后第一次输入时清除显示?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        string input = string.Empty;
        string operand1 = string.Empty;
        string operand2 = string.Empty;
        char operation;
        double result = 0.0;
        public Form1()
        {
            InitializeComponent();
        }
        private void Multiply_Click(object sender, EventArgs e)
        {
            operation = '*';
            if (operand1 != "")
            {
                calculate();
            }
            else
            {
                operand1 = input;
            }
        }
        private void Minus_Click(object sender, EventArgs e)
        {
            operation = '-';
            if (operand1 != "")
            {
                calculate();
            }
            else
            {
                operand1 = input;
            }
        }
        private void Addition_Click(object sender, EventArgs e)
        {
            operation = '+';
            if (operand1 != "")
            {
                calculate();
            }
            else
            {
                operand1 = input;
            }
        }
        private void Divide_Click(object sender, EventArgs e)
        {
            operation = '/';
            if (operand1 != "")
            {
                calculate();
            }
            else
            {
                operand1 = input;
            }
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
        }
        private void five_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "";
            input += "5";
            this.textBox1.Text += input;
        }
        private void Zero_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "";
            input += "0";
            this.textBox1.Text += input;
        }
        private void one_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "";
            input += "1";
            this.textBox1.Text += input;
        }
        private void two_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "";
            input += "2";
            this.textBox1.Text += input;
        }
        private void three_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "";
            input += "3";
            this.textBox1.Text += input;
        }
        private void four_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "";
            input += "4";
            this.textBox1.Text += input;
        }
        private void six_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "";
            input += "6";
            this.textBox1.Text += input;
        }
        private void seven_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "";
            input += "7";
            this.textBox1.Text += input;
        }
        private void eight_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "";
            input += "8";
            this.textBox1.Text += input;
        }
        private void nine_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "";
            input += "9";
            this.textBox1.Text += input;
        }
        private void Equal_Click(object sender, EventArgs e)
        {
            calculate(); 
        }
        private void Decimal_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "";
            input += ".";
            this.textBox1.Text += input;
        }
        private void Clear_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
        }
        public void calculate()
        {
            Console.WriteLine("performing operation" + operand1 + " " + operation + " " + operand2 + "= ???" + "the input is?" + input);
            operand2 = input;
            double num1, num2;
            double.TryParse(operand1, out num1);
            double.TryParse(operand2, out num2);
            if (operation == '*')
            {
                result = num1 * num2;    
            }
            if (operation == '-')
            {
                result = num1 * num2;
            }
            if (operation == '+')
            {
                result = num1 * num2;
            }
            if (operation == '/')
            {
                result = num1 * num2;
            }
            textBox1.Text = result.ToString();
            input = result.ToString();
        }
    }
}

C#计算器逻辑

将以下内容添加到您的类Form1:

bool operationExcecuted = false;

calculate方法中添加以下行:

operationExcecuted = true;

在每一个数字输入中,都要检查一个运算是否被超越。如果是,请清除输入并添加一个新数字。例如数字9:

private void nine_Click(object sender, EventArgs e)
{
    if(operationExcecuted)
    {
        input = String.Empty;
        operationExcecuted = false;
    }
    this.textBox1.Text = "";
    input += "9";
    this.textBox1.Text += input;
}

我想,那是因为你添加了

input += "9"
this.textBox1.text += input;

您总是将9添加到变量输入中。尝试清理输入。添加新编号之前。