-
-
Save RLGGHC/c519fccdc1e6b2f7a3eb 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
class Polynomial | |
def initialize(*num) | |
@expression = "" | |
@howmanynumbers = 0 | |
num.each{@howmanynumbers = @howmanynumbers + 1} | |
num.each{|n| @howmanynumbers = @howmanynumbers -1; if n ==1 then @expression = @expression + " + "+ "x^" + @howmanynumbers.to_s elsif n < 0 then @expression = @expression + " - " + (n*-1).to_s + "x^" + @howmanynumbers.to_s elsif n==0 then @expression = @expression else @expression = @expression + " + " + n.to_s + "x^" + @howmanynumbers.to_s end } | |
@expression.sub!('x^0', '') | |
@counter = -1 | |
@finalexpression = "" | |
@expression.scan(/./) {|ch| @counter = @counter + 1; if ch == "+" && @counter == 0 then @finalexpression = @finalexpression; @counter = 1 else @finalexpression = @finalexpression + ch.to_s end } | |
puts @finalexpression | |
end | |
end | |
series = Polynomial.new(-3,1,1,0,6) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment