Jump to content

Calculator problem


Recommended Posts

Hi,

i am trying to write a calculator like the windows calculator but i dont know how to define the options on solve button .

My problem is i dont know how i tell the programm to solve the line from input because its not like

5*3

its like

"5*3"

Here is my code and well its just the beginning of it.

Hotkeyset will be included and things like that

; ----------------------------------------------------------------------------
;
; AutoIt Version: 3.1.0
; Author:         A.N.Other <myemail@nowhere.com>
;
; Script Function:
;   Template AutoIt script.
;
; ----------------------------------------------------------------------------

; Script Start - Add your code below here

#include <GuiConstants.au3>


Dim $i = 0, $k = 0, $j = 0
Dim $mem

GuiCreate("Calculator", 310, 209,-1, -1 , BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))

$input = GuiCtrlCreateInput("", 10, 20, 280, 20)
$0 = GuiCtrlCreateButton("0", 10, 180, 40, 20)
$1 = GuiCtrlCreateButton("1", 10, 90, 40, 20)
$2 = GuiCtrlCreateButton("2", 50, 90, 40, 20)
$3 = GuiCtrlCreateButton("3", 90, 90, 40, 20)
$4 = GuiCtrlCreateButton("4", 10, 120, 40, 20)
$5 = GuiCtrlCreateButton("5", 50, 120, 40, 20)
$6 = GuiCtrlCreateButton("6", 90, 120, 40, 20)
$7 = GuiCtrlCreateButton("7", 10, 150, 40, 20)
$8 = GuiCtrlCreateButton("8", 50, 150, 40, 20)
$9 = GuiCtrlCreateButton("9", 90, 150, 40, 20)
$operator = GuiCtrlCreateButton("+/-", 50, 180, 40, 20)
$komma = GuiCtrlCreateButton(",", 90, 180, 40, 20)
$CE = GuiCtrlCreateButton("CE", 90, 60, 60, 20)
$C = GuiCtrlCreateButton("C", 170, 60, 60, 20)
$mal = GuiCtrlCreateButton("*", 150, 90, 30, 20)
$prozent = GuiCtrlCreateButton("%", 150, 150, 30, 20)
$plus = GuiCtrlCreateButton("+", 150, 120, 30, 20)
$sqrt = GuiCtrlCreateButton("Sqrt", 200, 150, 30, 20)
$1durch = GuiCtrlCreateButton("1/x", 150, 180, 30, 20)
$geteilt = GuiCtrlCreateButton("/", 200, 90, 30, 20)
$minus = GuiCtrlCreateButton("-", 200, 120, 30, 20)
$gleich = GuiCtrlCreateButton("=", 200, 180, 30, 20)
$back = GuiCtrlCreateButton("Back", 10, 60, 60, 20)
$label = GuiCtrlCreateLabel("", 240, 60, 50, 20, $SS_SUNKEN)
$memclear = GuiCtrlCreateButton("MC", 240, 90, 50, 20)
$memread = GuiCtrlCreateButton("MR", 240, 120, 50, 20)
$memsafe = GuiCtrlCreateButton("MS", 240, 150, 50, 20)
$memplus = GUICtrlCreateButton("M+", 240, 180, 50, 20)

GUISetState()

While 1
    $inputread = GUICtrlRead( $input )
    Select
        Case WinActivate( "Calculator" )
            _keyset()
        Case Not WinActive( "Calculator" )
            _keyunset()
    EndSelect
    $msg = GUIGetMsg()
    Switch $msg
        Case $minus
            If $j = 0 Then
                GUICtrlSetData( $input, $inputread & "-")
                $j = 1
            EndIf
        Case $gleich
            Execute( $inputread )
        Case $prozent
            GUICtrlSetData( $input , $inputread/100 )
        Case $back
            $new = StringTrimRight( $inputread , 1)
            GUICtrlSetData( $input, $new)
        Case $memclear
            GUICtrlSetData( $label , "")
        Case $memplus
            $mem = $mem * 2
        Case $memread
            GUICtrlSetData( $input, $mem)
        Case $memsafe
            $mem = $inputread
            GUICtrlSetData( $label, "M")
        Case $geteilt
            If $j = 0 Then
                GUICtrlSetData( $input, $inputread & "/")
                $j = 1
            EndIf
        Case $1durch
            GUICtrlSetData( $input, 1 / $inputread )
        Case $sqrt
            GUICtrlSetData( $input, Sqrt( $inputread ))
        Case $plus
            If $j = 0 Then
                GUICtrlSetData( $input, $inputread & "+")
                $j = 1
            EndIf
        Case $mal
            If $j = 0 Then
                GUICtrlSetData($input, $inputread & "*")
                $j = 1
            EndIf
        Case $CE
            GUICtrlSetData( $input, "")
            $j = 0
        Case $C
            GUICtrlSetData( $input , "")
            $j = 0
        Case $GUI_EVENT_CLOSE
            Exit
        Case $1
            GUICtrlSetData( $input, $inputread & "1")
            $j = 0
            $i = 0
        Case $2
            GUICtrlSetData( $input, $inputread & "2")
            $j = 0
            $i = 0
        Case $3
            GUICtrlSetData( $input, $inputread & "3")
            $j = 0
            $i = 0
        Case $4
            GUICtrlSetData( $input, $inputread & "4")
            $j = 0
            $i = 0
        Case $5
            GUICtrlSetData( $input, $inputread & "5")
            $j = 0
            $i = 0
        Case $6
            GUICtrlSetData( $input, $inputread & "6")
            $j = 0
            $i = 0
        Case $7
            GUICtrlSetData( $input, $inputread & "7")
            $j = 0
            $i = 0
        Case $8
            GUICtrlSetData( $input, $inputread & "8")
            $j = 0
            $i = 0
        Case $9
            GUICtrlSetData( $input, $inputread & "9")
            $j = 0
            $i = 0
        Case $0
            GUICtrlSetData( $input, $inputread & "0")
            $j = 0
            $i = 0
        Case $operator
            If $k = 0 Then
                GUICtrlSetData( $input, "-" & $inputread)
                $k = 1
            Else
                GUICtrlSetData( $input, StringTrimLeft($inputread, 1))
                $k = 0
            EndIf
        Case $komma
            If $i = 0 Then
                GUICtrlSetData( $input, $inputread & ",")
                $i = 1
            EndIf
    EndSwitch
