适用于Ubuntu的命令行计算器?

分享于 

3分钟阅读

ubuntu

  繁體

Bash算法

一种可解决方案是为Bash的内置一个简单的函数,把这个放在你的.bashrc文件中,尝试:


=() {


 echo"$(($@))"


}



替换

如果你想更快:你可以将p替换为+,将x替换为*


=() {


 local IFS=' '


 local calc="${*//p/+}"


 calc="${calc//x/*}"


 echo"$(($calc))"


}



= 5 x 5 # Returns 25


= 50p25 # Returns 75



十六进制输出

如果需要,输出可以同时以十进制和十六进制显示,注意:使用x会与0x...十六进制语法冲突)


=() {


 local answer="$(($@))"


 printf '%d (%#x)n'"$answer""$answer"


}



例如:


$ = 16 + 0x10


272 (0x110)



$ = 16**3 + 16**4


69632 (0x11000)



使用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)



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.



它也支持ifforwhile,像编程语言一样的变量。

使用gcalccmd

你还可以像这样调用gcalccmd (来自gnome-calculator )函数:


=() {


 local IFS=' '


 local calc="$*"


 # Uncomment the below for (p → +) and (x → *)


 #calc="${calc//p/+}"


 #calc="${calc//x/*}"


 printf '%sn quit'"$calc" | gcalccmd | sed 's:^> ::g'


}



= 'sqrt(2)' # Returns 1.4142135623


= '4^4' # Returns 256




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()





COM  COMM  Comma  CAL  命令行  Ubuntu  
相关文章