Last active
August 29, 2015 14:12
-
-
Save darth30joker/6226aebe707b4b837617 to your computer and use it in GitHub Desktop.
Ruby homework
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
require_relative 'calculation' | |
describe "Test Calculation" do | |
it "should return 17" do | |
calc = Calculation.new '( 2 + ( ( 4 + 6 ) * sqrt(9)) / 2 )' | |
expect(calc.calculate).to eq("17") | |
end | |
it "should return 178" do | |
calc = Calculation.new '( 2 + ( ( 4 + 6 ) * (9 * 2) - ( 5 - 1)))' | |
expect(calc.calculate).to eq("178") | |
end | |
it "should return 185.78" do | |
calc = Calculation.new '( 2.1 + ( ( 4.1 + 6.1 ) * (9.2 * 2) - ( 5 - 1)))' | |
expect(calc.calculate).to eq("185.78") | |
end | |
it "should return 12.48" do | |
calc = Calculation.new '( 2.1 + ( ( 4.1 + 6.1 ) * sqrt(2) - ( 5 - 1)))' | |
expect(calc.calculate).to eq("12.48") | |
end | |
it "should catch exception" do | |
calc = Calculation.new '( 2.1 + ( ( 4.1 + 6.1 () * (9.2 * 2) - ( 5 - 1)))' | |
expect{calc.calculate}.to raise_error(ArgumentError) | |
end | |
it "should catch exception" do | |
calc = Calculation.new '( 2 + ( ( 4 + 6 ) * sqr(9)) / 2 )' | |
expect{calc.calculate}.to raise_error(ArgumentError) | |
end | |
end |
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
include Math | |
class String | |
def integer? | |
self.to_i.to_s == self | |
end | |
def float? | |
format("%.2f", self.to_f).to_f.to_s == self | |
end | |
def number | |
if self.integer? | |
self.to_i | |
elsif self.float? | |
format("%.2f", self.to_f).to_f | |
end | |
end | |
end | |
class Calculation | |
def initialize(string) | |
@string = string.delete(' ') | |
end | |
def calculate | |
while @string.number.to_s != @string.to_s | |
# sqrt(a) | |
m = /sqrt\((\d+\.\d+|\d+)\)/.match @string | |
if m | |
result = sqrt(m[1].number) | |
if !result.integer? | |
result = format("%.2f", result).to_f | |
end | |
@string = @string.gsub("sqrt(#{m[1]})", result.to_s) | |
next | |
end | |
# a*b | |
m = /(\d+\.\d+|\d+)\*(\d+\.\d+|\d+)/.match @string | |
if m | |
result = m[1].number * m[2].number | |
if !result.integer? | |
result = format("%.2f", result).to_f | |
end | |
@string = @string.gsub("#{m[1]}*#{m[2]}", result.to_s) | |
next | |
end | |
# a/b | |
m = /(\d+\.\d+|\d+)\/(\d+\.\d+|\d+)/.match @string | |
if m | |
result = m[1].number / m[2].number | |
if !result.integer? | |
result = format("%.2f", result).to_f | |
end | |
@string = @string.gsub("#{m[1]}/#{m[2]}", result.to_s) | |
next | |
end | |
# (a) | |
m = /\((\d+\.\d+|\d+)\)/.match @string | |
if m | |
if !m[1].integer? | |
result = format("%.2f", m[1]).to_f | |
end | |
@string = @string.gsub("(#{m[1]})", result.to_s) | |
next | |
end | |
# (a+b) | |
m = /\((\d+\.\d+|\d+)\+(\d+\.\d+|\d+)\)/.match @string | |
if m | |
result = m[1].number + m[2].number | |
if !result.integer? | |
result = format("%.2f", result).to_f | |
end | |
@string = @string.gsub("(#{m[1]}+#{m[2]})", result.to_s) | |
next | |
end | |
# (a-b) | |
m = /\((\d+\.\d+|\d+)\-(\d+\.\d+|\d+)\)/.match @string | |
if m | |
result = m[1].number - m[2].number | |
if !result.integer? | |
result = format("%.2f", result).to_f | |
end | |
@string = @string.gsub("(#{m[1]}-#{m[2]})", result.to_s) | |
next | |
end | |
# a+b | |
m = /(\d+\.\d+|\d+)\+(\d+\.\d+|\d+)/.match @string | |
if m | |
result = m[1].number + m[2].number | |
if !result.integer? | |
result = format("%.2f", result).to_f | |
end | |
@string = @string.gsub("#{m[1]}+#{m[2]}", result.to_s) | |
next | |
end | |
# a-b | |
m = /(\d+|\d+\.\d+)\-(\d+|\d+\.\d+)/.match @string | |
if m | |
result = m[1].number - m[2].number | |
if !result.integer? | |
result = format("%.2f", result).to_f | |
end | |
@string = @string.gsub("#{m[1]}-#{m[2]}", result.to_s) | |
next | |
end | |
raise ArgumentError, "Wrong formula!" | |
end | |
m = /(\d+)\.0/.match @string | |
if m | |
@string = m[1] | |
end | |
@string | |
end | |
end |
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
task :default => :test | |
task :test do | |
sh 'rspec calc_spec.rb' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment