Click here to Skip to main content
15,885,936 members
Articles / Programming Languages / C#

Graphic Calculator

Rate me:
Please Sign up or sign in to vote.
3.53/5 (25 votes)
10 Jun 2008CPOL 81.7K   5.4K   65  
Software tool that interactively displays a graphical view of mathematical functions
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;
using System.Collections;

namespace Graficadora
{
    public partial class Calculadora : Form
    {
        public Stack p = new Stack();
        public int clear = 0;
        public double mem;
        public Calculadora()
        {
            InitializeComponent();
        }
        public int prioridad(string c)
        {
            if ((c == "+") || (c == "-"))
                return 0;
            if ((c == "*") || (c == "/"))
                return 1;
            if (c == "^")
                return 2;
            if ((c == "Log") || (c == "Sen") || (c == "Cos") || (c == "Tan") || (c == "Exp"))
                return 3;
            return -1;
        }

        public bool operador(string o)
        {
            if (char.IsDigit(o, 0) || o == "x")
                return false;

            if (char.IsLetter(o, 0))
                return true;
            return true;
        }

        public string[] postfijo(string ecuacion)
        {
            string[] infijo = new string[20];
            int l = ecuacion.Length;
            int j; int k = 0; int i;
            string c; string tmp = "";
            string t = "(";
            for (i = 0; i < l; i++)
            {
                c = ecuacion[i].ToString();
                int band = 0;
                int band2 = 0;
                int band3 = 0;
                //Operando
                if (char.IsDigit(c, 0) || c == "x")
                {
                    j = i;
                    if (j < l)
                    {
                        while (char.IsDigit(ecuacion, j) || ecuacion[j].ToString() == ".")
                        {
                            infijo[k] = infijo[k] + ecuacion[j].ToString();
                            j++;
                            band++;
                            if (j == l)
                            {
                                break;
                            }
                        }
                    }
                    if (band > 0)
                    {
                        k++;
                        j--;
                    }
                    if (c == "x")
                    {
                        infijo[k] = c;
                        k++;
                    }
                    i = j;
                }//Paréntesis (
                else if (c == "(")
                {
                    p.Push(c);
                }//Paréntesis )
                else if (c == ")")
                {
                    while ((p.Peek().ToString() != "("))
                    {
                        infijo[k] = p.Pop().ToString();
                        k++;
                    }
                    p.Pop();
                }
                //Operandos 
                //Funciones seno, coseno, tangente, logaritmo, raiz cuadrada
                else if (c == "-" || c == "+" || c == "*" || c == "/" || c == "T" || c == "S" || c == "C" || c == "L" || c == "^" || c == "E")
                {
                    j = i;
                    tmp = "";
                    while (char.IsLetter(ecuacion, j))
                    {
                        tmp = tmp + ecuacion[j].ToString();
                        j++;
                        band3++;
                    }
                    if (band3 > 0)
                    {
                        p.Push(tmp);
                        j--;
                    }
                    i = j;
                    if (band3 == 0)
                    {
                        while ((p.Count > 0) && (p.Peek().ToString() != "(") && (prioridad(p.Peek().ToString()) >= prioridad(c)))
                        {
                            infijo[k] = p.Pop().ToString();
                            k++;
                            band3++;
                        }
                        p.Push(c);
                    }
                }
            }
            while (p.Count > 0)
            {
                infijo[k] = p.Pop().ToString();
                k++;
            }

            return infijo;
        }

        public double evaluacion(string[] val, double x)
        {
            int i = 0;
            string operando1; string operando2;
            double op1; double op2;

            string c;
            double res = 0;
            for (i = 0; i < val.Length; i++)
            {
                int j = 0;
                if (val[i] == null)
                    break;
                c = val[i].ToString();

                if (operador(c))
                {
                    if (c == "+" || c == "-" || c == "*" || c == "/" || c == "^")
                    {
                        operando2 = p.Pop().ToString();
                        operando1 = p.Pop().ToString();
                        if (operando2 == "x")
                        {
                            op2 = x;
                        }
                        else
                        {
                            op2 = Convert.ToDouble(operando2);
                        }
                        if (operando1 == "x")
                        {
                            op1 = x;
                        }
                        else
                        {
                            op1 = Convert.ToDouble(operando1);
                        }
                        switch (c)
                        {
                            case "+":
                                res = op1 + op2;
                                p.Push(res);
                                break;
                            case "-":
                                res = op1 - op2;
                                p.Push(res);
                                break;
                            case "*":
                                res = op1 * op2;
                                p.Push(res);
                                break;
                            case "/":
                                res = op1 / op2;
                                p.Push(res);
                                break;
                            case "^":
                                res = Math.Pow(op1, op2);
                                p.Push(res);
                                break;
                            default:
                                break;
                        }
                    }
                    else
                    {

                        operando1 = p.Pop().ToString();
                        if (operando1 == "x")
                        {
                            op1 = x;
                        }
                        else
                        {
                            op1 = Convert.ToDouble(operando1);
                        }
                        switch (c)
                        {

                            case "Tan":
                                res = Math.Tan(op1);
                                p.Push(res);
                                break;
                            case "Cos":
                                res = Math.Cos(op1);
                                p.Push(res);
                                break;
                            case "Sen":
                                res = Math.Sin(op1);
                                p.Push(res);
                                break;
                            case "Log":
                                res = Math.Log(op1);
                                p.Push(res);
                                break;
                            case "Sqrt":
                                res = Math.Sqrt(op1);
                                p.Push(res);
                                break;
                            case "Exp":
                                res = Math.Exp(op1);
                                p.Push(res);
                                break;
                            default:
                                break;
                        }
                    }
                }
                else
                {
                    p.Push(c);
                }
            }
            double eval = Convert.ToDouble(p.Pop().ToString());
            return eval;
        }

