Giter Club home page Giter Club logo

math-eval's Introduction

Math Expression Evaluator

This is just a math expression evaluator in C that I wrote for fun and learning. If you want a production-quality library, take a look at TinyExpr.

Description

The Shunting-yard algorithm is used for parsing expressions written in Infix notation (e.g. 5 + 3). My implementation parses operators and operands and store each type in a separate stack (ops_stack and operands_stack) for ease of computation, and doesn't provide a function to produce an expression in Reverse Polish notation yet.

How to use

To use as a library, copy the necessary sources (all but demo.c, debug.*, tests.c) to your project and include evaluate.h. See evaluate.h for some docs and demo.c for a usage example.

Repo structure

  • demo.c a simple calculator built using the library.
  • evaluate.c uses the Shunting-yard algorithm to evaluate expressions written in infix notation.
  • ops.c defines the supported operations.
  • stack.h is a stack implementation to support the algorithm. It's based on the vector.h file, which I originally wrote for use in my C utilities, so it's generic (i.e macro hell).
  • debug.c prints some debugging info when the EVAL_DEBUG macro is defined.
  • tests.c: some tests

Building

To build and run the demo, run:

make demo

To run tests:

make tests

TODO

  • More operations and constants
  • Explore more algorithms for mathematical expression parsing
  • Rust version
  • Use an arbitrary-precision arithmetic library to save myself from headaches
  • Operator associativity. Currently, the exponentiation operator (^) is special-cased to be right-associative. If I'll ever add more right-associative operators, I should modify the operator struct to store associativity and remove the special case.

math-eval's People

Contributors

abdnh avatar

Watchers

 avatar

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.