命令行表达式求解器?

Luc*_*ips 8 linux software-rec math

我正在寻找与 Linux 兼容的基于 TTY 的计算器。例如:

user@host:~$ calculate
> 2
2
user@host:~$ calculate
> 8*6-4
44
user@host:~$ calculate
> 8*(6-4)
16
Run Code Online (Sandbox Code Playgroud)

是否有这样的东西支持基本操作、一些内置函数(如 )atan(),以及可能通过脚本编写的自定义函数?

slm*_*slm 8

公元前和直流

bc并且dc是我需要从终端访问时经常使用的 2 个计算器。

例子

$ bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
Run Code Online (Sandbox Code Playgroud)

然后你可以输入你的问题:

2
2
5+5
10
Run Code Online (Sandbox Code Playgroud)

完成后,您可以使用Ctrl+退出C

试驾

这些计算器功能非常丰富。

缩放

scale=5
193 * 1/3
64.33333
Run Code Online (Sandbox Code Playgroud)

方程

principal=100
ir = 0.05
years = 5
futurevalue = principal * (1 + ir)^years

futurevalue
127.62800
Run Code Online (Sandbox Code Playgroud)

你的例子

8*6-4
44

8*(6-4)
16
Run Code Online (Sandbox Code Playgroud)

计算

如果你想要一些更具交互性的东西,那就是calc.

例子

$ calc
C-style arbitrary precision calculator (version 2.12.4.4)
Calc is open software. For license details type:  help copyright
[Type "exit" to exit, or "help" for help.]

; 10+10
20
; 8*6-4
    44
; 8*(6-4)
    16
; 
Run Code Online (Sandbox Code Playgroud)

您可以使用向上/向下箭头浏览过去的命令,它还具有交互式帮助。

; help
Run Code Online (Sandbox Code Playgroud)

给你这个:

For more information while running calc, type  help  followed by one of the
following topics:

    topic               description
    -----               -----------
    intro               introduction to calc
    overview            overview of calc
    help                this file

    assoc               using associations
    builtin             builtin functions
    command             top level commands
    config              configuration parameters
    custom              information about the custom builtin interface
    define              how to define functions
    environment         how environment variables effect calc
    errorcodes          calc generated error codes
    expression          expression sequences
    file                using files
    history             command history
    interrupt           how interrupts are handled
    list                using lists
    mat                 using matrices
    ...
Run Code Online (Sandbox Code Playgroud)

参考


fro*_*utz 6

你的问题有很多答案...

您可以在 shell 中执行的简单操作。

$ echo $((8*(6-4)))
16
Run Code Online (Sandbox Code Playgroud)

作为一个专用程序,有bc.

$ echo "8*(6-4)" | bc
16
Run Code Online (Sandbox Code Playgroud)

通过脚本自定义函数?嗯,在某种程度上,shell 脚本和 bc 都有它们。取决于你想走多远。

为什么不是 Python?这很容易学习。

$ python
>>> from math import atan
>>> 8*(6-4)+atan(0)
16.0
Run Code Online (Sandbox Code Playgroud)


Sté*_*las 5

zsh

$ autoload zcalc  # best in ~/.zshrc
$ zcalc
1> 8*(6-4)
16
2> $1*2
32
Run Code Online (Sandbox Code Playgroud)