Saturday, December 11, 2010

C# Tutorial: Equation Calculator [Expression Evaluator]

Here is the code

// Math Parser
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EquationParser
{
    class MathParser
    {

        public static double EvalExpression(char[] expr)
        {
            return parseSummands(expr, 0); 
        }
        
        private static double parseSummands(char[] expr, int index)
        {
            double x = parseFactors(expr, ref index);
            while (true)
            {
                char op = expr[index];
                if (op != '+' && op != '-')
                    return x;
                index++;
                double y = parseFactors(expr, ref index);
                if (op == '+')
                    x += y;
                else
                    x -= y;
            }
        }
        
        private static double parseFactors(char[] expr, ref int index)
        {
            double x = GetDouble(expr, ref index);
            while (true)
            {
                char op = expr[index];
                if (op != '/' && op != '*')
                    return x;
                index++;
                double y = GetDouble(expr, ref index);
                if (op == '/')
                    x /= y;
                else
                    x *= y;
            }
        }
        
        private static double GetDouble(char[] expr, ref int index)
        {
            string dbl = "";
            while (((int)expr[index] >= 48 && (int)expr[index] <= 57) || expr[index] == 46)
            {
                dbl = dbl + expr[index].ToString();
                index++;
                if (index == expr.Length)
                {
                    index--;
                    break;
                }
            }
            return double.Parse(dbl); 
        }
    }
}

// Button1_Click
textBox2.Text = MathParser.EvalExpression(textBox1.Text.ToCharArray()).ToString();

5 comments:

  1. Nice. With so many articles where the author finds some strange technique to pull in some strange library or system call, I always love it when people just write code to accomplish something.

    ReplyDelete
  2. whether it is possible to learn in more detail about this part of a code and that it carries out. Thanks
    while (((int)expr[index] >= 48 && (int)expr[index] <= 57) || expr[index] == 46)
    {
    dbl = dbl + expr[index].ToString();
    index++;
    if (index == expr.Length)
    {
    index--;
    break;
    }

    ReplyDelete
  3. VladVlad : he simply runs through the expr array and adds the characters of the digit (in ascii digits are between 48 and 57 and 46 is the '.' symbol, so he can handle decimals too), he adds the digits to a string that he finally parses with double.Parse() and returns it so that the rest of the program can make computations with it. Not so hard. ^_^

    ReplyDelete
  4. hi, i want to ask something. why i get the right answer of 10x10+1000/2. it must be 600 but the programs give the 1100 as the answer

    ReplyDelete
  5. Whats the size of this array?
    I am getting IndexOutOfRangeException ? Any help pls

    ReplyDelete