Mostrando entradas con la etiqueta Desarrollo C#. Mostrar todas las entradas
Mostrando entradas con la etiqueta Desarrollo C#. Mostrar todas las entradas
miércoles, 26 de abril de 2017
lunes, 13 de febrero de 2012
Calculadora Cientifica

Calculadora Cientifica C Sharp
En esta ocacion les presento la nueva versión de la calculadora científica publicada en entradas anteriores .
Entre los cambios realizados se encuentran la integración de funciones para calculadora científica, funciones trigonométricas, botones de memoria y retroceso.


Lenguaje de programación C Sharp.
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 CalculadoraCientifica
{
public partial class Calculadora : Form
{
double Valor1; //-----------Declaracion de Variables
double Valor2;
double Resultado;
bool Inicio = true;
bool Operacion = true;
bool Igual = true;
bool TOperacion = true;
string TipoOperacion;
double Memoria = 0;
double a, b, c;
public Calculadora()
{
InitializeComponent();
}
//--------------------------------Botones Numericos
private void Btn1_Click(object sender, EventArgs e)
{
if (Inicio)
{
TxtPrincipal.Text = "";
TxtPrincipal.Text = "1";
Inicio = false;
}
else
TxtPrincipal.Text = TxtPrincipal.Text + "1";
}
private void Btn2_Click(object sender, EventArgs e)
{
if (Inicio)
{
TxtPrincipal.Text = "";
TxtPrincipal.Text = "2";
Inicio = false;
}
else
TxtPrincipal.Text = TxtPrincipal.Text + "2";
}
private void Btn3_Click(object sender, EventArgs e)
{
if (Inicio)
{
TxtPrincipal.Text = "";
TxtPrincipal.Text = "3";
Inicio = false;
}
else
TxtPrincipal.Text = TxtPrincipal.Text + "3";
}
private void Btn4_Click(object sender, EventArgs e)
{
if (Inicio)
{
TxtPrincipal.Text = "";
TxtPrincipal.Text = "4";
Inicio = false;
}
else
TxtPrincipal.Text = TxtPrincipal.Text + "4";
}
private void Btn5_Click(object sender, EventArgs e)
{
if (Inicio)
{
TxtPrincipal.Text = "";
TxtPrincipal.Text = "5";
Inicio = false;
}
else
TxtPrincipal.Text = TxtPrincipal.Text + "5";
}
private void Btn6_Click(object sender, EventArgs e)
{
if (Inicio)
{
TxtPrincipal.Text = "";
TxtPrincipal.Text = "6";
Inicio = false;
}
else
TxtPrincipal.Text = TxtPrincipal.Text + "6";
}
private void Btn7_Click(object sender, EventArgs e)
{
if (Inicio)
{
TxtPrincipal.Text = "";
TxtPrincipal.Text = "7";
Inicio = false;
}
else
TxtPrincipal.Text = TxtPrincipal.Text + "7";
}
private void Btn8_Click(object sender, EventArgs e)
{
if (Inicio)
{
TxtPrincipal.Text = "";
TxtPrincipal.Text = "8";
Inicio = false;
}
else
TxtPrincipal.Text = TxtPrincipal.Text + "8";
}
private void Btn9_Click(object sender, EventArgs e)
{
if (Inicio)
{
TxtPrincipal.Text = "";
TxtPrincipal.Text = "9";
Inicio = false;
}
else
TxtPrincipal.Text = TxtPrincipal.Text + "9";
}
private void Btn0_Click(object sender, EventArgs e)
{
if (!Inicio)
{
TxtPrincipal.Text = TxtPrincipal.Text + "0";
Inicio = false;
}
}
private void BtnDecimal_Click(object sender, EventArgs e)
{
if (TxtPrincipal.Text.Contains(".")) { }
else
{
TxtPrincipal.Text = TxtPrincipal.Text + ".";
Inicio = false;
}
}
//------------------------Botones C, CE, Retroceso, Cambio de signo y botones de Memoria
private void BtnCE_Click(object sender, EventArgs e)
{
TxtPrincipal.Text = "";
Inicio = true;
}
private void BtnC_Click(object sender, EventArgs e)
{
TxtPrincipal.Text = "0";
TxtPrevio.Text = "";
Inicio = true;
Operacion = true;
Igual = true;
TOperacion = true;
Valor1 = 0;
Valor2 = 0;
Resultado = 0;
}
private void BtnRetroceso_Click(object sender, EventArgs e)
{
if (TxtPrincipal.Text.Length > 1)
TxtPrincipal.Text = TxtPrincipal.Text.Remove(TxtPrincipal.Text.Length - 1, 1);
if (TxtPrincipal.Text.Length == 1)
{
TxtPrincipal.Text = "0";
Inicio = true;
}
if (TxtPrincipal.Text == "0") { }
}
private void BtnSigno_Click(object sender, EventArgs e)
{
TxtPrincipal.Text = Convert.ToString(0 - Convert.ToDouble(TxtPrincipal.Text));
}
private void BtnMmenos_Click(object sender, EventArgs e)
{
Memoria = Memoria - Convert.ToDouble(TxtPrincipal.Text);
this.LblMemoria.Visible = true;
}
private void BtnMmas_Click(object sender, EventArgs e)
{
Memoria = Memoria + Convert.ToDouble(TxtPrincipal.Text);
this.LblMemoria.Visible = true;
}
private void BtnMS_Click(object sender, EventArgs e)
{
Memoria = Convert.ToDouble(TxtPrincipal.Text);
this.LblMemoria.Visible = true;
}
private void BtnMR_Click(object sender, EventArgs e)
{
TxtPrincipal.Text = Memoria.ToString();
}
private void BtnMC_Click(object sender, EventArgs e)
{
Memoria = 0;
this.LblMemoria.Visible = false;
}
//---------------------------------------Operaciones Basicas
private void BtnSuma_Click(object sender, EventArgs e)
{
Inicio = true;
Igual = true;
if (Operacion)
{
Valor1 = Convert.ToDouble(TxtPrincipal.Text);
TxtPrevio.Text = "";
TxtPrevio.Text = TxtPrevio.Text + TxtPrincipal.Text + " + ";
Operacion = false;
}
else
{
if (TOperacion)
{
Valor2 = Convert.ToDouble(TxtPrincipal.Text);
TxtPrevio.Text = TxtPrevio.Text + TxtPrincipal.Text + " + ";
Operaciones(Valor1, Valor2); //---------------------------Llama ala funcion Operaciones para realizar la operacion indicada
TOperacion = false;
}
else
{
TxtPrevio.Text = TxtPrevio.Text + TxtPrincipal.Text + " + ";
Operaciones(Resultado, Valor2);
//TxtPrevio.Text = "";
}
}
TipoOperacion = "+";
}
private void BtnResta_Click(object sender, EventArgs e)
{
Inicio = true;
Igual = true;
if (Operacion)
{
Valor1 = Convert.ToDouble(TxtPrincipal.Text);
TxtPrevio.Text = "";
TxtPrevio.Text = TxtPrevio.Text + TxtPrincipal.Text + " - ";
Operacion = false;
}
else
{
if (TOperacion)
{
Valor2 = Convert.ToDouble(TxtPrincipal.Text);
TxtPrevio.Text = TxtPrevio.Text + TxtPrincipal.Text + " - ";
Operaciones(Valor1, Valor2);
TOperacion = false;
}
else
{
TxtPrevio.Text = TxtPrevio.Text + TxtPrincipal.Text + " - ";
Operaciones(Resultado, Valor2);
//TxtPrevio.Text = "";
}
}
TipoOperacion = "-";
}
private void BtnMultiplicacion_Click(object sender, EventArgs e)
{
Inicio = true;
Igual = true;
if (Operacion)
{
Valor1 = Convert.ToDouble(TxtPrincipal.Text);
TxtPrevio.Text = "";
TxtPrevio.Text = TxtPrevio.Text + TxtPrincipal.Text + " * ";
Operacion = false;
}
else
{
if (TOperacion)
{
Valor2 = Convert.ToDouble(TxtPrincipal.Text);
TxtPrevio.Text = TxtPrevio.Text + TxtPrincipal.Text + " * ";
Operaciones(Valor1, Valor2);
TOperacion = false;
}
else
{
TxtPrevio.Text = TxtPrevio.Text + TxtPrincipal.Text + " * ";
Operaciones(Resultado, Valor2);
//TxtPrevio.Text = "";
}
}
TipoOperacion = "*";
}
private void BtnDivision_Click(object sender, EventArgs e)
{
Inicio = true;
Igual = true;
if (Operacion)
{
Valor1 = Convert.ToDouble(TxtPrincipal.Text);
TxtPrevio.Text = "";
TxtPrevio.Text = TxtPrevio.Text + TxtPrincipal.Text + " / ";
Operacion = false;
}
else
{
if (TOperacion)
{
Valor2 = Convert.ToDouble(TxtPrincipal.Text);
TxtPrevio.Text = TxtPrevio.Text + TxtPrincipal.Text + " / ";
Operaciones(Valor1, Valor2);
TOperacion = false;
}
else
{
TxtPrevio.Text = TxtPrevio.Text + TxtPrincipal.Text + " / ";
Operaciones(Resultado, Valor2);
}
}
TipoOperacion = "/";
}
private void BtnPorcentaje_Click(object sender, EventArgs e)
{
Valor2 = Convert.ToDouble(TxtPrincipal.Text);
TxtPrevio.Text = TxtPrevio.Text + TxtPrincipal.Text;
TxtPrincipal.Text = Convert.ToString((Valor1 * Valor2) / 100);
Igual = true;
}
//------------------------Operaciones Cientificas
//Realiza la operacion del boton 1/x
private void Btn1x_Click(object sender, EventArgs e)
{
Valor1 = Convert.ToDouble(TxtPrincipal.Text);
TxtPrevio.Text = "";
TxtPrincipal.Text = Convert.ToString(1 / Valor1);
}
//Raiz Cuadrada
private void BtnRaizCuadrada_Click(object sender, EventArgs e)
{
Valor1 = Convert.ToDouble(TxtPrincipal.Text);
if (Valor1 >= 0)
{
TxtPrevio.Text = "√ " + Valor1;
TxtPrincipal.Text = Convert.ToString(Math.Sqrt(Valor1));
}
else
TxtPrincipal.Text = "Error";
}
//Log10 de x
private void Btnlog_Click(object sender, EventArgs e)
{
Valor1 = Convert.ToDouble(TxtPrincipal.Text);
TxtPrevio.Text = "log( " + Valor1 + " )";
TxtPrincipal.Text = Convert.ToString(Math.Log10(Valor1));
}
//Eleva a x al cuadrado
private void BtnCuadrado_Click(object sender, EventArgs e)
{
Valor1 = Convert.ToDouble(TxtPrincipal.Text);
TxtPrevio.Text = Valor1 + " ^ 2";
TxtPrincipal.Text = Math.Pow(Valor1, 2).ToString();
}
//Eleva a x al cubo
private void BtnCubo_Click(object sender, EventArgs e)
{
Valor1 = Convert.ToDouble(TxtPrincipal.Text);
TxtPrevio.Text = Valor1 + " ^ 3";
TxtPrincipal.Text = Math.Pow(Valor1, 3).ToString();
}
//Eleva a x a la -1
private void BtnXmenos1_Click(object sender, EventArgs e)
{
Valor1 = Convert.ToDouble(TxtPrincipal.Text);
TxtPrevio.Text = Valor1 + " ^ -1";
TxtPrincipal.Text = Math.Pow(Valor1, -1).ToString();
}
//Eleva un numero a x Potencia
private void BtnExponente_Click(object sender, EventArgs e)
{
Inicio = true;
Valor1 = Convert.ToDouble(TxtPrincipal.Text);
TxtPrevio.Text = Valor1 + " ^ ";
TipoOperacion = "potencia";
Operacion = false;
Igual = true;
}
//Devuelve el valor de Pi
private void BtnPi_Click(object sender, EventArgs e)
{
TxtPrevio.Text = "";
TxtPrincipal.Text = Math.PI.ToString();
}
//Raiza cubica
private void BtnRaizCubica_Click(object sender, EventArgs e)
{
Valor1 = Convert.ToDouble(TxtPrincipal.Text);
a = 1;
b = 3;
c = a / b;
TxtPrincipal.Text = Math.Pow(Valor1, c).ToString();
TxtPrevio.Text = "3√ " + Valor1;
}
//Raiz x de un numero
private void BtnRaizX_Click(object sender, EventArgs e)
{
Inicio = true;
Valor1 = Convert.ToDouble(TxtPrincipal.Text);
TxtPrevio.Text = " √ " + Valor1;
TipoOperacion = "raizX";
Operacion = false;
Igual = true;
}
//Boton EXP
private void BtnEXP_Click(object sender, EventArgs e)
{
Valor1 = Convert.ToDouble(TxtPrincipal.Text);
TxtPrincipal.Text = Convert.ToString(Math.Exp(Valor1));
}
//Eleva 10 a la x potencia
private void Btn10alaX_Click(object sender, EventArgs e)
{
Valor1 = Convert.ToDouble(TxtPrincipal.Text);
TxtPrevio.Text = "Potnecia( " + Valor1 + " )";
TxtPrincipal.Text = Convert.ToString(Math.Pow(10, Valor1));
}
//Devuelve el factorial de un numero
private void BtnFactorial_Click(object sender, EventArgs e)
{
a = 1;
Valor1 = Convert.ToDouble(TxtPrincipal.Text);
TxtPrevio.Text = Valor1 + "!";
for (b = 1; b <= Valor1; b++)
{
a = a * b;
}
TxtPrincipal.Text = Convert.ToString(a);
}
//Devuelve el valor de e
private void Btne_Click(object sender, EventArgs e)
{
TxtPrevio.Text = "";
TxtPrincipal.Text = Convert.ToString(Math.E);
}
//-----------------Funciones Trigonometricas
//Devuelve Sin de un numero
private void BtnSin_Click(object sender, EventArgs e)
{
Valor1 = Convert.ToDouble(TxtPrincipal.Text);
TxtPrevio.Text = "sin( " + Valor1 + " )";
TxtPrincipal.Text = Convert.ToString(Math.Sin(Valor1));
}
//Devuelve Sin hiperbolico de un numero
private void BtnSinh_Click(object sender, EventArgs e)
{
Valor1 = Convert.ToDouble(TxtPrincipal.Text);
TxtPrevio.Text = "sinh( " + Valor1 + " )";
TxtPrincipal.Text = Convert.ToString(Math.Sinh(Valor1));
}
//Devuelve Cos de un numero
private void BtnCos_Click(object sender, EventArgs e)
{
Valor1 = Convert.ToDouble(TxtPrincipal.Text);
TxtPrevio.Text = "cos( " + Valor1 + " )";
TxtPrincipal.Text = Convert.ToString(Math.Cos(Valor1));
}
//Devuelve Cos hiperbolico de un numero
private void BtnCosh_Click(object sender, EventArgs e)
{
Valor1 = Convert.ToDouble(TxtPrincipal.Text);
TxtPrevio.Text = "cosh( " + Valor1 + " )";
TxtPrincipal.Text = Convert.ToString(Math.Cosh(Valor1));
}
//Devuelve Tan de un numero
private void BtnTan_Click(object sender, EventArgs e)
{
Valor1 = Convert.ToDouble(TxtPrincipal.Text);
TxtPrevio.Text = "tan( " + Valor1 + " )";
TxtPrincipal.Text = Convert.ToString(Math.Tan(Valor1));
}
//Devuelve Tan hiperbolico de un numero
private void BtnTanh_Click(object sender, EventArgs e)
{
Valor1 = Convert.ToDouble(TxtPrincipal.Text);
TxtPrevio.Text = "tanh( " + Valor1 + " )";
TxtPrincipal.Text = Convert.ToString(Math.Tanh(Valor1));
}
//-----------------------Boton de Igualdad
private void BtnIgual_Click(object sender, EventArgs e)
{
Inicio = true;
Operacion = true;
if (Igual)
{
if (TipoOperacion == null) { }
else
{
Valor2 = Convert.ToDouble(TxtPrincipal.Text);
TxtPrevio.Text = TxtPrevio.Text + TxtPrincipal.Text;
Operaciones(Valor1, Valor2);
Igual = false;
}
}
else
{
TxtPrevio.Text = "";
Operaciones(Resultado, Valor2);
}
}
//-------------------Funcion que realiza las operaciones
private void Operaciones(double Valor1, double Valor2)
{
switch (TipoOperacion)
{
case "+":
Resultado = Valor1 + Valor2;
TxtPrincipal.Text = Resultado.ToString();
Valor1 = Convert.ToDouble(TxtPrincipal.Text);
break;
case "-":
Resultado = Valor1 - Valor2;
TxtPrincipal.Text = Resultado.ToString();
Valor1 = Convert.ToDouble(TxtPrincipal.Text);
break;
case "*":
Resultado = Valor1 * Valor2;
TxtPrincipal.Text = Resultado.ToString();
Valor1 = Convert.ToDouble(TxtPrincipal.Text);
break;
case "/":
if (Valor2 == 0)
{
TxtPrincipal.Text = "Error";
break;
}
else
{
Resultado = Valor1 / Valor2;
TxtPrincipal.Text = Resultado.ToString();
Valor1 = Convert.ToDouble(TxtPrincipal.Text);
break;
}
case "potencia":
{
Resultado = Math.Pow(Valor1, Valor2);
TxtPrincipal.Text = Resultado.ToString();
break;
}
case "raizX":
{
if (Valor2 <= 0)
{
TxtPrincipal.Text = "Error";
break;
}
else
{
TxtPrevio.Text = Valor2 + " " + TxtPrevio.Text;
TxtPrevio.Text = TxtPrevio.Text.Remove(TxtPrevio.Text.Count() - TxtPrincipal.Text.Count());
double a = 1;
double c = a / Valor2;
TxtPrincipal.Text = Math.Pow(Valor1, c).ToString();
break;
}
}
}
}
//-----------------------------Botones del Menu
private void copiarToolStripMenuItem_Click_1(object sender, EventArgs e)
{
Clipboard.SetDataObject(TxtPrincipal.Text);
}
private void pegarToolStripMenuItem_Click_1(object sender, EventArgs e)
{
TxtPrincipal.Text = "";
TxtPrincipal.Paste();
}
private void salirToolStripMenuItem_Click_1(object sender, EventArgs e)
{
Application.Exit();
}
private void acercadeToolStripMenuItem_Click(object sender, EventArgs e)
{
Acercade Acercade = new Acercade();
Acercade.Show();
}
}
}

