Jump to content

Simple math - multiplication table GUI


major_lee
 Share

Recommended Posts

It's just a basic multiplication table.
I figured might as well post since it only took a min.

 

#include <GUIConstantsEx.au3>
$Form1 = GUICreate("multiplication", 320, 320, 192, 124)

For $r = 1 To 14 Step 1
For $i = 1 To 14 Step 1
     GUICtrlCreateLabel($r*$i, $i*20, $r*20, 20, 20)
Next
Next

GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd




Updated version that can scale with as function.
 

#include <GUIConstantsEx.au3>
$offset = 20 ; text spacing offset

mathtable(14) ; Calls mathtable function variable 14x14

GUISetState(@SW_SHOW)
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func mathtable($l = 14) ; length
    $Form1 = GUICreate("multiplication", $l * $offset + $offset * 2, $l * $offset + $offset * 2, 192, 124)
    For $r = 1 To $l Step 1
        For $i = 1 To $l Step 1
            GUICtrlCreateLabel($r * $i, $i * $offset, $r * $offset, $offset, $offset)
            GUICtrlSetFont(-1, $offset * .4, 400, 0, "MS Sans Serif")
        Next
    Next
EndFunc   ;==>mathtable

 

Post note_ (for usage insite)
It's used to create with 2d grid array.  the following output nulls past 12, original code was manual null value

Below was the start of the table generator. quick code i began with. The GUI table is just a visual output for the 2d array for the program that I've not posted or completed. That i needed to fine tune and clean up. which in its posted above is, as current version. The 2d array will manipulate the code in GUI through the code below.(which code isn't posted) I posted the GUI table for learning purpose and the insite for potential ideas.
 

#include <MsgBoxConstants.au3>
$string = ""
For $i = 1 To 14 Step 1
    if $i > 12  Then
    $r = Null
    Else
    $r = $i
    EndIf

    $string = $string & ', [['&$r*1&', '&$r*2&', '&$r*3&', '&$r*4&', '&$r*5&', '&$r*6&', '&$r*7&', '&$r*8&', '&$r*9&', '&$r*10&', '&$r*11&', '&$r*12&', Null,  Null] _'& @CRLF

Next
ClipPut($string)
MsgBox($MB_SYSTEMMODAL, "", $string)

dirty output but manual cleaned up.

Local $aWords[14][14] =  [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, Null,  Null] _
, [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, Null,  Null] _
, [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, Null,  Null] _
, [4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, Null,  Null] _
, [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, Null,  Null] _
, [6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, Null,  Null] _
, [7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, Null,  Null] _
, [8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, Null,  Null] _
, [9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99, 108, Null,  Null] _
, [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, Null,  Null] _
, [11, 22, 33, 44, 55, 66, 77, 88, 99, 110, 121, 132, Null,  Null] _
, [12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144, Null,  Null] _
, [Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null,  Null] _
, [Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null,  Null]] ;


If some one has a better way of doing it, I welcome ideas. It's a basic function for a more sophisticated program that I might post later.

Any changes will be posted on github and if i remember i might reply to this topic.
https://github.com/majorleearmy/au3_multiplicationtable

 

Edited by major_lee
Link to comment
Share on other sites

Instead of building it as a string and copy + pasting it into your code, you could just create the array directly...

#include <Array.au3>

_ArrayDisplay(MultiplicationTable(12))
_ArrayDisplay(MultiplicationTable(14, 12))

; $iSize - The size of the final 2D array
; $iNull - The number at which to create Null values instead of multiplying
Func MultiplicationTable($iSize, $iNull = 999999)
    Local $aTemp[$iSize][$iSize]
    For $iRow=1 To $iSize
        For $iCol=1 To $iSize
            If ($iRow > $iNull) Or ($iCol > $iNull) Then
                $aTemp[$iRow - 1][$iCol - 1] = Null
            Else
                $aTemp[$iRow - 1][$iCol - 1] = $iRow * $iCol
            EndIf
        Next
    Next
    Return $aTemp
EndFunc

 

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

14 hours ago, seadoggie01 said:

Instead of building it as a string and copy + pasting it into your code, you could just create the array directly...

#include <Array.au3>

_ArrayDisplay(MultiplicationTable(12))
_ArrayDisplay(MultiplicationTable(14, 12))

; $iSize - The size of the final 2D array
; $iNull - The number at which to create Null values instead of multiplying
Func MultiplicationTable($iSize, $iNull = 999999)
    Local $aTemp[$iSize][$iSize]
    For $iRow=1 To $iSize
        For $iCol=1 To $iSize
            If ($iRow > $iNull) Or ($iCol > $iNull) Then
                $aTemp[$iRow - 1][$iCol - 1] = Null
            Else
                $aTemp[$iRow - 1][$iCol - 1] = $iRow * $iCol
            EndIf
        Next
    Next
    Return $aTemp
EndFunc

 

Thanks, that's a great approach.
Reference notes: contributor https://github.com/majorleearmy/au3_multiplicationtable/commit/65460b46fe3aa19ccba6a25699ece969b1b6505d

Link to comment
Share on other sites

An alternative that I would consider better;) (and much faster):

#include ".\Eigen4AutoIt.au3"

_Eigen_StartUp()

$matA = _Eigen_CreateMatrix_LinSpaced_Rowwise ( 1, 14, 1, 14 )

$matR = _Eigen_OuterProduct ( $matA, _Eigen_Transpose($matA) )
_MatrixDisplay ( $matR, "Outer Product" )

_Eigen_CleanUp()

NB does require E4A, of course.

Link to comment
Share on other sites

....if you want both in just one shot: an 2d array and an ascii string (with or without a grid) ... here are my 2 cents ;)

#include <array.au3>

ConsoleWrite(_mathtable(20, 15, True)[1])
_ArrayDisplay(_mathtable(20, 15, True)[0])

; returns a 2 elements array:
; element [0] contains a math table as a 2D array
; element [1] contains a string as an ascii table
;
; $iUpBound : nr. of wanted square's rows/columns
; $iFill    : nr. of cells to fill
; $bGrid    : True: draw Grid; False: no Grid
Func _mathtable($iUpBound = 10, $iFill = $iUpBound, $bGrid = False)
    Local $aTable[$iUpBound][$iUpBound]
    Local $sTable, $sSpace = StringLen($iUpBound * $iUpBound) + Not $bGrid, $sGrid, $sVert
    If $bGrid Then
        $sGrid = "+" & StringReplace(StringFormat('%' & $sSpace & 's', ""), " ", '-')
        $sGrid = StringReplace(StringFormat('%' & $iUpBound & 's', ""), " ", $sGrid) & '+' & @CRLF
        $sVert = '|'
        $sTable = $sGrid
    EndIf
    For $iRow = 1 To $iUpBound
        $sTable &= $sVert
        For $iCol = 1 To $iUpBound
            $aTable[$iRow - 1][$iCol - 1] = ($iRow > $iFill Or $iCol > $iFill) ? Null : $iRow * $iCol
            $sTable &= StringFormat("%" & $sSpace & "s", $aTable[$iRow - 1][$iCol - 1]) & $sVert
        Next
        $sTable &= @CRLF & $sGrid ; $x <=
    Next
    Local $aReturn[2] = [$aTable, $sTable]
    Return $aReturn
EndFunc   ;==>_mathtable

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...