Jump to content

Simple Calculator


Guest
 Share

Recommended Posts

I have not been doing much recently so i thought i should play around with variables a bit and do some math

Maths.au3

$1 = InputBox("Maths", "Number:", "")
$operation = InputBox("Maths", "+, -, *:", "")
$2 = InputBox("Maths", "plus Number:", "")
$plus = $1 + $2
$minus = $1 - $2
$times = $1 * $2
If $operation = "+" Then
MsgBox("", "Maths", "= " & $plus)
EndIf
If $operation = "-" Then
MsgBox("", "Maths", "= " & $minus)
EndIf
If $operation = "*" Then
MsgBox("", "Maths", "= " & $times)
EndIf

Maths.au3

Link to comment
Share on other sites

There are a couple of ways to make a calculator that accepts more inputs, like typing "5*3" into one InputBox. The easiest by far is to use the Execute function. Take a look, see what you come up with :oops: Then look at GUIs as well and you'll be well on your way to making a fully functional calculator.

Link to comment
Share on other sites

Thanks this is so much better

$input = InputBox("Maths", "Enter an equasion", "")
$ans = Execute($input)
MsgBox("", "Maths", "Answer = " & $ans)

As you said-GUI...

I will just get started!

Edited by Guest
Link to comment
Share on other sites

OK i am working on that but in the mean time here is a gui!

#include <GUIConstantsEx.au3>

Local $file, $btn, $msg
GUICreate(" Calculator", 395, 30, @DesktopWidth / 2 - 160, @DesktopHeight / 2 - 45, -1, 0x00000018); WS_EX_ACCEPTFILES
$file = GUICtrlCreateInput("", 10, 5, 300, 20)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)
$btn = GUICtrlCreateButton("=", 325, 5, 60, 20)
GUISetState()
$msg = 0
While $msg <> $GUI_EVENT_CLOSE
$msg = GUIGetMsg()
Select
  Case $msg = $btn
   ExitLoop
EndSelect
WEnd
$ans = execute(guictrlread($file))
MsgBox(4096, "Calculator", $ans)

Have fun!

Link to comment
Share on other sites

But will it blend? Haha, no. Secondly, I improved your script a bit.

#include <GUIConstantsEx.au3>
Main()
Func Main()
Local $file, $btn, $msg
GUICreate("Calculator", 395, 30, @DesktopWidth / 2 - 160, @DesktopHeight / 2 - 45, -1, 0x00000018); WS_EX_ACCEPTFILES
$file = GUICtrlCreateInput("", 10, 5, 300, 20)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)
$btn = GUICtrlCreateButton("=", 325, 5, 60, 20)
GUISetState()
While 1
  $msg = GUIGetMsg()
  Select
   Case $msg = $GUI_EVENT_CLOSE
    Exit (0)
   Case $msg = $btn
    GUICtrlSetData($file, Execute(GUICtrlRead($file)))
   Case Else
    ;;;
  EndSelect
WEnd
EndFunc   ;==>Main

My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

My Projects

WhyNotWin11
Cisco FinesseGithubIRC UDFWindowEx UDF

 

Link to comment
Share on other sites

  • 5 weeks later...

I couldn't resist playing this one out a little farther. This version works nicely (so did the first one).

Note that all of the code is like Lego blocks written by others. I just snapped the blocks together.

#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <Misc.au3>
Global $btn
Global $file
Global $pi = 4 * ATan(1)
Global $degrees = 180/$pi

_ReduceMemory()
Main()
Func Main()
Local $file, $btn, $msg
$gui = GUICreate("calc U lizer", 395, 35, @DesktopWidth -410, (@DesktopHeight *.925)-40, -1, $WS_EX_TOPMOST)
GUISetBkColor(0x000000)
$file = GUICtrlCreateInput("", 10, 5, 300, 25)
GUICtrlSetFont($file, 12, 400, 0, "MS Sans Serif")
GUICtrlSetColor($file, 0xFFFFFF)
GUICtrlSetBkColor($file, 0x000000)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)
$btn = GUICtrlCreateButton("=", 315, 1, 30, 20)
GUICtrlSetTip($btn, "enter key")
$restart = GUICtrlCreateButton("reset", 350, 1, 35, 20)
GUICtrlSetTip($restart, "del key")
$qButton = GUICtrlCreateButton("", 315, 23, 30, 11)
GUICtrlSetBkColor($qButton, 0xbb21ee)
GUICtrlSetTip($qButton, "WTF?")

Global $acceleartors[2][2] = [["{ENTER}", $btn], ["{DELETE}", $restart]] ;<<<<<<<<<<<<<
GUISetAccelerators($acceleartors, $gui) ;<<<<<<<<<<<<<

GUISetState()
While 1
$msg = GUIGetMsg()
Select
Case $msg = $GUI_EVENT_CLOSE
Exit (0)
Case $msg = $btn
GUICtrlSetData($file, Execute(GUICtrlRead($file)))
Case $msg = $restart
_restart()
Case $msg = $qButton
MsgBox(0, "examples", "exponent-N(squared)....N^2" & @CRLF & "" & @CRLF &"PI....pi()....pi()*2" & @CRLF & "" & @CRLF & "Degrees(57.259etc)....2*degrees()" & @CRLF & "" & @CRLF & "google() <enter> <enter> opens iexplore to google page")
;& no, I have not figured out proper use of the underscore yet
EndSelect
WEnd
EndFunc   ;==>Main

Func ans()
GUICtrlSetData($file, Execute(GUICtrlRead($file)))
EndFunc   ;==>ans

Func _restart()
Local $sAutoIt_File = @TempDir & "~Au3_ScriptRestart_TempFile.au3"
Local $sRunLine, $sScript_Content, $hFile

$sRunLine = @ScriptFullPath
If Not @Compiled Then $sRunLine = @AutoItExe & ' /AutoIt3ExecuteScript ""' & $sRunLine & '""'
If $CmdLine[0] > 0 Then $sRunLine &= ' ' & $CmdLineRaw

$sScript_Content &= '#NoTrayIcon' & @CRLF & _
'While ProcessExists(' & @AutoItPID & ')' & @CRLF & _
'   Sleep(10)' & @CRLF & _
'WEnd' & @CRLF & _
'Run("' & $sRunLine & '")' & @CRLF & _
'FileDelete(@ScriptFullPath)' & @CRLF

$hFile = FileOpen($sAutoIt_File, 2)
FileWrite($hFile, $sScript_Content)
FileClose($hFile)

Run(@AutoItExe & ' /AutoIt3ExecuteScript "' & $sAutoIt_File & '"', @ScriptDir, @SW_HIDE)
Sleep(1000)
Exit
EndFunc   ;==>_restart

Func _ReduceMemory($i_PID = -1)
    If $i_PID <> -1 Then
        Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $i_PID)
        $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0])
        DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0])
    Else
        $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)
    EndIf

    Return $ai_Return[0]
EndFunc   ;==>_ReduceMemory

Func pi()
Return $pi
EndFunc

Func degrees()
Return $degrees
EndFunc

;Any other func that you want

Func google()
Return 'ShellExecute("iexplore.exe","http://www.google.com")'
EndFunc
Edited by lorenkinzel
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...