简体   繁体   中英

C# Calculator Logic

when I press 9 and then press times the display shows 9 and when I press 9 again it'll add it to the first number (9) instead of clearing the first 9. so it thinks its 9*99. how do I clear the display on the first input after an operation?

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();
        }
    }
}

I think, that's because of you adding

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

You're always adding the 9 to the variable input. Try cleaning input. Before adding a new number.

Add the following to your class Form1 :

bool operationExcecuted = false;

And in your calculate method add the following line:

operationExcecuted = true;

And in every number input check if an operation is excecuted. If so clear the input and add a new number. For example the number 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM