Calculator does not detect two-digit numbers

Asked

Viewed 174 times

0

I’m making a calculator in c#, but calculator, I don’t know why, it doesn’t detect numbers with 2 digits, like 11+1 = 3, the calculator is detecting as if it were 1 + 1 + 1. Follow 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 managed to solve the problem of 11 + 1 = 3 now missing the second part of the problem which is 11 + 11 = 13

  • I didn’t understand your problem, and of decimal or sum of values? another where this is happening exactly in your code?

  • @Marconcíliosouza is not quite accurate because it is not a mistake but a bug prolema and that if I do the account for example 11+11 will give 13 is as if 11 +1 +1

  • So what you can do is debug your project and see what’s being done with the values of your variables. It has things like total1 = total1 + total3; that do not know why you add the value of total1 again if the initial value is 0.

  • the value of total 1 is changed when I click + wait a little I’ll give an update on the code

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, replicate via software what we do in the real process.

Its logic does not replicate the actual process of a mathematical operation giving room for bugs, so let’s analyze the process first:

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

  1. We chose the first number
  2. We chose the operation
  3. We chose the second number
  4. We calculate the operation because we already have all the necessary data!

This is the process! Your software cannot run away from it... ie.. it needs to store a number, an operation and the second number!

I see you’re on the right track with minor flaws...

Suggestions for improvement:

  1. Store the first number in a double call variable Numero1
  2. Store the operation as a ENUM, so your code is safer.
  3. Aramzene the second number in a double call variable Numero2
  4. Let only Total variable with name Total
  5. Remove all operations from within the operators' methods, leave only the same!

I think following these steps will solve your problem!

  • You can tell what an Enum is?

  • Enum is an enumeration, which ensures that only those types (based on value) can be accepted, is something very simple, for example: enum Operacoes
{
 Somar,
 Subtrair,
 Dividir,
 Multiplicar
} When you switch, you’ll see q is much better! Just put the variable of the type of this Enum that the visual studio mounts the entire switch for you (typing TAB)! For example, then you create a variable like this Enum: ;Operacoes Operador; To set it: Operador = Operacoes.Multiplicar;

  • Thanks for the help right now I’m making the code all over again the old one this saved

  • Nice @pekira! Good luck and may this little help always lead you in a better analysis process!

  • I was wondering if you could help me with one more thing like I do for when I click on a double numero1 add one more number in the centimeter when I click on a double numero1 adds the number 1 in the sense fe does this click 1 gets 1 click 2 gets 12 click 0 is 120

  • @pekira Can put a picture of Form in the question ?

  • as I add an img to the question

  • how do I do that

  • I’m already in the room

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.