Giter Club home page Giter Club logo

expreval's Introduction

ExprEval

Logo

Expression-Evaluator is a C++ library to evaluate a given string as a mathematical expression.

Options

  • -i for immediate calculation
  • --interactive for interactive mode

Interactive mode

Interactive mode allows you to create your custom symbols(functions). For example, you can create a function such that tell_percentage(x,y)=100*x/y and use it in your expressions. You can also delete any symbol that you have created before, and if you have long list of custom functions you can list them in your terminal.

./expreval -i "5+3-2"
 = 6
./expreval
: 5+3-2
 = 6
./expreval --interactive
(Enter 'h' for help)
: >   # To add new symbol
    > Symbol: my_function
    > Number of arguments: 3
    > Arguments: x y var # Positions: 1, 2, 3 respectively
    > Expression: x*y+var^2
= = = = = = = = =
: ?   # To list all user-defined symbols
    - my_function(x, y, var) = x*y+var^2
= = = = = = = = =
: (3+2)*my_function(3,0,2)
 = 20
: <   # To remove a user-defined symbol
    < Symbol: my_function
= = = = = = = = =
: ?
= = = = = = = = =
: s   # To save user-defined functions
    [w] File path(default: operators.spec): my_function.spec
= = = = = = = = =
: l   # To load user-defined functions
    [r] File path(default: operators.spec): my_function.spec
= = = = = = = = =

Errors and exception handling

You can also get an exception instead of an actual number as a result of the expression you have given. This can happen due to at least one of two main reasons:

  • The expression contains at least one symbol which is not defined(i.e. 5+3*a)
  • The expression is logically ill-formed(i.e. 5+3*!)

In both cases, the program will display a relevant error message. However, you do not need to worry; you can just define the undefined symbol as a function or you can just rewrite your expression in a correct form.

Note: If you do not know by which order your expression is going to be evaluated then you can always use parantheses('(' and ')') to make sure that the program will prioritize the operator(s) you want!

For example,

./expreval
(2^2)^3
 = 64

2^(2^3)
 = 256

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.