免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1486 | 回复: 1
打印 上一主题 下一主题

linux下的计算器--bc [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-31 09:28 |只看该作者 |倒序浏览

[root@CentOS4 ~]# bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.

(interrupt) use quit to exit.
(interrupt) use quit to exit.
quit
[root@CentOS4 ~]#

--在bc上常用的运算符有:
+    加法
-    减法
*    乘法
/    除法(在没有设置scale精确度时候,自动取整)
^    指数
%    求余数(取模)
在bc上还可以使用
    ++ 变量            
           -- 变量
           变量 ++   
           变量 --
[root@CentOS4 ~]# bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
1+2
3
234+23/2*3
267
25^3
15625
100%4
0
100%23
8
quit

[root@CentOS4 ~]#
[root@CentOS4 ~]# bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
scale=2 /*scale=number*/

11/4
2.75
11/3
3.66
quit
[root@CentOS4 ~]#

说明:scale=2 是设置小数精确度。/*备注部分*/
bc还可以运行一个带有复杂运算的程序文件(这属于bc的一个扩展属性),如:
The following is code that uses the extended features of bc to  implement  
a  simple program for calculating checkbook balances.  
This program is best kept in a file so that it can be used many times  without
having to retype it at every use.
--使用vi建立一个test运算程序:
root@CentOS4 ~]# vi test
scale=2
print ""nCheck book program!"n"
print " Remember,deposits are negative transaction."n"
print "Initial balance?";bal=read()
bal /= 1
print ""n"
while (1){
        "current balance = ";bal
        "transaction?";trans =read()
        if(trans == 0) break;
        bal -= trans
        bal /= 1
}

~
~
~
"test" [New] 14L, 268C written                                
[root@CentOS4 ~]# ls

--计算结果:
[root@CentOS4 ~]# bc "test
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
Check book program!
Remember,deposits are negative transaction.
Initial balance?560
current balance = 560.00
transaction?400
current balance = 160.00
transaction?100
current balance = 60.00
transaction?0
quit

[root@CentOS4 ~]#

还可以这么用:

   将10进制数转换成16进制数
  比如转换 65535 为 16进制
  echo 'obase=16; 65535' | bc
  得到 FFFF
  printf 是挺方便的,不过我还是喜欢 bc ,因为它能转换任意进制,比如英制的12进制,
  你也可以炮制并不存在的进制。比如
  echo 'obase=37; 65535' | bc
  就是37进制啦,哈哈,它等于 01 10 32 08
  'obase=37; 65535' 都传递给程序 bc 告诉它,我要求现在 output base 为 37 ,那 65535 为多少呢?
  另外,还有 ibase 表示 input base,比如
  echo 'obase=16; ibase=8; 177777' | bc
  可以直接将八进制的数177777变成十六进制,也是FFFF

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/61861/showart_1763609.html

论坛徽章:
0
2 [报告]
发表于 2012-03-13 10:01 |只看该作者
学习了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP