Click here to Skip to main content
11,502,518 members (36,217 online)
Click here to Skip to main content
Articles » Languages » C# » Windows Forms » Downloads
Add your own
alternative version

Graphic Calculator

, 10 Jun 2008 CPOL 42.6K 3.1K 62
Software tool that interactively displays a graphical view of mathematical functions
graficadora.zip
Graficadora
Application Files
Graficadora_1_0_0_0
Graficadora.application
Graficadora.exe.deploy
Graficadora.exe.manifest
ZedGraph.dll.deploy
Graficadora_1_0_0_1
Graficadora.application
Graficadora.exe.deploy
Graficadora.exe.manifest
ZedGraph.dll.deploy
Graficadora_1_0_0_2
Graficadora.application
Graficadora.exe.deploy
Graficadora.exe.manifest
ZedGraph.dll.deploy
Graficadora_1_0_0_3
Graficadora.application
Graficadora.exe.deploy
Graficadora.exe.manifest
ZedGraph.dll.deploy
Graficadora.application
setup.exe
GraficadoraSource.zip
Graficadora
bin
Debug
app.publish
Application Files
Graficadora_1_0_0_3
Graficadora.exe.deploy
Graficadora.exe.manifest
ZedGraph.dll.deploy
Graficadora.application
setup.exe
Graficadora.application
Graficadora.exe
Graficadora.exe.manifest
Graficadora.vshost.application
Graficadora.vshost.exe
ZedGraph.dll
Graficadora.csproj.user
Graficadora_TemporaryKey.pfx
Properties
Settings.settings
Resources
anahuac.png
applixware.png
cakes.png
calc.png
calc1.png
cancel.png
chat.png
chat1.png
cnrdelete-all.png
colors.png
colors1.png
exit.png
font_truetype.png
kcmx.png
ksplash.png
ktouch.png
log.png
logo.gif
msn.png
no3D.png
openofficeorg-20-calc.png
reload.png
restart-1.png
Volume Manager.png
Graficadora.suo
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 ZedGraph;
using System.Collections;
namespace Graficadora
{
    public partial class Form1 : Form
    {
        private double limSup;
        private double limInf;
        private double presicion;
        private string Titulo;
        private string EjeX;
        private string EjeY;
        private Color graphColor = Color.Blue;
        private Color graphColorLine = Color.Black;
        public Stack p = new Stack();
 
        public Form1()
        {
            InitializeComponent();
        }
        
        //Funciones 
        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 button1_Click(object sender, EventArgs e)
        {
            txtEq.Text = txtEq.Text + "1";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txtEq.Focus();
            
        }

        private void button29_Click(object sender, EventArgs e)
        {

        }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        private void button22_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 button16_Click(object sender, EventArgs e)
        {
            txtEq.Text = txtEq.Text + "Cos(";
        }

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

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

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

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

        private void button23_Click(object sender, EventArgs e)
        {
            txtEq.Text = "";
            txtEq.Focus();
            txtEq.Enabled = true; txtRangInf.Enabled = true;
            txtRanSup.Enabled = true; button38.Enabled = true;
            button39.Enabled = true; button24.Enabled = true;
            zg1.Enabled = false; txtPres.Enabled = true;
            button1.Enabled = true; button2.Enabled = true;
            button3.Enabled = true; button4.Enabled = true;
            button5.Enabled = true; button6.Enabled = true;
            button7.Enabled = true; button8.Enabled = true;
            button9.Enabled = true; button10.Enabled = true;
            button11.Enabled = true; button12.Enabled = true;
            button13.Enabled = true; button14.Enabled = true;
            button15.Enabled = true; button16.Enabled = true;
            button17.Enabled = true; button18.Enabled = true;
            button19.Enabled = true; button21.Enabled = true;
            button28.Enabled = true; button20.Enabled = true;
            button27.Enabled = true; button22.Enabled = true;
            button30.Enabled = true; button37.Enabled = true;
            txtTitulo.Enabled = true; txtEjeX.Enabled = true;
            txtEjeY.Enabled = true; button31.Enabled = true;

        }

