Calculator does not detect double-digit numbers

0

I'm doing a calculator in C #, but calculator, I do not know why, it does not detect numbers with 2 digits, like 11 + 1 = 3, the calculator is detecting as 1 + 1 + 1. the code:

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 Hello_World
{
    public partial class Calculadora : Form
    {
        public Calculadora()
        {
            InitializeComponent();
        }

        #region "Strings Doubles Bools"
        double total1 = 0;
        double total2 = 0;
        double total3 = 0;
        string theOperator = "";
        bool plusButtonClicked = false;
        bool minusButtonClicked = false;
        bool divideButtonClicked = false;
        bool multiplyButtonClicked = false;
#endregion

        #region "Função btn"
        //Função do btn de 0
        private void btn0_Click(object sender, EventArgs e)
        {
            TextDisplay.Text = TextDisplay.Text + btn0.Text;
        }

        //Função do btn de 1
        private void btn1_Click(object sender, EventArgs e)
        {
            TextDisplay.Text = TextDisplay.Text + btn1.Text;
        }

        //Função do btn de 2
        private void btn2_Click(object sender, EventArgs e)
        {
            TextDisplay.Text = TextDisplay.Text + btn2.Text;
        }

        //Função do btn de 3
        private void btn3_Click(object sender, EventArgs e)
        {
            TextDisplay.Text = TextDisplay.Text + btn3.Text;
        }

        //Função do btn de 4
        private void btn4_Click(object sender, EventArgs e)
        {
            TextDisplay.Text = TextDisplay.Text + btn4.Text;
        }

        //Função do btn de 5
        private void btn5_Click(object sender, EventArgs e)
        {
            TextDisplay.Text = TextDisplay.Text + btn5.Text;
        }

        //Função do btn de 6
        private void btn6_Click(object sender, EventArgs e)
        {
            TextDisplay.Text = TextDisplay.Text + btn6.Text;
        }

        //Função do btn de 7
        private void btn7_Click(object sender, EventArgs e)
        {
            TextDisplay.Text = TextDisplay.Text + btn7.Text;
        }

        //Função do btn de 8
        private void btn8_Click(object sender, EventArgs e)
        {
            TextDisplay.Text = TextDisplay.Text + btn8.Text;
        }

        //Função do btn de 9
        private void btn9_Click(object sender, EventArgs e)
        {
            TextDisplay.Text = TextDisplay.Text + btn9.Text;
        }

        //Função do btn de Equal
        private void btnClear_Click(object sender, EventArgs e)
        {
            Clear();
        }


        //Função do btn de Plus
        private void btnplus_Click(object sender, EventArgs e)
        {
            Plus();
        }

        //Função do btn de Equal
        private void btnequal_Click(object sender, EventArgs e)
        {
            Equal();
        }

        //Função do btn de Less
        private void btnless_Click(object sender, EventArgs e)
        {
            Less();
        }

        //Função do btn de Divisão
        private void btnDivisçao_Click(object sender, EventArgs e)
        {
            Divisão();
        }

        //Função do btn de Multiplicação
        private void bntMulti_Click(object sender, EventArgs e)
        {
            Multi();
        }

        //Função do btn virugla
        private void btnVirgula_Click(object sender, EventArgs e)
        {
            TextDisplay.Text = TextDisplay.Text + btnVirgula.Text;
        }

        //Quando a calculadora carregar
        private void Calculadora_Load(object sender, EventArgs e)
        {
            TextDisplay.Focus();
        }
#endregion

        #region "Public Voids"
        //Função Equal
        public void Equal()
        {
            switch (theOperator)
            {
                case "+":
                    total2 = total1 + double.Parse(total3.ToString());
                    break;
                case "-":
                    total2 = total1 - double.Parse(total3.ToString());
                    break;
                case "/":
                    total2 = total1 / double.Parse(total3.ToString());
                    break;
                case "*":
                    total2 = total1 * double.Parse(total3.ToString());
                    break;
                default:
                    MessageBox.Show("Este simbolo não é reconhecido", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
            }
            TextDisplay.Text = TextDisplay.Text + "\r\n" + total2.ToString();
            total1 = 0;
        }

        //Função Plus
        public void Plus()
        {

            if (total3 != 0)
            {
                total1 = total1 + total3;
                total3 = 0;
            }
            //else
            //    total1 = total1 + double.Parse(TextDisplay.Text);


            TextDisplay.Text = TextDisplay.Text + " + ";

            plusButtonClicked = true;
            minusButtonClicked = false;
            divideButtonClicked = false;
            multiplyButtonClicked = false;
            theOperator = "+";
        }

        //Função Multi
        public void Multi()
        {
            if (total3 != 0)
            {
                total1 = total1 * total3;
                total3 = 0;
            }
            TextDisplay.Text = TextDisplay.Text + " * ";

            plusButtonClicked = false;
            minusButtonClicked = false;
            divideButtonClicked = false;
            multiplyButtonClicked = true;
            theOperator = "*";
        }

        //Função Divisão
        public void Divisão()
        {
            total1 = total1 + double.Parse(TextDisplay.Text);
            TextDisplay.Text = TextDisplay.Text + " / ";

            plusButtonClicked = false;
            minusButtonClicked = false;
            divideButtonClicked = true;
            multiplyButtonClicked = false;
            theOperator = "/";
        }

        //Função Less
        public void Less()
        {
            total1 = total1 + double.Parse(TextDisplay.Text);
            TextDisplay.Text = TextDisplay.Text + " - ";

            plusButtonClicked = false;
            minusButtonClicked = true;
            divideButtonClicked = false;
            multiplyButtonClicked = false;
            theOperator = "-";
        }

        //Função Clear
        public void Clear()
        {
            TextDisplay.Clear();

        }

        //Funcção de deteção de presionamento de teclas
        public void Detect(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 48 || e.KeyChar == 49 || e.KeyChar == 50 || e.KeyChar == 51 || e.KeyChar == 52 || e.KeyChar == 53 || e.KeyChar == 54 || e.KeyChar == 55 || e.KeyChar == 56 || e.KeyChar == 57 || e.KeyChar == 58)
            {
                TextDisplay.Text = TextDisplay.Text + e.KeyChar.ToString();

                if(total1 != 0)
                    total3 = Convert.ToDouble(e.KeyChar.ToString());
                else
                    total1 = total1 + double.Parse(TextDisplay.Text);
                if (total1 != 0)
                { 

                }
                }

            //Detetor da tecla Equal
            if (e.KeyChar == 61)
            {

                Equal();
            }

            //Detetor da tecla Multi
            if (e.KeyChar == 42)
            {
                Multi();
            }

            //Detetor da tecla Plus
            if (e.KeyChar == 43)
            {
                total1 = Convert.ToDouble(TextDisplay.Text);
                Plus();
            }

            //Detetor da tecla Divisão
            if (e.KeyChar == 47)
            {
                Divisão();
            }

            //Detetor da tecla  less
            if (e.KeyChar == 45)
            {
                Less();
            }

            //DetetoR da tecla backspace
            if (e.KeyChar == 8)
            {
                if (TextDisplay.Text == "")
                {
                    System.Media.SystemSounds.Beep.Play();
                }
                else
                {
                    if (TextDisplay.Text.Length == 1)
                    {
                        TextDisplay.Text = "";
                    }
                    else
                    {
                        TextDisplay.Text = TextDisplay.Text.Substring(0, TextDisplay.Text.Length - 1);
                    }
                }
            }

            //Detetor da tecla enter
            if (e.KeyChar == 13)
            {
                Equal();
            }

            //Detetor da tecla Esc
            if (e.KeyChar == 27)
            {

                Clear();
            }

            //Detetor da tecla virgula ou ponto
            if (e.KeyChar == 46 || e.KeyChar == 44)
            {
                TextDisplay.Text = TextDisplay.Text + e.KeyChar.ToString();
            }
        }

        #endregion

        #region "Deteção de teclas"
        //Deteção de presionamento de teclas text Display
        private void TextDisplay_KeyPress(object sender, KeyPressEventArgs e)
        {
            Detect(sender, e);
        }
        #region "Numeros de 0 a 9"
        //Detecão de presionamento de teclas no btn 0
        private void btn0_KeyPress(object sender, KeyPressEventArgs e)
        {
            Detect(sender, e);
        }

        //Detecão de presionamento de teclas no btn 1
        private void btn1_KeyPress(object sender, KeyPressEventArgs e)
        {
            Detect(sender, e);
        }

        //Detecão de presionamento de teclas no btn 2
        private void btn2_KeyPress(object sender, KeyPressEventArgs e)
        {
            Detect(sender, e);
        }

        //Detecão de presionamento de teclas no btn 3
        private void btn3_KeyPress(object sender, KeyPressEventArgs e)
        {
            Detect(sender, e);
        }

        //Detecão de presionamento de teclas no btn 4
        private void btn4_KeyPress(object sender, KeyPressEventArgs e)
        {
            Detect(sender, e);
        }

        //Detecão de presionamento de teclas no btn 5
        private void btn5_KeyPress(object sender, KeyPressEventArgs e)
        {
            Detect(sender, e);
        }

        //Detecão de presionamento de teclas no btn 6
        private void btn6_KeyPress(object sender, KeyPressEventArgs e)
        {
            Detect(sender, e);
        }

        //Detecão de presionamento de teclas no btn 7
        private void btn7_KeyPress(object sender, KeyPressEventArgs e)
        {
            Detect(sender, e);
        }

        //Detecão de presionamento de teclas no btn 8
        private void btn8_KeyPress(object sender, KeyPressEventArgs e)
        {
            Detect(sender, e);
        }

        //Detecão de presionamento de teclas no btn 9
        private void btn9_KeyPress(object sender, KeyPressEventArgs e)
        {
            Detect(sender, e);
        }
        #endregion

        #region "Btns de Operações"
        //Deteção de percinamento da tecla +
        private void btnplus_KeyPress(object sender, KeyPressEventArgs e)
        {
            Detect(sender, e);
        }

        //Deteção de percinamento da tecla -
        private void btnless_KeyPress(object sender, KeyPressEventArgs e)
        {
            Detect(sender, e);
        }

        //Deteção de percinamento da tecla /
        private void btnDivisçao_KeyPress(object sender, KeyPressEventArgs e)
        {
            Detect(sender, e);
        }

        //Deteção de percinamento da tecla *
        private void bntMulti_KeyPress(object sender, KeyPressEventArgs e)
        {
            Detect(sender, e);
        }

        //Deteção de percinamento da tecla =
        private void btnequal_KeyPress(object sender, KeyPressEventArgs e)
        {
            Detect(sender, e);
        }

        //Deteção de percinamento da tecla esc
        private void btnClear_KeyPress(object sender, KeyPressEventArgs e)
        {
            Detect(sender, e);
        }

        //Deteção de percionamento da tecla ,
        private void btnVirgula_KeyPress(object sender, KeyPressEventArgs e)
        {
            Detect(sender, e);
        }
        #endregion
        #endregion
    }
}

As time passed and I tried to solve my code I was able to solve the problem of 11 + 1 = 3 now the second part of the problem is missing 11 + 11 = 13

    
asked by anonymous 03.08.2016 / 12:11

1 answer

3

Friend, frankly I think the approach is wrong with your software. I believe that you should consider for the development of any software the computerization of the process, and for that, to replicate via software what we do in the actual process.

Your logic does not replicate the actual process of a mathematical operation, giving scope for bugs, so let's look at the process first:

When we perform any mathematical operation (via physical calculator or paper) what we do:

  • We chose the first number
  • We chose the operation
  • We chose the second number
  • We calculate the operation because we already have all the necessary data!
  • This is the process! Your software can not escape it ... ie it needs to store a number, an operation and the second number!

    I see you're on the right path with minor glitches ...

    Suggestions for improvement:

  • Store the first number in a double variable called Number1
  • Store the operation as an ENUM , so your code is safer.
  • Aramzene the second number in a double variable called Number2
  • Leave just the Total variable with the name Total
  • Remove all operations from within the operators methods, just leave the same!
  • I think following these steps will solve your problem!

        
    03.08.2016 / 14:44