From: Svjatoslav Agejenko Date: Mon, 28 Oct 2024 19:42:07 +0000 (+0200) Subject: Using AI to improve code readability X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=4813e3450a749ff5b02a4b42ecdafd8874e6d4e8;p=qbasicapps.git Using AI to improve code readability --- diff --git a/Math/Lottery/loto.bas b/Math/Lottery/loto.bas index b34eb94..e950818 100755 --- a/Math/Lottery/loto.bas +++ b/Math/Lottery/loto.bas @@ -1,85 +1,79 @@ +DECLARE SUB loadData () +DECLARE SUB predictNextDraw () +DECLARE SUB parseDrawNumbers (drawString$) +DECLARE SUB displayDotGraph () +DECLARE SUB waitForUserInput () +DECLARE SUB displayLineGraph () +DECLARE SUB displayCombinatoricsGraph () +DECLARE SUB displayMenu () +DECLARE SUB startProgram () ' Program to analyze lottery winning numbers. ' By Svjatoslav Agejenko. ' Email: svjatoslav@svjatoslav.eu ' Homepage: http://www.svjatoslav.eu -' + ' Changelog: ' 2001, Initial version -' 2024.09, Improved program readability using AI +' 2024, Improved program readability using AI DEFINT A-Y -DIM SHARED mitus, sona$(1 TO 50) -DIM SHARED num(1 TO 500, 1 TO 7) -DIM SHARED mitun - -start -loaddata +DIM SHARED totalDraws, drawNumbers$(1 TO 50) +DIM SHARED lotteryNumbers(1 TO 500, 1 TO 7) +DIM SHARED currentDraw -menu - -SUB ennus - DIM buf(1 TO 48) - PRINT "During the last 10 draws:" +startProgram +loadData - ' Count occurrences of each number in the last 10 draws - FOR a = mitun - 10 TO mitun - FOR b = 2 TO 7 - buf(num(a, b)) = buf(num(a, b)) + 1 - NEXT b - NEXT a - - ' Print the most frequent numbers - FOR b = 1 TO 6 - c = 0 - FOR a = 1 TO 48 - IF buf(a) > c THEN c = buf(a): d = a - NEXT a - PRINT d; " appeared: "; c; " times" - buf(d) = 0 - NEXT b +displayMenu - PRINT "--------------------------------------" +SUB displayCombinatoricsGraph + CLS + PAINT (1, 1), 3 + DIM buffer(1 TO 48) + DIM buffer2(1 TO 20000) - ' Find when each number last appeared - FOR a = 1 TO mitun - FOR b = 2 TO 7 - buf(num(a, b)) = a - NEXT b - NEXT a - - FOR b = 1 TO 6 - c = 30000 - FOR a = 1 TO 48 - IF buf(a) < c THEN c = buf(a): d = a - NEXT a - PRINT d; " appeared last time: "; mitun - c; " draws ago" - buf(d) = 30000 - NEXT b + totalNumbers = 0 -END SUB + ' Prepare data for drawing + FOR drawIndex = 1 TO currentDraw + FOR number = 1 TO 48 + buffer(number) = 0 + NEXT number -SUB getson (a$) - mitus = 0 + FOR numberPosition = 2 TO 7 + buffer(lotteryNumbers(drawIndex, numberPosition)) = 10 + NEXT numberPosition - d = 1 - FOR b = 1 TO LEN(a$) - c$ = RIGHT$(LEFT$(a$, b), 1) - IF c$ = " " THEN - d = 1 - ELSE - IF d = 1 THEN - mitus = mitus + 1 - sona$(mitus) = "" - d = 0 - END IF - sona$(mitus) = sona$(mitus) + c$ - END IF - NEXT b + FOR number = 1 TO 48 + totalNumbers = totalNumbers + 1 + buffer2(totalNumbers) = buffer(number) + NEXT number + NEXT drawIndex + ' Draw the graph + FOR yAxisValue = 2 TO 400 + xAxisValue = 0 + FOR xPosition = 1 TO 639 + FOR yPosition = 1 TO yAxisValue + xAxisValue = xAxisValue + 1 + IF xAxisValue > totalNumbers THEN GOTO skipDrawing + PSET (xPosition, yPosition), buffer2(xAxisValue) + NEXT yPosition + NEXT xPosition +skipDrawing: + + LINE (xPosition + 1, 1)-(xPosition + 1, yAxisValue), 14 + LINE (xPosition + 1, yPosition)-(xPosition + 4, yPosition), 12 + + IF INKEY$ <> "" THEN GOTO 1 + NEXT yAxisValue + +waitForUserInput +1 END SUB -SUB gr1 +SUB displayDotGraph CLS LINE (0, 0)-(600, 0), 1 LINE (0, 49)-(600, 49), 1 @@ -87,148 +81,170 @@ SUB gr1 LINE (0, 48 * 6 + 51)-(600, 48 * 6 + 51), 1 ' Draw vertical lines for each draw and plot numbers - FOR a = 1 TO mitun - c = 0 - FOR b = 2 TO 7 - c = c + num(a, b) - PSET (a, num(a, b)), 15 - NEXT b - LINE (a, c + 50)-(a, 24 * 6 + 50), 10 - NEXT a - - wai + FOR drawIndex = 1 TO currentDraw + totalNumbers = 0 + FOR numberPosition = 2 TO 7 + totalNumbers = totalNumbers + lotteryNumbers(drawIndex, numberPosition) + PSET (drawIndex, lotteryNumbers(drawIndex, numberPosition)), 15 + NEXT numberPosition + LINE (drawIndex, totalNumbers + 50)-(drawIndex, 24 * 6 + 50), 10 + NEXT drawIndex + + waitForUserInput END SUB -SUB gr2 +SUB displayLineGraph CLS - DIM buf(1 TO 20000) + DIM buffer(1 TO 20000) ' Draw lines connecting consecutive lottery numbers - FOR x = 1 TO mitun - 1 - FOR a = 2 TO 7 - LINE (600, num(x, a))-(610, num(x + 1, a)), 3 - NEXT a + FOR x = 1 TO currentDraw - 1 + FOR numberPosition = 2 TO 7 + LINE (600, lotteryNumbers(x, numberPosition))-(610, lotteryNumbers(x + 1, numberPosition)), 3 + NEXT numberPosition LINE (600, 1)-(600, 48), 1 SOUND 0, 2 ' Scroll the screen to the left - GET (10, 1)-(610, 50), buf(1) - PUT (1, 1), buf(1), PSET + GET (10, 1)-(610, 50), buffer(1) + PUT (1, 1), buffer(1), PSET LINE (601, 1)-(610, 50), 0, BF IF INKEY$ <> "" GOTO 5 NEXT x - wai - 5 + waitForUserInput +5 END SUB -SUB gr3 +SUB displayMenu +menuLoop: CLS - PAINT (1, 1), 3 - DIM buf(1 TO 48) - DIM buf2(1 TO 20000) - c = 0 +7 + LOCATE 1, 1 - ' Prepare data for drawing - FOR a = 1 TO mitun - FOR b = 1 TO 48 - buf(b) = 0 - NEXT b + PRINT " Lottery statistics analysis" + + PRINT "1 - Dot graph" + PRINT "2 - Line graph" + PRINT "3 - Combinatorics" + PRINT "4 - Statistics" + PRINT "5 - Exit program" - FOR b = 2 TO 7 - buf(num(a, b)) = 10 - NEXT b + userInput$ = INPUT$(1) - FOR b = 1 TO 48 - c = c + 1 - buf2(c) = buf(b) - NEXT b - NEXT a + IF userInput$ = "1" THEN displayDotGraph + IF userInput$ = "2" THEN displayLineGraph + IF userInput$ = "3" THEN displayCombinatoricsGraph + IF userInput$ = "4" THEN predictNextDraw: GOTO 7 + IF userInput$ = "5" THEN SYSTEM + + GOTO menuLoop - ' Draw the graph - FOR e = 2 TO 400 - d = 0 - FOR x = 1 TO 639 - FOR y = 1 TO e - d = d + 1 - IF d > c THEN GOTO 4 - PSET (x, y), buf2(d) - NEXT y - NEXT x - 4 - - LINE (x + 1, 1)-(x + 1, e), 14 - LINE (x + 1, y)-(x + 4, y), 12 - - IF INKEY$ <> "" THEN GOTO 6 - NEXT e - - wai - 6 END SUB -SUB loaddata +SUB loadData PRINT "One moment..." - mitun = 0 + currentDraw = 0 OPEN "loos.txt" FOR INPUT AS #1 - 1 - IF EOF(1) <> 0 THEN GOTO 2 +loadNextLine: + IF EOF(1) <> 0 THEN GOTO endOfFile - LINE INPUT #1, a$ - getson a$ + LINE INPUT #1, drawString$ + parseDrawNumbers drawString$ - mitun = mitun + 1 + currentDraw = currentDraw + 1 ' Parse and store lottery numbers - FOR b = 1 TO 7 - num(mitun, b) = VAL(sona$(b)) - NEXT b + FOR numberPosition = 1 TO 7 + lotteryNumbers(currentDraw, numberPosition) = VAL(drawNumbers$(numberPosition)) + NEXT numberPosition - GOTO 1 + GOTO loadNextLine - 2 +endOfFile: CLOSE #1 CLS END SUB -SUB menu -3 - CLS +SUB parseDrawNumbers (drawString$) + totalDraws = 0 -7 - LOCATE 1, 1 + numberIndex = 1 + FOR charPosition = 1 TO LEN(drawString$) + currentChar$ = RIGHT$(LEFT$(drawString$, charPosition), 1) + IF currentChar$ = " " THEN + numberIndex = 1 + ELSE + IF numberIndex = 1 THEN + totalDraws = totalDraws + 1 + drawNumbers$(totalDraws) = "" + numberIndex = 0 + END IF + drawNumbers$(totalDraws) = drawNumbers$(totalDraws) + currentChar$ + END IF + NEXT charPosition - PRINT " Lottery statistics analysis" +END SUB - PRINT "1 - Dot graph" - PRINT "2 - Line graph" - PRINT "3 - Combinatorics" - PRINT "4 - Statistics" - PRINT "5 - Exit program" +SUB predictNextDraw + DIM buffer(1 TO 48) + PRINT "During the last 10 draws:" - a$ = INPUT$(1) + ' Count occurrences of each number in the last 10 draws + FOR drawIndex = currentDraw - 10 TO currentDraw + FOR numberPosition = 2 TO 7 + buffer(lotteryNumbers(drawIndex, numberPosition)) = buffer(lotteryNumbers(drawIndex, numberPosition)) + 1 + NEXT numberPosition + NEXT drawIndex - IF a$ = "1" THEN gr1 - IF a$ = "2" THEN gr2 - IF a$ = "3" THEN gr3 - IF a$ = "4" THEN ennus: GOTO 7 - IF a$ = "5" THEN SYSTEM + ' Print the most frequent numbers + FOR position = 1 TO 6 + maxCount = 0 + FOR number = 1 TO 48 + IF buffer(number) > maxCount THEN + maxCount = buffer(number) + mostFrequentNumber = number + END IF + NEXT number + PRINT mostFrequentNumber; " appeared: "; maxCount; " times" + buffer(mostFrequentNumber) = 0 + NEXT position + + PRINT "--------------------------------------" - GOTO 3 + ' Find when each number last appeared + FOR drawIndex = 1 TO currentDraw + FOR numberPosition = 2 TO 7 + buffer(lotteryNumbers(drawIndex, numberPosition)) = drawIndex + NEXT numberPosition + NEXT drawIndex + + FOR position = 1 TO 6 + minAppearances = 30000 + FOR number = 1 TO 48 + IF buffer(number) < minAppearances THEN + minAppearances = buffer(number) + leastRecentNumber = number + END IF + NEXT number + PRINT leastRecentNumber; " appeared last time: "; currentDraw - minAppearances; " draws ago" + buffer(leastRecentNumber) = 30000 + NEXT position END SUB -SUB start +SUB startProgram SCREEN 12 END SUB -SUB wai - a$ = INPUT$(1) +SUB waitForUserInput + userInput$ = INPUT$(1) END SUB +