Class: Calculator

Inherits:
Object
  • Object
show all
Defined in:
lib/calculator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCalculator

Returns a new instance of Calculator.



11
12
13
14
15
16
17
18
# File 'lib/calculator.rb', line 11

def initialize()
  @memory = 0
  @memory2 = Hash.new(0)
  @eol = false
  @eof = false
  @assigns = Hash.new("")
  @countAssigns = 0
end

Instance Attribute Details

#assignsObject

Returns the value of attribute assigns.



10
11
12
# File 'lib/calculator.rb', line 10

def assigns
  @assigns
end

#countAssignsObject

Returns the value of attribute countAssigns.



9
10
11
# File 'lib/calculator.rb', line 9

def countAssigns
  @countAssigns
end

#eofObject

Returns the value of attribute eof.



8
9
10
# File 'lib/calculator.rb', line 8

def eof
  @eof
end

#eolObject

Returns the value of attribute eol.



7
8
9
# File 'lib/calculator.rb', line 7

def eol
  @eol
end

#memoryObject

Returns the value of attribute memory.



5
6
7
# File 'lib/calculator.rb', line 5

def memory
  @memory
end

#memory2Object

Returns the value of attribute memory2.



6
7
8
# File 'lib/calculator.rb', line 6

def memory2
  @memory2
end

Instance Method Details

#eval(expr) ⇒ Object



20
21
22
23
24
# File 'lib/calculator.rb', line 20

def eval(expr)
  parser = Parser.new(StringIO.new(expr))
  ast = parser.parse()
  return ast.evaluate()
end

#evalFile(file) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/calculator.rb', line 33

def evalFile(file)
  file.each{|line|
    if(line!="\n")
      puts "= " + $calc.eval(line).to_s
    else
      puts "Needed an expresion"
    end      
  }    
end

#insert(id, val) ⇒ Object



26
27
28
29
30
31
# File 'lib/calculator.rb', line 26

def insert(id,val)
  @assigns[countAssigns] = id.to_s + " <- " + val.to_s
  @countAssigns += 1
  @memory2[id] = val
  
end

#search(id) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/calculator.rb', line 43

def search(id)
  if(@memory2.keys.include?(id))
    return @memory2[id]
  else
    return 0
  end
end