Linux 中的 dc 命令及示例
Linux 中的dc命令用于计算算术表达式。它以后缀表达式的形式计算表达式。输入一个数字会将其推入堆栈,输入一个运算符会计算一个表达式并将结果推回堆栈。它可以计算 +、-、/、*、%、^。可以使用不同的命令来操作堆栈。
句法:
dc [OPTION] [file ...]
下表说明了不同的命令及其解释:
Command | Operation |
---|---|
p | Prints the value on the top of the stack and ends the statement with a newline. |
n | Prints the value on the top of the stack and ends the line with a null statement. |
f | Prints the entire stack, without any alteration. |
P | Pops the value from the top of the stack. |
c | Clear the stack. |
d | Duplicates the top value and push it into the main stack. |
r | Reverses the order of top two elements in the stack. |
Z | Pops the value from the stack, calculate the number of digits in it and pushes that number. |
X | Pops the value from the stack, calculate the number of fraction digits in it and pushes that number. |
z | Pushes the stack length into the stack. |
i | Pops the value from the stack and uses it as input radix. |
o | Pops the value from the stack and uses it as output radix. |
k | Pops the values from the stack and uses it to set precision. |
I | Pushes the value of input radix into the stack. |
O | Pushes the value of output radix into the stack |
K | Pushes the precision value into the stack. |
示例 1:
示例 2:
注意:在第二个例子中,输出改变是因为我们改变了输出基数。
选项:
- 表达式:此命令用于评估特定表达式。
- file :此命令用于评估文件的内容。
- help :此命令显示帮助信息。
- version :该命令用于显示版本信息。