import std/strutils
import nimib
import ./theme/codeOutput
var nbToc: NbBlock
template addToc =
newNbBlock("nbText", false, nb, nbToc, ""):
nbToc.output = "## 目录:\n\n"
template nbNewSection(name:string) =
let anchorName = name.toLower.replace(" ", "-")
nbText "<a name=\"" & anchorName & "\"></a>\n<br>\n### " & name & "\n\n---"
nbToc.output.add "1. <a href=\"#" & anchorName & "\">" & name & "</a>\n"
template nbSubSection(name:string) =
let anchorName = name.toLower.replace(" ", "-")
nbText "<a name=\"" & anchorName & "\"></a>\n<br>\n#### " & name & "\n\n---"
nbToc.output.add " * <a href=\"#" & anchorName & "\">" & name & "</a>\n"
template nbQuoteBlock(code: untyped) =
nbText "<blockquote>"
code
nbText "</blockquote>"
nbInit
initCodeTheme()
nbText: """
《rpn_calc_C++》
======================================
- 作者:linghui
- 版本:24.0
本教程中的所有代码示例,均遵循 C++11 语言风格指南。"""
addToc()
nbNewSection "安装和编译指南"
nbText("""
1. 环境依赖:Ubuntu 64位系统,需安装g++、Git、Nim及Nimib
2. 安装命令:
- 安装编译器与Git
- 安装Nim
- 安装Nimib
3. 编译代码:g++ rpn_calc.cpp -o rpn_calc -lm
""")
nbCodeSkip:
sudo apt update && sudo apt install g++ git
curl -fsSL https://codeberg.org/janAkali/grabnim/raw/branch/main/grabnim.sh | sh && grabnim nim
nimble install nimib
nbNewSection "基本使用示例"
nbText("""
- 启动计算器
""")
nbCodeSkip:
./rpn_calc
nbText("""
- 执行计算
12 + 3 + 7 7 * +
- 最终结果:55
- 退出计算器: q
""")
nbNewSection("支持的操作说明")
nbText("""
- 四则运算:+(加)、-(减)、*(乘)、/(除)
- 高级运算:sqrt(平方根,如16 sqrt 计算4)
- 历史记录:history(查看所有计算记录)
- 退出程序:q
""")
nbNewSection("错误代码和异常说明")
nbText("""
| 错误类型 | 触发场景 | 提示信息 |
|------------|------------------------------|----------------------------------|
| 格式错误 | 输入非数字/非支持运算符 | 错误:不支持的运算符 'xxx' |
| 平方根错误 | 对负数计算平方根 | 错误:负数无法计算平方根 |
""")
nbNewSection("示例输入输出")
nbText("""
示例1:基础四则运算
> 10 5 - 3 * 6 /
- 中间结果:5
- 中间结果:15
- 中间结果:2.5
- 最终结果:2.5
示例2:高级功能(平方根+历史记录)
> 25 sqrt
- 中间结果:5
- 最终结果:5
> history
- 计算历史记录:
- 1. 25 sqrt = 5.000000
示例3:错误处理
> 8 0 /
- 错误:除数为0,无法执行除法运算
- 栈已清空
""")
nbSave