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?
– Marco Souza
@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
– pekira
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.– Marco Souza
the value of total 1 is changed when I click + wait a little I’ll give an update on the code
– pekira