elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Trabajando con las ramas de git (tercera parte)


  Mostrar Mensajes
Páginas: [1]
1  Programación / .NET (C#, VB.NET, ASP) / Re: Ejercicios básicos C# en: 3 Marzo 2011, 03:57 am
aqui le dejo mi aporte de mi calculadora

Código
  1. namespace WindowsFormsApplication2
  2. {
  3.    public partial class Calculadora : Form
  4.    {
  5.        bool initial = true;
  6.        string operacion;
  7.        Double Resultado;
  8.        Double numero1;
  9.        Double numero2;
  10.        public Calculadora()
  11.        {
  12.            InitializeComponent();
  13.        }
  14.  
  15.        private void GlobalClick(object sender, EventArgs e)
  16.        {
  17.            // Convertir objeto sender en un button
  18.            Button bt = (Button)sender;
  19.  
  20.            // Si es una operacion que inicia se quita el cero
  21.            if (initial)
  22.                txtpantalla.Text = "";
  23.  
  24.            // Escribir el numero seleccionado en el TextBox
  25.            txtpantalla.Text += bt.Text.ToString().Trim();
  26.  
  27.            // Desabilitar la funcion initial
  28.            initial = false;
  29.        }
  30.  
  31.        private void cmdRetroceso_Click(object sender, EventArgs e)
  32.        {
  33.            // Si solo hay un valor entonces el retroseso manda al valor inicial cero
  34.            if (txtpantalla.Text.Trim().Length == 1)
  35.            {
  36.                txtpantalla.Text = "0";
  37.                initial = true;
  38.            }
  39.            else
  40.            {
  41.                // Se elimina el ultimo numero en el campo de txtpantalla
  42.                // 123 usando SubString solo cojo 12
  43.                txtpantalla.Text = txtpantalla.Text.Trim().Substring(0, txtpantalla.Text.Trim().Length - 1);
  44.            }
  45.        }
  46.  
  47.        private void cmdC_Click(object sender, EventArgs e)
  48.        {
  49.            txtpantalla.Text = "0";
  50.            initial = true;
  51.  
  52.        }
  53.        // Botones de operaciones matematicas
  54.        private void cmdsuma_Click(object sender, EventArgs e)
  55.        {
  56.            operacion = "+";
  57.            initial = true;
  58.            numero1 = double.Parse(txtpantalla.Text);
  59.        }
  60.  
  61.        private void cmdresta_Click(object sender, EventArgs e)
  62.        {
  63.            operacion = "-";
  64.            initial = true;
  65.            numero1 = double.Parse(txtpantalla.Text);
  66.        }
  67.  
  68.        private void cmdmulti_Click(object sender, EventArgs e)
  69.        {
  70.            operacion = "*";
  71.            initial = true;
  72.            numero1 = double.Parse(txtpantalla.Text);
  73.        }
  74.  
  75.        private void cmddividir_Click(object sender, EventArgs e)
  76.        {
  77.            operacion = "/";
  78.            initial = true;
  79.            numero1 = double.Parse(txtpantalla.Text);
  80.        }
  81.  
  82.        private void cmdigual_Click(object sender, EventArgs e)
  83.        {
  84.            numero2 = double.Parse(txtpantalla.Text);
  85.            initial = true;
  86.  
  87.  
  88.            switch (operacion)
  89.            {
  90.                case "+":
  91.                    Resultado = numero1 + numero2;
  92.                    txtpantalla.Text = Resultado.ToString();
  93.                    break;
  94.  
  95.                case "-":
  96.                    Resultado = numero1 - numero2;
  97.                    txtpantalla.Text = Resultado.ToString();
  98.                    break;
  99.  
  100.                case "*":
  101.                    Resultado = numero1 * numero2;
  102.                    txtpantalla.Text = Resultado.ToString();
  103.                    break;
  104.  
  105.  
  106.                case "/":
  107.                    Resultado = numero1 / numero2;
  108.                    txtpantalla.Text = Resultado.ToString();
  109.                    break;
  110.                //me falta el %
  111.  
  112.            }
  113.        }
  114.    }
  115. }


esto es lo que tengo hasta el momento  espera sus comentarios
Páginas: [1]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines