HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Math equation as string to reverse Polish notation parser

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
polishreverseparsermathnotationstringequation

Problem

I have created a console program where users type in a math equation such as 5+4-6 or (5+16(8-4))/16 that follows order of operations. The equation is sent to a char array that is then parsed. If a # or . is encountered, the char is placed into a buffer. The buffer is tokenized into a token class, followed by the operator itself being tokenized. The program works beautifully and as expected. Exceptions and errors still need to be handled where right now it is only writing to the console and continuing.

What I am looking to do next is add in higher functions, more in-line with a scientific calculator, starting with the constants such as e and pi, then moving into trig functions. It is in handling the functions that I am having an issue with.

Would you recommend storing the chars in a buffer similar to the #'s and treat them as a function if a ) is found immediately following, and a constant otherwise? What would you suggest? Also, what could I improve upon?

PROGRAM.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CalcTest.Core;

namespace CalcTest
{
    class Program
    {
        static Calculator calculator;
        static void Main(string[] args)
        {
            calculator = new Calculator();
            calculator.Entry();
        }
    }
}


ENUMS.CS

using CalcTest;
using CalcTest.Core;

namespace CalcTest.Core
{
    public enum TokenType
    {
        Value,
        Operand,
        Function,
        Paramater,
        Variable
    }
}


TOKEN.CS

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CalcTest.Core
{
public class Token
{
private TokenType type;
private string val;

public TokenType TYPE
{
get { return type; }
set { type = value; }
}
public string VALUE
{
get { re

Solution

One thing, using a Dictionary> will simplify your translation from the parsed formula to the actual calculations:

public class Calculator
{

    public static  readonly  Dictionary> functions = new Dictionary>()
        {
            {'+',new Func(Add)},
            {'-', new Func(Subtract)},
            {'/',new Func(Divide)},
            {'*',new Func(Multiply)}
        };

    static double Add(double num1, double num2)
    {
        return num1 + num2;
    }
    static double Subtract(double num1, double num2)
    {
        return num1 - num2;
    }
    static double Multiply(double num1, double num2)
    {
        return num1 * num2;
    }
    static double Divide(double num1, double num2)
    {
        return num1 / num2;
    }
}


The calculation becomes a simple look up of the dictionary with the appropriate operator:

char operation = '+';
double num1 = 1;
double num2 = 2;
double newresult = Calculator.functions[operation](num1, num2);

Code Snippets

public class Calculator
{

    public static  readonly  Dictionary<char, Func<double, double, double>> functions = new Dictionary<char, Func<double, double, double>>()
        {
            {'+',new Func<double,double,double>(Add)},
            {'-', new Func<double,double,double>(Subtract)},
            {'/',new Func<double,double,double>(Divide)},
            {'*',new Func<double,double,double>(Multiply)}
        };

    static double Add(double num1, double num2)
    {
        return num1 + num2;
    }
    static double Subtract(double num1, double num2)
    {
        return num1 - num2;
    }
    static double Multiply(double num1, double num2)
    {
        return num1 * num2;
    }
    static double Divide(double num1, double num2)
    {
        return num1 / num2;
    }
}
char operation = '+';
double num1 = 1;
double num2 = 2;
double newresult = Calculator.functions[operation](num1, num2);

Context

StackExchange Code Review Q#48632, answer score: 2

Revisions (0)

No revisions yet.