Вы находитесь на странице: 1из 12

Jose Santiago Jiménez Sarmiento Calculadora en C#

www.iseron.com

Calculadora muy simple en C#

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.

Para la realización de este software se ha usado el entorno SharpDevelop2 que puedes


descargar en la siguiente dirección http://www.sharpdevelop.net/OpenSource/SD/Download/.
#Develop requiere tener instalado en el sistema el .NET Framework 2.0 Runtime download
(x86), y opcionalmente el .NET Framework 2.0 SDK download (x86) (recomendable si
quieres tener la ayuda instalada).

Como se puede observar, SharpDevelop es Open Source


(http://www.opensource.org/), y como no, éste código también lo es. Si deseas conocer más
sobre Open Source puedes ver una definición muy simple en

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
{

// Se usa cada vez que queramos iniciar el valor en lbVisor


private bool empieza = true;

[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 "-": dato_a -= dato_d; break;

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;

case "^": dato_a = Math.Pow(dato_a, dato_d); break;

case "v¯": dato_a = Math.Sqrt(dato_d); break;


}
lbdatoanterior.Text = dato_a.ToString();

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;
}

if ((lbVisor.Text == "0") || empieza) {


lbVisor.Text = dato;
empieza = false;
}
else lbVisor.Text += dato;
}

void Bt0Click(object sender, System.EventArgs e) { AddDato("0"); }


void Bt1Click(object sender, System.EventArgs e) { AddDato("1"); }
void Bt2Click(object sender, System.EventArgs e) { AddDato("2"); }
void Bt3Click(object sender, System.EventArgs e) { AddDato("3"); }
void Bt4Click(object sender, System.EventArgs e) { AddDato("4"); }
void Bt5Click(object sender, System.EventArgs e) { AddDato("5"); }
void Bt6Click(object sender, System.EventArgs e) { AddDato("6"); }
void Bt7Click(object sender, System.EventArgs e) { AddDato("7"); }
void Bt8Click(object sender, System.EventArgs e) { AddDato("8"); }
void Bt9Click(object sender, System.EventArgs e) { AddDato("9"); }

void BtDecimalClick(object sender, System.EventArgs e) {AddDato(",");}


void BtporClick(object sender, System.EventArgs e) { AddDato("*"); }
void BtentreClick(object sender, System.EventArgs e) { AddDato("/"); }
void BtmenosClick(object sender, System.EventArgs e) { AddDato("-"); }
void BtmasClick(object sender, System.EventArgs e) { AddDato("+"); }
void BtIgualClick(object sender, System.EventArgs e) { AddDato("="); }
void BtElevarClick(object sender, System.EventArgs e) { AddDato("^"); }
void BtRaizCuadradaClick(object sender, System.EventArgs e)
{
AddDato("v¯");
}
/// <summary>
/// Encargada de inicializar todo a su valor por defecto
/// para poder empezar un nuevo cálculo
/// </summary>
void BtBorrarTodoClick(object sender, System.EventArgs e)
{
lbVisor.Text = "0";
lbdatoanterior.Text = "";
lbUltOp.Text = "";
empieza = true;
}

-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

this.btAtras.Location = new System.Drawing.Point(67, 56);


this.btAtras.Name = "btAtras";
this.btAtras.Size = new System.Drawing.Size(40, 34);
this.btAtras.TabIndex = 2;
this.btAtras.Text = "<--";
this.btAtras.UseVisualStyleBackColor = true;
this.btAtras.Click += new
System.EventHandler(this.BtAtrasClick);
//
// bt7
//
this.bt7.Location = new System.Drawing.Point(18, 109);
this.bt7.Name = "bt7";
this.bt7.Size = new System.Drawing.Size(43, 39);
this.bt7.TabIndex = 3;
this.bt7.Text = "7";
this.bt7.UseVisualStyleBackColor = true;
this.bt7.Click += new System.EventHandler(this.Bt7Click);
//
// bt8
//
this.bt8.Location = new System.Drawing.Point(67, 109);
this.bt8.Name = "bt8";
this.bt8.Size = new System.Drawing.Size(43, 39);
this.bt8.TabIndex = 4;
this.bt8.Text = "8";
this.bt8.UseVisualStyleBackColor = true;
this.bt8.Click += new System.EventHandler(this.Bt8Click);
//
// bt9
//
this.bt9.Location = new System.Drawing.Point(116, 109);
this.bt9.Name = "bt9";
this.bt9.Size = new System.Drawing.Size(43, 39);
this.bt9.TabIndex = 5;
this.bt9.Text = "9";
this.bt9.UseVisualStyleBackColor = true;
this.bt9.Click += new System.EventHandler(this.Bt9Click);
//
// bt6
//
this.bt6.Location = new System.Drawing.Point(116, 154);
this.bt6.Name = "bt6";
this.bt6.Size = new System.Drawing.Size(43, 39);
this.bt6.TabIndex = 8;
this.bt6.Text = "6";
this.bt6.UseVisualStyleBackColor = true;
this.bt6.Click += new System.EventHandler(this.Bt6Click);
//
// bt5
//
this.bt5.Location = new System.Drawing.Point(67, 154);
this.bt5.Name = "bt5";
this.bt5.Size = new System.Drawing.Size(43, 39);
this.bt5.TabIndex = 7;
this.bt5.Text = "5";
this.bt5.UseVisualStyleBackColor = true;

-7-
Jose Santiago Jiménez Sarmiento Calculadora en C#
www.iseron.com

this.bt5.Click += new System.EventHandler(this.Bt5Click);


//
// bt4
//
this.bt4.Location = new System.Drawing.Point(18, 154);
this.bt4.Name = "bt4";
this.bt4.Size = new System.Drawing.Size(43, 39);
this.bt4.TabIndex = 6;
this.bt4.Text = "4";
this.bt4.UseVisualStyleBackColor = true;
this.bt4.Click += new System.EventHandler(this.Bt4Click);
//
// bt3
//
this.bt3.Location = new System.Drawing.Point(116, 199);
this.bt3.Name = "bt3";
this.bt3.Size = new System.Drawing.Size(43, 39);
this.bt3.TabIndex = 11;
this.bt3.Text = "3\r\n";
this.bt3.UseVisualStyleBackColor = true;
this.bt3.Click += new System.EventHandler(this.Bt3Click);
//
// bt2
//
this.bt2.Location = new System.Drawing.Point(67, 199);
this.bt2.Name = "bt2";
this.bt2.Size = new System.Drawing.Size(43, 39);
this.bt2.TabIndex = 10;
this.bt2.Text = "2";
this.bt2.UseVisualStyleBackColor = true;
this.bt2.Click += new System.EventHandler(this.Bt2Click);
//
// bt1
//
this.bt1.Location = new System.Drawing.Point(18, 199);
this.bt1.Name = "bt1";
this.bt1.Size = new System.Drawing.Size(43, 39);
this.bt1.TabIndex = 9;
this.bt1.Text = "1";
this.bt1.UseVisualStyleBackColor = true;
this.bt1.Click += new System.EventHandler(this.Bt1Click);
//
// bt0
//
this.bt0.Location = new System.Drawing.Point(18, 244);
this.bt0.Name = "bt0";
this.bt0.Size = new System.Drawing.Size(92, 39);
this.bt0.TabIndex = 12;
this.bt0.Text = "0";
this.bt0.UseVisualStyleBackColor = true;
this.bt0.Click += new System.EventHandler(this.Bt0Click);
//
// btDecimal
//
this.btDecimal.Location = new System.Drawing.Point(116, 244);
this.btDecimal.Name = "btDecimal";
this.btDecimal.Size = new System.Drawing.Size(43, 39);

-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

this.btIgual.Size = new System.Drawing.Size(92, 39);


this.btIgual.TabIndex = 19;
this.btIgual.Text = "=";
this.btIgual.UseVisualStyleBackColor = true;
this.btIgual.Click += new
System.EventHandler(this.BtIgualClick);
//
// lbdatoanterior
//
this.lbdatoanterior.BorderStyle =
System.Windows.Forms.BorderStyle.Fixed3D;
this.lbdatoanterior.Font = new
System.Drawing.Font("Tahoma", 11F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.World);
this.lbdatoanterior.ForeColor =
System.Drawing.SystemColors.Desktop;
this.lbdatoanterior.ImageAlign =
System.Drawing.ContentAlignment.MiddleLeft;
this.lbdatoanterior.Location = new
System.Drawing.Point(135, 56);
this.lbdatoanterior.Name = "lbdatoanterior";
this.lbdatoanterior.Size = new System.Drawing.Size(140, 33);
this.lbdatoanterior.TabIndex = 20;
this.lbdatoanterior.TextAlign =
System.Drawing.ContentAlignment.MiddleRight;
//
// lbUltOp
//
this.lbUltOp.BorderStyle =
System.Windows.Forms.BorderStyle.Fixed3D;
this.lbUltOp.Font = new System.Drawing.Font("Tahoma", 11F,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.World);
this.lbUltOp.ForeColor =
System.Drawing.SystemColors.Desktop;
this.lbUltOp.ImageAlign =
System.Drawing.ContentAlignment.MiddleLeft;
this.lbUltOp.Location = new System.Drawing.Point(109, 56);
this.lbUltOp.Name = "lbUltOp";
this.lbUltOp.Size = new System.Drawing.Size(20, 33);
this.lbUltOp.TabIndex = 21;
this.lbUltOp.TextAlign =
System.Drawing.ContentAlignment.MiddleRight;
//
// btElevar
//
this.btElevar.Location = new System.Drawing.Point(229,
154);
this.btElevar.Name = "btElevar";
this.btElevar.Size = new System.Drawing.Size(43, 39);
this.btElevar.TabIndex = 22;
this.btElevar.Text = "^";
this.btElevar.UseVisualStyleBackColor = true;
this.btElevar.Click += new
System.EventHandler(this.BtElevarClick);
//
// btRaizCuadrada
//

- 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

private System.Windows.Forms.Button bt3;


private System.Windows.Forms.Button bt4;
private System.Windows.Forms.Button bt5;
private System.Windows.Forms.Button bt6;
private System.Windows.Forms.Button bt9;
private System.Windows.Forms.Button bt8;
private System.Windows.Forms.Button bt7;
private System.Windows.Forms.Button btAtras;
private System.Windows.Forms.Button btBorrarTodo;
private System.Windows.Forms.Label lbVisor;
}
}

- 12 -

Вам также может понравиться