Hay an alle Programmierer da draußen,
ich habe mich die letzten zwei Tage mit C# auseinander gesetzt und einen Taschenrechner programmiert. Er funktioniert auch schon, nur möchte ich noch einen kleinen "Extra" einbauen.
Ich habe mir als Vorlage, den Taschenrechner von Windows genommen. Und nun möchte ich noch einbauen, das man in der Textbox auch rechnen kann. Sprich man gibt eine Zahl ein dann die + - * oder / Taste und dann nochmal eine Zahl und bei Enter sollte man dann die Lösung sehen. (Das alles geschieht in 1! Textbox)
Das mit der Entertaste hab ich hinbekommen, war nicht als zu schwer, da ich einfach den Code vom = Button kopiert habe.
Hier mal mein Code:
[ICH => Anfänger]
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258:
| 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 Taschenrechner { public partial class Form1 : Form {
public enum Operation { Addieren, Subtrahieren, Multiplizieren, Dividieren, Keine }
double? zahl1 = null, zahl2 = null; bool neueZahl = false; Operation op = Operation.Keine;
public Form1() { InitializeComponent(); }
private void btZahl_Click(object sender, EventArgs e) { if (neueZahl) { this.txtErgebnis.Text = ((Button)sender).Text; neueZahl = false;
} else this.txtErgebnis.Text += ((Button)sender).Text; }
private void btMultiplizieren_Click(object sender, EventArgs e) { if (zahl1 == null) { if (this.txtErgebnis.Text != "") zahl1 = double.Parse(this.txtErgebnis.Text);
else
zahl1 = 0;
} neueZahl = true; op = Operation.Multiplizieren;
}
private void btDividieren_Click(object sender, EventArgs e) { if (zahl1 == null) { if (this.txtErgebnis.Text != "") zahl1 = double.Parse(this.txtErgebnis.Text); else zahl1 = 0; } neueZahl = true; op = Operation.Dividieren; }
private void btSubtrahieren_Click(object sender, EventArgs e) { if (zahl1 == null) if (this.txtErgebnis.Text != "") zahl1 = double.Parse(this.txtErgebnis.Text); else zahl1 = 0; neueZahl = true; op = Operation.Subtrahieren; }
private void btAddieren_Click(object sender, EventArgs e) { if (zahl1 == null) if (this.txtErgebnis.Text != "") zahl1 = double.Parse(this.txtErgebnis.Text); else zahl1 = 0; neueZahl = true; op = Operation.Addieren; }
private void btIstGleich_Click(object sender, EventArgs e) { if (zahl1 != null && op != Operation.Keine)
zahl2 = double.Parse(this.txtErgebnis.Text);
switch (op) { case Operation.Addieren: this.txtErgebnis.Text = (zahl1 + zahl2).ToString(); break; case Operation.Subtrahieren: this.txtErgebnis.Text = (zahl1 - zahl2).ToString(); break; case Operation.Dividieren: try { this.txtErgebnis.Text = (zahl1 / zahl2).ToString(); } catch (DivideByZeroException) { MessageBox.Show("Division durch 0 nicht möglich"); } break; case Operation.Multiplizieren: this.txtErgebnis.Text = (zahl1 * zahl2).ToString(); break; case Operation.Keine: break;
} zahl1 = zahl2 = null; neueZahl = true; }
private void txtErgebnis_KeyUp(object sender, KeyEventArgs e) {
String aa = txtErgebnis.Text; String bb = ""; bool marker = false;
foreach (char c in aa) { if (c == '.' && !marker) { marker = true; } if (c == ',' && !marker) { marker = true; bb += c; } if (c == '-' && !marker) { marker = true; bb += c; } if (c == '+' && !marker) { marker = true; bb += c; } if (char.IsDigit(c)) { bb += c; } }
txtErgebnis.Text = bb;
if (e.KeyCode == Keys.Enter) { { if (zahl1 != null && op != Operation.Keine)
zahl2 = double.Parse(this.txtErgebnis.Text);
switch (op) { case Operation.Addieren: this.txtErgebnis.Text = (zahl1 + zahl2).ToString(); break; case Operation.Subtrahieren: this.txtErgebnis.Text = (zahl1 - zahl2).ToString(); break; case Operation.Dividieren: try { this.txtErgebnis.Text = (zahl1 / zahl2).ToString(); } catch (DivideByZeroException) { MessageBox.Show("Division durch 0 nicht möglich"); } break; case Operation.Multiplizieren: this.txtErgebnis.Text = (zahl1 * zahl2).ToString(); break; case Operation.Keine: break; } zahl1 = zahl2 = null; neueZahl = true; }
{ if (e.KeyCode == Keys.Add) { }
} } }
private void btRücktaste_Click(object sender, EventArgs e) { if (this.txtErgebnis.Text.Length > 0) this.txtErgebnis.Text = this.txtErgebnis.Text.Substring(0, this.txtErgebnis.Text.Length - 1); }
private void btNeu_Click(object sender, EventArgs e) { if (txtErgebnis.GetType() == typeof(TextBox)) { ((TextBox)txtErgebnis).Clear(); } }
private void btVorzeichen_Click(object sender, EventArgs e) { if (this.txtErgebnis.Text != "") txtErgebnis.Text = Convert.ToString(double.Parse(this.txtErgebnis.Text) * -1); }
private void btKomma_Click(object sender, EventArgs e) { if (this.txtErgebnis.Text != txtErgebnis + ",") { txtErgebnis.AppendText(","); }
}
}
} |
Weiß vielleicht einer wie ich das hinbekomme, dass wenn man Zahlen eingibt das die Zahl dann, wie bei durch drücken der Buttons die erste Zahl weg geht und man eine neue eingeben kann? Und logischerweise das man die Rechenart(+ - * /) vorher eingibt.
Moderiert von
Th69: Quote- durch C#-Tags ersetzt