Lógica em uma Calculadora estilo Windows

05/08/2008

0

Boa noite pessoal,

Seguinte comecei a desenvolver uma calculadora no estilo Windows no Visual Studio 2008 usando o template WPF e estou com o seguinte problema de lógica :

A calculadora faz as operacoes básicas normalmente mas se eu desejo realizar outra operacao em cima do resultado não dá certo.

Alguem tem uma ideia de como é a lógica para esse problema?
Segue o código abaixo para ver como estou fazendo até então

Assim que eu terminar essa calculadora vou criar um tutorial pro pessoal iniciante assim como eu.

Obrigado

public partial class Window1 : Window
{
string varA, varB;
string operador;
Boolean opePrimario = true;
double resultado;

public Window1()
{
InitializeComponent();
}

#region buttons
private void btn1_Click(object sender, RoutedEventArgs e)
{
if (opePrimario == true)
{
varA += ´1´;
textBox1.Text = varA.ToString();
}
else
{
varB += ´1´;
textBox1.Text = varB.ToString();
}

}

private void btn2_Click(object sender, RoutedEventArgs e)
{
if (opePrimario == true)
{
varA += ´2´;
textBox1.Text = varA.ToString();
}
else
{
varB += ´2´;
textBox1.Text = varB.ToString();
}
}

private void btn3_Click(object sender, RoutedEventArgs e)
{
if (opePrimario == true)
{
varA += ´3´;
textBox1.Text = varA.ToString();
}
else
{
varB += ´3´;
textBox1.Text = varB.ToString();
}
}

private void btn4_Click(object sender, RoutedEventArgs e)
{
if (opePrimario == true)
{
varA += ´4´;
textBox1.Text = varA.ToString();
}
else
{
varB += ´4´;
textBox1.Text = varB.ToString();
}
}

private void btn5_Click(object sender, RoutedEventArgs e)
{
if (opePrimario == true)
{
varA += ´5´;
textBox1.Text = varA.ToString();
}
else
{
varB += ´5´;
textBox1.Text = varB.ToString();
}
}

private void btn6_Click(object sender, RoutedEventArgs e)
{
if (opePrimario == true)
{
varA += ´6´;
textBox1.Text = varA.ToString();
}
else
{
varB += ´6´;
textBox1.Text = varB.ToString();
}
}

private void btn7_Click(object sender, RoutedEventArgs e)
{
if (opePrimario == true)
{
varA += ´7´;
textBox1.Text = varA.ToString();
}
else
{
varB += ´7´;
textBox1.Text = varB.ToString();
}
}

private void btn8_Click(object sender, RoutedEventArgs e)
{
if (opePrimario == true)
{
varA += ´8´;
textBox1.Text = varA.ToString();
}
else
{
varB += ´8´;
textBox1.Text = varB.ToString();
}
}

private void btn9_Click(object sender, RoutedEventArgs e)
{
if (opePrimario == true)
{
varA += ´9´;
textBox1.Text = varA.ToString();
}
else
{
varB += ´9´;
textBox1.Text = varB.ToString();
}
}

private void btn0_Click(object sender, RoutedEventArgs e)
{
if (opePrimario == true)
{
if (textBox1.Text != ´0,´)
{
varA += ´0´;
textBox1.Text = varA.ToString();
}
}
else
{
varB += ´0´;
textBox1.Text = varB.ToString();
}
}

private void btnC_Click(object sender, RoutedEventArgs e)
{
opePrimario = true;
textBox1.Text = ´0,´;
varA = ´´;
varB = ´´;
}

#endregion



region operators

private void btnDivisao_Click(object sender, RoutedEventArgs e)
{
opePrimario = false;
operador = ´/´;
}

private void btnMultiplicacao_Click(object sender, RoutedEventArgs e)
{
opePrimario = false;
operador = ´*´;
}

private void btnAdicao_Click(object sender, RoutedEventArgs e)
{
opePrimario = false;
operador = ´+´;
}

private void btnSubtracao_Click(object sender, RoutedEventArgs e)
{
opePrimario = false;
operador = ´-´;
}

private void btnIgual_Click(object sender, RoutedEventArgs e)
{
if (operador == ´/´)
{
resultado = double.Parse(varA) / double.Parse(varB);
textBox1.Text = resultado.ToString();
}

if (operador == ´*´)
{
resultado = double.Parse(varA) * double.Parse(varB);
textBox1.Text = resultado.ToString();

}

if (operador == ´-´)
{
resultado = double.Parse(varA) - double.Parse(varB);
textBox1.Text = resultado.ToString();

}

if (operador == ´+´)
{
resultado = double.Parse(varA) + double.Parse(varB);
textBox1.Text = resultado.ToString();
}

}


#endregion


Thiago Scaranto

Thiago Scaranto

Responder

Posts

05/08/2008

Thiago Scaranto

consegui a logica é essa :

public partial class Window1 : Window
{
double varA, varB;
double resultado;
string operador;


public Window1()
{
InitializeComponent();
textBox1.Text = ´0,´;
}

#region buttons
private void btn1_Click(object sender, RoutedEventArgs e)
{
if (textBox1.Text == ´0,´)
textBox1.Text = ´1´;
else
textBox1.Text += ´1´;
}

private void btn2_Click(object sender, RoutedEventArgs e)
{
if (textBox1.Text == ´0,´)
textBox1.Text = ´2´;
else
textBox1.Text += ´2´;
}

private void btn3_Click(object sender, RoutedEventArgs e)
{
if (textBox1.Text == ´0,´)
textBox1.Text = ´3´;
else
textBox1.Text += ´3´;
}

private void btn4_Click(object sender, RoutedEventArgs e)
{
if (textBox1.Text == ´0,´)
textBox1.Text = ´4´;
else
textBox1.Text += ´4´;
}

private void btn5_Click(object sender, RoutedEventArgs e)
{
if (textBox1.Text == ´0,´)
textBox1.Text = ´5´;
else
textBox1.Text += ´5´;
}

private void btn6_Click(object sender, RoutedEventArgs e)
{
if (textBox1.Text == ´0,´)
textBox1.Text = ´6´;
else
textBox1.Text += ´6´;
}

private void btn7_Click(object sender, RoutedEventArgs e)
{
if (textBox1.Text == ´0,´)
textBox1.Text = ´7´;
else
textBox1.Text += ´7´;
}

private void btn8_Click(object sender, RoutedEventArgs e)
{
if (textBox1.Text == ´0,´)
textBox1.Text = ´8´;
else
textBox1.Text += ´8´;
}

private void btn9_Click(object sender, RoutedEventArgs e)
{
if (textBox1.Text == ´0,´)
textBox1.Text = ´9´;
else
textBox1.Text += ´9´;
}

private void btn0_Click(object sender, RoutedEventArgs e)
{

if (textBox1.Text != ´0,´)
{
textBox1.Text += ´0´;

}

}

private void btnC_Click(object sender, RoutedEventArgs e)
{
textBox1.Text = ´0,´;

}

#endregion



region operators

private void btnDivisao_Click(object sender, RoutedEventArgs e)
{
varA = double.Parse(textBox1.Text);
operador = ´/´;
textBox1.Text = ´0,´;
}

private void btnMultiplicacao_Click(object sender, RoutedEventArgs e)
{
varA = double.Parse(textBox1.Text);
operador = ´*´;
textBox1.Text = ´0,´;
}

private void btnAdicao_Click(object sender, RoutedEventArgs e)
{
varA = double.Parse (textBox1.Text);
operador = ´+´;
textBox1.Text = ´0,´;
}

private void btnSubtracao_Click(object sender, RoutedEventArgs e)
{
varA = double.Parse(textBox1.Text);
operador = ´-´;
textBox1.Text = ´0,´;
}

private void btnIgual_Click(object sender, RoutedEventArgs e)
{
varB = double.Parse (textBox1.Text);

if (operador == ´+´)
resultado = varA + varB;
else if (operador == ´-´)
resultado = varA - varB;
else if (operador == ´*´)
resultado = varA * varB;
else if (operador == ´/´)
resultado = varA / varB;

textBox1.Text = resultado.ToString();

}


#endregion


Responder

Assista grátis a nossa aula inaugural

Assitir aula

Saiba por que programar é uma questão de
sobrevivência e como aprender sem riscos

Assistir agora

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar