' Console Table Generator — headers, rows, column alignment, formatting
#COMPILE EXE
#DIM ALL

FUNCTION MaxLen(s AS STRING, minLen AS LONG) AS LONG
    LOCAL l AS LONG
    l = LEN(s)
    IF l < minLen THEN l = minLen
    FUNCTION = l
END FUNCTION

SUB PrintSeparator(widths() AS LONG, numCols AS LONG)
    LOCAL i AS LONG
    LOCAL j AS LONG
    PRINT "+"
    FOR i = 1 TO numCols
        FOR j = 1 TO widths(i) + 2
            PRINT "-"
        NEXT j
        PRINT "+"
    NEXT i
    PRINT
END SUB

SUB NumericTable()
    LOCAL i AS LONG
    LOCAL d1 AS STRING
    LOCAL d2 AS STRING
    LOCAL d3 AS STRING
    LOCAL d4 AS STRING
    LOCAL d5 AS STRING

    PRINT "Numeric Table:"
    PRINT "+-------+--------+--------+----------+----------+"
    PRINT "| Num   | Square | Cube   | Sqrt     | Recip    |"
    PRINT "+-------+--------+--------+----------+----------+"
    FOR i = 1 TO 8
        d1 = FORMAT$(i, "#####")
        d2 = FORMAT$(i * i, "#####")
        d3 = FORMAT$(i * i * i, "#####")
        d4 = FORMAT$(SQR(i), "###.####")
        d5 = FORMAT$(1.0 / i, "#.#####")
        PRINT "| " & d1 & " | " & d2 & " | " & d3 & " | " & d4 & " | " & d5 & " |"
    NEXT i
    PRINT "+-------+--------+--------+----------+----------+"
    PRINT
END SUB

SUB EmployeeTable()
    PRINT "Employee Table:"
    PRINT "+--------------+-------------+---------+-------+"
    PRINT "| Name         | Department  | Salary  | Years |"
    PRINT "+--------------+-------------+---------+-------+"
    PRINT "| Alice Smith  | Engineering | $85,000 | 5     |"
    PRINT "| Bob Jones    | Marketing   | $62,000 | 3     |"
    PRINT "| Carol Lee    | Finance     | $91,000 | 8     |"
    PRINT "| David Brown  | Engineering | $78,000 | 4     |"
    PRINT "| Eve Davis    | HR          | $55,000 | 2     |"
    PRINT "| Frank Wilson | Engineering | $120,000| 12    |"
    PRINT "+--------------+-------------+---------+-------+"
    PRINT
END SUB

SUB MonthDaysTable()
    LOCAL i AS LONG
    LOCAL m AS STRING
    LOCAL d AS STRING
    PRINT "Month Days Table:"
    PRINT "+------------+------+"
    PRINT "| Month      | Days |"
    PRINT "+------------+------+"
    FOR i = 1 TO 12
        IF i = 1 THEN m = "January"
        IF i = 2 THEN m = "February"
        IF i = 3 THEN m = "March"
        IF i = 4 THEN m = "April"
        IF i = 5 THEN m = "May"
        IF i = 6 THEN m = "June"
        IF i = 7 THEN m = "July"
        IF i = 8 THEN m = "August"
        IF i = 9 THEN m = "September"
        IF i = 10 THEN m = "October"
        IF i = 11 THEN m = "November"
        IF i = 12 THEN m = "December"
        IF i = 1 THEN d = "31"
        IF i = 2 THEN d = "28/29"
        IF i = 3 THEN d = "31"
        IF i = 4 THEN d = "30"
        IF i = 5 THEN d = "31"
        IF i = 6 THEN d = "30"
        IF i = 7 THEN d = "31"
        IF i = 8 THEN d = "31"
        IF i = 9 THEN d = "30"
        IF i = 10 THEN d = "31"
        IF i = 11 THEN d = "30"
        IF i = 12 THEN d = "31"
        PRINT "| " & m & STRING$(10 - LEN(m), 32) & " |  " & d & "   |"
    NEXT i
    PRINT "+------------+------+"
    PRINT
END SUB

SUB FibonacciTable()
    LOCAL i AS LONG
    LOCAL a AS QUAD
    LOCAL b AS QUAD
    LOCAL c AS QUAD
    LOCAL ratio AS STRING
    PRINT "Fibonacci Ratio Table:"
    PRINT "+-------+--------------+---------------+"
    PRINT "|   n   |   Fib(n)     |    Ratio      |"
    PRINT "+-------+--------------+---------------+"
    a = 0
    b = 1
    FOR i = 1 TO 15
        IF i > 2 AND b <> 0 THEN
            ratio = FORMAT$(a / b, "###.##########")
        ELSE
            ratio = "     N/A     "
        END IF
        PRINT "| " & FORMAT$(i, "##") & "    | " & FORMAT$(a, "##########") & "    | " & ratio & " |"
        c = a + b
        a = b
        b = c
    NEXT i
    PRINT "+-------+--------------+---------------+"
    PRINT
END SUB

SUB MultiplicationTable()
    LOCAL i AS LONG
    LOCAL j AS LONG
    PRINT "Multiplication Table (5x5):"
    PRINT "     1   2   3   4   5"
    PRINT "   +-------------------+"
    FOR i = 1 TO 5
        PRINT STR$(i) & "  | " & FORMAT$(i * 1, "##") & "  " & FORMAT$(i * 2, "##") & "  " & FORMAT$(i * 3, "##") & "  " & FORMAT$(i * 4, "##") & "  " & FORMAT$(i * 5, "##")
    NEXT i
    PRINT
END SUB

SUB PowersTable()
    LOCAL i AS LONG
    LOCAL pow AS QUAD
    LOCAL fact AS QUAD
    PRINT "Powers of 2 Table:"
    PRINT "+------+---------+------------+"
    PRINT "|  n   |  2^n    |   n!       |"
    PRINT "+------+---------+------------+"
    pow = 1
    FOR i = 0 TO 10
        IF i > 0 THEN pow = pow * 2
        fact = 1
        FOR j = 2 TO i
            fact = fact * j
        NEXT j
        PRINT "| " & FORMAT$(i, "##") & "   | " & FORMAT$(pow, "#######") & " | " & FORMAT$(fact, "#########") & " |"
    NEXT i
    PRINT "+------+---------+------------+"
    PRINT
END SUB

FUNCTION PBMAIN() AS LONG
    PRINT "=========================="
    PRINT " Console Table Generator "
    PRINT "=========================="
    PRINT
    CALL NumericTable()
    CALL EmployeeTable()
    CALL MonthDaysTable()
    CALL FibonacciTable()
    CALL MultiplicationTable()
    CALL PowersTable()
    PRINT "All tables generated."
    FUNCTION = 0
END FUNCTION
