Академический Документы
Профессиональный Документы
Культура Документы
www.iseron.com
Quiero empezar diciendo que el código aquí mostrado es simplemente didáctico, como
“proyecto” para los alumnos que se inician en la programación. Por supuesto, este código es
susceptible de mejora, pero a nivel académico es suficiente. Así y todo, si desean aportar
algún comentario constructivo estoy abierto a la mejora de dicho código.
http://es.wikipedia.org/wiki/Open_Source
-1-
Jose Santiago Jiménez Sarmiento Calculadora en C#
www.iseron.com
MainForm.cs
/*
* Created by SharpDevelop.
* User: Jose Santiago Jiménez Sarmiento
* Date: 28/05/2006
* Time: 1:28
*
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace calculadora
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm
{
[STAThread]
public static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO:
// Add constructor code after the InitializeComponent() call.
//
}
-2-
Jose Santiago Jiménez Sarmiento Calculadora en C#
www.iseron.com
/// <summary>
/// Encargada de controlar las operaciones (suma, resta, ...)
/// sobre los valores que deben encontrarse en lbVisor,
/// lbUltOp y lbdatoanterior.
/// </summary>
/// <param name="op">Operación a realizar (+, -, =, ...)</param>
void opera(string op) {
double dato_d = System.Convert.ToDouble(lbVisor.Text);
if (lbdatoanterior.Text == "") {
lbdatoanterior.Text = lbVisor.Text;
empieza = true;
if (op != "=") lbUltOp.Text = op;
return;
}
double dato_a = System.Convert.ToDouble(lbdatoanterior.Text);
string op_real = "";
empieza = true;
if (op == "=") op_real = lbUltOp.Text;
else op_real = op;
switch (op_real) {
case "+": dato_a += dato_d; break;
case "/":
if (dato_d == 0) {
MessageBox.Show("No se puede dividir entre 0");
return;
}
dato_a /= dato_d;
break;
if (op == "=") {
lbUltOp.Text = "";
lbVisor.Text = lbdatoanterior.Text;
lbdatoanterior.Text = "";
}
else lbUltOp.Text = op_real;
}
-3-
Jose Santiago Jiménez Sarmiento Calculadora en C#
www.iseron.com
/// <summary>
/// Controla el dato al pulsar un botón y según este,
/// llama a la función "opera" o introduce el valor
/// en el lbVisor
/// </summary>
/// <param name="dato">Acción del botón pulsado</param>
void AddDato(string dato)
{
if ((dato == ",") && (lbVisor.Text.Contains(","))) return;
if ((dato == "+") || (dato == "-") || (dato == "*") ||
(dato == "/") || (dato == "=") || (dato == "^") ||
(dato == "v¯")) {
opera(dato);
return;
}
-4-
Jose Santiago Jiménez Sarmiento Calculadora en C#
www.iseron.com
/// <summary>
/// Si nos equivocamos... podemos quitar el
/// último dígito.
/// </summary>
void BtAtrasClick(object sender, System.EventArgs e)
{
if (lbVisor.Text.Length == 1) lbVisor.Text = "0";
else lbVisor.Text = lbVisor.Text.Substring(0,lbVisor.Text.Length-1);
}
}
}
MainForm.Designer.cs
/*
* Created by SharpDevelop.
* User: Jose
* Date: 28/05/2006
* Time: 1:28
*
* To change this template use Tools | Options | Coding | Edit Standard
Headers.
*/
namespace calculadora
{
partial class MainForm : System.Windows.Forms.Form
{
/// <summary>
/// Designer variable used to keep track of non-visual components.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Disposes resources used by the form.
/// </summary>
/// <param name="disposing">true if managed resources
/// should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor.
/// The Forms designer might not be able to load this method
/// if it was changed manually.
/// </summary>
private void InitializeComponent()
-5-
Jose Santiago Jiménez Sarmiento Calculadora en C#
www.iseron.com
{
this.lbVisor = new System.Windows.Forms.Label();
this.btBorrarTodo = new System.Windows.Forms.Button();
this.btAtras = new System.Windows.Forms.Button();
this.bt7 = new System.Windows.Forms.Button();
this.bt8 = new System.Windows.Forms.Button();
this.bt9 = new System.Windows.Forms.Button();
this.bt6 = new System.Windows.Forms.Button();
this.bt5 = new System.Windows.Forms.Button();
this.bt4 = new System.Windows.Forms.Button();
this.bt3 = new System.Windows.Forms.Button();
this.bt2 = new System.Windows.Forms.Button();
this.bt1 = new System.Windows.Forms.Button();
this.bt0 = new System.Windows.Forms.Button();
this.btDecimal = new System.Windows.Forms.Button();
this.btentre = new System.Windows.Forms.Button();
this.btpor = new System.Windows.Forms.Button();
this.btmenos = new System.Windows.Forms.Button();
this.btmas = new System.Windows.Forms.Button();
this.btIgual = new System.Windows.Forms.Button();
this.lbdatoanterior = new System.Windows.Forms.Label();
this.lbUltOp = new System.Windows.Forms.Label();
this.btElevar = new System.Windows.Forms.Button();
this.btRaizCuadrada = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lbVisor
//
this.lbVisor.BackColor =
System.Drawing.SystemColors.ActiveCaption;
this.lbVisor.BorderStyle =
System.Windows.Forms.BorderStyle.Fixed3D;
this.lbVisor.Font = new System.Drawing.Font("Verdana",
18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.World);
this.lbVisor.ForeColor =
System.Drawing.SystemColors.ActiveCaptionText;
this.lbVisor.Location = new System.Drawing.Point(18, 15);
this.lbVisor.Name = "lbVisor";
this.lbVisor.Size = new System.Drawing.Size(252, 31);
this.lbVisor.TabIndex = 0;
this.lbVisor.Text = "0";
this.lbVisor.TextAlign =
System.Drawing.ContentAlignment.MiddleRight;
//
// btBorrarTodo
//
this.btBorrarTodo.Location = new System.Drawing.Point(17, 56);
this.btBorrarTodo.Name = "btBorrarTodo";
this.btBorrarTodo.Size = new System.Drawing.Size(44, 33);
this.btBorrarTodo.TabIndex = 1;
this.btBorrarTodo.Text = "C";
this.btBorrarTodo.UseVisualStyleBackColor = true;
this.btBorrarTodo.Click += new
System.EventHandler(this.BtBorrarTodoClick);
//
// btAtras
//
-6-
Jose Santiago Jiménez Sarmiento Calculadora en C#
www.iseron.com
-7-
Jose Santiago Jiménez Sarmiento Calculadora en C#
www.iseron.com
-8-
Jose Santiago Jiménez Sarmiento Calculadora en C#
www.iseron.com
this.btDecimal.TabIndex = 13;
this.btDecimal.Text = ".";
this.btDecimal.UseVisualStyleBackColor = true;
this.btDecimal.Click += new
System.EventHandler(this.BtDecimalClick);
//
// btentre
//
this.btentre.Location = new System.Drawing.Point(180,
199);
this.btentre.Name = "btentre";
this.btentre.Size = new System.Drawing.Size(43, 39);
this.btentre.TabIndex = 17;
this.btentre.Text = "/";
this.btentre.UseVisualStyleBackColor = true;
this.btentre.Click += new
System.EventHandler(this.BtentreClick);
//
// btpor
//
this.btpor.Location = new System.Drawing.Point(180, 154);
this.btpor.Name = "btpor";
this.btpor.Size = new System.Drawing.Size(43, 39);
this.btpor.TabIndex = 16;
this.btpor.Text = "*";
this.btpor.UseVisualStyleBackColor = true;
this.btpor.Click += new
System.EventHandler(this.BtporClick);
//
// btmenos
//
this.btmenos.Location = new System.Drawing.Point(229,
109);
this.btmenos.Name = "btmenos";
this.btmenos.Size = new System.Drawing.Size(43, 39);
this.btmenos.TabIndex = 15;
this.btmenos.Text = "-";
this.btmenos.UseVisualStyleBackColor = true;
this.btmenos.Click += new
System.EventHandler(this.BtmenosClick);
//
// btmas
//
this.btmas.Location = new System.Drawing.Point(178, 109);
this.btmas.Name = "btmas";
this.btmas.Size = new System.Drawing.Size(43, 39);
this.btmas.TabIndex = 18;
this.btmas.Text = "+";
this.btmas.UseVisualStyleBackColor = true;
this.btmas.Click += new
System.EventHandler(this.BtmasClick);
//
// btIgual
//
this.btIgual.Location = new System.Drawing.Point(180,
244);
this.btIgual.Name = "btIgual";
-9-
Jose Santiago Jiménez Sarmiento Calculadora en C#
www.iseron.com
- 10 -
Jose Santiago Jiménez Sarmiento Calculadora en C#
www.iseron.com
this.btRaizCuadrada.Location = new
System.Drawing.Point(229, 199);
this.btRaizCuadrada.Name = "btRaizCuadrada";
this.btRaizCuadrada.Size = new System.Drawing.Size(43, 39);
this.btRaizCuadrada.TabIndex = 23;
this.btRaizCuadrada.Text = "v¯";
this.btRaizCuadrada.UseVisualStyleBackColor = true;
this.btRaizCuadrada.Click += new
System.EventHandler(this.BtRaizCuadradaClick);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F,
13F);
this.AutoScaleMode =
System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(282, 296);
this.Controls.Add(this.btRaizCuadrada);
this.Controls.Add(this.btElevar);
this.Controls.Add(this.lbUltOp);
this.Controls.Add(this.lbdatoanterior);
this.Controls.Add(this.btIgual);
this.Controls.Add(this.btmas);
this.Controls.Add(this.btentre);
this.Controls.Add(this.btpor);
this.Controls.Add(this.btmenos);
this.Controls.Add(this.btDecimal);
this.Controls.Add(this.bt0);
this.Controls.Add(this.bt3);
this.Controls.Add(this.bt2);
this.Controls.Add(this.bt1);
this.Controls.Add(this.bt6);
this.Controls.Add(this.bt5);
this.Controls.Add(this.bt4);
this.Controls.Add(this.bt9);
this.Controls.Add(this.bt8);
this.Controls.Add(this.bt7);
this.Controls.Add(this.btAtras);
this.Controls.Add(this.btBorrarTodo);
this.Controls.Add(this.lbVisor);
this.Name = "MainForm";
this.Text = "calculadora";
this.ResumeLayout(false);
}
private System.Windows.Forms.Button btRaizCuadrada;
private System.Windows.Forms.Button btElevar;
private System.Windows.Forms.Label lbUltOp;
private System.Windows.Forms.Label lbdatoanterior;
private System.Windows.Forms.Button btIgual;
private System.Windows.Forms.Button btmas;
private System.Windows.Forms.Button btmenos;
private System.Windows.Forms.Button btpor;
private System.Windows.Forms.Button btentre;
private System.Windows.Forms.Button btDecimal;
private System.Windows.Forms.Button bt0;
private System.Windows.Forms.Button bt1;
private System.Windows.Forms.Button bt2;
- 11 -
Jose Santiago Jiménez Sarmiento Calculadora en C#
www.iseron.com
- 12 -