Code Monkey home page Code Monkey logo

calculator's Introduction

calculator

A simple calculator written in C++

Currently (2017/06/06) supported are bracket terms, custom functions and custom operators.

Usage example

    #include <memory>
    #include <iostream>
    #include <string>
    
    #include "Parser.h"
    
    int main() 
    {
        // Parser::Number is the data type which is used for calculations internally
        // (long double atm)
        
        //
        // add custom operators
        //
        // first argument takes the character representation of the operator (only single char operators are supported)
        // the second parameter is priority of the operator (higher priorities will be executed sooner)
        // the third argument takes a function with the signature
        //     Parser::Number(const Expression &, const Expression &)
        // OP is a macro for this, the first two parameters being the names of the parameters and the third one
        // being an expression which should directly return the value
        // (see Expression.h for implementation)
        Expression::AddOperator('+', 1, OP(e1, e2, e1 + e2));
        Expression::AddOperator('*', 2, OP(e1, e2, e1 * e2));
        
        //
        // add custom function
        //
        // first argument takes the string representation of the operator (only alpha numerical names are supported)
        // the second argument takes a function with the signature
        //     Parser::Number(const Parser::Number)
        // FN is a macro for this, the first parameter being the names of the parameter and the second one
        // being an expression which should directly return the value
        // (see Expression.h for implementation)
        Expression::AddFunction("twice", FN(v, v * 2));

        try {
            // Parse max throw if the format is not valid
            std::shared_ptr<Expression> expr = Parser::Parse("(twice(1)+2)*(2+3)+2+2*3");
            
            // expected output is '(twice(1)+2)*(2+3)+2+2*3=28'
            std::cout << "(twice(1)+2)*(2+3)+2+2*3=" << expr->getValue() << std::endl;
        } catch (const std::string &ex) {
            std::cerr << ex << std::endl;
            return 1;
        }
        
        return 0;
    }

calculator's People

Contributors

skaupper 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.