-
Notifications
You must be signed in to change notification settings - Fork 0
/
d3e04_onlinecalc.rb
63 lines (48 loc) · 1.39 KB
/
d3e04_onlinecalc.rb
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
#encoding: utf-8
require 'sinatra'
require 'sinatra/reloader'
set :port, 3000
set :bind, '0.0.0.0'
# SL10. The online calculator
# So, our Online Calculator will:
# * Have a home page (‘/‘) where you see all four available operations: add, substract, multiply and divide. Pretty simple stuff.
# * For each one of the available operations, we will have a form with two input fields and a button, which will go to another URL/route and display the result, like “The multiplication of 4 and 15 is 60” or “The addition of 10 and 39 is 49".
# * There will also be a link, in the home page, that goes to ‘/counting’ and should display the numbers from 1 to 200, one number per line.
# Feel free to add more features! :D
# class Calculator
# def add(a,b)
# a+b
# end
# def substract(a,b)
# a-b
# end
# def multiply(a,b)
# a*b
# end
# def divide(a,b)
# a/b
# end
# end
# calculator = Calculator.new
finalvalue = 0
get '/' do
@finalvalue = finalvalue
erb :calculator
end
post '/add' do
@finalvalue = params[:a].to_i + params[:b].to_i
#@finalvalue = calculator.add(params[:a].to_i,params[:b].to_i)
erb :calculator
end
post '/substract' do
@finalvalue = params[:a].to_i - params[:b].to_i
erb :calculator
end
post '/multiply' do
@finalvalue = params[:a].to_i * params[:b].to_i
erb :calculator
end
post '/divide' do
@finalvalue = params[:a].to_i / params[:b].to_i
erb :calculator
end