martes, 7 de agosto de 2012

calculadora cientifica en csharp


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 WindowsFormsApplication1
{
    public partial class formaPrincipal : Form
    {
        double resultado, suma, numero1,numero2 = 0;
        bool swi = true ;
        string operador = "";
        public formaPrincipal()
        {
            InitializeComponent();
         
        }

        private void button16_Click(object sender, EventArgs e)
        {
            try
            {
                operador = "-";
                numero1 = double.Parse(cajaTexto.Text);
                swi = true;
            }
            catch (Exception)
            {
                MessageBox.Show("digite un valor valido", "error");
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {
            cajaTexto.Text = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (swi)
            {
                cajaTexto.Text = "";
                cajaTexto.Text = "1";
                swi = false;
            }
            else
                cajaTexto.Text = cajaTexto.Text + "1";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (swi)
            {
                cajaTexto.Text = "";
                cajaTexto.Text = "2";
                swi = false;
            }
            else
                cajaTexto.Text = cajaTexto.Text + "2";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (swi)
            {
                cajaTexto.Text = "";
                cajaTexto.Text = "3";
                swi = false;
            }
            else
                cajaTexto.Text = cajaTexto.Text + "3";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (swi)
            {
                cajaTexto.Text = "";
                cajaTexto.Text = "4";
                swi = false;
            }
            else
                cajaTexto.Text = cajaTexto.Text + "4";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (swi)
            {
                cajaTexto.Text = "";
                cajaTexto.Text = "5";
                swi = false;
            }
            else
                cajaTexto.Text = cajaTexto.Text + "5";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (swi)
            {
                cajaTexto.Text = "";
                cajaTexto.Text = "6";
                swi = false;
            }
            else
                cajaTexto.Text = cajaTexto.Text + "6";
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (swi)
            {
                cajaTexto.Text = "";
                cajaTexto.Text = "7";
                swi = false;
            }
            else
                cajaTexto.Text = cajaTexto.Text + "7";
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (swi)
            {
                cajaTexto.Text = "";
                cajaTexto.Text = "8";
                swi = false;
            }
            else
                cajaTexto.Text = cajaTexto.Text + "8";
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (swi)
            {
                cajaTexto.Text = "";
                cajaTexto.Text = "9";
                swi = false;
            }
            else
                cajaTexto.Text = cajaTexto.Text + "9";
        }

        private void button10_Click(object sender, EventArgs e)
        {
            cajaTexto.Text = cajaTexto.Text + "0";
        }

        private void button12_Click(object sender, EventArgs e)
        {
            cajaTexto.Text = "0";
            suma = 0;
            swi = true;
        }

        private void button13_Click(object sender, EventArgs e)
        {
            try
            {
             
                operador = "+";
               numero1 = double.Parse(cajaTexto.Text);
               suma = suma + numero1;
               swi = true;                  
            }
            catch(Exception)
            {
                MessageBox.Show("digite un valor valido","error");
            }
        }

        private void cajaTexto_TextChanged(object sender, EventArgs e)
        {
         
        }

        private void button15_Click(object sender, EventArgs e)
        {
            try
            {
                operador = "*";
                numero1 = double.Parse(cajaTexto.Text);
                if (suma != 0)
                    suma = suma * numero1;

             
                 
                swi = true;
            }
            catch (Exception)
            {
                MessageBox.Show("digite un valor valido", "error");
            }
        }

        private void button14_Click(object sender, EventArgs e)
        {
            try
            {
                operador = "/";
                numero1 = double.Parse(cajaTexto.Text);
                swi = true;
            }
            catch (Exception)
            {
                MessageBox.Show("digite un valor valido", "error");
            }
        }

        private void button28_Click(object sender, EventArgs e)
        {
            try
            {
                if (cajaTexto.Text != "")
                    numero2 = double.Parse(cajaTexto.Text);
            }
            catch(Exception)
            {
                MessageBox.Show("digite un valor valido", "error");
            }
            switch (operador)
            {
                case "+":
                    resultado= suma+ numero2;
                    cajaTexto.Text = resultado.ToString();
                    suma = 0;
                    swi = true;
                    break;
                case "-":
                    if (suma != 0)
                    {
                        resultado = suma - numero2;
                        cajaTexto.Text = resultado.ToString();
                    }
                    else
                    {
                        suma = numero1 - numero2;
                        cajaTexto.Text = suma.ToString();
                    }
                    swi = true;
                    suma = 0;
                    break;
                case "*":
                    if (suma != 0)
                    {
                        resultado = suma * numero2;
                        cajaTexto.Text = resultado.ToString();
                    }
                    else
                    {
                        suma = numero1 * numero2;
                        cajaTexto.Text = suma.ToString();
                    }
                    swi = true;
                    suma = 0;
                    break;
                case "/":
                    if (suma != 0)
                    {
                        resultado = suma / numero2;
                        cajaTexto.Text = resultado.ToString();
                    }
                    else
                    {
                        suma = numero1 / numero2;
                        cajaTexto.Text = suma.ToString();
                    }
                    swi = true;
                    suma = 0;
                    break;
                case "%":
                    if (suma != 0)
                    {
                        resultado = suma %numero2;
                        cajaTexto.Text = resultado.ToString();
                    }
                    else
                    {
                        suma = numero1 % numero2;
                        cajaTexto.Text = suma.ToString();
                    }
                    swi = true;
                    suma = 0;
                    break;
                case "sen":
                    resultado =Math.Sin(numero1) ;
                    cajaTexto.Text = resultado.ToString();
                    swi = true;
                    break;
                case "cos":
                    resultado = Math.Cos(numero1);
                    cajaTexto.Text = resultado.ToString();
                    swi = true;
                    break;
                case "tan":
                    resultado = Math.Tan(numero1);
                    cajaTexto.Text = resultado.ToString();
                    swi = true;
                    break;

                case "pow":
                    if (suma != 0)
                    {
                        resultado = Math.Pow(suma, numero2);
                        cajaTexto.Text = resultado.ToString();
                    }
                    else
                    {
                        suma = Math.Pow(numero1,numero2);
                        cajaTexto.Text = suma.ToString();
                    }
                    swi = true;
                    suma = 0;
                    break;
                case "hyp":
                    if (suma != 0)
                    {
                        resultado = Math.Sqrt((Math.Pow(numero1, 2)) + Math.Pow(numero2, 2));
                        cajaTexto.Text = resultado.ToString();
                    }
                    else
                    {
                        suma = Math.Sqrt((Math.Pow(numero1, 2)) + Math.Pow(numero2, 2));
                        cajaTexto.Text = suma.ToString();
                    }
                    swi = true;
                    suma = 0;
                    break;


            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            try
            {
                operador = "%";
                numero1 = double.Parse(cajaTexto.Text);
                cajaTexto.Text = "";
            }
            catch (Exception)
            {
                MessageBox.Show("digite un valor valido", "error");
            }
        }

        private void button17_Click(object sender, EventArgs e)
        {
            try
            {
                operador = "sen";
                numero1 = double.Parse(cajaTexto.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("digite un valor valido", "error");
            }
        }

        private void button20_Click(object sender, EventArgs e)
        {
            try
            {
                operador = "cos";
                numero1 = double.Parse(cajaTexto.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("digite un valor valido", "error");
            }
        }

        private void button19_Click(object sender, EventArgs e)
        {
            try
            {
                operador = "tan";
                numero1 = double.Parse(cajaTexto.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("digite un valor valido", "error");
            }
        }

        private void button21_Click(object sender, EventArgs e)
        {
            try
            {
                operador = "pow";
                numero1 = double.Parse(cajaTexto.Text);
                swi = true;
            }
            catch (Exception)
            {
                MessageBox.Show("digite un valor valido", "error");
            }
        }

        private void button18_Click(object sender, EventArgs e)
        {
            try
            {
                operador = "sqr";
                numero1 = double.Parse(cajaTexto.Text);
                if (suma != 0)
                {
                    suma = Math.Sqrt(suma);
                    cajaTexto.Text = suma.ToString();
                    suma = 0;
                }
                else
                {
                    suma = Math.Sqrt(numero1);
                    cajaTexto.Text = suma.ToString();
                    suma = 0;
                }
                swi = true;
            }
            catch (Exception)
            {
                MessageBox.Show("digite un valor valido", "error");
            }
        }

        private void button22_Click(object sender, EventArgs e)
        {
            if (suma != 0)
            {
                suma = Math.Pow(Math.E, suma);
                cajaTexto.Text = suma.ToString();
                suma = 0;
                swi = true;
            }
            else
            {
                numero1 = double.Parse(cajaTexto.Text);
                suma = Math.Pow(Math.E, numero1);
                cajaTexto.Text = suma.ToString();
                suma = 0;
                swi = true;
            }
        }

        private void button27_Click(object sender, EventArgs e)
        {
            double acumulador = 1;
            double contador=1;
            numero1 = double.Parse(cajaTexto.Text);
            while(contador <= numero1)
            {
                acumulador = acumulador * contador;
                contador++;

            }
            cajaTexto.Text = acumulador.ToString();
            suma = 0;
            swi = true;
        }

        private void button24_Click(object sender, EventArgs e)
        {
            try
            {
                numero1 = double.Parse(cajaTexto.Text);
                swi = true;
                operador = "hyp";
            }
            catch (Exception)
            {
                MessageBox.Show("error de operacion");
            }
        }

        private void button25_Click(object sender, EventArgs e)
        {

        }
    }
}

No hay comentarios:

Publicar un comentario