C# :: Aufgabe #274 :: Lösung #1

3 Lösungen Lösungen öffentlich
#274

Windows Forms Taschenrechner

Anfänger - C# von paddlboot - 04.12.2019 um 17:09 Uhr
Erstelle mit Hilfe von Windows Forms einen Taschenrechner!

- Der Taschenrechner soll zwei Zahlen speichern können.
- Wie bei einem Taschenrechner üblich, sollen alle Zahleneingaben gesammelt werden bis der Operator gewählt wurde.
- Mit jedem Drücken einer Zahlentaste wird der Variable also eine Dezimalstelle hinzugefügt.
- Wenn ein Operator ausgewählt wurde, soll die erste eingegebene Zahl gespeichert werden, ebenso der Operator (in einer separaten Variable).
- Mit Drücken der Ergebnis Taste (=) soll das Ergebnis in der selben Textbox ausgegeben werden, wie die, in der die Zahlen eingegeben wurden.

Im Anhang ein Beispiel wie ich das gelöst habe.
#1
vote_ok
von Kotgreifer (1100 Punkte) - 18.12.2019 um 11:04 Uhr
Ich arbeite normalerweise nur mit WPF deswegen weiß ich net ob es reicht den Designer zu kopieren :D
Wenn man mehr für die UI braucht dann glaubt einfach dran das es funktioniert.
Tatsächlich kannte ich noch keinen Weg mehrer Zahlen hintereinander zusammen zurechnen und auf die Punkt vor Strich Regel zu achten.
Daher habe ich mir selbst einen Weg überlegt.


Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Taschenrechner
{
	public partial class Form1 : Form
	{
		List<String> numbers = new List<String>();
		public Form1()
		{
			InitializeComponent();
		}

		private void Number_Click(object sender, EventArgs e)
		{
			Button tmpBtn = (Button)sender;
			labelActive.Text += tmpBtn.Text;
		
		}

		private void Operator_Click(object sender, EventArgs e)
		{
			if (labelActive.Text !="")
			{
				Button tmpBtn = (Button)sender;
				numbers.Add(labelActive.Text);
				numbers.Add(tmpBtn.Text);
				labelPassiv.Text += labelActive.Text+tmpBtn.Text;
				labelActive.Text = null;

			}
		}

		private void zDelete_Click(object sender, EventArgs e)
		{
			numbers = new List<string>();
			labelActive.Text = string.Empty;
			labelPassiv.Text = string.Empty;
		}


		private void zResult_Click(object sender, EventArgs e)
		{
			if (labelActive.Text != "")
			{
				numbers.Add(labelActive.Text);
				labelPassiv.Text += labelActive.Text + "=";
				labelActive.Text = null;

				if (numbers.Contains("*") || numbers.Contains("/"))
				{
					PunktVorStrich();
				}

				for (int i = 0; i < numbers.Count; i++)
				{
					if (numbers.Count == 1)
						break;


					if (numbers[1] == "+")
					{
						int.TryParse(numbers[0], out int num1);
						int.TryParse(numbers[2], out int num2);
						double erg = num1 + num2;
						numbers[0] = erg.ToString();
						numbers.RemoveAt(1);
						numbers.RemoveAt(1);

					}
					else if (numbers[1] == "-")
					{
						int.TryParse(numbers[0], out int num1);
						int.TryParse(numbers[2], out int num2);
						double erg = num1 - num2;
						numbers[0] = erg.ToString();
						numbers.RemoveAt(1);
						numbers.RemoveAt(1);

					}
				}
				labelActive.Text = numbers[0];
			}
		}



		private void PunktVorStrich()
		{
			for (int i = 1; i < numbers.Count; i++)
			{
				if (numbers[i] == "*")
				{
					int.TryParse(numbers[i - 1], out int num1);
					int.TryParse(numbers[i + 1], out int num2);
					double erg = num1 * num2;
					numbers[i - 1] = erg.ToString();
					numbers.RemoveAt(i);
					numbers.RemoveAt(i);
					i -= 2;
				}
				else if (numbers[i] == "/")
				{
					int.TryParse(numbers[i - 1], out int num1);
					int.TryParse(numbers[i + 1], out int num2);
					double erg = num1 / num2;
					numbers[i - 1] = erg.ToString();
					numbers.RemoveAt(i);
					numbers.RemoveAt(i);
					i -= 2;
				}

			}
		}

	}
}




Quellcode ausblenden C#-Code
namespace Taschenrechner
{
	partial class Form1
	{
		/// <summary>
		/// Erforderliche Designervariable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;

		/// <summary>
		/// Verwendete Ressourcen bereinigen.
		/// </summary>
		/// <param name="disposing">True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		#region Vom Windows Form-Designer generierter Code

