Jump to content

Arithmetic evaluation from an input control


BPCM
 Share

Recommended Posts

What I would like to do is have a user enter in an arithmetic expression, such as 26+4, and have autoit calculate an answer.

It's being stored as a string "26+4," I would like it to be stored as an Int ~30. When the input is used in later calculations, only the 26 is actually used in any computations.

Below is a basic code that shows what I mean.

CODE
#include <GUIConstants.au3>

GUICreate("Arithmetic Error", 200, 90)

GUISetState(@SW_SHOW)

GUISetFont(10)

$success = 0

$LUCK = GUICtrlCreateInput(" ", 50, 8, 70, 20, $ES_LEFT)

GUICtrlCreateLabel("Eq:", 10, 10)

$label = GUICtrlCreateLabel("Success Rate is " & $success & "%.", 10, 35, 300, 20)

While 1

$msg = GUIGetMsg()

Sleep(100)

If $msg = $GUI_EVENT_CLOSE Then Exit

Call("_Success")

WEnd

Func _Success()

$success = GUICtrlRead($LUCK) * 2

GUICtrlSetData($label, "Success Rate is " & $success & "%.")

EndFunc ;==>_Success

A single set of numbers work perfectly, but when any non-numerical characters are added, the rest of the statement is truncated.

Link to comment
Share on other sites

If there are only 2 numbers - as you said ... like 25+6 or 20*2 or anything else then the job is easy because you can use a Select statement and StringSplit cases.

something like:

$string = "25*5"
Global $result
Select
    Case StringInStr ($string, "+")
        strip ("+")
    Case StringInStr ($string, "-")
        strip ("-")
    Case StringInStr ($string, "*")
        strip ("*")
    Case StringInStr ($string, "/")
        strip ("/")
EndSelect
MsgBox (0, "result", $result)
Exit
        
Func strip ($separator)
    Local $array = StringSplit($string, $separator)
    If $array[0] <> 1 Then
        Select
            Case $separator = "+"
                $result = int ($array[1])+int($array[2])
            Case $separator = "-"
                $result = int ($array[1])-int($array[2])
            Case $separator = "*"
                $result = int ($array[1])*int($array[2])
            Case $separator = "/"
                $result = int ($array[1])/int($array[2])
        EndSelect
    EndIf
EndFunc

just replace the $string var from my example with GuiCtrlRead (your inputbox) and it will work.

However if there are more than 2 numbers ... the soolution will be more complicated ... but you can use this example ... it will give you the idea what to do.

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Thanks for your help. :shocked: I tried your code, and I was able to get it to work within the program, the only problem is, there are going to be 3 input boxes in total.

I've edited my code, and incorporated your idea into it. Only the first input box works correctly.

I'm not sure how to create seperate results from that one function. Hope that dosent make things more difficult.

CODE
#include <GUIConstants.au3>
GUICreate("Arithmetic Error", 150, 150)
GUISetState(@SW_SHOW)
GUISetFont(10)
Global $result
$ans = 0
$success = 0
$string = 0
GUICtrlCreateLabel("Eq:", 10, 10)
$input = GUICtrlCreateInput("", 50, 8, 70, 20, $ES_LEFT)
GUICtrlCreateLabel("Eq2:", 10, 40)
$input2 = GUICtrlCreateInput("", 50, 38, 70, 20, $ES_LEFT)
GUICtrlCreateLabel("Eq2:", 10, 70)
$input3 = GUICtrlCreateInput("", 50, 68, 70, 20, $ES_LEFT)
$label = GUICtrlCreateLabel("Success Rate is " & $success & "%.", 10, 95, 300, 20)
While 1
    $msg = GUIGetMsg()
    Sleep(100)
    If $msg = $GUI_EVENT_CLOSE Then Exit
    If $msg = $input Then
        $string = GUICtrlRead($input)
        If StringInStr($string, "+") Then
            strip("+")
        Else
            $ans = GUICtrlRead($input)
        EndIf
        _Success()
    EndIf
WEnd
Func strip($separator)
    Local $array = StringSplit($string, $separator)
    If $array[0] <> 1 Then
        Select
            Case $separator = "+"
                $ans = Int($array[1]) + Int($array[2])
        EndSelect
    EndIf
EndFunc   ;==>strip
Func _Success()
    $success = $ans * 2
    GUICtrlSetData($label, "Success Rate is " & $success & "%.")
EndFunc   ;==>_Success
Edited by BPCM
Link to comment
Share on other sites

Now it will work.

I've rewrited a part of your code, added a new function to handle all the strings/inputboxes and also I've added a new parameter to "strip" function for the same reason.

It works for all inputs.

If you need more help ... you know what to do :shocked:

#include <GUIConstants.au3>
GUICreate("Arithmetic Error", 150, 150)
GUISetState(@SW_SHOW)
GUISetFont(10)
Global $ans, $string1, $string2, $string3 
;removed the $result variable since it was not used
$success = 0
GUICtrlCreateLabel("Eq1:", 10, 10)
$input1 = GUICtrlCreateInput("", 50, 8, 70, 20, $ES_LEFT)
GUICtrlCreateLabel("Eq2:", 10, 40)
$input2 = GUICtrlCreateInput("", 50, 38, 70, 20, $ES_LEFT)
GUICtrlCreateLabel("Eq3:", 10, 70)
$input3 = GUICtrlCreateInput("", 50, 68, 70, 20, $ES_LEFT)
$label = GUICtrlCreateLabel("Success Rate1 is " & $success & "%.", 10, 95, 300, 20)
While 1
    $msg = GUIGetMsg()
    Sleep(100)
    If $msg = $GUI_EVENT_CLOSE Then Exit
    If $msg = $input1 Then
        $string1 = GUICtrlRead($input1)
        string_handler($string1, $input1)
        _Success()
    EndIf
    If $msg = $input2 Then
        $string2 = GUICtrlRead($input2)
        string_handler($string2, $input2)
        _Success()
    EndIf
    If $msg = $input3 Then
        $string3 = GUICtrlRead($input3)
        string_handler($string3, $input3)
        _Success()
    EndIf
WEnd

Func string_handler($dif_str, $dif_input)   ;this function will handle all inputboxes depending which one is passed as parameter
    If StringInStr($dif_str, "+") Then
            strip("+", $dif_str)
        Else
            $ans = GUICtrlRead($dif_input)
        EndIf
EndFunc

Func strip($separator, $str)        ;added a new parameter $str to give the ability to handle all strings
    Local $array = StringSplit($str, $separator)
    If $array[0] <> 1 Then
        Select
            Case $separator = "+"
                $ans = Int($array[1]) + Int($array[2])
        EndSelect
    EndIf
EndFunc   ;==>strip
Func _Success()
    $success = $ans * 2
    GUICtrlSetData($label, "Success Rate is " & $success & "%.")
EndFunc   ;==>_Success

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

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