        private void button24_Click(object sender, EventArgs e)
        {
            if (txtEq.Text == "")
            {
                MessageBox.Show("Ecuación vacía", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtEq.Focus();

            }
            else if (txtRangInf.Text == "")
            {
                MessageBox.Show("Límite inferior vacío", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtRangInf.Focus();
            }
            else if (txtRanSup.Text == "")
            {
                MessageBox.Show("Límite inferior vacío", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtRanSup.Focus();
            }
            else if (txtPres.Text == "")
            {
                MessageBox.Show("Presicion Vacío", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtPres.Focus();
            }
            else
            {
                button1.Enabled = false; button2.Enabled = false;
                button3.Enabled = false; button4.Enabled = false;
                button5.Enabled = false; button6.Enabled = false;
                button7.Enabled = false; button8.Enabled = false;
                button9.Enabled = false; button10.Enabled = false;
                button11.Enabled = false; button12.Enabled = false;
                button13.Enabled = false; button14.Enabled = false;
                button15.Enabled = false; button16.Enabled = false;
                button17.Enabled = false; button18.Enabled = false;
                button19.Enabled = false; button21.Enabled = false;
                button28.Enabled = false; button20.Enabled = false;
                button27.Enabled = false; button22.Enabled = false;
                txtTitulo.Enabled = false; txtEjeX.Enabled = false;
                txtEjeY.Enabled = false; button31.Enabled = false;
                txtPres.Enabled = false; button38.Enabled = false;
                button39.Enabled = false; button24.Enabled = false;

                try
                {
                    txtEq.Enabled = false;
                    Titulo = txtTitulo.Text;
                    EjeX = txtEjeX.Text;
                    EjeY = txtEjeY.Text;
                    Grafica(zg1, Titulo, EjeX, EjeY, Convert.ToDouble(limInf), Convert.ToDouble(limSup), postfijo(txtEq.Text), presicion, graphColor, graphColorLine);
                    zg1.Enabled = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void button30_Click(object sender, EventArgs e)
        {
            try
            {
                limInf = Convert.ToDouble(txtRangInf.Text);
                limSup = Convert.ToDouble(txtRanSup.Text);
                if (limSup > limInf)
                {
                    txtRangInf.Enabled = false;
                    txtRanSup.Enabled = false;
                    DialogResult di = MessageBox.Show("Ha establecido como Límite Superior: " + txtRanSup.Text + " \n Límite Inferior: " + txtRangInf.Text, "Pregunta", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    button30.Enabled = false;
                    if (di == DialogResult.Cancel)
                    {
                        button31_Click(sender, e);
                    }

                }
                else
                {
                    MessageBox.Show("Limite superior debe ser mayor al inferior", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    txtRanSup.Focus();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button31_Click(object sender, EventArgs e)
        {
            txtRangInf.Text = "";
            txtRanSup.Text = "";
            txtRanSup.Enabled = true;
            txtRangInf.Enabled = true;
            button30.Enabled = true;
        }

        private void txtRanSup_TextChanged(object sender, EventArgs e)
        {

        }

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

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

        private void Grafica(ZedGraphControl zgc, string titulo, string ejeX, string ejeY, double limInf, double limSup, string[] postfija, double presicion, Color graph, Color graphLine)
        {
            GraphPane myPane = zgc.GraphPane;

            myPane.Title.Text = titulo;
            myPane.XAxis.Title.Text = ejeX;
            myPane.YAxis.Title.Text = ejeY;
            PointPairList list = new PointPairList();

            for (double x = limInf; x <= limSup; x+=presicion)
            {
                double y = evaluacion(postfija, x);

                list.Add(x, y);
            }

            LineItem myCurve = myPane.AddCurve(titulo, list, graphLine, SymbolType.Diamond);
            myCurve.Symbol.Fill = new Fill(Color.Black);
            myCurve.Line.Fill = new Fill(Color.White, graph, 45F);
            myPane.Chart.Fill = new Fill(Color.Snow, Color.LightGoldenrodYellow, 45F);
            myPane.Fill = new Fill(Color.White, Color.FromArgb(220, 220, 255), 45F);
            zgc.AxisChange();
        }

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

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

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

        private void txtEq_KeyDown(object sender, KeyEventArgs e)
        {
            e.SuppressKeyPress = true;
            if (e.KeyData == Keys.Enter)
            {
                e.SuppressKeyPress = false;
                button24_Click(sender, e);
            }
            if (e.KeyData == Keys.X || e.KeyData == Keys.Shift || e.KeyData == Keys.ShiftKey || e.KeyData == Keys.Subtract || e.KeyData== Keys.Decimal)
                e.SuppressKeyPress = false;
            if (e.KeyValue > 47 && e.KeyValue < 58)
                e.SuppressKeyPress = false;
            if (e.KeyValue == 42 || e.KeyValue == 43 || e.KeyValue == 45 || e.KeyValue == 47 || e.KeyValue == 94)
                e.SuppressKeyPress = false;
            if (e.KeyData == Keys.Back)
                e.SuppressKeyPress = false;
         
            
            
        }

        private void button34_Click(object sender, EventArgs e)
        {
            Application.Restart();
        }

        private void button35_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void txtEq_KeyUp(object sender, KeyEventArgs e)
        {

        }

        private void txtEq_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }

        private void txtRangInf_KeyDown(object sender, KeyEventArgs e)
        {
        }

        private void txtRanSup_KeyDown(object sender, KeyEventArgs e)
        {
        }

        private void txtRangInf_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }

        private void button37_Click(object sender, EventArgs e)
        {
            if (txtPres.Text == "")
            {
                MessageBox.Show("Presición vacio, establesca la presición deseada", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                txtPres.Enabled = false;
                button37.Enabled = false;
                presicion = Convert.ToDouble(txtPres.Text);
            }
        }

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

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

        private void button40_Click(object sender, EventArgs e)
        {
            DialogResult colr = colorDialog1.ShowDialog();
            if (colr == DialogResult.OK)
            {
                graphColor = colorDialog1.Color;
            }
        }

        private void button41_Click(object sender, EventArgs e)
        {
            DialogResult colr = colorDialog1.ShowDialog();
            if (colr == DialogResult.OK)
            {
                graphColorLine = colorDialog1.Color;
            }
        }

        private void button33_Click(object sender, EventArgs e)
        {
            About ab = new About();
            ab.ShowDialog();
        }

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

        private void button43_Click(object sender, EventArgs e)
        {
            Calculadora calc = new Calculadora();
            calc.ShowDialog();
        }
        /*private string[] postfijo(string ecuacion)
        {
            int lenght = ecuacion.Length;
            int k=0;
            string c;
            string[] postfija;
            string tmp="";
            int j;
            
            Stack n= new Stack();
            postfija = new string[30];
            for (int i = 0; i < lenght; i++)
            {
                c = ecuacion[i].ToString();
                tmp = "";
                int band = 0;
                if (char.IsDigit(c, 0) || c == "x")
                {
                    j=i;
                    if (j<lenght)
                    {
                        while (char.IsDigit(ecuacion, j) || ecuacion[j].ToString() == ".")
                        {
                            postfija[k] = postfija[k] + ecuacion[j].ToString();
                            j++;
                            band++;
                        }
                    }
                    if (band > 0)
                        k++;
                    if (c == "x")
                    {
                        postfija[k] = ecuacion[j].ToString();
                        k++;
                    }
                        i = j;  
                }
                else if (ecuacion[i].ToString() == "(")
                {
                    tmp = ecuacion[i].ToString();
                    n.Push(tmp);
                }
                else if (ecuacion[i].ToString() == ")")
                {
                    while (n.Contains("("))
                    {
                        postfija[k] = postfija[k] + n.Pop().ToString();
                        k++;
                    }
                    n.Pop();
                }
                else if (ecuacion[i].ToString() == "+" || ecuacion[i].ToString() == "-" || ecuacion[i].ToString() =="/" || ecuacion[i].ToString() =="*")
                {
                    while (n==null && (n.Contains("(") && prioridad(n.Peek().ToString())>= prioridad(c.ToString())))
                    {
                        postfija[k] = postfija[k] + n.Pop().ToString();
                        k++;
                    }
                    n.Push(c);
                    }            
            }
            while (n!=null)
            {
                postfija[k] = postfija[k] + n.Pop().ToString();
                k++;
            }
        return postfija;
        }*/
        
    }
}

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)

Share

About the Author

pechan00000
Systems / Hardware Administrator
Mexico Mexico
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 11 Jun 2008
Article Copyright 2008 by pechan00000
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid