计算贷款的shell脚本


计算贷款的shell脚本
 
   目的就是在温度转换上再进一步的了解数学计算。
 
代码:
 
01
#!/bin/sh
02
 
03
# loancalc.sh -- 指定贷款的本金、税率、年限
04
 
05
# 公式: M = P * (J / (1- (1 + J)** - N))
06
# 其中, P = 本金、J = 每月税率、N = 年限(月表示)
07
# 用户输入P、I(年利率)、L(长度,年份)
08
 
09
# 注意,公式中后面的 (1 + J)** - N 是一个整体,表示指数
10
# 比如,(1 + 1)** - 2 就是 2 ^ -2,相当于 1/2*2 = 0.25
11
 
12
#source library.sh
13
 
14
if [ $# -ne 3 ]; then
15
    echo "Usage: `basename $0` principal interest loan-duration-years" >&2
16
    exit 1
17
fi
18
 
19
# 这儿用到的scriptbc.sh是第九个脚本的程序
20
# 传给它的参数是一个公式,部分运算符号需要转义
21
# 转义的有: ( ) * ^
22
P=$1 I=$2 L=$3
23
J="$(scriptbc.sh -p 8 $I/\(12 \* 100\))"
24
N="$(($L*12))"
25
M="$(scriptbc.sh -p 8 $P\*\($J/\(1-\(1+$J\)\^-$N\)\))"
26
 
27
dollars="$(echo $M | cut -d. -f1)"
28
cents="$(echo $M | cut -d. -f2 | cut -c1-2)"
29
 
30
# newnicenum.sh是第4个脚本中的程序
31
cat << EOF
32
A $L year loan at $I% interest with a principal amount of $(newnicenum.sh $P 1)
33
results in a payment of \$$dollars.$cents each month for the duration of
34
the loan($N payments).
35
EOF
36
 
37
exit 0
运行结果:
1
$ loancalc 40000 6.75 4
2
A 4 year loan at 6.75% interest with a principal amount of 40,000
3
results in a payment of $953.21 each month for the duration of
4
the loan (48 payments).
5
 
6
$ loancalc 40000 6.75 5
7
A 5 year loan at 6.75% interest with a principal amount of 40,000
8
results in a payment of $787.33 each month for the duration of
9
the loan (60 payments).
分析脚本: 
这个脚本比较完整的展示了一个数学公式的运算。 
 

相关内容

    暂无相关文章