rɑː*_*dʒɑ 124 command-line software-recommendation calculator
我正在寻找一个计算器,它可以在终端本身进行计算,而无需任何其他额外的前缀和后缀。
例如:如果我在终端输入 10000-9000 之类的东西,答案应该是 1000。
我再说一次,我只需要一个终端中的快速计算器,不添加任何字符。我知道如果我切换到 Python,它可以做到这一点,但我不希望它以这种方式。
ste*_*ver 97
您可以使用((...))语法在 bash 中本地进行简单的整数运算,例如
$ echo $((10000-9000))
1000
Run Code Online (Sandbox Code Playgroud)
还有bc计算器,它可以接受标准输入上的算术表达式
$ echo "10000-9000" | bc
1000
Run Code Online (Sandbox Code Playgroud)
该bc程序也可以进行浮点运算
$ echo "scale = 3; 0.1-0.09" | bc
.01
Run Code Online (Sandbox Code Playgroud)
Rad*_*anu 96
您可以使用calc. 默认情况下不安装,但您可以使用以下命令快速安装:
sudo apt-get install apcalc
Run Code Online (Sandbox Code Playgroud)
安装后,您可以根据需要进行任何计算:
$ calc 5+2
7
$ calc 5-2
3
$ calc 5*2
10
$ calc 5/2
2.5
$ calc 5^2
25
$ calc 'sqrt(2)'
1.4142135623730950488
$ calc 'sin(2)'
0.9092974268256816954
$ calc 'cos(2)'
-0.416146836547142387
$ calc 'log(2)'
~0.30102999566398119521
$ calc 'sqrt(sin(cos(log(2))))^2'
~0.81633199125847958126
$ # and so on...
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请查看其手册页
kir*_*iri 83
另一种可能的解决方案是为 Bash 的内置算法添加一个简单的函数。将其放入您的.bashrc文件中以尝试:
=() {
echo "$(($@))"
}
Run Code Online (Sandbox Code Playgroud)
所以现在,你甚至不需要$((...))了,=这看起来很自然。
另一件事,如果你想更快:你可以把它替换p用+和x用*。这将适用于:
=() {
local IFS=' '
local calc="${*//p/+}"
calc="${calc//x/*}"
echo "$(($calc))"
}
= 5 x 5 # Returns 25
= 50p25 # Returns 75
Run Code Online (Sandbox Code Playgroud)
现在你甚至不需要Shift了,唯一的事情就是= 前面的算术。
如果需要,输出可以以十进制和十六进制显示。(注意:使用x替换会与0x...十六进制语法冲突)
=() {
local answer="$(($@))"
printf '%d (%#x)\n' "$answer" "$answer"
}
Run Code Online (Sandbox Code Playgroud)
例子:
$ = 16 + 0x10
272 (0x110)
$ = 16**3 + 16**4
69632 (0x11000)
Run Code Online (Sandbox Code Playgroud)
bc如果你想要更高级的计算,你可以bc像这样通过管道将它:
=() {
local IFS=' '
local calc="${*//p/+}"
calc="${calc//x/*}"
bc -l <<<"scale=10;$calc"
}
= 'sqrt(2)' # Returns 1.4142135623
= '4*a(1)' # Returns pi (3.1415926532)
Run Code Online (Sandbox Code Playgroud)
提供的功能bc如下(可以从 中找到man bc):
sqrt ( expression )
The value of the sqrt function is the square root of the expression.
If the expression is negative, a run time error is generated.
s (x) The sine of x, x is in radians.
c (x) The cosine of x, x is in radians.
a (x) The arctangent of x, arctangent returns radians.
l (x) The natural logarithm of x.
e (x) The exponential function of raising e to the value x.
j (n,x)
The Bessel function of integer order n of x.
Run Code Online (Sandbox Code Playgroud)
它还支持if,for,while和像编程语言变量但如果如果你想,这可能是更好的写入文件。
请记住,它将替换p和x函数/变量名称。最好只删除替换项。
gcalccmd您还可以像这样进行函数调用gcalccmd(from gnome-calculator):
=() {
local IFS=' '
local calc="$*"
# Uncomment the below for (p ? +) and (x ? *)
#calc="${calc//p/+}"
#calc="${calc//x/*}"
printf '%s\n quit' "$calc" | gcalccmd | sed 's:^> ::g'
}
= 'sqrt(2)' # Returns 1.4142135623
= '4^4' # Returns 256
Run Code Online (Sandbox Code Playgroud)
可用函数似乎是(直接取自源代码),==表示等效函数:
ln()
sqrt()
abs()
int()
frac()
sin()
cos()
tan()
sin?¹() == asin()
cos?¹() == acos()
tan?¹() == atan()
sinh()
cosh()
tanh()
sinh?¹() == asinh()
cosh?¹() == acosh()
tanh?¹() == atanh()
ones()
twos()
Run Code Online (Sandbox Code Playgroud)
Tho*_*ard 30
不幸的是,没有“更简单”的方法可以做到这一点。命令行上的交互式 python 界面最适合您的需要,因为与apcalc\不同,python它包含在 Ubuntu 中。我不确定是否bc仍然包含在内,但是,python 是这些东西的首选。
您可以python在命令行上运行交互式界面,然后以这种方式进行数学运算。您可以将其用作计算器。
为此,您打开终端,输入python,然后点击Enter按钮。
然后,在出现的 python 提示中,您可以输入您的数学。例如,10000 - 9000. 下一行输出是结果。
但是,如果您的意思是,您只需加载终端即可执行此操作...
$ 10000 - 9000 1000 $
......那么没有没有办法在这样做只是没有其他任何终端,因为击不处理的数字参数那样。
kir*_*iri 23
我建议您为基本的 Python 计算创建一个简单的函数。在你的类似这样的东西.bashrc:
calc() {
python3 -c 'import sys; print(eval(" ".join(sys.argv[1:])))' "$@"
}
calc 5 + 5
# Returns 10
result="$(calc 5+5)"
# Stores the result into a variable
Run Code Online (Sandbox Code Playgroud)
如果您想进行更高级的数学运算,可以使用以下导入math模块所有功能的方法。(请参阅此处了解更多信息)
calc() {
python3 -c 'from math import *; import sys; print(eval(" ".join(sys.argv[1:])))' "$@"
}
calc 'sqrt(2)' # Needs quotes because (...) is special in Bash
# Returns 1.4142135623730951
result="$(calc 'sqrt(2)')"
# Stores the result into a variable
Run Code Online (Sandbox Code Playgroud)
(注意:因为 Python 是一种编程语言,有些东西可能看起来很奇怪,例如对于模的**幂和%模)
或者,您可以创建一个 Python 脚本calc,
#!/usr/bin/python3
from math import *
import sys
print(eval(' '.join(sys.argv[1:])))
Run Code Online (Sandbox Code Playgroud)
将其放在PATH变量中包含的目录中并设置其可执行标志以获得与calc上述相同的命令(无需创建 Bash 函数来运行 Python 脚本)。
如果您想要纯 Bash 中的方法,请使用 steeldriver 的答案。只有当您需要更高级的函数(即 from math)时,这个答案才真正有用,因为与 Bash 相比,Python 相对较慢。
我不确定这是否会破坏你的“切换到 python 它可以做到这一点,我不希望它以这种方式”。请注意,但您不需要输入交互式提示,并且可以在 Bash 中访问结果,因此这个答案似乎有效(至少对我而言)。
Fli*_*int 21
使用gcalccmdfrom gnome-calculator(>=13.04) 或gcalctool(<13.04) 包。我认为该软件包是默认安装的
% gcalccmd
> 2+3
5
> 3/2
1.5
> 3*2
6
> 2-3
?1
>
Run Code Online (Sandbox Code Playgroud)
use*_*873 11
这是一个快速的shell脚本:
#!/bin/bash
echo "$@" | bcRun Code Online (Sandbox Code Playgroud)
将其另存为“c”,然后将其放在路径中的某个位置(例如 /bin),然后将其标记为可执行。
# nano /bin/c
# chmod +x /bin/c
Run Code Online (Sandbox Code Playgroud)
从现在开始,您可以像这样在终端中运行计算:
$ c 10000-9000
1000Run Code Online (Sandbox Code Playgroud)
JW.*_*JW. 10
我在这里没有看到的另一个解决方案是Qalculate (qalc)。
sudo apt-get install qalc
Run Code Online (Sandbox Code Playgroud)
对于 CLI 版本,
sudo apt-get install qalculate-gtk
Run Code Online (Sandbox Code Playgroud)
对于 GUI。
它有很多功能,例如:
20 m / s * 12 h = 864 kilompi, e, c,avogadrosin(pi) = 0, gamma(4) = 6, 5! = 120,log(1024, 2) = 10
> 120 in
120 * inch = 120 in
> convert cm
120 in = 304.8 centim
(x + y)^2 = x^2 + 2xy + y^2integrate 3*x^2 = x^3,diff sin(x), pihelp convert,help integratefactorial(5)和faculteit(5)。你说你想在没有前缀的情况下使用它,嗯……你可以用前缀来使用它:
$ qalc 5 ft + 3 cm
(5 * foot) + (3 * centim) = 1.554 m
以及将其作为 repl 运行。
下面是适当的部分的变形例/etc/bash.bashrc(在Ubuntu 10.04),将修改command_not_found处理程序以运行该壳的表达式计算器,如果未知命令的第一个字符是数字或-或+。
你可以用这种方式做任何 shell 算术;有关算术运算符列表,请参阅http://www.gnu.org/software/bash/manual/bashref.html#Shell-Arithmetic。
请注意,如果要计算的表达式包含 a *,则必须引用*with\或引号,因为 shell 在决定运行哪个命令之前会进行文件名扩展。对于其他运算符(如>>.
把它放在你的 中~/.bashrc,然后输入. ~/.bashrc并尝试一下。
# if the command-not-found package is installed, use it
if [ -x /usr/lib/command-not-found -o -x /usr/share/command-not-found ]; then
function command_not_found_handle {
if [[ $1 == [0-9+-]* ]]; then
echo $(( $@ ))
elif [ -x /usr/lib/command-not-found ]; then
/usr/bin/python /usr/lib/command-not-found -- $1
return $?
elif [ -x /usr/share/command-not-found ]; then
/usr/bin/python /usr/share/command-not-found -- $1
return $?
else
return 127
fi
}
fi
Run Code Online (Sandbox Code Playgroud)
示例输出:(我正在输入cta,一个错字,只是为了测试我们的新 command_not_found 处理程序是否仍会尝试查找未知命令)。
mp@ubuntu:~$ cta
No command 'cta' found, did you mean:
Command 'cda' from package 'xmcd' (universe)
Command 'cat' from package 'coreutils' (main)
cta: command not found
mp@ubuntu:~$ 9000-1000
8000
Run Code Online (Sandbox Code Playgroud)
dc!它是 coreutils 的一部分,因此它安装在 OS X、Ubuntu 和几乎所有其他系统上。这是一个 RPN 计算器,所以如果你不喜欢那些,它不适合你。
非常基本的命令如下(手册页包含我没有包含的所有语法。求幂,有人吗?)
您只需要数字之间的空格。在所有其他情况下,它们将被忽略。
输入一个数字会将它推到堆栈的顶部。
+ Adds top 2 items in stack, then pushes result to stack (`2 4 +p` outputs 6)
- Subtracts top 2 items in stack, then pushes result to stack (`4 2 -p` outputs 2)
* Multiplies top 2 items in stack, then pushes result to stack (`6 5 *p` outputs 30)
/ Divides top 2 items in stack, then pushes result to stack (`54 7 /p` outputs 8)
p Print top item in stack, without destroying it
c Clear stack
r Swap top 2 items on stack
d Duplicate top item on stack
k Pops top item off stack, using it to determine precision (so 10 k would print 10 numbers after the decimal point). Default is 0, so it won't do floating point math by default.
n Pops top value off stack, then sends to stdout without a trailing newline
f Dump stack. Useful for finding what something does
Run Code Online (Sandbox Code Playgroud)
我将 Octave 用于此类事情:http : //www.gnu.org/software/octave/
它几乎是一个 matlab 克隆(抱歉,如果这过于简化了),可以通过键入八度在终端中使用它。安装 sudo apt-get install八度
它不是你想要的,但我想我会添加它作为 python 的替代品。
用法示例:
~ $ octave
octave:1> 9000 - 8000
ans = 1000
octave:2>
Run Code Online (Sandbox Code Playgroud)
您也可以使用awk在终端上进行一些算术计算,
echo 10000 9000 | awk '{print $1-$2}'
1000
echo 10000 9000 | awk '{print $1+$2}'
19000
echo 10000 9000 | awk '{print $1/$2}'
1.11111
echo 10000 9000 | awk '{print $1*$2}'
90000000
Run Code Online (Sandbox Code Playgroud)
我非常喜欢 wcalc。这是一个命令行科学计算器。在 Ubuntu 软件中心很容易找到,或者直接使用 apt-get。
sudo apt-get install wcalc
Run Code Online (Sandbox Code Playgroud)
它接受命令行参数以及“shell”模式:
# simple operation
$ wcalc 2+2
= 4
# Quoting is necessary to prevent shell from evaluating parenthesis
$ wcalc "(2+2)*10"
= 40
$ wcalc "sqrt(25)"
~= 5
# in shell mode you can evaluate multiple commands repeatedly
$ wcalc
Enter an expression to evaluate, q to quit, or ? for help:
-> 12*20+1
= 241
-> sin(90)
= 1
-> sin(pi/2)
= 0.0274121
Run Code Online (Sandbox Code Playgroud)
如果有人像我一样从事工程学,则可以使用 GNU Octave。它可以做各种各样的事情,绘图,解联立方程。此外,它是 Matlab 的免费替代品
| 归档时间: |
|
| 查看次数: |
79125 次 |
| 最近记录: |