boeseskeksi Geschrieben 3. November 2009 Geschrieben 3. November 2009 Programm + Sprache: Visual Studio 2008 C# Hallo ^^ Mein TR funktioniert eigl ganz gut, allerdings habe ich einen Fehler entdeckt: Rechne ich bsp: -3 + -2 gibt der TR -6 aus, was nun definitiv falsch ist. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace calc2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private double valHolder1; private double valHolder2; private double tmpValue; private bool hasDecimal = false; private bool inputStatus = true; private string calcFunc; private void btn_1_Click(object sender, EventArgs e) { if (inputStatus) { textBox1.Text += btn_1.Text; } else { textBox1.Text = btn_1.Text; inputStatus = true; } } private void btn_2_Click(object sender, EventArgs e) { if (inputStatus) { textBox1.Text += btn_2.Text; } else { textBox1.Text = btn_2.Text; inputStatus = true; } } private void btn_3_Click(object sender, EventArgs e) { if (inputStatus) { textBox1.Text += btn_3.Text; } else { textBox1.Text = btn_3.Text; inputStatus = true; } } private void btn_4_Click(object sender, EventArgs e) { if (inputStatus) { textBox1.Text += btn_4.Text; } else { textBox1.Text = btn_4.Text; inputStatus = true; } } private void btn_5_Click(object sender, EventArgs e) { if (inputStatus) { textBox1.Text += btn_5.Text; } else { textBox1.Text = btn_5.Text; inputStatus = true; } } private void btn_6_Click(object sender, EventArgs e) { if (inputStatus) { textBox1.Text += btn_6.Text; } else { textBox1.Text = btn_6.Text; inputStatus = true; } } private void btn_7_Click(object sender, EventArgs e) { if (inputStatus) { textBox1.Text += btn_7.Text; } else { textBox1.Text = btn_7.Text; inputStatus = true; } } private void btn_8_Click(object sender, EventArgs e) { if (inputStatus) { textBox1.Text += btn_8.Text; } else { textBox1.Text = btn_8.Text; inputStatus = true; } } private void btn_9_Click(object sender, EventArgs e) { if (inputStatus) { textBox1.Text += btn_9.Text; } else { textBox1.Text = btn_9.Text; inputStatus = true; } } private void btn_0_Click(object sender, EventArgs e) { if (inputStatus) { if (textBox1.Text.Length <= 1) { textBox1.Text += btn_0.Text; } } } private void btn_plus_Click(object sender, EventArgs e) { if (textBox1.Text.Length != 0) { if (calcFunc == string.Empty) { valHolder1 = System.Double.Parse(textBox1.Text); textBox1.Text = String.Empty; } else { CalculateTotals(); } calcFunc = "Add"; hasDecimal = false; } } private void btn_minus_Click(object sender, EventArgs e) { if (textBox1.Text.Length != 0) { if (calcFunc == String.Empty) { valHolder1 = System.Double.Parse(textBox1.Text); textBox1.Text = string.Empty; } else { CalculateTotals(); } calcFunc = "Subtract"; hasDecimal = false; } } private void btn_ex_Click(object sender, EventArgs e) { if (textBox1.Text.Length != 0) { if (calcFunc == string.Empty) { valHolder1 = System.Double.Parse(textBox1.Text); textBox1.Text = String.Empty; } else { CalculateTotals(); } calcFunc = "PowerOf"; hasDecimal = false; } } private void btn_sqrt_Click(object sender, EventArgs e) { if (textBox1.Text.Length != 0) { tmpValue = System.Double.Parse(textBox1.Text); tmpValue = System.Math.Sqrt(tmpValue); textBox1.Text = tmpValue.ToString(); hasDecimal = false; } } private void back_Click(object sender, EventArgs e) { string str; int loc; if (textBox1.Text.Length > 0) { str = textBox1.Text.Substring(textBox1.Text.Length - 1); if (str == ".") { hasDecimal = false; } loc = textBox1.Text.Length; textBox1.Text = textBox1.Text.Remove(loc - 1, 1); } } private void btn_ce_Click(object sender, EventArgs e) { textBox1.Text = string.Empty; hasDecimal = false; } private void btn_c_Click(object sender, EventArgs e) { textBox1.Text = string.Empty; valHolder1 = 0; valHolder2 = 0; calcFunc = string.Empty; hasDecimal = false; } private void btn_divi_Click(object sender, EventArgs e) { if (textBox1.Text.Length != 0) { if (calcFunc == string.Empty) valHolder1 = System.Double.Parse(textBox1.Text); textBox1.Text = string.Empty; } else { CalculateTotals(); } calcFunc = "Divide"; hasDecimal = false; } private void btn_multi_Click(object sender, EventArgs e) { if (textBox1.Text.Length != 0) { if (calcFunc == string.Empty) { valHolder1 = System.Double.Parse(textBox1.Text); textBox1.Text = string.Empty; } else { CalculateTotals(); } calcFunc = "Multiply"; hasDecimal = false; } } private void btn_kehr_Click(object sender, EventArgs e) { if (textBox1.Text.Length != 0) { tmpValue = System.Double.Parse(textBox1.Text); tmpValue = 1 / tmpValue; textBox1.Text = tmpValue.ToString(); hasDecimal = false; } } private void btn_plusminus_Click(object sender, EventArgs e) { if (inputStatus) { if (textBox1.Text.Length > 0) { valHolder1 = -1 * System.Double.Parse(textBox1.Text); textBox1.Text = valHolder1.ToString(); } } } private void btn_equals_Click(object sender, EventArgs e) { if (textBox1.Text.Length != 0 && valHolder1 != 0) { CalculateTotals(); calcFunc = String.Empty; hasDecimal = false; } } private void btn_komma_Click(object sender, EventArgs e) { if (inputStatus) { if (!hasDecimal) { if (textBox1.Text.Length != 0) { if (textBox1.Text != "0") { textBox1.Text += btn_komma.Text; hasDecimal = true; } } else { textBox1.Text = "0,"; } } } } private void CalculateTotals() { valHolder2 = System.Double.Parse(textBox1.Text); switch (calcFunc) { case "Add": valHolder1 = valHolder1 + valHolder2; break; case "Subtract": valHolder1 = valHolder1 - valHolder2; break; case "Divide": valHolder1 = valHolder1 / valHolder2; break; case "Multiply": valHolder1 = valHolder1 * valHolder2; break; case "PowerOf": valHolder1 = System.Math.Pow(valHolder1, valHolder2); break; } textBox1.Text = valHolder1.ToString(); inputStatus = false; } } } Es war für mich schon ein "Krampf" das hinzubekommen ( mit einigen Tuts), ich finde einfach den Fehler nicht, evtl sieht es einer von euch Zitieren
Klotzkopp Geschrieben 3. November 2009 Geschrieben 3. November 2009 In btn_plusminus_Click darfst du nicht valHolder1 ändern, sondern nur den Text. Zitieren
Empfohlene Beiträge
Dein Kommentar
Du kannst jetzt schreiben und Dich später registrieren. Wenn Du ein Konto hast, melde Dich jetzt an, um unter Deinem Benutzernamen zu schreiben.