Chinaunix首页 | 论坛 | 博客
  • 博客访问: 21794
  • 博文数量: 14
  • 博客积分: 275
  • 博客等级: 二等列兵
  • 技术积分: 125
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-24 13:32
文章分类

全部博文(14)

文章存档

2012年(5)

2011年(9)

最近访客

分类: LINUX

2011-12-09 09:58:21

转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://tanzj.blog.51cto.com/802764/162464

bc - An arbitrary precision calculator language
 
       -i, --interactive
              Force interactive mode.   交互式对话
#   /* */   注释用
 
A simple variable is just a
              name      单个变量
and an array variable is specified as
              name[expr]  数组
Boolean operations are also legal.
       !expr  The result is 1 if expr is 0.
       expr && expr
              The result is 1 if both expressions are non-zero.
       expr || expr
              The result is 1 if either expression is non-zero.
       length ( expression )
              The value of the length function is the number of significant digits in the expression.
       read ( )
      The  read  function  (an extension) will read a number from the standard input, regardless of where the function occurs.  Beware, this can cause problems with the mixing of data and program in the  standard input. The best use for this function is in a previously written program that needs input from the              user, but never allows program code to be input from the user.  The value of the read function  is  the              number  read  from  the standard input using the current value of the variable ibase for the conversion              base.
 
STATEMENTS
Statements (as in most algebraic languages) provide the sequencing of expression evaluation.
expression
string
print list
{ statement_list }
if ( expression ) statement1 [else statement2]
while ( expression ) statement
              expression1;
              while (expression2) {
                 statement;
                 expression3;
              }
for ( [expression1] ; [expression2] ; [expression3] ) statement
break
continue
halt
return
return ( expression )
FUNCTIONS
define name ( parameters ) { newline
                  auto_list   statement_list }
 
MATH LIBRARY     要调用这些数学函数,使用 –l 参数。
       -l, --mathlib
              Define the standard math library.
       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.
EXAMPLES
1.     
pi=$(echo "scale=10; 4*a(1)" | bc -l)
2.                   
scale = 20            #  scale 定义 有效小数位数
bc 預設僅輸出整數,如果要輸出小數點下位數,那麼就必須要執行 scale=number ,那個 number 就是小數點位數
              /* Uses the fact that e^x = (e^(x/2))^2
                 When x is small enough, we use the series:
                   e^x = 1 + x + x^2/2! + x^3/3! + ...
              */
              define e(x) {
                auto  a, d, e, f, i, m, v, z
                /* Check the sign of x. */
                if (x<0) {
                  m = 1
                  x = -x
                }
                /* Precondition x. */
                z = scale;
                scale = 4 + z + .44*x;
                while (x > 1) {
                  f += 1;
                  x /= 2;
                }
                /* Initialize the variables. */
                v = 1+x
                a = x
                d = 1
                for (i=2; 1; i++) {
                  e = (a *= x) / (d *= i)
                  if (e == 0) {
                    if (f>0) while (f--)  v = v*v;
                    scale = z
                    if (m) return (1/v);
                    return (v/1);
                  }
                  v += e
                }
              }
3.
scale=2
              print "\nCheck book program!\n"
              print "  Remember, deposits are negative transactions.\n"
              print "  Exit by a 0 transaction.\n\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
              }
              quit
4.
           define f (x) {
                if (x <= 1) return (1);
                return (f(x-1) * x);
              }
5.      二、八、十、十六进制转化
[frost:/xin]# echo 'obase=16;ibase=2;1010'|bc
6.         一个递归的应用 IP
#!/usr/bin/bc
 
p=32;
define f(x) {
        if(x<=1) return(p);
p--;
return f(x/2);
}
f(32);
 
后来全用awk 替代了。。。这些东西用的少了

阅读(535) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~