Sign in to follow this  
Followers 0
mymusicmanager

Need help making a calculator

6 posts in this topic

The calculator does not work the way I want it to. It appears that when I type for instance 55 + 2, it turns out to be 110 (55 * 2)
 
Any help / suggestions is well accepted
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include "Math.au3"

Global $Memory = 0
Global $TempMem = 0
Global $DoPlus = False
Global $DoMinus = False
Global $DoDivide= False
Global $DoMultiply = False
Global $Button[11]


#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Calculator", 347, 357, 192, 124)
$Input1 = GUICtrlCreateInput("", 10, 20, 220, 20)
$Button[1] = GUICtrlCreateButton("1", 56, 40, 49, 33)
$Button[2] = GUICtrlCreateButton("2", 120, 40, 49, 33)
$Button[3] = GUICtrlCreateButton("3", 184, 40, 49, 33)
$Button[4] = GUICtrlCreateButton("4", 56, 88, 49, 33)
$Button[5] = GUICtrlCreateButton("5", 120, 88, 49, 33)
$Button[6] = GUICtrlCreateButton("6", 184, 88, 49, 33)
$Button[7] = GUICtrlCreateButton("7", 56, 136, 49, 33)
$Button[8] = GUICtrlCreateButton("8", 120, 136, 49, 33)
$Button[9]= GUICtrlCreateButton("9", 184, 136, 49, 33)
$Button[0] = GUICtrlCreateButton("0", 120, 184, 49, 33)
$Button11 = GUICtrlCreateButton("Flush Memory", 30, 184, 76, 33)
$Button12 = GUICtrlCreateButton("+", 184, 184, 49, 33)
$Button13 = GUICtrlCreateButton("=", 248, 40, 49, 33)
$Button14 = GUICtrlCreateButton("/", 248, 136, 49, 33)
$Button15 = GUICtrlCreateButton("*", 248, 88, 49, 33)
$Button16 = GUICtrlCreateButton("-", 248, 184, 49, 33)
$Label1 = GUICtrlCreateLabel("Output = ", 24, 294, 312, 17)
$label2 = GUICtrlCreateLabel ("Current number(s): ", 24, 260, 312, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $Msg = GUIGetMsg()
        for $lNum = 0 To 9
            If $Msg = $Button[$lNum] Then GUICtrlSetData( $Input1, GUICtrlRead( $Input1 )  & $lNum )




        Next

    Switch $Msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button11
            _ClearID($Input1)
        Case $Button12
            GUICtrlSetData($input1, GUICtrlRead($input1) & ("+"))
            $TempMem = GUICtrlRead($input1)
            $Memory=$TempMem
            $DoPlus=True
        Case $Button13
            If $DoPlus=True Then Calculate(1)
            If $DoDivide=True Then Calculate(2)
            If $DoMinus=True Then Calculate(3)
            if $DoMultiply=True Then Calculate(4)
        Case $Button14
            GUICtrlSetData($input1, GUICtrlRead($input1) & ("/"))
            $TempMem = GUICtrlRead($input1)
            $Memory=$TempMem
            $DoDivide=True
        Case $Button15
            GUICtrlSetData($input1, GUICtrlRead($input1) & ("*"))
            $TempMem = GUICtrlRead($input1)
            $Memory=$TempMem
            $DoMultiply=True
        Case $Button16
            GUICtrlSetData($input1, GUICtrlRead($input1) & ("-"))
            $TempMem = GUICtrlRead($input1)
            $Memory=$TempMem
            $DoMinus=True





    EndSwitch
WEnd



Func _ClearID( $aID )
    GUICtrlSetData( $aID, "" )
    GUICtrlSetState( $aID, $GUI_FOCUS )
EndFunc

Func Calculate($CalcVar)
    If $CalcVar=1 Then $Memory=$Memory+$TempMem
    If $CalcVar=2 Then $Memory=$Memory/$TempMem
    if $CalcVar=3 Then $Memory=$Memory-$TempMem
    if $CalcVar=4 then $Memory=$Memory*$TempMem
    GUICtrlSetData ($input1, " "&$Memory)
    $DoPlus=False
    $DoDivide=False
    $DoMinus=False
    $DoMultiply=False
EndFunc

Thank you in advance

 

Share this post


Link to post
Share on other sites



The result is always twice the value of the first operand because you don#t read the second operand when "=" is clicked.

$TempMem always has the value of operand 1. But should be operand 2.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Any way to explain that easier / more in.depth?

Share this post


Link to post
Share on other sites

Add

ConsoleWrite($CalcVar & "-" & $Memory & "-" & $TempMem & @LF)

as first line into function Calculate and you will see that both variables always have the same value.

When adding 12+2 $Memory should be 12 and $TempMem should be 2 but you will see that $TempMem is 12 as well, which is wrong.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

I added your line to the top of the Calculate function, but 9 * 2 is still 81

Your line made the console say how it calculated the line, which showed "4-9*-9*"

Share this post


Link to post
Share on other sites

My line doesn't solve your problem - it simply shows that both variables have the same value.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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
Sign in to follow this  
Followers 0