tyumener
tyumener

Reputation: 51

How to parse arithmetic operators from string?

Is it possible to assign an int variable a value that is a result of expression written in a string? E.g. I have a string "5 - 3" and the expected result is 2.

Upvotes: 1

Views: 7167

Answers (3)

RichK
RichK

Reputation: 11871

If you fancy writing your own (I did, it's fun!). Have a look on Wikipedia for:

Shunting Yard Algorithm and Reverse Polish Notation

The pseudo-code is really clear and it's a great learning experience

Upvotes: 1

Oded
Oded

Reputation: 498952

There is nothing built into the framework, so you can either built your own mathematical expression parser, or use one of the many libraries out there.

A search on ".net arithmetic parser" comes up with many results, some free, some not.

For instance:

Upvotes: -1

Giorgi
Giorgi

Reputation: 30873

You can use Calculator.NET

Upvotes: 4

Related Questions