		/// <summary>
		/// Erforderliche Methode für die Designerunterstützung.
		/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
		/// </summary>
		private void InitializeComponent()
		{
			this.b1 = new System.Windows.Forms.Button();
			this.b2 = new System.Windows.Forms.Button();
			this.labelActive = new System.Windows.Forms.Label();
			this.b3 = new System.Windows.Forms.Button();
			this.b4 = new System.Windows.Forms.Button();
			this.b5 = new System.Windows.Forms.Button();
			this.b6 = new System.Windows.Forms.Button();
			this.b7 = new System.Windows.Forms.Button();
			this.b8 = new System.Windows.Forms.Button();
			this.b9 = new System.Windows.Forms.Button();
			this.zPlus = new System.Windows.Forms.Button();
			this.zMinus = new System.Windows.Forms.Button();
			this.zMal = new System.Windows.Forms.Button();
			this.zGeteilt = new System.Windows.Forms.Button();
			this.zGleich = new System.Windows.Forms.Button();
			this.zDelete = new System.Windows.Forms.Button();
			this.b0 = new System.Windows.Forms.Button();
			this.labelPassiv = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// b1
			// 
			this.b1.Location = new System.Drawing.Point(30, 168);
			this.b1.Name = "b1";
			this.b1.Size = new System.Drawing.Size(30, 30);
			this.b1.TabIndex = 0;
			this.b1.Text = "1";
			this.b1.UseVisualStyleBackColor = true;
			this.b1.Click += new System.EventHandler(this.Number_Click);
			// 
			// b2
			// 
			this.b2.Location = new System.Drawing.Point(66, 168);
			this.b2.Name = "b2";
			this.b2.Size = new System.Drawing.Size(30, 30);
			this.b2.TabIndex = 1;
			this.b2.Text = "2";
			this.b2.UseVisualStyleBackColor = true;
			this.b2.Click += new System.EventHandler(this.Number_Click);
			// 
			// labelActive
			// 
			this.labelActive.BackColor = System.Drawing.SystemColors.ActiveBorder;
			this.labelActive.Location = new System.Drawing.Point(30, 63);
			this.labelActive.Name = "labelActive";
			this.labelActive.Size = new System.Drawing.Size(138, 20);
			this.labelActive.TabIndex = 2;
			// 
			// b3
			// 
			this.b3.Location = new System.Drawing.Point(102, 168);
			this.b3.Name = "b3";
			this.b3.Size = new System.Drawing.Size(30, 30);
			this.b3.TabIndex = 3;
			this.b3.Text = "3";
			this.b3.UseVisualStyleBackColor = true;
			this.b3.Click += new System.EventHandler(this.Number_Click);
			// 
			// b4
			// 
			this.b4.Location = new System.Drawing.Point(30, 132);
			this.b4.Name = "b4";
			this.b4.Size = new System.Drawing.Size(30, 30);
			this.b4.TabIndex = 4;
			this.b4.Text = "4";
			this.b4.UseVisualStyleBackColor = true;
			this.b4.Click += new System.EventHandler(this.Number_Click);
			// 
			// b5
			// 
			this.b5.Location = new System.Drawing.Point(66, 132);
			this.b5.Name = "b5";
			this.b5.Size = new System.Drawing.Size(30, 30);
			this.b5.TabIndex = 5;
			this.b5.Text = "5";
			this.b5.UseVisualStyleBackColor = true;
			this.b5.Click += new System.EventHandler(this.Number_Click);
			// 
			// b6
			// 
			this.b6.Location = new System.Drawing.Point(102, 132);
			this.b6.Name = "b6";
			this.b6.Size = new System.Drawing.Size(30, 30);
			this.b6.TabIndex = 6;
			this.b6.Text = "6";
			this.b6.UseVisualStyleBackColor = true;
			this.b6.Click += new System.EventHandler(this.Number_Click);
			// 
			// b7
			// 
			this.b7.Location = new System.Drawing.Point(30, 96);
			this.b7.Name = "b7";
			this.b7.Size = new System.Drawing.Size(30, 30);
			this.b7.TabIndex = 7;
			this.b7.Text = "7";
			this.b7.UseVisualStyleBackColor = true;
			this.b7.Click += new System.EventHandler(this.Number_Click);
			// 
			// b8
			// 
			this.b8.Location = new System.Drawing.Point(66, 96);
			this.b8.Name = "b8";
			this.b8.Size = new System.Drawing.Size(30, 30);
			this.b8.TabIndex = 8;
			this.b8.Text = "8";
			this.b8.UseVisualStyleBackColor = true;
			this.b8.Click += new System.EventHandler(this.Number_Click);
			// 
			// b9
			// 
			this.b9.Location = new System.Drawing.Point(102, 96);
			this.b9.Name = "b9";
			this.b9.Size = new System.Drawing.Size(30, 30);
			this.b9.TabIndex = 9;
			this.b9.Text = "9";
			this.b9.UseVisualStyleBackColor = true;
			this.b9.Click += new System.EventHandler(this.Number_Click);
			// 
			// zPlus
			// 
			this.zPlus.Location = new System.Drawing.Point(138, 96);
			this.zPlus.Name = "zPlus";
			this.zPlus.Size = new System.Drawing.Size(30, 30);
			this.zPlus.TabIndex = 10;
			this.zPlus.Text = "+";
			this.zPlus.UseVisualStyleBackColor = true;
			this.zPlus.Click += new System.EventHandler(this.Operator_Click);
			// 
			// zMinus
			// 
			this.zMinus.Location = new System.Drawing.Point(138, 132);
			this.zMinus.Name = "zMinus";
			this.zMinus.Size = new System.Drawing.Size(30, 30);
			this.zMinus.TabIndex = 11;
			this.zMinus.Text = "-";
			this.zMinus.UseVisualStyleBackColor = true;
			this.zMinus.Click += new System.EventHandler(this.Operator_Click);
			// 
			// zMal
			// 
			this.zMal.Location = new System.Drawing.Point(138, 168);
			this.zMal.Name = "zMal";
			this.zMal.Size = new System.Drawing.Size(30, 30);
			this.zMal.TabIndex = 12;
			this.zMal.Text = "*";
			this.zMal.UseVisualStyleBackColor = true;
			this.zMal.Click += new System.EventHandler(this.Operator_Click);
			// 
			// zGeteilt
			// 
			this.zGeteilt.Location = new System.Drawing.Point(138, 204);
			this.zGeteilt.Name = "zGeteilt";
			this.zGeteilt.Size = new System.Drawing.Size(30, 30);
			this.zGeteilt.TabIndex = 13;
			this.zGeteilt.Text = "/";
			this.zGeteilt.UseVisualStyleBackColor = true;
			this.zGeteilt.Click += new System.EventHandler(this.Operator_Click);
			// 
			// zGleich
			// 
			this.zGleich.Location = new System.Drawing.Point(102, 204);
			this.zGleich.Name = "zGleich";
			this.zGleich.Size = new System.Drawing.Size(30, 30);
			this.zGleich.TabIndex = 14;
			this.zGleich.Text = "=";
			this.zGleich.UseVisualStyleBackColor = true;
			this.zGleich.Click += new System.EventHandler(this.zResult_Click);
			// 
			// zDelete
			// 
			this.zDelete.Location = new System.Drawing.Point(66, 204);
			this.zDelete.Name = "zDelete";
			this.zDelete.Size = new System.Drawing.Size(30, 30);
			this.zDelete.TabIndex = 15;
			this.zDelete.Text = "C";
			this.zDelete.UseVisualStyleBackColor = true;
			this.zDelete.Click += new System.EventHandler(this.zDelete_Click);
			// 
			// b0
			// 
			this.b0.Location = new System.Drawing.Point(30, 204);
			this.b0.Name = "b0";
			this.b0.Size = new System.Drawing.Size(30, 30);
			this.b0.TabIndex = 16;
			this.b0.Text = "0";
			this.b0.UseVisualStyleBackColor = true;
			this.b0.Click += new System.EventHandler(this.Number_Click);
			// 
			// labelPassiv
			// 
			this.labelPassiv.BackColor = System.Drawing.SystemColors.ActiveCaption;
			this.labelPassiv.Location = new System.Drawing.Point(30, 34);
			this.labelPassiv.Name = "labelPassiv";
			this.labelPassiv.Size = new System.Drawing.Size(138, 20);
			this.labelPassiv.TabIndex = 17;
			this.labelPassiv.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// Form1
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(227, 242);
			this.Controls.Add(this.labelPassiv);
			this.Controls.Add(this.b0);
			this.Controls.Add(this.zDelete);
			this.Controls.Add(this.zGleich);
			this.Controls.Add(this.zGeteilt);
			this.Controls.Add(this.zMal);
			this.Controls.Add(this.zMinus);
			this.Controls.Add(this.zPlus);
			this.Controls.Add(this.b9);
			this.Controls.Add(this.b8);
			this.Controls.Add(this.b7);
			this.Controls.Add(this.b6);
			this.Controls.Add(this.b5);
			this.Controls.Add(this.b4);
			this.Controls.Add(this.b3);
			this.Controls.Add(this.labelActive);
			this.Controls.Add(this.b2);
			this.Controls.Add(this.b1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);

		}

		#endregion

		private System.Windows.Forms.Button b1;
		private System.Windows.Forms.Button b2;
		private System.Windows.Forms.Label labelActive;
		private System.Windows.Forms.Button b3;
		private System.Windows.Forms.Button b4;
		private System.Windows.Forms.Button b5;
		private System.Windows.Forms.Button b6;
		private System.Windows.Forms.Button b7;
		private System.Windows.Forms.Button b8;
		private System.Windows.Forms.Button b9;
		private System.Windows.Forms.Button zPlus;
		private System.Windows.Forms.Button zMinus;
		private System.Windows.Forms.Button zMal;
		private System.Windows.Forms.Button zGeteilt;
		private System.Windows.Forms.Button zGleich;
		private System.Windows.Forms.Button zDelete;
		private System.Windows.Forms.Button b0;
		private System.Windows.Forms.Label labelPassiv;
	}
}

Kommentare:

Für diese Lösung gibt es noch keinen Kommentar

Bitte melden Sie sich an um eine Kommentar zu schreiben.
Kommentar schreiben
2091586

Du scheinst einen AdBlocker zu nutzen. Ich würde mich freuen, wenn du ihn auf dieser Seite deaktivierst und dich davon überzeugst, dass die Werbung hier nicht störend ist.