Skip to content

A C++ library for parsing and evaluating arithmetic expressions

License

Notifications You must be signed in to change notification settings

jerboa88/calculator

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

65 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MiniCalc C++

Project type Language Repository size Project license

A C++ library for parsing and evaluating arithmetic expressions


MiniCalc C++ is a simple but fast operator-precedence parser. It compiles with any C++ compiler and should work with any floating-point type in the future.

Supported operators

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

Operator Description
+ Addition
- Subtraction
* or x Multiplication
/ Division
% Modulo
^ or ** Raise to power

C++ API

Functions defined in calculator.hpp.

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

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

How to use it

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

#include "calculator.hpp"
#include <stdint.h>
#include <iostream>
using namespace std;

int main() {
	try {
		int result = calculator::eval("(2^2+1)/3"); // 1.66666666667
		cout << result << endl;
	}
	catch (calculator::error& e) {
		cerr << e.what() << endl;
	}

	return 0;
}

License

This project is licensed under the BSD 2-Clause License. See LICENSE for details.

About

A C++ library for parsing and evaluating arithmetic expressions

Topics

Resources

License

Stars

Watchers

Forks

Languages

  • C++ 98.7%
  • Makefile 1.3%