Class: Expression
- Inherits:
-
Object
- Object
- Expression
- Defined in:
- lib/calc_dongqs/expression.rb
Constant Summary collapse
- PRECEDENCE =
shunting-yard algorithm
{ '+' => 0, '-' => 0, '*' => 1, '/' => 1, }
- OPERATORS =
PRECEDENCE.keys
- FUNCTIONS =
['sqrt']
Instance Attribute Summary collapse
-
#exp ⇒ Object
Returns the value of attribute exp.
-
#result ⇒ Object
Returns the value of attribute result.
-
#rpn ⇒ Object
Returns the value of attribute rpn.
-
#tokens ⇒ Object
Returns the value of attribute tokens.
Class Method Summary collapse
-
.evaluate(tokens) ⇒ Object
evaluate.
- .shunt(tokens) ⇒ Object
-
.tokenize(expression) ⇒ Object
tokenize by regex, can NOT process negative numbers.
Instance Method Summary collapse
-
#initialize(exp) ⇒ Expression
constructor
A new instance of Expression.
Constructor Details
#initialize(exp) ⇒ Expression
Returns a new instance of Expression.
5 6 7 8 9 10 11 |
# File 'lib/calc_dongqs/expression.rb', line 5 def initialize exp @exp = exp @tokens = self.class.tokenize @exp @rpn = self.class.shunt @tokens @result = self.class.evaluate @rpn end |
Instance Attribute Details
#exp ⇒ Object
Returns the value of attribute exp.
3 4 5 |
# File 'lib/calc_dongqs/expression.rb', line 3 def exp @exp end |
#result ⇒ Object
Returns the value of attribute result.
3 4 5 |
# File 'lib/calc_dongqs/expression.rb', line 3 def result @result end |
#rpn ⇒ Object
Returns the value of attribute rpn.
3 4 5 |
# File 'lib/calc_dongqs/expression.rb', line 3 def rpn @rpn end |
#tokens ⇒ Object
Returns the value of attribute tokens.
3 4 5 |
# File 'lib/calc_dongqs/expression.rb', line 3 def tokens @tokens end |
Class Method Details
.evaluate(tokens) ⇒ Object
evaluate
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 |
# File 'lib/calc_dongqs/expression.rb', line 66 def self.evaluate tokens stack = [] while !tokens.empty? token = tokens.shift case token when 'sqrt' stack.push Math.sqrt stack.pop when *OPERATORS b = stack.pop a = stack.pop case token when '+' c = a + b when '-' c = a - b when '*' c = a * b when '/' c = a / b end stack.push c else if token.to_i.to_s == token stack.push token.to_i else stack.push token.to_f end end end if stack.length == 1 stack.last else raise end end |
.shunt(tokens) ⇒ Object
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 |
# File 'lib/calc_dongqs/expression.rb', line 28 def self.shunt tokens stack = [] output = [] while !tokens.empty? token = tokens.shift case token when *FUNCTIONS stack.push token when *OPERATORS while OPERATORS.include?(stack.last) and PRECEDENCE[token] <= PRECEDENCE[stack.last] output.push stack.pop end stack.push token when '(' stack.push token when ')' while !stack.empty? and stack.last != '(' output.push stack.pop end raise 'mismatched parentheses' if stack.empty? stack.pop output.push stack.pop if FUNCTIONS.include? stack.last else output.push token end end while !stack.empty? token = stack.pop raise 'mismatched parentheses' if ['(', ')'].include? token output.push token end output end |
.tokenize(expression) ⇒ Object
tokenize by regex, can NOT process negative numbers
14 15 16 |
# File 'lib/calc_dongqs/expression.rb', line 14 def self.tokenize expression expression.scan /\d+\.\d+|\d+\.|\.\d+|\d+|\+|-|\*|\/|\(|\)|sqrt/ end |