分类: Python/Ruby
2011-04-25 22:02:21
示例和输出:
功能:对用户从键盘输入的数字,进行排序,以1,2,3,4,5……升序格式输出,同时
不限定个数。
用到知识点:
比上篇多一个read和不定循环的实现(while until)。
备注:如下实现,有bug存在哟
实现
1 #!/bash/shell
2 #Fuction:read different numbers and sort them
3 #Date:2011 04 19
4 #Version:1.0.0.0
5
6 PATH=$PATH
7 LANG=C
8
9 export C
10
11 i=0
12 var[1]=0
13
14 #1.input intergers
15 while [ "$num" != "NO" ]
16 do
17 read -p "please input interger,else input NO to stop:" num
18 i=$(( $i+1 ))
19 var[i]=$num
20 done
21 i=$(( $i-1 ))
22
23 #2.sort
24 for (( a=1; a<=$i-1; a=a+1 ))
25 do
26 for (( j=1; j<=$i-a; j=j+1 ))
27 do
28 t=$(( $j+1 ))
29 if [ "${var[$j]}" -gt "${var[$t]}" ]; then
30 tmp="${var[$j]}"
31 var[$j]="${var[$t]}"
32 var[$t]="$tmp"
33 fi
34 done
35 done
36
37 #3.output
38 result=""
39 for ((a=1; a<=$i-1; a=a+1 ))
40 do
41 result="$result${var[$a]},"
42 done
43
44 echo "$result${var[$i]}"
45
46 exit 0