在Windows下,当我需要执行基本计算时,我使用内置计算器。现在我想知道如果你只有一个外壳,常用的方法是什么。

谢谢

有帮助吗?

解决方案

你总是可以使用python解释器,它通常包含在linux发行版中。

http://docs.python.org /tutorial/introduction.html#using-python-as-a-calculator

$ python
Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)]
Type "help", "copyright", "credits" or "license" for more information.
>>> 2+2
4
>>> # This is a comment
... 2+2
4
>>> 2+2  # and a comment on the same line as code
4
>>> (50-5*6)/4
5
>>> # Integer division returns the floor:
... 7/3
2
>>> 7/-3
-3
>>> # use float to get floating point results.
>>> 7/3.0
2.3333333333333335

等号('=')用于为变量赋值。之后,在下一个交互式提示之前不会显示任何结果:

>>> width = 20
>>> height = 5*9
>>> width * height
900

当然还有数学模块,它可以解决您的大部分计算器需求

>>> import math
>>> math.pi
3.1415926535897931
>>> math.e
2.7182818284590451
>>> math.cos() # cosine
>>> math.sqrt()
>>> math.log()
>>> math.log10()

其他提示

来自此网页(适用于 csh 和衍生品,因为你问过):

% @ x = (354 - 128 + 52 * 5 / 3)
% echo Result is $x
Result is 174

% set y = (354 - 128 + 52 / 3)
% echo Result is $y
Result is 354 - 128 + 52 / 3

注意不同的结果。

就个人而言,我坚持 / bin / sh 并调用 awk 或其他东西(最大可移植性),或者其他人已经展示了 bash 方法

您可以使用 dc 。或 bc

这里给出了许多好的解决方案,但是 在shell中进行算术运算的“经典”方法是 与expr。

重击 支持基本(仅限整数)算术 里面 $(( )):

$ echo $(( 100 / 3 ))
33
$ myvar="56"
$ echo $(( $myvar + 12 ))
68
$ echo $(( $myvar - $myvar ))
0
$ myvar=$(( $myvar + 1 ))
$ echo $myvar
57

(直接从 IBM 链接复制的示例)

如果您正在使用 bash ,这里有一个简单的小shell脚本示例,它允许您从命令行进行计算(包括指定浮点数的精度):

http://www.novell.com/coolsolutions/tools/17043.html

你也可以轻松使用Perl,其中bc或expr不够强大:

$ perl5.8 -e '$a=1+2; print "$a\n"' 
3

替代选项是使用内置的BC命令

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top