Ruby :: Aufgabe #5 :: Lösung #1

1 Lösung Lösung öffentlich
#5

Ausgabe Weihnachtsbaum

Anfänger - Ruby von Gustl - 07.12.2012 um 18:44 Uhr
Schreiben Sie ein Konsolenprogramm welches einen Weihnachtsbaum ausgibt.
Vor der Ausgabe soll noch die Höhe des Weihnachtsbaum abgefragt werden. (MIN 3)
Der Weihnachtsbaum soll bei einer Höheneingabe von 10 folgendermaßen aussehen:

Konsolenausgabe:

      *
***
*****
***
*****
*******
*****
*******
*********
###


Die Schwierigkeit besteht in den Stufenabschnitten der Tanne. Die Zeile die die meisten Sterne enthält sollte natürlich dann auch die letzte sein, also muss das Programm vielleicht bei einer zu großen Höheneingabe das Zeichnen der Sterne abbrechen und dann den Stamm ausgeben. (So wie bei diesem Beispiel, angegeben ist 10 aber der Baum hat nur 10 Reihen, ohne Baumstamm)

#1
vote_ok
von Idef1x (1320 Punkte) - 23.01.2018 um 12:06 Uhr
Quellcode ausblenden Ruby-Code
=begin
 T.y.P. #5
 ChristmasTree with variable height
 By Lars I.
 Lines of code:     51 == maximum height :D
=end

#================================================================================================

def christmastree (height)
=begin
 In:
  - entered height of the tree
 
 Out:
  - tree with entered height + stump
  
=end

#Section - CODE
 
 #Section - Declaration
    count = 1 # how many * per line
    part = 0 # in which part (3 rows = 1 part) is the loop
    width = 0 # width in a specific row
    
 #Section - Checking the entered height
    
    if height >= 51 then
        
        height = 51
        
    elsif height < 3 then
        
        height = 3
        
    else
        
        height = ( ( height / 3 ).to_i ) * 3
        
    end
    
 #Section - Getting the part
    
    if height % 3 == 0
        
        part = height / 3
        
    else
        
        part = ( ( height / 3 ).to_i ) + 1
        
    end
    
 #Section - Calculating the maximum width
    
    case height % 3
    when 0
       
        width = 4 + height + ( 3 - part )
        
    when 1
        
        width = 4 + height - ( part - 1 )
        
    when 2
        
        width = 4 + height + ( 2 - part ) 
        
    end
    
 #Section - Creating the tree
    
    1.upto(height) do |h|
        
        if h != 1 && h % 3 == 1 then # excluding the first run ( top of the tree )
            
            count -= 4 # decreasing count by 4 for the beginnig of the next part of the tree
            
        end
        
        # outputting the row
        ( ( width - count ) / 2 ).times { print "\s" }
        count.times { print "\*" }
        ( ( width - count ) / 2 ).times { print "\s" }
        
        # next row
        print "\n"
        
        # increasing count for the next row
        count += 2
        
    end
    
    if height >= 35 then
        
        count = 9
        
    elsif height >= 25 && height < 35 then
        
        count = 7
        
    elsif height >= 15 && height < 25 then
        
        count = 5
        
    elsif height >= 5 && height < 15 then
        
        count = 3
        
    else
        
        count = 1
        
    end
    
    ( ( width - count ) / 2 ).times { print " " }
        
    count.times { print "\#" }
        
    ( ( width - count ) / 2 ).times { print " " }
    
end

h = gets.to_i

christmastree(h)

Kommentare:

Für diese Lösung gibt es noch keinen Kommentar

Bitte melden Sie sich an um eine Kommentar zu schreiben.
Kommentar schreiben