Jump to content

Initial Nums Calculator


EKY32
 Share

Recommended Posts

Hey there,

Well, I'm not good at all in math, but my cousin asked me if I can make a script which calculates the initial numbers in a defined range. He gave me some math-shit and I tried to code it:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <GuiButton.au3>
#include <GuiMenu.au3>
#include <IE.au3>
#include <Timers.au3>
#include <Math.au3>

$Form1 = GUICreate("Initial Nums Calculator v1.1", 293, 295)
GUISetFont(9, 400, 0, "Tahoma")
$Input1 = GUICtrlCreateInput("0", 56, 16, 129, 22, BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER))
$Updown1 = GUICtrlCreateUpdown($Input1)
$Input2 = GUICtrlCreateInput("0", 56, 48, 129, 22, BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER))
$Updown2 = GUICtrlCreateUpdown($Input2)
$Label1 = GUICtrlCreateLabel("From:", 16, 20, 35, 18)
$Label2 = GUICtrlCreateLabel("To:", 16, 51, 23, 18)
$Button1 = GUICtrlCreateButton("Start", 200, 15, 35, 25, $BS_DEFPUSHBUTTON)
GUICtrlSetFont(-1, 8, 400, 0, "Tahoma")
$StatusBar1 = _GUICtrlStatusBar_Create($Form1)
Dim $StatusBar1_PartsWidth[2] = [230, -1]
_GUICtrlStatusBar_SetParts($StatusBar1, $StatusBar1_PartsWidth)
_GUICtrlStatusBar_SetText($StatusBar1, "Set the values and click Start.", 0)
$Group1 = GUICtrlCreateGroup("", 6, 0, 281, 81)
$Input3 = GUICtrlCreateInput("", 200, 48, 73, 22, BitOR($GUI_SS_DEFAULT_INPUT, $ES_CENTER, $ES_NUMBER))
$Button2 = GUICtrlCreateButton("Jumb", 240, 15, 35, 25)
GUICtrlSetFont(-1, 8, 400, 0, "Tahoma")
$Button3 = _GUICtrlButton_Create($Form1, "Save", 0, 0, 10, 50, $BS_SPLITBUTTON)
_GUICtrlStatusBar_EmbedControl($StatusBar1, 1, $Button3)
$Group2 = GUICtrlCreateGroup("Result", 6, 88, 281, 177)
$ListView1 = GUICtrlCreateListView("Index|Value|", 16, 112, 257, 145)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 75)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 150)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 2, 0)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    $btn = _GUICtrlButton_GetState($Button3)
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            If GUICtrlRead($Input1) < GUICtrlRead($Input2) Then
                _GUICtrlListView_DeleteAllItems($ListView1)
                GUICtrlSetState($Button1, $GUI_DISABLE)
                GUICtrlSetState($Button2, $GUI_DISABLE)
                GUICtrlSetState($Button3, $GUI_DISABLE)
                GUICtrlSetState($Input1, $GUI_DISABLE)
                GUICtrlSetState($Input2, $GUI_DISABLE)
                GUICtrlSetState($Input3, $GUI_DISABLE)
                _GUICtrlButton_Enable($Button3, False)
                $starttime = _Timer_Init()
                For $i = GUICtrlRead($Input1) To GUICtrlRead($Input2)
                    If _MathCheckDiv($i, 2) = 1 Then
                        $x1 = Sqrt($i)
                        $str = StringSplit($x1, ".")
                        For $t = 1 To $str[1]
                            If IsInt($i / $t) = 1 Then IniWrite(@TempDir & "\NUM.db", $i, $i & $t & Random(0, 99999, 1), $t)
                        Next
                        _GUICtrlStatusBar_SetText($StatusBar1, "Processing: " & $i & " - Started: " & Round(_Timer_Diff($starttime) / 1000) & " Sec", 0)
                    EndIf
                Next
                $res = Round(_Timer_Diff($starttime) / 1000)
                _GUICtrlStatusBar_SetText($StatusBar1, "Done in " & $res & " secs, Listing the result now..", 0)
                $c = 0
                $x = _IniReadSectionNamesEx(@TempDir & "\NUM.db")
                If Not @error Then
                    For $n = 1 To $x[0]
                        $s = _IniReadSectionEx(@TempDir & "\NUM.db", $x[$n])
                        If Not @error Then
                            If $s[0][0] = 1 Then
                                $c = $c + 1
                                GUICtrlCreateListViewItem($c & "|" & $x[$n], $ListView1)
                            EndIf
                        EndIf
                    Next
                EndIf
                FileDelete(@TempDir & "\NUM.db")
                _GUICtrlStatusBar_SetText($StatusBar1, "Done, total result: " & $c & " - Ended: " & $res & " Sec", 0)
                GUICtrlSetState($Button1, $GUI_ENABLE)
                GUICtrlSetState($Button2, $GUI_ENABLE)
                GUICtrlSetState($Button3, $GUI_ENABLE)
                GUICtrlSetState($Input1, $GUI_ENABLE)
                GUICtrlSetState($Input2, $GUI_ENABLE)
                GUICtrlSetState($Input3, $GUI_ENABLE)
                _GUICtrlButton_Enable($Button3, True)
            Else
                _GUICtrlStatusBar_SetText($StatusBar1, "Error, change the numbers range.", 0)
            EndIf


        Case $Button2
            If Not _GUICtrlListView_GetItemCount($ListView1) = 0 Then
                If GUICtrlRead($Input3) <> "" Then
                    If GUICtrlRead($Input3) <= _GUICtrlListView_GetItemCount($ListView1) Then
                        _GUICtrlListView_EnsureVisible($ListView1, GUICtrlRead($Input3) - 1)
                        _GUICtrlListView_SetItemSelected($ListView1, GUICtrlRead($Input3) - 1)
                        _GUICtrlStatusBar_SetText($StatusBar1, "Jumbed to num " & GUICtrlRead($Input3) & ".", 0)
                    Else
                        _GUICtrlStatusBar_SetText($StatusBar1, "Error, this num is not indexed.", 0)
                    EndIf
                Else
                    GUICtrlSetState($Input3, $GUI_FOCUS)
                EndIf
            Else
                _GUICtrlStatusBar_SetText($StatusBar1, "Error, the result list is empty.", 0)
            EndIf

    EndSwitch
WEnd



Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    Local $tNMBHOTITEM = DllStructCreate("hwnd hWndFrom;int IDFrom;int Code;dword dwFlags", $lParam)
    Local $nNotifyCode = DllStructGetData($tNMBHOTITEM, "Code")
    If $nNotifyCode = $BCN_DROPDOWN Then _Popup_Menu($Button3)
EndFunc   ;==>WM_NOTIFY

Func _Popup_Menu($hCtrl)
    Local $hMenu
    Local Enum $TXT = 1000, $HTML, $NOINDEX
    $hMenu = _GUICtrlMenu_CreatePopup()
    _GUICtrlMenu_InsertMenuItem($hMenu, 0, "Save as TXT (Simple)", $TXT)
    _GUICtrlMenu_InsertMenuItem($hMenu, 1, "Save as HTML (In Tables)", $HTML)
    _GUICtrlMenu_InsertMenuItem($hMenu, 3, "", 0)
    _GUICtrlMenu_InsertMenuItem($hMenu, 3, "Save without indexing", $NOINDEX)
    Switch _GUICtrlMenu_TrackPopupMenu($hMenu, $hCtrl, -1, -1, 1, 1, 2)
        Case $TXT
            $var = FileSaveDialog("Save as TXT", "", "Text Files (*.txt)", 2 + 16, "Initial " & GUICtrlRead($Input1) & "-" & GUICtrlRead($Input2) & ".txt", $Form1)
            If Not @error Then
                FileDelete($var)
                FileWrite($var, "Initial Numbers Between " & GUICtrlRead($Input1) & " And " & GUICtrlRead($Input2) & " - Total Result [" & $c & "], In [" & $res & "] Secs:" & @CRLF & @CRLF)
                For $i = 0 To _GUICtrlListView_GetItemCount($ListView1) - 1
                    $Index = _GUICtrlListView_GetItemText($ListView1, $i, 0)
                    $Value = _GUICtrlListView_GetItemText($ListView1, $i, 1)
                    FileWrite($var, $Index & " > " & $Value & @CRLF)
                Next
                FileWrite($var, @CRLF & "Generated By: Initial Nums Calculator | EKY32")
                _GUICtrlStatusBar_SetText($StatusBar1, "TXT file saved.", 0)
                Run("notepad.exe " & $var)
            EndIf


        Case $HTML
            $var = FileSaveDialog("Save as HTML", "", "HTML Files (*.htm)", 2 + 16, "Initial " & GUICtrlRead($Input1) & "-" & GUICtrlRead($Input2) & ".htm", $Form1)
            If Not @error Then
                FileDelete($var)
                $oIE = _IECreate()
                $sHTML = "<HTML>" & @CR
                $sHTML &= "<HEAD>" & @CR
                $sHTML &= "<TITLE>Initial Nums Calculator</TITLE>" & @CR
                $sHTML &= '<style type="text/css">'
                $sHTML &= ".auto-style1 {"
                $sHTML &= "border-style: solid;"
                $sHTML &= "border-width: 1px;"
                $sHTML &= "}"
                $sHTML &= "</style>"
                $sHTML &= "</HEAD>" & @CR
                $sHTML &= "<body>" & @CR
                $sHTML &= "Initial Numbers Between " & GUICtrlRead($Input1) & " And " & GUICtrlRead($Input2) & " - Total Result [" & $c & "], In [" & $res & "] Secs: <br /><br />" & @CR
                $sHTML &= '<table border-style="solid" border-width="1px" style="width: 75">' & @CR
                $sHTML &= '<tr><td>Index</td><td  class="auto-style1">Value</td></tr>' & @CR
                For $i = 0 To _GUICtrlListView_GetItemCount($ListView1) - 1
                    $Index = _GUICtrlListView_GetItemText($ListView1, $i, 0)
                    $Value = _GUICtrlListView_GetItemText($ListView1, $i, 1)
                    $sHTML &= '<tr><td>' & $Index & '.</td><td  class="auto-style1">' & $Value & "</td></tr>" & @CR
                Next
                $sHTML &= "</body>" & @CR
                $sHTML &= "</HTML>" & @CR
                $sHTML &= "</table>" & @CR
                $sHTML &= "<br />Generated By: Initial Nums Calculator | EKY32" & @CR
                _IEDocWriteHTML($oIE, $sHTML)
                FileWrite($var, _IEDocReadHTML($oIE))
                _GUICtrlStatusBar_SetText($StatusBar1, "HTML page saved.", 0)
            EndIf
        Case $NOINDEX
            $var = FileSaveDialog("Save as TXT without indexing", "", "Text Files (*.txt)", 2 + 16, "Initial " & GUICtrlRead($Input1) & "-" & GUICtrlRead($Input2) & ".txt", $Form1)
            If Not @error Then
                FileDelete($var)
                FileWrite($var, "Initial Numbers Between " & GUICtrlRead($Input1) & " And " & GUICtrlRead($Input2) & " - Total Result [" & $c & "], In [" & $res & "] Secs:" & @CRLF & @CRLF)
                For $i = 0 To _GUICtrlListView_GetItemCount($ListView1) - 1
                    $Index = _GUICtrlListView_GetItemText($ListView1, $i, 0)
                    $Value = _GUICtrlListView_GetItemText($ListView1, $i, 1)
                    FileWrite($var, $Value & @CRLF)
                Next
                FileWrite($var, @CRLF & "Generated By: Initial Nums Calculator | EKY32")
                _GUICtrlStatusBar_SetText($StatusBar1, "TXT file saved.", 0)
                Run("notepad.exe " & $var)
            EndIf
    EndSwitch
    _GUICtrlMenu_DestroyMenu($hMenu)
EndFunc   ;==>_Popup_Menu


; _IniEx by SmOke_N

Func _IniReadSectionNamesEx($v_file)

    If Not $v_file Then Return SetError(-1, 0, 0)

    Local $f_exists = FileExists($v_file)

    Local $i_size, $a_secs

    If $f_exists Then
        $i_size = FileGetSize($v_file) / 1024

        ; if the file is smaller than 32kb, no need for regex
        If $i_size <= 31 Then
            $a_secs = IniReadSectionNames($v_file)
            If @error Then Return SetError(@error, 0, 0)
            If Not IsArray($a_secs) Then Return SetError(-2, 0, 0)
            Return $a_secs
        EndIf
    EndIf

    Local $s_fread
    If Not $f_exists Then
        ; string of data was passed
        $s_fread = $v_file
    Else
        $s_fread = FileRead($v_file)
    EndIf

    Local $s_secpatt = "(?m)(?:^|\v)\h*\[\h*(.*?)\h*\]"
    Local $a_secsre = StringRegExp($s_fread, $s_secpatt, 3)
    If @error Then Return SetError(-3, 0, 0)

    Local $i_ub = UBound($a_secsre)
    Local $a_secret[$i_ub + 1] = [$i_ub]

    For $isec = 0 To $i_ub - 1
        $a_secret[$isec + 1] = $a_secsre[$isec]
    Next

    Return $a_secret
EndFunc   ;==>_IniReadSectionNamesEx


Func _IniReadSectionEx($v_file, $s_section)

    If Not $v_file Then Return SetError(-1, 0, 0)

    Local $f_exists = FileExists($v_file)

    Local $i_size, $a_secread

    If $f_exists Then
        $i_size = FileGetSize($v_file) / 1024

        ; if the file is smaller than 32kb, no need for regex
        If $i_size <= 31 Then
            $a_secread = IniReadSection($v_file, $s_section)
            If @error Then Return SetError(@error, 0, 0)
            If Not IsArray($a_secread) Then Return SetError(-2, 0, 0)
            Return $a_secread
        EndIf
    EndIf

    Local $s_fread
    If Not $f_exists Then
        ; string of data was passed
        $s_fread = $v_file
    Else
        $s_fread = FileRead($v_file)
    EndIf

    ; data between sections or till end of file
    Local $s_datapatt = "(?is)(?:^|\v)(?!;|#)\h*\[\h*\Q"
    $s_datapatt &= $s_section
    $s_datapatt &= "\E\h*\]\h*\v+(.*?)(?:\z|\v\h*\[)"
    Local $a_data = StringRegExp($s_fread, $s_datapatt, 1)
    If @error Then Return SetError(-3, 0, 0)

    ; sanity check for inf people
    If Not StringInStr($a_data[0], "=", 1, 1) Then
        Return SetError(-4, 0, 0)
    EndIf

    ; since we stop at cr/lf then lets just split
    Local $a_lines
    If StringInStr($a_data[0], @CRLF, 1, 1) Then
        $a_lines = StringSplit(StringStripCR($a_data[0]), @LF)
    ElseIf StringInStr($a_data[0], @LF, 1, 1) Then
        $a_lines = StringSplit($a_data[0], @LF)
    Else
        $a_lines = StringSplit($a_data[0], @CR)
    EndIf

    ; prevent capturing commented keys
    Local $a_key, $a_value
    Local $s_keypatt = "\h*(?!;|#)(.*?)\h*="
    Local $s_valpatt = "\h*=\h*(.*)"
    Local $a_secs[$a_lines[0] + 1][2], $i_add = 0

    For $iline = 1 To $a_lines[0]

        $a_key = StringRegExp($a_lines[$iline], $s_keypatt, 1)
        If @error Then ContinueLoop

        $s_valpatt = "\h*=\h*(.*)"

        $a_value = StringRegExp($a_lines[$iline], $s_valpatt, 1)
        If @error Then ContinueLoop

        If StringLeft($a_key[0], 1) = '"' And StringRight($a_key[0], 1) = '"' Then
            $a_key[0] = StringTrimLeft(StringTrimRight($a_key[0], 1), 1)
        EndIf
        If StringLeft($a_value[0], 1) = '"' And StringRight($a_value[0], 1) = '"' Then
            $a_value[0] = StringTrimLeft(StringTrimRight($a_value[0], 1), 1)
        EndIf

        $i_add += 1
        $a_secs[$i_add][0] = $a_key[0]
        $a_secs[$i_add][1] = $a_value[0]
    Next

    If Not $i_add Then Return SetError(-5, 0, 0)

    ; cleanup return array
    ReDim $a_secs[$i_add + 1][2]
    $a_secs[0][0] = $i_add

    Return $a_secs
EndFunc   ;==>_IniReadSectionEx

post-74823-0-23065700-1377900961_thumb.j

I'm sure it can be updated somehow.

I hope it helps someone. :ermm:

Edited by EKY32

[font="'trebuchet ms', helvetica, sans-serif;"]Please mark the answer of your question if you found it.[/font]

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...