        private void button22_Click(object sender, EventArgs e)
        {
            if (clear > 0)
                txtEq.Text = "";
            txtEq.Text = txtEq.Text + "1"; clear = 0;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (clear > 0)
                txtEq.Text = "";
            txtEq.Text = txtEq.Text + "2"; clear = 0;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (clear > 0)
                txtEq.Text = "";
            txtEq.Text = txtEq.Text + "3"; clear = 0;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (clear > 0)
                txtEq.Text = "";
            txtEq.Text = txtEq.Text + "4"; clear = 0;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (clear > 0)
                txtEq.Text = "";
            txtEq.Text = txtEq.Text + "7"; clear = 0;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (clear > 0)
                txtEq.Text = "";
            txtEq.Text = txtEq.Text + "5"; clear = 0;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (clear > 0)
                txtEq.Text = "";
            txtEq.Text = txtEq.Text + "6"; clear = 0;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (clear > 0)
                txtEq.Text = "";
            txtEq.Text = txtEq.Text + "8"; clear = 0;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (clear > 0)
                txtEq.Text = "";
            txtEq.Text = txtEq.Text + "9"; clear = 0;
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (clear > 0)
                txtEq.Text = "";
            txtEq.Text = txtEq.Text + "0"; clear = 0;
        }

        private void button11_Click(object sender, EventArgs e)
        {
            if (clear > 0)
                txtEq.Text = "";
            txtEq.Text = txtEq.Text + "+"; clear = 0;
        }

        private void button20_Click(object sender, EventArgs e)
        {
            
            if (txtEq.Text == "")
            {
                MessageBox.Show("Expresión Vacía", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                try
                {
                   
                    txtEq.Text = txtEq.Text + "\n\n" + Convert.ToString(evaluacion(postfijo(txtEq.Text), 1));
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            clear++;
        }

        private void button12_Click(object sender, EventArgs e)
        {
            if (clear > 0)
                txtEq.Text = "";
            txtEq.Text = txtEq.Text + "-"; clear = 0;
        }

        private void button13_Click(object sender, EventArgs e)
        {
            if (clear > 0)
                txtEq.Text = "";
            txtEq.Text = txtEq.Text + "*"; clear = 0;
        }

        private void button21_Click(object sender, EventArgs e)
        {
            if (clear > 0)
                txtEq.Text = "";
            txtEq.Text = txtEq.Text + "^"; clear = 0;
        }

        private void button14_Click(object sender, EventArgs e)
        {
            if (clear > 0)
                txtEq.Text = "";
            txtEq.Text = txtEq.Text + "/"; clear = 0;
        }

        private void button38_Click(object sender, EventArgs e)
        {
            if (clear > 0)
                txtEq.Text = "";
            txtEq.Text = txtEq.Text + "."; clear = 0;
        }

        private void button39_Click(object sender, EventArgs e)
        {
            if (clear > 0)
                txtEq.Text = "";
            txtEq.Text = txtEq.Text + "Exp("; clear = 0;
        }

        private void button19_Click(object sender, EventArgs e)
        {
            if (clear > 0)
                txtEq.Text = "";
            txtEq.Text = txtEq.Text + "Sqrt("; clear = 0;
        }

        private void button27_Click(object sender, EventArgs e)
        {
            if (clear > 0)
                txtEq.Text = "";
            txtEq.Text = txtEq.Text + "("; clear = 0;
        }

        private void button28_Click(object sender, EventArgs e)
        {
            if (clear > 0)
                txtEq.Text = "";
            txtEq.Text = txtEq.Text + ")";
            clear = 0;
        }

        private void button18_Click(object sender, EventArgs e)
        {
            if (clear > 0)
                txtEq.Text = "";
            txtEq.Text = txtEq.Text + "Log("; clear = 0;
        }

        private void button16_Click(object sender, EventArgs e)
        {
            if (clear > 0)
                txtEq.Text = "";
            txtEq.Text = txtEq.Text + "Cos("; clear = 0;
        }

        private void button15_Click(object sender, EventArgs e)
        {
            if (clear > 0)
                txtEq.Text = "";
            txtEq.Text = txtEq.Text + "Sen("; clear = 0;
        }

        private void button17_Click(object sender, EventArgs e)
        {
            if (clear > 0)
                txtEq.Text = "";
            txtEq.Text = txtEq.Text + "Tan("; clear = 0;
        }

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

        private void button23_Click(object sender, EventArgs e)
        {
            int i = txtEq.Text.Length;
            try
            {
                txtEq.Text = txtEq.Text.Remove(i - 1, 1);
            }
            catch (Exception ex)
            {
                MessageBox.Show("No hay nada para eliminar, error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void txtEq_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void txtEq_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue < 47 || e.KeyValue > 58)
                e.SuppressKeyPress = true;
            else if (e.KeyData == Keys.Decimal || e.KeyData == Keys.Divide || e.KeyData == Keys.Subtract)
                e.SuppressKeyPress = false;
        }

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

        private void Calculadora_Load(object sender, EventArgs e)
        {

        }

        private void button26_Click(object sender, EventArgs e)
        {
            if (clear > 0)
            { 
              
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems / Hardware Administrator
Mexico Mexico
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions