Created
November 15, 2014 04:14
-
-
Save anonymous/c5f36862b961f06c4b3e to your computer and use it in GitHub Desktop.
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
def calcCircleArea(radius) | |
return (Math::PI * (radius * radius)).round(2) | |
end | |
def calcCirclePerimeter(radius) | |
return (2*Math::PI*radius).round(2) | |
end | |
def calcRectangleArea(length, width) | |
return (length * width) | |
end | |
def calcRectanglePerimeter(length, width) | |
return ((length*2) + (width*2)) | |
end | |
def calcSquareArea(side1, side2) | |
return (side1 * side2) | |
end | |
def calcSquarePerimeter(side1, side2) | |
return ((side1*2) + (side2 * 2)) | |
end | |
def calcTriangleArea(height, base, hyp) | |
return ((1/2)* base * height) | |
end | |
def calcTrianglePerimeter(height, base, hyp) | |
return (height + base + hyp) | |
end | |
def calcTriangleHypotenuse(height, base) | |
height2 = height**2 | |
base2 = base**2 | |
hyp = Math.sqrt(height2 + base2) | |
return hyp | |
end | |
def create_writefile(file, shape_type, area, perimeter, data) | |
File.open(file, 'a') do |file| | |
file.write("%-15s %-10s %-10s %-10s\n" % [shape_type, area, perimeter, data]) | |
end | |
end | |
unless ARGV.size == 2 | |
STDERR.puts "Error: Incorrect number of arguements\n\nOK. Goodbye." | |
else | |
puts " " | |
puts " " | |
s = Time.now | |
puts "Start Date/Time: " + s.inspect | |
puts "Processing: #{ARGV[0]} (input), #{ARGV[1]} (output)" | |
create_writefile(ARGV[1], "Shape Type", "Area", "Perimeter", "Data\n") | |
in_file = File.new(ARGV[0], 'r') | |
in_file.each_line do |line| | |
if line.include?("S") || line.include?("s") | |
shape_code = "S" | |
shape_type = "Square" | |
nums = line.scan(/\d+/).map(&:to_i) | |
data = line | |
data_points = nums.size | |
data_desc = "Length" | |
name = "Square" | |
if data_points == 2 | |
side1 = nums[0] | |
side2 = nums[1] | |
area = calcSquareArea(side1, side2) | |
perimeter = calcSquarePerimeter(side1, side2) | |
elsif data_points == 1 | |
side1 = nums[0] | |
side2 = nums[0] | |
area = calcSquareArea(side1, side2) | |
perimeter = calcSquarePerimeter(side1, side2) | |
else | |
side1 = nums[0] | |
end | |
elsif line.include?("R") || line.include?("r") | |
shape_code = "R" | |
shape_type = "Rectange" | |
nums = line.scan(/\d+/).map(&:to_i) | |
data = line | |
data_points = nums.size | |
data_desc = "Length" | |
name = "Rectangle" | |
if data_points == 2 | |
length = nums[0] | |
width = nums[1] | |
area = calcRectangleArea(length, width) | |
perimeter = calcRectanglePerimeter(length, width) | |
elsif data_points == 1 | |
side1 = nums[0] | |
else | |
side1 = nums[0] | |
end | |
elsif line.include?("C") || line.include?("c") | |
shape_code = "C" | |
shape_type = "Circle" | |
nums = line.scan(/\d+/).map(&:to_i) | |
data = line | |
data_points = nums.size | |
data_desc = "Radius" | |
name = "Circle" | |
if data_points == 1 | |
radius = nums[0] | |
area = calcCircleArea(radius) | |
perimeter = calcCirclePerimeter(radius) | |
else | |
end | |
elsif line.include?("T") || line.include?("t") | |
shape_code = "T" | |
shape_type = "Triangle" | |
nums = line.scan(/\d+/).map(&:to_i) | |
data = line | |
data_points = nums.size | |
data_desc = "Height, Base" | |
name = "Triangle" | |
if data_points == 3 | |
base = nums[0] | |
height = nums[1] | |
hyp = nums[2] | |
else data_points == 2 | |
height = nums[0] | |
base = nums[1] | |
hyp = calcTriangleHypotenuse(height, base) | |
print hyp | |
perimeter = calcTrianglePerimeter(height, base, hyp) | |
area = calcTriangleArea(height, base, hyp) | |
end | |
end | |
create_writefile(ARGV[1], shape_type, area, perimeter, data) | |
end | |
t = Time.now() | |
f = t - s | |
puts " " | |
puts "Processing: Complete" | |
puts "Stop Date/Time: " + t.inspect | |
puts "Total Processing time: " + f.inspect + " seconds" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment