Does your company or startup need an expert? If so, write to me to talk about possibilities of professional cooperation.
PROGRAMMING on-line course is approaching! Write to me if you are interested in free early access.

MARCIN.com

Marcin Jamro, PhD, DSc

How to calculate expression in Reverse Polish Notation (RPN)?

C#  |  .NET  |  Algorithms

Reverse Polish Notation (also named postfix notation) is a mathematical notation. To explain it in a simple way, in this notation you first specify operands and then operator that should be applied, such as:

5 6 +

which is the same as:

5 + 6

from the notation that you know much better, so the expression is equal to 11.

Let's take a look at a bit more complex example:

5 6 + 2 4 - *

which is the same as:

(5+6)*(2-4)

so the result is equal to -22.

The code is presented below:

int rpn(string[] tokens)
{
    string[] operators = ["+", "-", "*", "/"];
    Stack stack = new();

    foreach (string token in tokens)
    {
        if (!operators.Contains(token))
        {
            stack.Push(int.Parse(token));
            continue;
        }

        int x = stack.Pop();
        int y = stack.Pop();
        int result = token switch
        {
            "+" => y + x,
            "-" => y - x,
            "*" => y * x,
            _ => y / x
        };
        stack.Push(result);
    }

    return stack.Pop();
}

You can check this code by the following lines:

string[] tokens = ["5", "6", "+", "2", "4", "-", "*"];
Console.WriteLine($"Result is: {rpn(tokens)}");

The results are shown as follows:

Result is: -22

The content, including any part of code, is presented without warranty, either express or implied. The author cannot be held liable for any damages caused or alleged to have been caused directly or indirectly by any content shown on this website.

Hello, I am Marcin

Reliable entrepreneur with 10+ years of companies operation, such as CEO at a few IT companies. I was an author of a few software & hardware products, still open to new ideas & cooperation.

Helpful expert with 10+ years of experience, together with PhD and DSc in Computer Science. I was an author of books and publications, as well as an expert in international projects.

Experienced developer with 10+ years of development and 100+ completed projects. I worked on various complex international projects, e.g. at Microsoft in USA and as CTO at a few companies. I have MCP, MCTS and MCPD certificates.

You can read more about me in my short bio. I am waiting for contact at [email protected], as well as at my Facebook and LinkedIn profiles.

Books

I am an author of a few books and numerous publications, also in high-quality international scientific journals.

If you want to learn data structures and algorithms in the context of C#, let's take a look at my newest book. It is the second edition of C# Data Structures and Algorithms. You can buy it here.

Projects

Do you like traveling? If so, discover an amazing world with local guides, right here and right now at: https://camaica.com

Do you follow your favorite creators? If so, check it out what products they recommend and buy them at: https://refspace.com

Travels

I love travels, learning new cultures and regions, meeting outstanding people, as well as taking pictures of beautiful places. Take a look at my travel diary.