Dzień dobry,
czy jest jakiś prosty sposób na obliczenie działania typu (2+2)*2 znajdującego się w zmiennej string w języku c#?
Dzięki za pomoc.
EDIT:
Próbowałem już konwertować string na int'a, jednak wyskakuje wyjątek.
System.FormatException: „Nieprawidłowy format ciągu wejściowego.”
string calculation;
public void UpdateSum()
{
lb_sum.Content = calculation;
}
private void lb_0_MouseUp(object sender, MouseButtonEventArgs e)
{
calculation = calculation + "0";
UpdateSum();
}
private void lb_1_MouseUp(object sender, MouseButtonEventArgs e)
{
calculation = calculation + "1";
UpdateSum();
}
private void lb_2_MouseUp(object sender, MouseButtonEventArgs e)
{
calculation = calculation + "2";
UpdateSum();
}
private void lb_3_MouseUp(object sender, MouseButtonEventArgs e)
{
calculation = calculation + "3";
UpdateSum();
}
private void lb_4_MouseUp(object sender, MouseButtonEventArgs e)
{
calculation = calculation + "4";
UpdateSum();
}
private void lb_5_MouseUp(object sender, MouseButtonEventArgs e)
{
calculation = calculation + "5";
UpdateSum();
}
private void lb_6_MouseUp(object sender, MouseButtonEventArgs e)
{
calculation = calculation + "6";
UpdateSum();
}
private void lb_7_MouseUp(object sender, MouseButtonEventArgs e)
{
calculation = calculation + "7";
UpdateSum();
}
private void lb_8_MouseUp(object sender, MouseButtonEventArgs e)
{
calculation = calculation + "8";
UpdateSum();
}
private void lb_9_MouseUp(object sender, MouseButtonEventArgs e)
{
calculation = calculation + "9";
UpdateSum();
}
private void lb_addition_MouseUp(object sender, MouseButtonEventArgs e)
{
calculation = calculation + "+";
UpdateSum();
}
private void lb_subtra_MouseUp(object sender, MouseButtonEventArgs e)
{
calculation = calculation + "-";
UpdateSum();
}
private void lb_multiplication_MouseUp(object sender, MouseButtonEventArgs e)
{
calculation = calculation + "*";
UpdateSum();
}
private void lb_division_MouseUp(object sender, MouseButtonEventArgs e)
{
calculation = calculation + "/";
UpdateSum();
}
private void lb_bracket1_MouseUp(object sender, MouseButtonEventArgs e)
{
calculation = calculation + "(";
UpdateSum();
}
private void lb_bracket2_MouseUp(object sender, MouseButtonEventArgs e)
{
calculation = calculation + ")";
UpdateSum();
}
private void lb_equal_MouseUp(object sender, MouseButtonEventArgs e)
{
int res = Convert.ToInt32(calculation);
calculation = Convert.ToString(res);
UpdateSum();
}
private void lb_c_MouseUp(object sender, MouseButtonEventArgs e)
{
calculation = "";
UpdateSum();
}
private void lb_back_MouseUp(object sender, MouseButtonEventArgs e)
{
if (calculation != "")
calculation = calculation.Remove(calculation.Length - 1);
UpdateSum();
}
EDIT2:
Znalazłem rozwiązanie na stackoverflow (nie widziałem tego przed napisaniem posta). LINK DO STACK OVERFLOW
Przepraszam za spam na forum. Temat zamykam.