Thanks to visit codestin.com
Credit goes to github.com

Skip to content

amitkm9204/calculator

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

calculator

Build Status

calculator is a simple C++ operator-precedence parser for integer arithmetic expressions. calculator.hpp is a header-only library that compiles with any C++ compiler and works with any integer type e.g. int, long long, __uint128_t.

Supported operators

calculator.hpp uses the same operator precedence and associativity as the C++ programming language and also supports the power operator.

Operator Description
| Bitwise Inclusive OR
^ Bitwise Exclusive OR
& Bitwise AND
~ Unary Complement
<< Shift Left
>> Shift Right
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo
** Raise to power

How to use it

calculator::eval("1+2") takes a string with an integer arithmetic expression, evaluates the arithmetic expression and returns the result. If the expression string is not a valid integer arithmetic expression a calculator::error exception is thrown.

#include "calculator.hpp"
#include <iostream>

int main()
{
    try
    {
        int result = calculator::eval("(0 + ~(255 & 1000)*3) / -2");
        std::cout << result << std::endl;
    
        // 64-bit arithmetic
        long long r64 = calculator::eval<long long>("2**60");
        std::cout << r64 << std::endl;
    }
    catch (calculator::error& e)
    {
        std::cerr << e.what() << std::endl;
    }

    return 0;
}

C++ API

Functions defined in calculator.hpp.

int calculator::eval(const std::string& expression);
int calculator::eval(char c);

template <typename T> T calculator::eval<T>(const std::string& expression);
template <typename T> T calculator::eval<T>(char c);

About

C++ operator precedence parser

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C++ 99.0%
  • Makefile 1.0%