It's a basic C library that supports functions helpful for parsing and evaluating any mathematical expression entered as a string of characters.
Currently this supports only Real numbers.
No build system is needed. Simply copy the appropriate headers and C files into the project and use the functions.
The library has been tested on over a hundred test cases and the results turned out to be fairly accurate upto 4 decimal cases on most test cases.
Key things to keep in consideration while using this library:
- Division operation has been assigned greater priority than multiplication. So the the order of operation around consecutive multiplication and division has to be explicitly specified by the use of parantheses.
- The order of operation has to be explicitly specified if same operator is being consecutively used for more than once.
- Supported funtions:
- sin, cos, tan, cot, cosec, sec
- arcsin, arccos, arctan, arccot, arccosec, arcsec
- ln(natural logarithm), log(base 10 logarithm)
- fact(factorial), gamma
Stack *lex(char *inputString);
The Lexer recognizes 4 different kinds of Tokens:
- Variable 'x'
- Decimal numerals
- Parantheses
- Elementary functions (sin, cos, ln, etc)
Stack *parser(token *linkedlistHead);
The parser scans through the linked list of lexical tokens generated by our lexer and derives a dynamic array of elements arranged in Reverse Polish Notation (postfix nota tion).
The shunting yard algorithm is employed to achieve this task.
double eval(Stack* postfixArray, double x0);
A very simple recursive algorithm though memory heavy and slow.
-
Sequentially Scans across the received Array of tokens.
-
If a sub-array of the form |operand | operand | operator | or |operand | function | is found,evaluate,
store in a token of type numeral. Push the token into stackA. -
In case three elements don't form the defined subarray, simply push consecutively.
-
Repeat steps 2-3 untill all tokens have been scanned.
-
Use stackA as the new Array of tokens and repeat steps 1-3 but using stackB for the intended purpose
of stackA in step 2. -
Empty stackA ans repeat steps 1-5 untill the stack has a single element. !!