目录
目录README.md

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

  • 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

支持的运算 | Supported Operations

运算符 描述 示例 结果
+ 加法 3 + 5 8
- 减法 10 - 3 7
* 乘法 4 * 6 24
/ 除法 15 / 3 5
^ 幂运算 2 ^ 3 8
() 括号 (2 + 3) * 4 20
- 负数 -5 + 3 -2
Operator Description Example Result
+ Addition 3 + 5 8
- Subtraction 10 - 3 7
* Multiplication 4 * 6 24
/ Division 15 / 3 5
^ Exponentiation 2 ^ 3 8
() Parentheses (2 + 3) * 4 20
- Negative numbers -5 + 3 -2

运算优先级 | Operator Precedence

  1. 括号 () (最高优先级 | Highest)
  2. 幂运算 ^ (右结合 | Right associative)
  3. 乘法 * 和除法 /
  4. 加法 + 和减法 - (最低优先级 | Lowest)

项目结构 | Project Structure

src/
├── main.rs              # 主程序入口 | Main entry point
└── calc/                # 计算器核心模块 | Calculator core module
    ├── mod.rs           # 模块定义 | Module definition
    ├── ast.rs           # 抽象语法树 | Abstract syntax tree
    ├── error.rs         # 错误类型定义 | Error type definitions
    ├── lexer.rs         # 词法分析器 | Lexical analyzer
    ├── parser.rs        # 语法分析器 | Parser
    └── token.rs         # 词法单元定义 | Token definitions

技术实现 | 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_decimal library to avoid floating-point precision issues

依赖 | Dependencies

  • ctrlc - 处理 Ctrl+C 信号 | Handle Ctrl+C signals
  • rust_decimal - 高精度十进制运算 | High-precision decimal arithmetic
  • thiserror - 错误处理 | Error handling

测试 | Testing

# 运行测试 | Run tests
cargo test

# 运行测试并显示输出 | Run tests with output
cargo test -- --nocapture

许可证 | License

本项目采用 Apache 2.0 许可证 - 详见 LICENSE 文件。

This project is licensed under the Apache 2.0 License - see the LICENSE file for details.

关于
54.0 KB
邀请码