首页 > 解决方案 > 如何修复减法、乘法和除法 C# 的代码

问题描述

我正在做一个计算器。当我输入两个数字并按相等时,我创建了代码,它完成了它的工作,例如减法、加法、乘法和除法。帮助我制作连续减法、乘法和除法的代码,而不用按相等加法我已经完成了代码,请帮我做按钮。我将在下面包含整个代码,请帮助我修复它。

namespace WindowsFormsApp1
{
    public partial class Windows10Calcoo : Form
    {
        string Input, NumStore1, NumStore2 = string.Empty ;
        char Operation;
        double Answ;
        public Windows10Calcoo()
        {
            InitializeComponent();
        }

    private void PlusBtn_Click(object sender, EventArgs e)
    {

        //Creating + and Storing Num1
        NumStore1 = Input;
        Operation = '+';
        Input = string.Empty;

        //Storing Number 2
        NumStore2 = Input;
        double num1, num2;
        double.TryParse(NumStore1, out num1);
        double.TryParse(NumStore2, out num2);

        // Adding Code
        Answ += num1 + num2;
        textBox1.Text = Answ.ToString();



    }

    private void SubBtn_Click(object sender, EventArgs e)
    {
        NumStore1 = Input;
        Operation = '-';
        Input = string.Empty;

        //Storing Number 2
        NumStore2 = Input;
        double num1, num2;
        double.TryParse(NumStore1, out num1);
        double.TryParse(NumStore2, out num2);

        // Adding Code
        Answ += num1 - num2 ;
        textBox1.Text = Answ.ToString();
    }

    private void DivideBtn_Click(object sender, EventArgs e)
    {
        NumStore1 = Input;
        Operation = '/';
        Input = string.Empty;

    }

    private void MultiBtn_Click(object sender, EventArgs e)
    {
        NumStore1 = Input;
        Operation = '*';
        Input = string.Empty;

    }

    private void BackSpaceBtn_Click(object sender, EventArgs e)
    {
        int textlength = textBox1.Text.Length;
        textBox1.Text = textBox1.Text.Substring(0, textlength - 1);
    }

    private void OneBtn_Click(object sender, EventArgs e)
    {
        this.textBox1.Text = "";
        Input += "1";
        this.textBox1.Text += Input;
    }

    private void TwoBtn_Click(object sender, EventArgs e)
    {
        this.textBox1.Text = "";
        Input += "2";
        this.textBox1.Text += Input;
    }

    private void ThreeBtn_Click(object sender, EventArgs e)
    {
        this.textBox1.Text = "";
        Input += "3";
        this.textBox1.Text += Input;
    }

    private void FourBtn_Click(object sender, EventArgs e)
    {
        this.textBox1.Text = "";
        Input += "4";
        this.textBox1.Text += Input;
    }

    private void FiveBtn_Click(object sender, EventArgs e)
    {
        this.textBox1.Text = "";
        Input += "5";
        this.textBox1.Text += Input;
    }

    private void SixBtn_Click(object sender, EventArgs e)
    {
        this.textBox1.Text = "";
        Input += "6";
        this.textBox1.Text += Input;
    }

    private void SevenBtn_Click(object sender, EventArgs e)
    {
        this.textBox1.Text = "";
        Input += "7";
        this.textBox1.Text += Input;
    }

    private void EightBtn_Click(object sender, EventArgs e)
    {
        this.textBox1.Text = "";
        Input += "8";
        this.textBox1.Text += Input;
    }

    private void NineBtn_Click(object sender, EventArgs e)
    {
        this.textBox1.Text = "";
        Input += "9";
        this.textBox1.Text += Input;
    }

    private void ZeroBtn_Click(object sender, EventArgs e)
    {
        this.textBox1.Text = "";
        Input += "0";
        this.textBox1.Text += Input;
    }

    private void DecimalBtn_Click(object sender, EventArgs e)
    {
        this.textBox1.Text = "";
        Input += ".";
        this.textBox1.Text += Input;
    }

    private void ClearBtn_Click(object sender, EventArgs e)
    {
        Input += "C";

        this.textBox1.Text = "";
        this.Input = string.Empty;
        this.NumStore1 = string.Empty;
        this.NumStore2 = string.Empty;
        Answ = 0;

    }

    private void EqualBtn_Click(object sender, EventArgs e)
    {

        NumStore2 = Input;
        double num1, num2;
        double.TryParse(NumStore1, out num1);
        double.TryParse(NumStore2, out num2);


        this.textBox1.Text = "";
        this.Input = string.Empty;
        this.NumStore1 = string.Empty;
        this.NumStore2 = string.Empty;

        if (Operation == '+')
        {
            Answ = num1 + num2;
            textBox1.Text = Answ.ToString();
        }
        else if (Operation == '-')
        {
            Answ = num1 - num2;
            textBox1.Text = Answ.ToString();
        }
        else if (Operation == '*')
        {
            Answ = num1 * num2;
            textBox1.Text = Answ.ToString();
        }
        else if (Operation == '/')
        {
            if (num2 != 0)
            {
                Answ = num1 / num2;
                textBox1.Text = Answ.ToString();
            }
            else
            {
                textBox1.Text = "DIV/Zero!";
            }
        }

    }
    private void Windows10Calcoo_Load(object sender, EventArgs e)
    {

    }
}

}

对于减法按钮,当我使用 [Answ += num1 - num2 ;] 时,它的不减法继续添加,如果我使用 [Answ -= num1 - num2 ;],它的加法是负数。

标签: c#

解决方案


推荐阅读