觀看可讀性高的原始碼請按此
class Stack
def initialize ary=0, max=0, lvl=0
@ary = ary
@max = max
@lvl = lvl
end
def add
if @lvl puts "Please input a value for add to the stack!"
@ary[@lvl] = self.get_value
@lvl+=1
puts "Now you have #{@lvl} elements for the array!"
else
puts "Stack is full!"
end
end
def del
puts "#{@ary[@lvl-1]} has been removed!"
@lvl -= 1
if @lvl puts "You have no element in the array!"
else
puts "Now you have #{@lvl} elements in the array!"
end
end
def get_value
return gets.chomp
end
def list
if @lvl puts "You have no element in the array!"
else
for ele in 0 ... @lvl
puts @ary[ele]
end
end
true
end
public :initialize, :add, :del, :list
protected :get_value
end
puts "Please input a number for the stack max!"
ary = {}
max = gets.chomp.to_i
lvl = 0
stack = Stack.new(ary, max, lvl)
=begin
Actions:
1. add -- for add elements
2. del -- for remove elements
3. list -- for list elements
4. exit -- end this program
=end
while true
puts "Please select a action!"
puts Actions:
1. add -- for add elements
2. del -- for remove elements
3. list -- for list elements
4. exit -- end this program
EOF
action = gets.chomp
case action
when "add" ,"1"
puts "Adding array element!"
stack.add
when "del", "2"
puts "Removing array element!"
stack.del
when "list", "3"
puts "Listing array elements!"
stack.list
when "exit", "4"
puts "Bye~ bye!"
exit
end
end
Google自訂搜尋
- May 19 Fri 2006 15:21
Stack (2)
close
搭配物件導向@@
全站熱搜
留言列表