Previously in this series:
Crystal is a general-purpose, object-oriented programming language. With syntax inspired by Ruby, it's a compiled language with static type-checking.
def c2f(c)
c * 9.0 / 5 + 32;
end
def is_mirror(a, b)
massage(a).reverse == massage(b)
end
def massage(n)
if n < 10
"0#{n}"
elsif n >= 100
massage(n - 100)
else
n.to_s
end
end
def print_conv(c, f)
puts "#{c}°C ~= #{f}°F"
end
(4...100).step(12).each do |c|
f = c2f(c)
if is_mirror(c, f.ceil.to_i)
print_conv(c, f.ceil.to_i)
elsif is_mirror(c, f.floor.to_i)
print_conv(c, f.floor.to_i)
else
break
end
end
And this is its diff with the Ruby version:
< if is_mirror(c, f.ceil)
< print_conv(c, f.ceil)
< elsif is_mirror(c, f.floor)
< print_conv(c, f.floor)
---
> if is_mirror(c, f.ceil.to_i)
> print_conv(c, f.ceil.to_i)
> elsif is_mirror(c, f.floor.to_i)
> print_conv(c, f.floor.to_i)
Run it like this:
crystal conversion.cr
or build and run:
crystal build -o conversion-cr conversion.cr
./conversion-cr
and the output becomes:
4°C ~= 40°F 16°C ~= 61°F 28°C ~= 82°F 40°C ~= 104°F 52°C ~= 125°F
Comments