Acá dejo el proyecto en VisualStudio 2010 para que lo descargues
viernes, 16 de septiembre de 2011
Calculadora en C#

Últimamente he estado algo flojo en cuanto a programación, hace mucho que no lo hago y empezare a recuperar el tiempo perdido, comenzare con C# en Visual Studio 2010 del que no se mucho y con algo sencillo como una calculadora simple, después podre ir mejorando mis proyectos.
Mostrare el código utilizado y también dejare el proyecto completo terminado al final del post.
Para comenzar, con este esquema donde se muestran los elementos de la ventana que utilice como los botones y en este caso utilice 2 pantallas o TextBox.

Lo primero es añadir los elementos y organizarlos como queramos que se vea nuestra calculadora y darle nombre a nuestros elementos para no confundirlos en este caso nombre a los botones bt1 al botón con el numero 1, bt2, bt3, btsum, etc., a la pantalla principal la nombre Pantalla y a la otra donde se muestran las operaciones que se van haciendo la nombre Previo.
Ahora una breve explicación de lo que queremos que haga nuestra calculadora tomando como referencia la calculadora de windows.
Cuando abrimos la calculadora lo primero que vemos es que en la pantalla de inicio nos muestra el numero cero.
Botones numéricos. al iniciar la calculadora y apretar el numero cero, no pasa nada hasta que presionamos otro numero se borra el cero de inicio y entonces podemos poner el numero que queramos.
Botón del punto decimal, solo queremos que el punto se imprima en pantalla una sola vez como debe de ser.
Botones de operación. si lo primero que hacemos al iniciar la calculadora es presionar uno de estos números entonces se tomara al cero de inicio como primer valor para la operación.
También tenemos dos estados, por decirlo de alguna manera, de estos botones, primero cuando se utilizan pro primera vez, es decir, la primera vez que presionemos cualquiera de estos botones, y segundo, cuando ya hemos hecho una operación nos muestre el resultado automáticamente.
Botón de igualdad. también puse dos estados, el primero que realiza la operación correspondiente y el segundo que muestra el resultado según el segundo numero introducido de forma automática al seguir presionando este botón. Eje. 2 + 1 = 3, si seguimos presionando el botón de igual nos mostrara el valor del resultado + 1 que es el segundo valor que introdujimos.
Botones de borrado que conocemos bien, el botón C nos borra absolutamente todo, pantalla y variables, todo lo regresa al estado de inicio, y el botón CE que solamente nos limpia la pantalla actual y la devuelve a cero.
Tambien se utilizan funciones en el menuStrip, es este proyecto solo tres menus:
Archivo --> Cerrar. Lo que hacer es cerrar la aplicación.
Editar. Aquí tenemos las funciones de Copiar y Pegar lo que esta en pantalla.
Ayuda --> Acerca de... Una breve explicación del proyecto, versión y demás información que queramos poner, yo lo puse en un nuevo form al que llamo desde este menú.
Dicho lo anterior vamos a lo que nos interesa comenzare por las variables.
bool ceroinicial = true; //para controlar el cero que tiene la calculadora al iniciar bool pdecimal = true; //para controlar el punto decimal bool op = true; //para controlar el resultado cuando se presiona cada signo aritmetico bool igual = true; //para controlar el estado del boton de igualdad string operacion; //variable usada en el case de la igualdad double numero1 = 0; //variable para el primer valor double numero2 = 0; //variabla para el segundo valor double temp = 0; //variable para el resultado
Ahora vamos con los botones numéricos.
if (ceroinicial)
{
txtPantalla.Text = " ";
txtPantalla.Text = "1";
ceroinicial = false;
}
else
{
txtPantalla.Text = txtPantalla.Text + "1";
}
Ese código va para los números del 1 al 9, ya que el cero se comporta diferente y el código va como sigue.
if (txtPantalla.Text == "0")
{
}
else
{
if (ceroinicial)
{
txtPantalla.Text = " ";
txtPantalla.Text = "0";
ceroinicial = false;
}
else
{
txtPantalla.Text = txtPantalla.Text + "0";
}
}
El punto decimal.
if (ceroinicial) //cuando el punto es el primer boton que presionas
{
txtPantalla.Text = " ";
txtPantalla.Text = "0";
ceroinicial = false;
}
if (pdecimal) // cuando no hay punto en pantalla imprime el punto, de lo contrario no hace nada
{
txtPantalla.Text = txtPantalla.Text + ".";
pdecimal = false;
}
Seguimos con las operaciones básicas.
Suma.
ceroinicial = true;
pdecimal = true;
if (op) //Guarda primer valor en variable al presionar el boton suma por primera vez
{
numero1 = double.Parse(txtPantalla.Text);
txtPrevio.Text = txtPrevio.Text + txtPantalla.Text + " + ";
op = false;
}
else //Muestra el resultado al presionar el boton suma por segunda ocacion
{
numero2 = double.Parse(txtPantalla.Text);
txtPrevio.Text = txtPrevio.Text + txtPantalla.Text + " + ";
switch (operacion)
{
case "+":
temp = numero1 + numero2;
txtPantalla.Text = temp.ToString();
numero1 = double.Parse(txtPantalla.Text);
break;
case "-":
temp = numero1 - numero2;
txtPantalla.Text = temp.ToString();
numero1 = double.Parse(txtPantalla.Text);
break;
case "*":
temp = numero1 * numero2;
txtPantalla.Text = temp.ToString();
numero1 = double.Parse(txtPantalla.Text);
break;
case "/":
if (numero2 == 0)
{
txtPantalla.Text = "Error";
break;
}
else
{
temp = numero1 / numero2;
txtPantalla.Text = temp.ToString();
numero1 = double.Parse(txtPantalla.Text);
break;
}
}
}
operacion = "+";
Resta
ceroinicial = true;
pdecimal = true;
if (op) //Guarda primer valor en variable al presionar el boton resta por primera vez
{
numero1 = double.Parse(txtPantalla.Text);
txtPrevio.Text = txtPrevio.Text + txtPantalla.Text + " - ";
op = false;
}
else //Muestra el resultado al presionar el boton resta por segunda ocacion
{
numero2 = double.Parse(txtPantalla.Text);
txtPrevio.Text = txtPrevio.Text + txtPantalla.Text + " - ";
switch (operacion)
{
case "+":
temp = numero1 + numero2;
txtPantalla.Text = temp.ToString();
numero1 = double.Parse(txtPantalla.Text);
break;
case "-":
temp = numero1 - numero2;
txtPantalla.Text = temp.ToString();
numero1 = double.Parse(txtPantalla.Text);
break;
case "*":
temp = numero1 * numero2;
txtPantalla.Text = temp.ToString();
numero1 = double.Parse(txtPantalla.Text);
break;
case "/":
if (numero2 == 0)
{
txtPantalla.Text = "Error";
break;
}
else
{
temp = numero1 / numero2;
txtPantalla.Text = temp.ToString();
numero1 = double.Parse(txtPantalla.Text);
break;
}
}
}
operacion = "-";
Multiplicación
ceroinicial = true;
pdecimal = true;
if (op) //Guarda primer valor en variable al presionar el boton multiplicar por primera vez
{
numero1 = double.Parse(txtPantalla.Text);
txtPrevio.Text = txtPrevio.Text + txtPantalla.Text + " * ";
op = false;
}
else //Muestra el resultado al presionar el boton multiplicar por segunda ocacion
{
numero2 = double.Parse(txtPantalla.Text);
txtPrevio.Text = txtPrevio.Text + txtPantalla.Text + " * ";
switch (operacion)
{
case "+":
temp = numero1 + numero2;
txtPantalla.Text = temp.ToString();
numero1 = double.Parse(txtPantalla.Text);
break;
case "-":
temp = numero1 - numero2;
txtPantalla.Text = temp.ToString();
numero1 = double.Parse(txtPantalla.Text);
break;
case "*":
temp = numero1 * numero2;
txtPantalla.Text = temp.ToString();
numero1 = double.Parse(txtPantalla.Text);
break;
case "/":
if (numero2 == 0)
{
txtPantalla.Text = "Error";
break;
}
else
{
temp = numero1 / numero2;
txtPantalla.Text = temp.ToString();
numero1 = double.Parse(txtPantalla.Text);
break;
}
}
}
operacion = "*";
División. Sabemos que no se puede dividir entre cero.
ceroinicial = true;
pdecimal = true;
if (op) //Guarda primer valor en variable al presionar el boton division por primera vez
{
numero1 = double.Parse(txtPantalla.Text);
txtPrevio.Text = txtPrevio.Text + txtPantalla.Text + " / ";
op = false;
}
else //Muestra el resultado al presionar el boton division por segunda ocacion
{
numero2 = double.Parse(txtPantalla.Text);
txtPrevio.Text = txtPrevio.Text + txtPantalla.Text + " / ";
switch (operacion)
{
case "+":
temp = numero1 + numero2;
txtPantalla.Text = temp.ToString();
numero1 = double.Parse(txtPantalla.Text);
break;
case "-":
temp = numero1 - numero2;
txtPantalla.Text = temp.ToString();
numero1 = double.Parse(txtPantalla.Text);
break;
case "*":
temp = numero1 * numero2;
txtPantalla.Text = temp.ToString();
numero1 = double.Parse(txtPantalla.Text);
break;
case "/":
if (numero2 == 0)
{
txtPantalla.Text = "Error";
break;
}
else
{
temp = numero1 / numero2;
txtPantalla.Text = temp.ToString();
numero1 = double.Parse(txtPantalla.Text);
break;
}
}
}
operacion = "/";
Botón de igualdad
//devuelve las variables a su estado inicial
ceroinicial = true;
pdecimal = true;
op = true;
if (igual) // realiza la operacion correspondiente si es la primeravez que se presiona el boton
{
numero2 = double.Parse(txtPantalla.Text);
switch (operacion)
{
case "+":
txtPrevio.Text = txtPrevio.Text + txtPantalla.Text;
temp = numero1 + numero2;
txtPantalla.Text = temp.ToString();
break;
case "-":
txtPrevio.Text = txtPrevio.Text + txtPantalla.Text;
temp = numero1 - numero2;
txtPantalla.Text = temp.ToString();
break;
case "*":
txtPrevio.Text = txtPrevio.Text + txtPantalla.Text;
temp = numero1 * numero2;
txtPantalla.Text = temp.ToString();
break;
case "/":
if (numero2 == 0)
{
txtPrevio.Text = txtPrevio.Text + txtPantalla.Text;
txtPantalla.Text = "Error";
break;
}
else
{
txtPrevio.Text = txtPrevio.Text + txtPantalla.Text;
temp = numero1 / numero2;
txtPantalla.Text = temp.ToString();
break;
}
}
igual = false;
}
else //Realiza la operacion correspondiente al seguir presionando el boton
{
switch (operacion)
{
case "+":
temp = temp + numero2;
txtPantalla.Text = temp.ToString();
break;
case "-":
temp = temp - numero2;
txtPantalla.Text = temp.ToString();
break;
case "*":
temp = temp * numero2;
txtPantalla.Text = temp.ToString();
break;
case "/":
if (numero2 == 0)
{
txtPrevio.Text = txtPrevio.Text + txtPantalla.Text;
txtPantalla.Text = "Error";
break;
}
else
{
temp = temp / numero2;
txtPantalla.Text = temp.ToString();
break;
}
}
}
txtPrevio.Text = " "; //limpia la pantalla Previo
Boton C
ceroinicial = true;
pdecimal = true;
operacion = null;
op = true;
igual = true;
numero1 = 0;
numero2 = 0;
temp = 0;
txtPrevio.Text = " ";
txtPantalla.Text = "0";
Boton CE
ceroinicial = true;
pdecimal = true;
txtPantalla.Text = "0";
Para cerrar la aplicación desde el menú salir.
Application.Exit();
Para copiar al portapapeles.
Clipboard.SetDataObject(txtPantalla.Text);
Para pegar del portapapeles.
txtPantalla.Text = " "; //Limpiar pantalla primero
txtPantalla.Paste(); //Pegar del portapapeles
Para llamar a otro form, acerca de...
Form2 frm = new Form2();
frm.Show(); //llamar a otro form
Para cerrar el nuevo formulario sin cerrar la aplicación.
this.Close(); //cerrar form2
Con este código agregamos un link a una web con LinkLabel.
System.Diagnostics.Process.Start("http://jaguarnegro18.blogspot.com/"); //Linklabel
Quizás mucho código, falta la creación de una estructura para no tener el mismo código por ejemplo en cada signo de operación y solo llamarlo una sola vez, pero eso lo haré mas adelante, junto con las funciones de una calculadora científica, por el momento esta creo que funcional con las 4 operaciones básicas.
Dejo el proyecto para descargar, tal como lo tengo yo.
Suscribirse a:
Entradas (Atom)
