-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolve
executable file
·133 lines (109 loc) · 2.76 KB
/
solve
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env ruby
require "readline"
require "cmath"
require "bigdecimal"
# PREFIXES = { -15 => "f", -12 => "p", -9 => "n",
# -6 => "µ", -3 => "m", 0 => "",
# 3 => "k", 6 => "M", 9 => "G",
# 12 => "T", 15 => "P", 18 => "E" }
def define_equation(file)
File.foreach(file) { |line|
name, var, equation = line.split(":")
Equations.class_eval %[
def #{name}(#{var})
#{equation}
end
]
}
end
def all_formulas
(Equations.instance_methods(false) - [:method_missing])
.map { |i| i.to_s.gsub(/_/, "-") }
end
class Equations
C = 3e8
def m_with_hz(frequency)
C / frequency
end
alias_method :hz_with_m, :m_with_hz
def qudrtc_frml_with_a_b_c(a, b, c)
positive = (-b + CMath.sqrt(b ** 2 - 4 * a * c)) / (2 * a)
negative = (-b - CMath.sqrt(b ** 2 - 4 * a * c)) / (2 * a)
# possibly add positive and negative .to_r as well as the float
[positive, negative]
end
def pythgrn_thrm_with_a_b(a, b)
a = Math.sqrt(a ** 2 + b ** 2)
a = "sqrt(#{(a ** 2).round})" unless a.to_i == a
a
end
def pythgrn_thrm_with_b_c(b, c)
a = Math.sqrt(c ** 2 - b ** 2)
a = "sqrt(#{(a ** 2).round})" unless a.to_i == a
a
end
def method_missing(m, *args, &block)
"That equation doesn't exist here. (yet)"
end
end
class Cli
attr_reader :equations
def initialize
@equations = Equations.new
end
def help
["To use an equation, just type something like this: hz-with-m 100",
"Thats 100 meters, converted to hertz",
"Or something like this: pythgrn-thrm-with-b-c 4 5",
"That's a^2 = 5^2 - 4^2, or more familiarly, a^2 + 4^2 = 5^2",
"You also can use e for scientific notation. Eg. 3e5 = 300000",
"Type quit to quit",
"",
"Available Equations:",
all_formulas]
end
def method_missing(m, *args, &block)
# checks if it only contains numbers, math symbols, and e
if args.all? { |arg| arg.match(/\A[\d+\-*\/.e ]+\z/) }
args.map! { |arg| BigDecimal.new(arg).to_f }
@equations.send(m, *args)
else
"Invalid number"
end
end
def quit
exit
end
end
cli = Cli.new
case ARGV[0]
when "-l", "--load"
ARGV.shift
define_equation(ARGV.shift)
# I will add more stuff here
end
if __FILE__ == $0
puts "Type help for help."
Readline.completion_append_character = " "
Readline.completion_proc = proc { |s|
all_formulas.grep(/^#{Regexp.escape(s)}/)
}
loop do
command, *variables = Readline::readline("> ", true)
.downcase.strip.squeeze(" ").split(" ")
unless command.nil?
command.gsub!(/-/, "_")
puts begin
cli.send(command, *variables)
rescue ArgumentError
argc = if cli.public_methods(false).include? command.to_sym
cli.method(command).arity
else
cli.equations.method(command).arity
end
arg_list = (1..argc).map { |i| "<number_#{i}>" }.join(" ")
"Usage: #{command} #{arg_list}"
end
end
end
end