Created
September 12, 2019 11:22
-
-
Save sandiks/db921ecf8f8fe9e14b5572bc7e0d8faf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def main | |
if ARGV.length < 2 | |
STDERR << "Usage: #{$0} <a> <b> <c>\n" | |
exit 1 | |
end | |
a = ARGV[0].to_f | |
b = ARGV[1].to_f | |
c = ARGV[2].to_f | |
is_quadratic = (ARGV.length>2) | |
if is_quadratic | |
solve_quadratic(a,b,c) | |
else | |
solve_linear(a,b) | |
end | |
end | |
def solve_quadratic(a,b,c) | |
if a == 0 | |
STDERR << "<a> should not be zero\n" | |
exit 1 | |
end | |
disc = b**2 - 4*a*c | |
d_str = (disc >= 0) ? (Math.sqrt(disc) % 1 == 0) ? Math.sqrt(disc).to_i : "√#{disc}" : "√#{disc}" | |
s = "#{-b} ± #{d_str}" | |
d = "#{2*a}" | |
puts "\e[4m#{s}\e[0m" | |
#puts "─"*(s.length) | |
puts d.center s.length | |
end | |
def solve_linear(a,b) | |
puts "#{-b/a}" | |
end | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment