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.
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 |
Functions defined in calculator.hpp
.
int calculator::eval(const std::string& expression);
template <typename T>
T calculator::eval<T>(const std::string& expression);
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;
}
This project is licensed under the BSD 2-Clause License. See LICENSE for details.