Click here to Skip to main content
15,860,844 members
Articles / Programming Languages / C#
Article

Graphic Calculator

Rate me:
Please Sign up or sign in to vote.
3.53/5 (25 votes)
10 Jun 2008CPOL 81.2K   5.4K   65   11
Software tool that interactively displays a graphical view of mathematical functions
GraficadoraSource

Introduction

This software graphs an equation written only by clicking the buttons and setting the limits and the steps to a graph. The problem is to recognize the equation operators and operands, set its priority and evaluate all the equations.

Background

This link explains the process to create a Postfix from an Infix expression.

Using the Code

Postfix Method

This method returns the Postfix equation in a string array:

C#
public string[] postfijo(string ecuacion)
        {
            int l = ecuacion.Length;
            string[] infijo = new string[l];
            int j; int k = 0; int i; 
            string c; string tmp = "";
            for ( i = 0; i < l; i++)
            {
                c = ecuacion[i].ToString();
                int band = 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;            
        }

Priority and Operator Methods

C#
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") || (c == "Sqrt"))
                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;
        }

Evaluation Method

This method returns the result from evaluating the Postfix equation with an unknown variable:

C#
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++)
            {
                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;
        }

Graph Method

This method receives the graph title, axis title, limits, colors, Postfix Equation.

C#
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();
        }

Points of Interest

The Windows Form Application includes events to validate the input equation. The final version tested by the Sinc Function(Sin(x)/x) with limits (-10,10), step (0.1) looks like this:

graph1.jpg

The application also includes a simple calculator (calc button), which is something like Windows Calc - this is only a plus. Enjoy and if there is something wrong, please let me know.

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

 
GeneralMy vote of 1 Pin
Clifford Nelson22-Aug-12 13:54
Clifford Nelson22-Aug-12 13:54 
GeneralSimple y Útil Pin
owltiger7-Jul-09 5:32
owltiger7-Jul-09 5:32 
GeneralNo habla Espanol. No liko yo articleo. Pin
jacobjordan9-Aug-08 11:40
jacobjordan9-Aug-08 11:40 
GeneralRe: No habla Espanol. No liko yo articleo. Pin
Member 871442121-Jan-13 3:13
Member 871442121-Jan-13 3:13 
QuestionWhat the Heck? Pin
The Cake of Deceit14-Jun-08 7:32
The Cake of Deceit14-Jun-08 7:32 
GeneralSuggestions Pin
PIEBALDconsult11-Jun-08 17:31
mvePIEBALDconsult11-Jun-08 17:31 
Generalpretty nice Pin
thebeekeeper11-Jun-08 7:47
thebeekeeper11-Jun-08 7:47 
GeneralRe: pretty nice Pin
pechan0000011-Jun-08 7:51
pechan0000011-Jun-08 7:51 
GeneralDoesn't Work Pin
Code Deamon14-May-08 3:40
Code Deamon14-May-08 3:40 
GeneralRe: Doesn't Work Pin
pechan0000010-Jun-08 19:51
pechan0000010-Jun-08 19:51 
GeneralFix Pin
pechan0000017-Apr-08 6:33
pechan0000017-Apr-08 6:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.