Home   Cover Cover Cover Cover
 

Taschenrechner

A01.cs
/* Calculator
   This is a sample program that shows how to implement a GUI
   under .NET using Windows Forms. It implements a simple
   calculator with the 4 basic arithmetic operations 
   (without precedence)
------------------------------------------------------------*/
using System;
using System.Drawing;
using System.Windows.Forms;

enum Op {Plus, Minus, Times, Div, Eql}

public class Calculator {
  static Form f;             // the calculator window
  static TextBox box;        // input field for numbers
  
  static double lastVal = 0; // last entered number
  static Op lastOp = Op.Eql; // last entered operator
  
  // shows val in box n
  static void Show(double val) {
    box.Text = val.ToString();
    box.Focus();
    box.SelectionStart = 0;
    box.SelectionLength = box.Text.Length;
  }
  
  // The op button was clicked. The previous operation is executed.
  static void Do(Op op) {
    double val = Convert.ToDouble(box.Text);
    switch (lastOp) {
      case Op.Plus:  lastVal += val; break;
      case Op.Minus: lastVal -= val; break;
      case Op.Times: lastVal *= val; break;
      case Op.Div:   lastVal /= val; break;
      case Op.Eql:   lastVal = val;  break;
    }
    Show(lastVal);
    lastOp = op;
  }
  
  // Event handlers for the operator buttons
  static void Plus (object sender, EventArgs e) { Do(Op.Plus); }
  static void Minus(object sender, EventArgs e) { Do(Op.Minus); }
  static void Times(object sender, EventArgs e) { Do(Op.Times); }
  static void Div  (object sender, EventArgs e) { Do(Op.Div); }
  static void Eql  (object sender, EventArgs e) { Do(Op.Eql); }
  static void Clear(object sender, EventArgs e) { lastOp = Op.Eql; Show(0); }
  
  public static void Main() {
    Font bold = new Font("Arial", 12, FontStyle.Bold);
    
    //--- input field for numbers
    box = new TextBox();
    box.Name = "box";
    box.Location = new Point(10, 10);
    box.Size = new Size(185, 20);
    
    //--- operator buttons
    Button plus = new Button();
    plus.Name = "plus";
    plus.Location = new Point(10, 40);
    plus.Size = new Size(20, 20);
    plus.Text = "+";
    plus.Font = bold;
    plus.Click += new EventHandler(Plus);
    
    Button minus = new Button();
    minus.Name = "minus";
    minus.Location = new Point(35, 40);
    minus.Size = new Size(20, 20);
    minus.Text = "-";
    minus.Font = bold;
    minus.Click += new EventHandler(Minus);
    
    Button times = new Button();
    times.Name = "times";
    times.Location = new Point(60, 40);
    times.Size = new Size(20, 20);
    times.Text = "*";
    times.Font = bold;
    times.Click += new EventHandler(Times);
    
    Button div = new Button();
    div.Name = "div";
    div.Location = new Point(85, 40);
    div.Size = new Size(20, 20);
    div.Text = "/";
    div.Font = bold;
    div.Click += new EventHandler(Div);
    
    Button eql = new Button();
    eql.Name = "eql";
    eql.Location = new Point(110, 40);
    eql.Size = new Size(20, 20);
    eql.Text = "=";
    eql.Font = bold;
    eql.Click += new EventHandler(Eql);
    
    Button clear = new Button();
    clear.Name = "clear";
    clear.Location = new Point(135, 40);
    clear.Size = new Size(60, 20);
    clear.Text = "Clear";
    clear.Font = new Font("Arial", 10, FontStyle.Bold);
    clear.Click += new EventHandler(Clear);
    
    //--- create and open the window
    f = new Form();
    f.DesktopLocation = new Point(0, 0);
    f.ClientSize = new Size(205, 70);
    f.Text = "Desktop Calculator";
    f.Controls.Add(box);
    f.Controls.Add(plus);
    f.Controls.Add(minus);
    f.Controls.Add(times);
    f.Controls.Add(div);
    f.Controls.Add(eql);
    f.Controls.Add(clear);
    Application.Run(f);
  }
}