The calculator is a JavaScript-based library designed to parse, convert, and evaluate mathematical expressions. It supports basic arithmetic operations and handles complex expressions with parentheses for operator precedence. This project is created for learning purposes and is inspired by the Microsoft Calculator app on Windows 10.
- Arithmetic Operations: Supports addition (
+), subtraction (-), multiplication (*), division (/), and exponentiation (^). - Parentheses Handling: Supports nested parentheses for proper operator precedence.
- Expression Parsing: Converts infix expressions to postfix notation using the Shunting Yard algorithm.
- Error Handling: Provides detailed error messages for invalid expressions or unsupported tokens.
- Division by Zero Handling: Explicitly handles cases like
0 / 0and1 / 0. - Negative Numbers: Supports negative numbers in expressions (e.g.,
2^-2). - Right-Associative Operators: Handles right-associativity for operators like
^.
-
Negative Number Support:
- Added support for negative numbers in expressions (e.g.,
2^-2). - Updated
isNumberfunction to handle both positive and negative numbers.
- Added support for negative numbers in expressions (e.g.,
-
Exponentiation:
- Implemented right-associativity for the
^operator (e.g.,2^2^3is evaluated as2^(2^3)).
- Implemented right-associativity for the
-
Improved Error Handling:
- Enhanced error messages for invalid expressions and unsupported tokens.
To use the calculator library in your project, clone the repository and install the dependencies:
git clone https://github.com/WebDevsOrg/calculator.git
cd calculator
npm installconst calculator = require("./calculator");
// Simple addition
console.log(calculator.parseExpr("5+5")); // Output: 10
// Complex expression
console.log(calculator.parseExpr("2+3*3/(4-3)")); // Output: 11
// Nested parentheses
console.log(calculator.parseExpr("(1+(2*3))/(2+4)")); // Output: 1
// Division by zero
try {
console.log(calculator.parseExpr("1/0"));
} catch (error) {
console.error(error.message); // Output: Division by zero is undefined (0 / 0).
}
// Invalid expression
try {
console.log(calculator.parseExpr("2^3"));
} catch (error) {
console.error(error.message); // Output: Unsupported token encountered: '^'
}
// Exponentiation
console.log(calculator.parseExpr("2^3")); // Output: 8
console.log(calculator.parseExpr("2^2^3")); // Output: 256 (Right-associative)To normalize files in the repository:
-
Ensure
.gitattributesis committed. -
Run the
run_git_normalization.shscript:bash run_git_normalization.sh
- Node.js
- Git (for repository management)
This project uses mocha for testing. To run the test suite, use the following command:
npm testThe project uses eslint and prettier for linting and formatting. Run the following commands to lint and format the code:
-
Lint the code:
npm run lint
-
Format the code:
npm run format
Contributions are welcome! If you have ideas for new features or improvements, feel free to open an issue or submit a pull request.
- Fork the repository.
- Create a feature branch.
- Commit your changes.
- Push the branch and create a pull request.
The calculator library provides the following exported functions:
parseExpr: Parses an infix algebraic expression, converts it to postfix notation, and evaluates the result.
Currently, parseExpr is the only function accessible outside the calculator.js file. If you need access to additional internal functions, consider exporting them explicitly in calculator.js.
For a detailed list of changes in each version, see the CHANGELOG.md file.
This project is licensed under the MIT License.
If you encounter any issues or have questions, please open an issue on the GitHub repository.