jansky / PlusCalc

A library and interpreter written in C++ to do infix notation mathematics with the correct order of operations - now with functions and variables

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PlusCalc is a library and interpreter written in C++ to do infix notation mathematics with the correct order of operations.

The library is licensed under the LGPL v3 and the interpreter is licensed under the GPL v3.

The library is designed to be simple to use. After installing PlusCalc on your computer (follow the instructions contained in the file 'INSTALL'), any C++ code can use the library by including pluscalc.h and calling the function pc_evaluate_math_expression, which returns a long double, as below:

#include <iostream>
#include <pluscalc.h>

int main(void)
{
	std::string expr = "3+4*5";
	long double result = pc_evaluate_math_expression(expr);
	std::cout << result << std::endl;
	return 0;
}

After compiling this program with the -lpluscalc flag, you should get the output '23'.

The PlusCalc library also supports defining and using variables in your expressions:

#include <iostream>
#include <pluscalc.h>

int main(void)
{
	std::string expr = "x+3";
	//Variables list
	std::vector<PCVariable> variables;

	//Create variable 'y' with value of 10
	variables.push_back(pc_create_variable(10, 'y'));

	//Create variable 'x' with value of '2^5'.
	variables.push_back(pc_create_variable(pc_parse_string("2^5", true), 'x'));

	long double result = pc_evaluate_math_expression(expr, variables);
	std::cout << result << std::endl;
	return 0;
}

The above will output '35'.

The PlusCalc interpreter is designed to emulate a fully functional desktop calculator, as well as providing support for probability and trigonometric functions, as well as user-defined functions and variables. The interpreter can also execute user-written scripts when run with the '-e' flag (see bottom for more details). After installing PlusCalc, you can run 'pluscalcint' to start it. If you do not pass it any command line arguments, it will launch in interactive mode:

PlusCalc Interpreter v1.99c
Copyright (c) 2015 Ian Duncan
This is free software with ABSOLUTELY NO WARRANTY.
Enter 'license' for more information.
Enter 'quit' to exit and 'clear' to clear the history file (~/.pluscalcint_history).

Trigonometry mode: degrees

> 


Enter mathematical expressions at the prompt '>' such as 3^(2*4) and press enter to evaluate them. The interpreter contains the macro 'ans' which is expanded before evaluation to the result of the previous calculation:

> 3^(2*4)
6561
> ans+3
6564
> 

The PlusCalc interpreter makes use of the GNU readline library. All expressions that are entered are stored in a history file located at (~/.pluscalcint_history). When you are running the interpreter you can page through the history using the arrow keys as in any other program that uses the readline library.

To clear the history file enter 'clear'. To quit enter 'quit'. To view licensing and warranty information enter 'license'.

The PlusCalcInterpreter can also be run in non-interactive mode, in which it evaluates an expression passed as a command line argument. The interpreter concatenates all arguments into a single expression before evaluating, so spaces may be used:

$ pluscalcint 2+3
5
$ pluscalcint 2 + 3 * 7
23
$ pluscalcint "(3+4)*(1+2)"
21

Expressions evaluated in non-interactive mode are not added to the history file.

Expressions that you wish to be evaluated when the PlusCalc interpreter starts in both interactive and non-interactive mode can be added to the file ~/.pluscalcintrc, with one expression per line.

PlusCalc supports the following operations: + - * / ^ % sqrt root abs log logb ln ! nPr nCr sin cos tan asin acos atan
PlusCalc supports nested parentheses, such as in (3*(7+8*(92/2)))
PlusCalc defines the following constants: pi e

Some of PlusCalc's operations are explained below:

^ - exponent
	Example: 3^2 = 9

% - modulo
	Example: 3%2 = 1

sqrt - square root
	Example: sqrt 4 = 2

root - any root
	Example: 3 root 27 = 3

abs - absolute value
	Example: abs -90 = 90
	
log - base 10 log
	Example: log 1000 = 3

logb - log of any base
	Example: logb 6 36 = 2

ln - natural log
	Example: ln e = 1

! - factorial
	Example: 4! = 24
	Note: PlusCalc does not handle factorials of numbers above 20 correctly (they're too large), as well as decimals and negative numbers

nPr - Permutation formula
	Example: 10nPr3 = 720
	Note: The name is case sensitive. 'npr' and 'NPR' are not valid.

nCr - Combination formula
	Example: 10nCr3 = 120
	Note: The name is case sensitive. 'ncr' and 'NCR' are not valid.

sin - sine
	Example: sin 90 = 1 (in degrees mode)

cos - cosine
	Example: cos 60 = 0.5 (in degrees mode)

tan - tangent
	Example: tan 45 = 1 (in degrees mode)

asin - arcsine (inverse of sine)
	Example: asin 1 = 90 (in degrees mode)

acos - arccosine (inverse of cosine)
	Example: acos 0.5 = 60 (in degrees mode)

atan - arctangent (inverse of tangent)
	Example: atan 1 = 45 (in degrees mode)

Note that PlusCalc does not support taking even roots of negative numbers (e.g. sqrt -4 or 4 root -256).

You can determine or view the unit of angle measure (trigonometric mode) used by trigonometric functions with the 'tmode' function. Just running 'tmode' will print the current trigonometric mode:

> tmode
Trigonometry mode: degrees

Passing deg (degrees), rad (radians), grad (gradians), turn (turns), will set the trigonometric mode:

> tmode grad
Trigonometry mode: gradians
> tan 50
1.000000000000000222

The PlusCalc interpreter also contains extra functionality to perform synthetic division and to test if a number is prime.

> synthdiv 1;1,2,3,4,5
1	3	5	7	9	
> set a 3
> synthdiv 1;1,2,a,4,5
1	3	5	7	9	
> ans
9
> isprime ans
Not prime
> 

The PlusCalc interpreter also includes the variable support of the PlusCalc library, as well as functions (not a part of the library).

Variables can be any character that is not a defined operation or constant (e.g. e, +, *).

Examples of valid variable names: a,b,c,$
Examples of invalid variables names (don't use these): max,+,**

Defining and using variables is simple: 

> set a 3
> a+3
6
> set b 2^5
> a+b
35

The PlusCalc library, and by extension, the interpreter, evaluates the value of variables at the time of expression evaluation. This means that values of variables can change dynamically:

> set a 3
> set b a+3
> b
6
> set a 4
> b
7

Functions can have multiple arguments. Argument names follow the same rules as variable names. If there is a conflict (e.g. you define variable x and have a function with an argument called x), you cannot access the defined variable from within the function.

Defining and using functions is simple:

> set x 5
> func test x,y,z;x+y+z
> call test 1,2,3
6
> x
5

Defined variables and functions can be saved to a file by using the save command to allow them to be used in later sessions.

> save filename.pc

To load them, use the load command:

> load filename.pc

These filenames can also be specified on the command line by using the -l switch:

$ pluscalcint -l filename1.pc filename2.pc ...

Users can also write their own scripts to be executed by the PlusCalc interpreter using the -e flag:

$ pluscalcint -e script.pc

You can also use the following shebang:

#!/usr/bin/pluscalcint -e

The print, printr, and input commands, although usable in interactive mode, were designed for scripts. Scripts can contain any instruction that you could evaluate in interactive mode, including defining variables and functions. Comment lines are preceded by a '#'.

Example:

#!/usr/bin/pluscalcint -e
print This is a PlusCalc script
input a
input b This is my custom prompt: 
printr The result is: 
a+b

Result

This is a PlusCalc script
a? 5
This is my custom prompt: 6
The result is: 11

You can look at the 'quadratic.pc' script as another example. It is designed to find the zeroes of quadratic functions.

Details on print, printr, and input

print - Prints a string and a newline
	Example: print This is a test
	Result: This is a test\n
	(\n = newline)

printr - Prints a string without a newline
	Example: The result is: 
	Result: The result is: 

input - Allows a variable to be defined by user input
	Example: input a
	Result: a? 
	Example: input a My custom prompt: 
	Result: My custom prompt: 
	NOTE: Any valid PlusCalc expression may be entered at the prompt


	







About

A library and interpreter written in C++ to do infix notation mathematics with the correct order of operations - now with functions and variables

License:Other


Languages

Language:C++ 99.0%Language:Makefile 1.0%