Rust 1.70+ (建议使用最新稳定版 | Latest stable version recommended)
编译和运行 | Build and Run
# 克隆仓库 | Clone the repository
git clone https://github.com/Yiki21/rust_calculator.git
cd rust_calculator
# 编译项目 | Build the project
cargo build --release
# 运行计算器 | Run the calculator
cargo run
使用示例 | Usage Examples
启动程序后,输入数学表达式即可:
After starting the program, simply enter mathematical expressions:
print your math expression here, support (, ), +, -, *, /, ^, and numbers
Type 'ctrl + c' to quit.
3 + 5
Result: 8
10 - 2 * 3
Result: 4
(1 + 2) * 3
Result: 9
2 ^ 3 ^ 2
Result: 512
-5 + 3
Result: -2
3 + (4 * 2 - 1) / 5 ^ 2
Result: 3.28
Rust Calculator
一个用 Rust 编写的交互式命令行计算器,支持基本的数学运算和表达式求值。
A command-line calculator written in Rust that supports basic mathematical operations and expression evaluation.
功能特性 | Features
✅ 基本算术运算:加法 (
+)、减法 (-)、乘法 (*)、除法 (/)✅ 幂运算 (
^)✅ 括号支持 (
()) 用于改变运算优先级✅ 负数支持
✅ 高精度十进制运算(使用
rust_decimal)✅ 交互式命令行界面
✅ 优雅的错误处理
✅ Basic arithmetic operations: addition (
+), subtraction (-), multiplication (*), division (/)✅ Power operations (
^)✅ Parentheses support (
()) for precedence✅ Negative numbers support
✅ High-precision decimal arithmetic (using
rust_decimal)✅ Interactive command-line interface
✅ Graceful error handling
安装和使用 | Installation and Usage
前置要求 | Prerequisites
编译和运行 | Build and Run
使用示例 | Usage Examples
启动程序后,输入数学表达式即可:
After starting the program, simply enter mathematical expressions:
支持的运算 | Supported Operations
+3 + 58-10 - 37*4 * 624/15 / 35^2 ^ 38()(2 + 3) * 420--5 + 3-2+3 + 58-10 - 37*4 * 624/15 / 35^2 ^ 38()(2 + 3) * 420--5 + 3-2运算优先级 | Operator Precedence
()(最高优先级 | Highest)^(右结合 | Right associative)*和除法/+和减法-(最低优先级 | Lowest)项目结构 | Project Structure
技术实现 | Technical Implementation
词法分析器 (Lexer): 将输入字符串转换为词法单元序列
语法分析器 (Parser): 使用递归下降算法构建抽象语法树
抽象语法树 (AST): 表示数学表达式的树形结构
高精度运算: 使用
rust_decimal库避免浮点精度问题Lexer: Converts input strings into token sequences
Parser: Uses recursive descent algorithm to build abstract syntax tree
AST: Tree structure representing mathematical expressions
High-precision arithmetic: Uses
rust_decimallibrary to avoid floating-point precision issues依赖 | Dependencies
ctrlc- 处理 Ctrl+C 信号 | Handle Ctrl+C signalsrust_decimal- 高精度十进制运算 | High-precision decimal arithmeticthiserror- 错误处理 | Error handling测试 | Testing
许可证 | License
本项目采用 Apache 2.0 许可证 - 详见 LICENSE 文件。
This project is licensed under the Apache 2.0 License - see the LICENSE file for details.