A powerful calculator written in Rust with both command-line and desktop graphical interfaces. This calculator supports various arithmetic operations, mathematical functions, unit conversions, and user-defined variables with a clean and intuitive interface.
- Basic Operations: Addition, subtraction, multiplication, division, modulo, and exponentiation
- Mathematical Functions: Square root, trigonometric functions (sin, cos, tan), inverse trigonometric functions (asin, acos, atan), logarithms (log, ln), exponential, absolute value, floor, ceiling, and factorial
- Mathematical Constants: Pi (π), e, tau (τ), phi (φ), and infinity
- Unit Conversions:
- Length (km to mi, mi to km, in to cm, cm to in)
- Weight (kg to lb, lb to kg)
- Temperature (C to F, F to C)
- Volume (gal to l, l to gal)
- Angular (degrees to radians, radians to degrees)
- Memory Functions: Memory add (M+), memory subtract (M-), memory recall (MR), memory clear (MC)
- User-defined Variables: Create, store, and use custom variables in expressions
- Expression History: Persistent calculation history between sessions
- Multiple Interfaces:
- Command-line interface with history support and tab completion
- Desktop graphical interface with keyboard support and modern styling
- Error Handling: Robust error handling for invalid inputs and mathematical errors
- Rust and Cargo installed on your system. If not, install them from rust-lang.org.
-
Clone the repository:
git clone https://github.com/thickkoezz/rust-calculator.git cd rust-calculator -
Build the project:
cargo build --release
-
Run the command-line calculator:
./target/release/rust-calculator-cli
-
Run the desktop GUI calculator:
./target/release/rust-calculator-gui
Pre-built binaries for Windows, macOS, and Linux are available on the Releases page.
Once the CLI calculator is running, you can enter mathematical expressions in the following formats:
Format: number operator number
> 5 + 3
= 8
> 10 - 4
= 6
> 6 * 7
= 42
> 20 / 5
= 4
> 10 % 3
= 1
> 2 ^ 3
= 8
Format: function(number) or function number
> sqrt 16
= 4
> sin 90
= 1
> cos 0
= 1
> tan 45
= 1
> log 100
= 2
> ln 1
= 0
> exp 1
= 2.718281828459045
> abs -5
= 5
> floor 3.7
= 3
> ceil 3.2
= 4
> fact 5
= 120
Format: constant or constant operator number
> pi
= 3.141592653589793
> 2 * e
= 5.43656365691809
> tau
= 6.283185307179586
> phi
= 1.618033988749895
Format: number conversion_function or number<space>conversion_function
> 10 km_to_mi
= 6.21371
> 5 mi_to_km
= 8.0467
> 100 c_to_f
= 212
> 32 f_to_c
= 0
> 180 deg_to_rad
= 3.141592653589793
> pi rad_to_deg
= 180
> 5 m+ # Add 5 to memory
= 5
> 3 m+ # Add 3 to memory
= 8
> mr # Recall memory value
= 8
> 2 m- # Subtract 2 from memory
= 6
> mc # Clear memory
= 0
> let x = 10
Variable x = 10
> let y = 5
Variable y = 5
> x + y
= 15
> vars # List all variables
Defined Variables:
x = 10
y = 5
> help # Display help information
> history # Show calculation history
> clearhistory # Clear calculation history
> clear # Clear the screen
> exit # Exit the calculator
The calculator supports complex expressions with proper operator precedence:
> 2 + 3 * 4
= 14
> (2 + 3) * 4
= 20
> sin(45) + cos(45)
= 1.414213562373095
> log(100) / ln(e^2)
= 1
The desktop calculator provides a graphical interface with:
- Button grid for quick access to numbers, operators, and functions
- Text input field for complex expressions
- Result display area
- Full keyboard support
- Modern, styled interface with visual feedback
- Error handling with visual feedback
You can use the desktop calculator in several ways:
- Click buttons on the interface to build expressions
- Type directly into the input field
- Use keyboard shortcuts for common operations
- Number keys (0-9): Input numbers
+,-,*,/: Basic operations^: Exponentiation(,): ParenthesesEnteror=: Calculate resultEsc: Clear allDelete: Clear entryBackspace: Delete last character
The calculator handles various errors gracefully:
- Division by zero
- Square root of negative numbers
- Logarithm of non-positive numbers
- Factorial of negative numbers or non-integers
- Invalid number formats
- Unknown operators or functions
- Incorrect expression formats
- Mismatched parentheses
The project includes comprehensive documentation:
Generate the API documentation using Rust's built-in documentation tool:
cargo doc --openThis will generate and open the API documentation in your browser, showing detailed information about the calculator's functions, structures, and interfaces.
For more detailed information on using the calculator, refer to the User Manual in the project wiki.
Run the test suite with:
cargo testThe project includes:
- Unit tests for core functionality
- Integration tests for the calculator library
- Property-based tests to verify mathematical properties
Run the benchmarks with:
cargo benchBenchmarks measure performance of various calculator operations, helping to identify potential optimizations.
rust-calculator/
├── src/
│ ├── lib.rs # Core calculator functionality
│ ├── main.rs # CLI interface implementation
│ └── gui.rs # Desktop GUI implementation
├── tests/
│ ├── calculator_tests.rs # Standard tests
│ └── property_tests.rs # Property-based testing
├── benches/
│ └── calculator_benchmark.rs # Performance benchmarks
├── Cargo.toml # Project configuration
└── README.md # This file
- rustyline - Line editing for the CLI
- iced - GUI framework
- directories - Cross-platform file paths
- lazy_static - Lazy static initialization
- proptest - Property-based testing
- criterion - Benchmarking
This project uses GitHub Actions for continuous integration. Every push and pull request triggers:
- Running all tests
- Building the project
- Running benchmarks
- Checking code formatting
- Running Clippy for linting
Contributions are welcome! Feel free to submit pull requests or open issues to improve the calculator.
To contribute:
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Commit your changes:
git commit -m 'Add amazing feature' - Push to the branch:
git push origin feature/amazing-feature - Open a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
[Mieky Sofyan Yudinata] - @thickkoezz