WEnd
 
Func _keyset()
    For $i = 48 to 57
        HotKeySet( Chr( $i ), Chr( $i ))
        $name = Chr( $i )
        Func $name
            GUICtrlSetData( $input , $inputread & $name)
        EndFunc
    Next
EndFunc

Func _keyunset()
    For $i = 48 to 57
        HotKeySet( Chr( $i ), "")
    Next
EndFunc
Edited by Daniel W.

--------------------------------------------------------------------------------------------------------------------------------Scripts : _Encrypt UDF_UniquePCCode UDF MS like calculatorInstall programm *UPDATED* --------------------------------------------------------------------------------------------------------------------------------[quote name='Helge' post='213117' date='Jul 26 2006, 10:22 AM']Have you ever tried surfing the internet with a milk-carton ?This is similar to what you're trying to do.[/quote]

Link to comment
Share on other sites

Ok i took a look at it but if i use it the way its used in the help file i only get the number of "+" in the string

$plusreplace = StringReplace( $inputread , "+" , "+")
$re = @extended
MsgBox(0, "", $re)
oÝ÷ Ù8b²·­º¹ìjw«®ÚºÚ"µÍÌÍÜÜXÙHHÝ[ÔXÙJ   ÌÍÚ[]XY  ][ÝÊÉ][ÝÈ
ÊBÌÍÜHH^[YÙÐÞ
    ][ÝÉ][ÝË    ÌÍÜJB
Edited by Daniel W.

--------------------------------------------------------------------------------------------------------------------------------Scripts : _Encrypt UDF_UniquePCCode UDF MS like calculatorInstall programm *UPDATED* --------------------------------------------------------------------------------------------------------------------------------[quote name='Helge' post='213117' date='Jul 26 2006, 10:22 AM']Have you ever tried surfing the internet with a milk-carton ?This is similar to what you're trying to do.[/quote]

Link to comment
Share on other sites

this works for all of them

$result = "5-3"
$re = StringSplit($result, "")
For $x = 1 To $re[0]
    If $re[$x] = "*" Then
        $result = $result * $re[$x + 1]
        $x = $x + 1
    EndIf
    If $re[$x] = "+" Then
        $result = $result + $re[$x + 1]
        $x = $x + 1
    EndIf
    If $re[$x] = "-" Then
        $result = $result - $re[$x + 1]
        $x = $x + 1
    EndIf
    If $re[$x] = "/" Then
        $result = $result / $re[$x + 1]
        $x = $x + 1
    EndIf
    
Next
MsgBox(0, "", $result)

8)

NEWHeader1.png

Link to comment
Share on other sites

Wow thanks :)

That is reallllllyyyyyyyyyyyy nice :(

--------------------------------------------------------------------------------------------------------------------------------Scripts : _Encrypt UDF_UniquePCCode UDF MS like calculatorInstall programm *UPDATED* --------------------------------------------------------------------------------------------------------------------------------[quote name='Helge' post='213117' date='Jul 26 2006, 10:22 AM']Have you ever tried surfing the internet with a milk-carton ?This is similar to what you're trying to do.[/quote]

Link to comment
Share on other sites

Ok new code is at first post.

I got the problem that i want to automize hotkeyset while winactive and writing the functions for each button

These are my Scite errors:

>C:\Programme\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.exe /beta /AU3Check /in "C:\Dokumente und Einstellungen\Admin\Desktop\Test.au3"
>Running AU3Check (1.54.1.1)  params:  from:C:\Programme\AutoIt3\beta
C:\Dokumente und Einstellungen\Admin\Desktop\Test.au3(171,3) : ERROR: missing Next.
        Func
        ^
C:\Dokumente und Einstellungen\Admin\Desktop\Test.au3(168,14) : REF: missing Next.
    For $i = 48 to
    ~~~~~~~~~~~~^
C:\Dokumente und Einstellungen\Admin\Desktop\Test.au3(171,3) : ERROR: syntax error
        Func
        ^
C:\Dokumente und Einstellungen\Admin\Desktop\Test.au3(59,14) : ERROR: _keyunset(): undefined function.
            _keyunset()
            ~~~~~~~~~~^
C:\Dokumente und Einstellungen\Admin\Desktop\Test.au3 - 3 error(s), 0 warning(s)
!>AU3Check ended.rc:2
>Exit code: 0   Time: 0.303
Edited by Daniel W.

--------------------------------------------------------------------------------------------------------------------------------Scripts : _Encrypt UDF_UniquePCCode UDF MS like calculatorInstall programm *UPDATED* --------------------------------------------------------------------------------------------------------------------------------[quote name='Helge' post='213117' date='Jul 26 2006, 10:22 AM']Have you ever tried surfing the internet with a milk-carton ?This is similar to what you're trying to do.[/quote]

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