📄 stack.sh
字号:
#!/bin/bash# stack.sh: push-down stack simulation# Similar to the CPU stack, a push-down stack stores data items#+ sequentially, but releases them in reverse order, last-in first-out.BP=100 # Base Pointer of stack array. # Begin at element 100.SP=$BP # Stack Pointer. # Initialize it to "base" (bottom) of stack.Data= # Contents of stack location. # Must use global variable, #+ because of limitation on function return range.declare -a stackpush() # Push item on stack.{if [ -z "$1" ] # Nothing to push?then returnfilet "SP -= 1" # Bump stack pointer.stack[$SP]=$1return}pop() # Pop item off stack.{Data= # Empty out data item.if [ "$SP" -eq "$BP" ] # Stack empty?then returnfi # This also keeps SP from getting past 100, #+ i.e., prevents a runaway stack.Data=${stack[$SP]}let "SP += 1" # Bump stack pointer.return}status_report() # Find out what's happening.{echo "-------------------------------------"echo "REPORT"echo "Stack Pointer = $SP"echo "Just popped \""$Data"\" off the stack."echo "-------------------------------------"echo}# =======================================================# Now, for some fun.echo# See if you can pop anything off empty stack.popstatus_reportechopush garbagepopstatus_report # Garbage in, garbage out. value1=23; push $value1value2=skidoo; push $value2value3=FINAL; push $value3pop # FINALstatus_reportpop # skidoostatus_reportpop # 23status_report # Last-in, first-out!# Notice how the stack pointer decrements with each push,#+ and increments with each pop.echoexit 0# =======================================================# Exercises:# ---------# 1) Modify the "push()" function to permit pushing# + multiple element on the stack with a single function call.# 2) Modify the "pop()" function to permit popping# + multiple element from the stack with a single function call.# 3) Add error checking to the critical functions.# That is, return an error code, depending on# + successful or unsuccessful completion of the operation,# + and take appropriate action.# 4) Using this script as a starting point,# + write a stack-based 4-function calculator.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -