在windows下,当我需要执行基本计算时,我会使用内置的计算器。现在我想找出如果你只有一个shell的话最常见的方法是什么。
谢谢
发布于 2009-09-15 17:09:23
你可以随时使用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
当然还有math模块,它可以解决你的大部分计算器需求。
>>> import math
>>> math.pi
3.1415926535897931
>>> math.e
2.7182818284590451
>>> math.cos() # cosine
>>> math.sqrt()
>>> math.log()
>>> math.log10()
发布于 2009-09-15 17:17:28
来自this web page (对于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
方法的人。
https://stackoverflow.com/questions/1430395
复制相似问题