A high-precision command-line expression evaluator built in C#. This project implements a full compilation pipeline—Tokenization, Parsing, and Visualization/Evaluation—to process complex mathematical strings.
- Environment: Visual Studio 2022 or higher (.NET Console Project).
- Compilation: Open the
.slnfile and build (Ctrl+Shift+B). - Testing: Comprehensive Unit Tests are included to verify operator precedence and edge cases.
The application supports both interactive shell mode and batch processing.
- Interactive: Run
MathOne.exe. Enter expressions line-by-line. - Batch:
MathOne [inputFilePath]to process a text file of expressions. - Exit: Simply enter an empty line to terminate the session.
The engine uses a Recursive Descent Parser for an LR(0) grammar.
- Tokenize(): Lexical analysis breaks strings into typed symbols.
- Parse(): Builds a tree based on Left-to-Right associativity and operator precedence.
- Visit(): Traverses the tree to compute the final result.
| Category | Function Codes & Operators |
|---|---|
| Arithmetic | +, -, *, /, % (Mod), ^ (Power), @ (Negate), S (Sign) |
| Trig | SIN, COS, TAN, ASIN, ACOS, ATAN, DEG (ToDeg), RAD (ToRad) |
| Algebra | A (Abs), SR (Sqrt), CR (CubeRt), RT (NthRoot), X2 (Sq), X3/CB (Cube) |
| Probability | NCR (Comb), NPR (Perm), FAC (Fact), RAN (RandInt), RND (RandDouble) |
| Distributions | PHI/CDF (Normal CDF), ND (Normal), GAU (Gauss), STU (Student-T) |
| Number Theory | GCF (Greatest Factor), LCM (Lowest Multiple), PD (Prime Divisor) |
| Formatting | CL (Ceiling), FL (Floor), RD (Round), I (Integer), F (Fraction) |
| Constants | PI (3.1415...), EN (Euler: 2.718...), TAU (6.2831...) |
- Order of Operations:
40 - (1 + 2)→37 - Trigonometry:
COS(RAD(45))→0.7071067811865476 - Nested Negation:
-(2 * 5)→-10 - Large Number Theory:
LCM(13342, 234334)→1563242114 - Absolute Value:
A(-5)→5
The parser uses a Recursive Descent strategy for an LR(0) language. The Eval() method executes a three-stage pipeline:
- Tokenize(): Lexical analysis converts the string into a list of symbols.
- Parse(): Converts tokens into an Abstract Syntax Tree (AST) respecting precedence.
- Visit(): Recursively evaluates the tree nodes to produce a double-precision result.
- James McCaffrey: "The Normal Cumulative Density Function in C#", 2014. Provided the fundamental logic for the
PHIandGAUdistribution functions. - Unit Testing: Includes a comprehensive suite of tests to verify the accuracy of all 40+ operators and the integrity of the recursive descent parser.