51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

利用Linux shell编程解方程

在去年暑假,我刚中考完毕,抽出时间来好好学了学Linux的shell编程,感觉非常有意思,于是写了个shell来解方程,里面用到了挺多东西,印象最深的是函数的应用,毕竟这玩意对我来说确实挺新鲜,函数能把代码变成一个一个的模块,读起来方便,修改代码也方便。其次我还用到了其他一些有趣的内容,比如你输入参数,不允许输入英文字符,我便利用expr写了数字的检测;再比如我还用到了exit 1和exit 0,这样在解完方程后玩家输入echo $?可以清楚地看到状态码,不为0的则表示出现错误,为0则表示解题无误;此外我还用到了bc这个命令行计算器工具,使得浮点数可以进行运算了,这大大增加了计算器的使用范围;在一元二次方程的求解中,我用到了awk来进行开平方运算;在二元一次和三元一次方程组的求解中,我利用了线性代数中克莱姆法则进行求解,这玩意还可以判断方程组是否有唯一解,这就很舒服。这些都是技术细节,至于详细内容,请各位大佬们看看代码吧。代码中变量很多,而且没有注释(所以说我现在也看不懂我写的啥玩意了),同时感觉写得也比较罗里吧嗦,如果有大佬乐意读一读代码的话,希望能给一些建议,代码欢迎白嫖,谢谢支持(doge

#!/bin/bash
function QEOV() {
echo "ax^2+bx+c=0"
read -t 10 -p "a=" a
read -t 10 -p "b=" b
read -t 10 -p "c=" c
[ ${#a} == 0 -o ${#b} == 0 -o ${#c} == 0 ] && {
echo "ERROR,some of the numbers are NULL!"
exit 1
}
ab=$(echo "$a" | tr "." "0")
expr $ab + 23 &>/dev/null
AB=$?
bc=$(echo "$b" | tr "." "0")
expr $bc + 23 &>/dev/null
BC=$?
de=$(echo "$c" | tr "." "0")
expr $de + 23 &>/dev/null
DE=$?
[ $AB == 0 -a $BC == 0 -a $DE == 0 ] || {
echo "Have something that are not ALLOWD!"
exit 1
}
delta=$(echo "$b * $b - 4 * $a * $c" | bc -l)
bijiao=$(echo "$delta >= 0" | bc -l)
[ $bijiao == 0 ] && {
echo "Delta<0,NO SOLUTION!"
exit 0
}
sqrtDelta=$(awk 'BEGIN{print sqrt("'$delta'")}')
e=$(echo "- $b + $sqrtDelta" | bc -l)
f=$(echo "- $b - $sqrtDelta" | bc -l)
g=$(echo " $a + $a " | bc -l)
[ $g == 0 ] && {
echo "a=0,ERROR"
exit 1
}
x1=$(echo "$e / $g" | bc -l)
x2=$(echo "$f / $g" | bc -l)
echo "x1=$x1"
echo "x2=$x2"
exit 0
}
function BLE() {
echo -e "ax+by=c\ndx+ey=f"
read -t 10 -p "a=" a
read -t 10 -p "b=" b
read -t 10 -p "c=" c
read -t 10 -p "d=" d
read -t 10 -p "e=" e
read -t 10 -p "f=" f
[ ${#a} == 0 -o ${#b} == 0 -o ${#c} == 0 -o ${#d} == 0 -o ${#e} == 0 -o ${#f} == 0 ] && {
echo "ERROR,some of the numbers are NULL!"
exit 1
}
ab=$(echo "$a" | tr "." "0")
expr $ab + 23 &>/dev/null
AB=$?
bc=$(echo "$b" | tr "." "0")
expr $bc + 23 &>/dev/null
BC=$?
de=$(echo "$c" | tr "." "0")
expr $de + 23 &>/dev/null
DE=$?
ef=$(echo "$d" | tr "." "0")
expr $ef + 23 &>/dev/null
EF=$?
gh=$(echo "$e" | tr "." "0")
expr $gh + 23 &>/dev/null
GH=$?
hi=$(echo "$f" | tr "." "0")
expr $hi + 23 &>/dev/null
HI=$?
[ $AB == 0 -a $BC == 0 -a $DE == 0 -a $EF == 0 -a $GH == 0 -a $HI == 0 ] || {
echo "Have something that are NOT ALLOWED"
exit 1
}
D=$(echo "$a * $e - $b * $d" | bc -l)
[ $D == 0 ] && {
echo "No SOLUTION,ERROR"
exit 1
}
Dx=$(echo "$c * $e - $b * $f" | bc -l)
Dy=$(echo "$a * $f - $c * $d" | bc -l)
x=$(echo "$Dx / $D" | bc -l)
y=$(echo "$Dy / $D" | bc -l)
echo "x=$x"
echo "y=$y"
exit 0
}
function TLE() {
echo -e "ax+by+cz=d\nex+fy+gz=h\nix+jy+kz=l"
read -t 10 -p "a=" a
read -t 10 -p "b=" b
read -t 10 -p "c=" c
read -t 10 -p "d=" d
read -t 10 -p "e=" e
read -t 10 -p "f=" f
read -t 10 -p "g=" g
read -t 10 -p "h=" h
read -t 10 -p "i=" i
read -t 10 -p "j=" j
read -t 10 -p "k=" k
read -t 10 -p "l=" l
[ ${#a} == 0 -o ${#b} == 0 -o ${#c} == 0 -o ${#d} == 0 -o ${#e} == 0 -o ${#f} == 0 -o ${#g} == 0 -o ${#h} == 0 -o ${#i} == 0 -o ${#j} == 0 -o ${#k} == 0 -o ${#l} == 0 ] && {
echo "ERROR,some of the numbers are NULL!"
exit 1
}
ab=$(echo "$a" | tr "." "0")
expr $ab + 23 &>/dev/null
AB=$?
bc=$(echo "$b" | tr "." "0")
expr $bc + 23 &>/dev/null
BC=$?
de=$(echo "$c" | tr "." "0")
expr $de + 23 &>/dev/null
DE=$?
ef=$(echo "$d" | tr "." "0")
expr $ef + 23 &>/dev/null
EF=$?
gh=$(echo "$e" | tr "." "0")
expr $gh + 23 &>/dev/null
GH=$?
hi=$(echo "$f" | tr "." "0")
expr $hi + 23 &>/dev/null
HI=$?
ij=$(echo "$g" | tr "." "0")
expr $ij + 23 &>/dev/null
IJ=$?
jk=$(echo "$h" | tr "." "0")
expr $jk + 23 &>/dev/null
JK=$?
kl=$(echo "$i" | tr "." "0")
expr $kl + 23 &>/dev/null
KL=$?
lm=$(echo "$j" | tr "." "0")
expr $lm + 23 &>/dev/null
LM=$?
mn=$(echo "$k" | tr "." "0")
expr $mn + 23 &>/dev/null
MN=$?
no=$(echo "$l" | tr "." "0")
expr $no + 23 &>/dev/null
NO=$?
[ $AB == 0 -a $BC == 0 -a $DE == 0 -a $EF == 0 -a $GH == 0 -a $HI == 0 -a $IJ == 0 -a $JK == 0 -a $KL == 0 -a $LM == 0 -a $MN == 0 -a $NO == 0 ] || {
echo "Have something that are NOT ALLOWED"
exit 1
}
D=$(echo "$a * $f * $k - $a * $g * $j - $b * $e * $k + $b * $g * $i + $c * $e * $j - $c * $f * $i" | bc -l)
[ $D == 0 ] && {
echo "No SOLUTION,ERROR"
exit 1
}
Dx=$(echo "$d * $f * $k - $d * $g * $j - $b * $h * $k + $b * $g * $l + $c * $h * $j - $c * $f * $l" | bc -l)
Dy=$(echo "$a * $h * $k - $a * $g * $l - $d * $e * $k + $d * $g * $i + $c * $e * $l - $c * $h * $i" | bc -l)
Dz=$(echo "$a * $f * $l - $a * $h * $j - $b * $e * $l + $b * $h * $i + $d * $e * $j - $d * $f * $i" | bc -l)
x=$(echo "$Dx / $D" | bc -l)
y=$(echo "$Dy / $D" | bc -l)
z=$(echo "$Dz / $D" | bc -l)
echo "x=$x"
echo "y=$y"
echo "z=$z"
exit 0
}
function MAIN() {
echo '
===========================================
| A) Quadratic Equation of One Variable |
| B) Binary Linear Equations |
| C) Ternary Linear Equations |
===========================================
'
read -t 10 -p " Input A,B,C or EXIT: " chosen
case "$chosen" in
A|a)
QEOV
exit 0 ;;
B|b)
BLE
exit 0 ;;
C|c)
TLE
exit 0 ;;
EXIT|exit)
echo "The script is closen"
exit 0 ;;
*)
echo "ERROR,You must input A,B,C or EXIT"
MAIN
esac
}
MAIN

更多精彩内容

赞(0)
未经允许不得转载:工具盒子 » 利用Linux shell编程解方程