用SHELL寫的,那時候稍微瞭解了一下的,現在完全忘記了,不過看還是看的懂的,就是不會寫了,呵呵。都是老師布置的簡單題
1. 使用者選擇加減乘除,輸入兩個數字和結果,判斷使用者輸入的結果是否正確。
#! /bin/bash<br /># this is the first homework<br />echo "input 1 to start and other numbers to exit:"<br />read sig<br />while [ $sig -eq 1 ]<br />do<br />echo -n "Please choose(+,-,*,/)"<br />read operation<br />echo -n "please input the 2 numbers to calculate:"<br />read a b<br />declare c<br />echo -n "please input the result:"<br />read result<br />mul=*<br />if [ "$operation" == "$mul" ]<br />then c=`expr $a /* $b`<br /> elif [ $operation = + ]<br /> then c=`expr $a + $b`<br /> elif [ $operation = - ]<br /> then c=`expr $a - $b`<br />elif [ $operation = / ]<br />then c=`expr $a / $b`<br />else<br />echo "error"<br />exit 1<br />fi<br />if [ $result -eq $c ]<br />then echo "you are right!"<br />else echo "you are wrong,the right answer is $c!"<br />fi<br />echo "input 1 to start again and any other numbers to exit"<br />read sig<br />done<br />echo "goodbye!"
2. 實現將輸入的三個數由下到大排列
#! /bin/bash<br /># this is the second homework<br />function order<br />{<br />if [ $c -gt $a ]<br />then<br /> t=$c<br /> c=$a<br /> a=$t<br />fi<br />if [ $b -gt $a ]<br />then<br /> t=$a<br /> a=$b<br /> b=$t<br />fi<br />if [ $c -gt $b ]<br />then<br /> t=$c<br /> c=$b<br /> b=$t<br />fi<br />}</p><p>echo -n "please input 3 numbers:"<br />read a b c<br />order a b c<br />echo "$a $b $c"<br />