Κυριακή, 7 Οκτωβρίου 2018

Αναθεωρηση 22 Έκδοση 9.4

Έγιναν δυο διορθώσεις στο μεταφραστή. Μια διόρθωση λόγω πρότερου ανασχεδιασμού (refactoring) στο τμήμα που διαβάζει τα στοιχεία μιας ομάδας, και ειδικότερα στα στοιχεία Γεγονότα. Μια άλλη διόρθωση έγινε σε μια ειδική περίπτωση που σε μια Επανέλαβε Μέχρι έχουμε επιλέξει το Συνέχισε, ενώ υπάρχει ένα μπλοκ εσωτερικά, το οποίο από λάθος παίρνει και αυτό το "συνέχισε" και έτσι μπαίνει σε ατέρμονη επανάληψη. Διορθώθηκε!

Έχουν γραφτεί ωραία προγράμματα στο rosettacode.


Box the compass

Module CheckIt {
      Locale 1033 'change decimal point char to dot.
      Form 80,50 ' set console to 80 characters by 50 lines
      \\ Function heading() get a positive double as degrees and return the  compass index (1 for North)
      Function heading(d) {
            d1=d div 11.25
            if d1 mod 3= 1 then d+=5.62 :d1=d div 11.25
            =d1 mod 32 +1
      }
      Dim wind$(1 to 32)
      wind$(1)="North", "North by east", "North-northeast", "Northeast by north", "Northeast"
      wind$(6)="Northeast by east", "East-northeast", "East by north", "East", "East by south", "East-southeast"
      wind$(12)="Southeast by east", "Southeast", "Southeast by south", "South-southeast", "South by east", "South"
      wind$(18)="South by west", "South-southwest", "Southwest by south", "Southwest", "Southwest by west", "West-southwest"
      wind$(24)="West by south", "West", "West by north", "West-northwest", "Northwest by west", "Northwest", "Northwest by north"
      wind$(31)="North-northwest", "North by west"
      oldvalue=-2
      newvalue=2
      Print " angle | box | compass point"
      Print "-------+-----+---------------------"
      For i=0 to 360 step 0.005
            newvalue=heading(i)
            if (newvalue mod 3) =2 then i+=5.62: newvalue=heading(i)
            if oldvalue<>newvalue then Print format$("{0:2:-6}°|  {1::-2} | {2}",i, newvalue, wind$(newvalue)) : oldvalue=newvalue : refresh
      Next i
}
CheckIt



Animate a pendulum

Module Pendulum {
      back()
      degree=180/pi
      THETA=Pi/2
      SPEED=0
      G=9.81
      L=0.5
      Profiler
      lasttimecount=0
      cc=40 ' 40 ms every draw
      accold=0
      Every cc {
            ACCEL=G*SIN(THETA*degree)/L/50
            SPEED+=ACCEL/cc
            THETA+=SPEED
            Pendulum(THETA)
            if KeyPress(32) Then Exit
      }

      Sub back()
            If not IsWine then Smooth On
            Cls 7,0
            Pen 0
            Move 0, scale.y/4
            Draw scale.x,0
            Step -scale.x/2
            circle fill #AAAAAA, scale.x/50
            Hold ' hold this as background
      End Sub

      Sub Pendulum(x)
            x+=pi/2
            Release ' place stored background to screen
            Width scale.x/2000 {
                  Draw Angle x, scale.y/2.5
                  Width 1 {
                        Circle Fill 14, scale.x/25
                  }
                  Step Angle x, -scale.y/2.5
            }
            Print @(1,1), lasttimecount
            if sgn(accold)<>sgn(ACCEL) then lasttimecount=timecount: Profiler
            accold=ACCEL
            Refresh 1000
      End Sub
}
Pendulum




Number reversal game

Module Number_Reversal_Game {
      PRINT "Given a jumbled list of the numbers 1 to 9,"
      PRINT "you must select how many digits from the left to reverse."
      PRINT "Your goal is to get the digits in order with 1 on the left and 9 on the right."
      \\ work on a new stack - old stack parked, and attached at the exit of this block
      Stack New {
            Data 1,2,3,4,5,6,7,8,9
            \\ Create jumbled list
            For i=1 to 30: Reverse(Random(2,9)):Next i
            Tries=0
            fin=false
           Repeat {
            \\ Show Stack
                  Stack
                  Try ok {
                        INPUT " -- How many numbers should be flipped:", flp%
                  }
                  if not Ok then print: Restart
                  if flp%<2 or flp%>9 then Restart
                  Reverse(flp%)
                  Tries++
                  CheckStack(&fin)
            } until Fin
            \\ show stack again
            Stack
            PRINT "You took "; tries; " tries to put the digits in order."
      }
      Sub Reverse(n)
            Shift 1, -n ' shift one item nth times in reverse
      End Sub
      Sub CheckStack(&ok)
            ok=true
            if stack.size<2 then exit sub
            Local i
            For i=2 to stack.size {
                        ok=stackitem(i)-stackitem(i-1)=1
                        if ok else exit
            }
      End Sub
}
Number_Reversal_Game


Memory allocation
Module Checkit {
      Buffer Clear Mem1 as Byte*12345
      Print Len(Mem1)
      Hex Mem1(0) ' print in Hex address of first element
      Print Mem1(Len(Mem1)-1)-Mem1(0)+1=12345
      Buffer Mem1 as Byte*20000 ' redim block
      Print Mem1(Len(Mem1)-1)-Mem1(0)+1=20000
      Try {
            Print Mem1(20000) ' it is an error
      }
      Print Error$ ' return message: Buffer Locked, wrong use of pointer
}
Checkit

Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου