⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex79.sh

📁 Shall高级编程
💻 SH
字号:
#!/bin/bash# Cards:# Deals four random hands from a deck of cards.UNPICKED=0PICKED=1DUPE_CARD=99LOWER_LIMIT=0UPPER_LIMIT=51CARDS_IN_SUIT=13CARDS=52declare -a Deckdeclare -a Suitsdeclare -a Cards#  It would have been easier to implement and more intuitive#+ with a single, 3-dimensional array.#  Perhaps a future version of Bash will support multidimensional arrays.initialize_Deck (){i=$LOWER_LIMITuntil [ "$i" -gt $UPPER_LIMIT ]do  Deck[i]=$UNPICKED   # Set each card of "Deck" as unpicked.  let "i += 1"doneecho}initialize_Suits (){Suits[0]=C #ClubsSuits[1]=D #DiamondsSuits[2]=H #HeartsSuits[3]=S #Spades}initialize_Cards (){Cards=(2 3 4 5 6 7 8 9 10 J Q K A)# Alternate method of initializing an array.}pick_a_card (){card_number=$RANDOMlet "card_number %= $CARDS"if [ "${Deck[card_number]}" -eq $UNPICKED ]then  Deck[card_number]=$PICKED  return $card_numberelse    return $DUPE_CARDfi}parse_card (){number=$1let "suit_number = number / CARDS_IN_SUIT"suit=${Suits[suit_number]}echo -n "$suit-"let "card_no = number % CARDS_IN_SUIT"Card=${Cards[card_no]}printf %-4s $Card# Print cards in neat columns.}seed_random ()  # Seed random number generator.{               # What happens if you don't do this?seed=`eval date +%s`let "seed %= 32766"RANDOM=$seed#  What are some other methods#+ of seeding the random number generator?}deal_cards (){echocards_picked=0while [ "$cards_picked" -le $UPPER_LIMIT ]do  pick_a_card  t=$?  if [ "$t" -ne $DUPE_CARD ]  then    parse_card $t    u=$cards_picked+1    # Change back to 1-based indexing (temporarily). Why?    let "u %= $CARDS_IN_SUIT"    if [ "$u" -eq 0 ]   # Nested if/then condition test.    then     echo     echo    fi    # Separate hands.    let "cards_picked += 1"  fi  done  echoreturn 0}# Structured programming:# Entire program logic modularized in functions.#================seed_randominitialize_Deckinitialize_Suitsinitialize_Cardsdeal_cards#================exit 0# Exercise 1:# Add comments to thoroughly document this script.# Exercise 2:# Add a routine (function) to print out each hand sorted in suits.# You may add other bells and whistles if you like.# Exercise 3:# Simplify and streamline the logic of the script.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -