linux shell程序设计,输入n,计算1+1/2+1/3+...+1/n。要求调用bc
[oracle@localhost test]$ cat c.sh
#!/bin/sh
echo "请出入n的值:"
read n
for ((a=1;a<=n;a++))
b=1/$a+$b
c=`bc<
scale=5
echo "运算式是:$b"
echo "最后的结果是:$c"
[oracle@localhost test]$ sh c.sh
请出入n的值:
运算式是:1/20+1/19+1/18+1/17+1/16+1/15+1/14+1/13+1/12+1/11+1/10+1/9+1/8+1/7+1/6+1/5+1/4+1/3+1/2+1/1+0
最后的结果是:3.59768
[oracle@localhost test]$应该是scale ,不是sacle。sh-3.2#?cat?a
#!/bin/bash
sum=0
for?j?in?`seq?1?1?$i`
s=`echo?1/$j|bc?-l`
sum=`echo?$sum?+?$s|bc?-l`
echo?$sum
sh-3.2#?sh?a?5
2.28333333333333333333
sh-3.2#?sh?a?6
2.44999999999999999999
sh-3.2#?sh?a?7
2.59285714285714285713
linux shell程序设计,输入n,计算1+1/2+1/3+...+1/n。要求调用bc
[oracle@localhost test]$ cat c.sh
#!/bin/sh
echo "请出入n的值:"
read n
for ((a=1;a<=n;a++))
b=1/$a+$b
c=`bc<
scale=5
echo "运算式是:$b"
echo "最后的结果是:$c"
[oracle@localhost test]$ sh c.sh
请出入n的值:
运算式是:1/20+1/19+1/18+1/17+1/16+1/15+1/14+1/13+1/12+1/11+1/10+1/9+1/8+1/7+1/6+1/5+1/4+1/3+1/2+1/1+0
最后的结果是:3.59768
[oracle@localhost test]$应该是scale ,不是sacle。sh-3.2#?cat?a
#!/bin/bash
sum=0
for?j?in?`seq?1?1?$i`
s=`echo?1/$j|bc?-l`
sum=`echo?$sum?+?$s|bc?-l`
echo?$sum
sh-3.2#?sh?a?5
2.28333333333333333333
sh-3.2#?sh?a?6
2.44999999999999999999
sh-3.2#?sh?a?7
2.59285714285714285713
原创文章,作者:墨羽SEO,如若转载,请注明出处:https://www.moyuseo.com/fuwuqi/4372.html