Encode-decode ruby

Solo disponible en BuenasTareas
  • Páginas : 4 (870 palabras )
  • Descarga(s) : 0
  • Publicado : 29 de marzo de 2011
Leer documento completo
Vista previa del texto
class EDMethod
def verifyEncodingStream(stream)
if /[^0,1]/.match(stream)
return nil
else
return 1
end
end
def encode (stream)
puts "Encode functionmust be implemented"
end
def decode (stream)
puts "Decode function must be implemented"
end
end

class NRZL < EDMethod
def verifyDecodingStream(stream)
if/[^+,-]/.match(stream)
return nil
else
return 1
end
end
def encode (stream)
puts "The NRZL encoding for the bit stream is: "
stream.length.times do |num|
ifstream[num..num].to_i==0
putc '-'
elsif stream[num..num].to_i==1
putc '+'
end
end
end
def decode (stream)
puts "The NRZL decoding for the signal is: "stream.length.times do |num|
if stream[num..num]=='-'
putc '0'
elsif stream[num..num]=='+'
putc '1'
end
end
end
end

class NRZI < EDMethod
defverifyDecodingStream(stream)
if /[^+,-]/.match(stream)
return nil
else
return 1
end
end
def encode (stream)
puts "The NRZI encoding for the bit stream is: "
ant='-'
stream.length.times do |num|
if stream[num..num] =='1'
if ant == '+'
ant = '-'
putc '-'
elsif ant == '-'
ant = '+'
putc'+'
end
elsif stream[num..num] =='0'
if ant == '+'
ant = '+'
putc '+'
elsif ant == '-'
ant = '-'
putc '-'
endend
end
end
def decode (stream)
puts "The NRZI decoding for the signal is: "
ant = '-'
stream.length.times do |num|
if stream[num..num]==ant
putc'0'
else
putc '1'
end
ant = stream[num..num]
end
end
end

class AMI < EDMethod
def verifyDecodingStream(stream)
if /[^\+,\-,0]/.match(stream)...
tracking img