This is a command-line calculator, written in C, supporting the standard mathematical operations and a set of functions. Commands can be entered in standard infix syntax, with parentheses denoting nonstandard order of operations. You know, like you were taught in elementary and middle school.
+
Addition-
Subtraction*
Multiplication/
Division^
Exponent%
Modulus
abs(...)
Absolute valuefloor(...)
Floorceil(...)
Ceilingsin(...)
Sinecos(...)
Cosinetan(...)
Tangentarcsin(...)
Arcsinearccos(...)
Arccosinearctan(...)
Arctangentsqrt(...)
Square rootcbrt(...)
Cube rootlog(...)
Logarithmexp(...)
Exponentiation (e^x)min(...)
Minimummax(...)
Maximumsum(...)
Summationmean(...)
Meanavg(...)
Meanmedian(...)
Medianvar(...)
Variance
To investigate tokenization of an input string, mathematical expressions in infix and postfix notation, and an untyped stack data structure.
Computation begins with the tokenization of the input string, maintaining original infix order. They are then converted to postfix notation (Reverse Polish Notation) for evaluation using the shunting-yard algorithm. As operators are pushed onto the postfix stack, terms are evaluated.
-
Untyped stack
Elements are stored on the stack asvoid *
types. This means that any type of element can be stored on the stack at the same time as any element of any other type - including custom types - as long as an element's type is known when it is popped off the stack. -
Early evaluation
There is no separate evaluation step; elements are evaluated on the postfix stack as soon as all terms are available. Due to the nature of the shunting-yard algorithm, as soon as an operator is pushed, it can be evaluated. When theevalStackPush()
function sees an operator, rather than pushing it, pops its operands, runs the computation, and pushes the result. This means that once the input expression has been converted to postfix notation, the only element on the postfix stack is the result of the calculation. -
Settings
Try typingset display tokens on
and entering an expression. The calculator will display the result of its tokenization. Starting the calculator and enteringget mode
reveals that it defaults toradians
. Options are as follows:get/set
Read or change the value of a settingdisplay
Settings to do with the display of the evaluation processpostfix (off/on)
Display the postfix stack before evaluationtokens (off/on)
Display the result of tokenization
mode (radians/degrees)
Evaluation mode of trigonometric functionsprecision (X/auto)
Set precision toX
decimal places orauto
to reduce decimal places as far as possible without loosing precision. Default is 5 decimal places
Build with make
. Clean with make clean
. Run with ./calc
. Type any mathematical expression, for example, 3*(2^4) - 3*floor(2 * sin(3.14 / 2))
and press the Enter key. Type quit
to close.
There is a -r
command line option which removes the =
from the output, outputting only the result value. This is designed for use in situations such as shell scripting, where only the raw, unprocessed value is desired.
There is a -m
command line option which sets the maximal length of a token. Default is 512 characters.