Jump to content

basic simple calculator :)


ludocus
 Share

Recommended Posts

I made this basic calculator, its basic but pretty handy I would say.. :)

Hope you enjoy!

#cs ----------------------------------------------------------------------------

AutoIt Version: 3.2.10.0
Author: ludocus

Script Function:
Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here

#include <GUIConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Lc", 116, 225, 193, 125)
GUISetBkColor(0xFFFF00)
$1 = GUICtrlCreateButton("1", 8, 40, 27, 25, 0)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
;GUICtrlSetColor(-1, 0x800000)
$2 = GUICtrlCreateButton("2", 40, 40, 27, 25, 0)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
;GUICtrlSetColor(-1, 0x800000)
$3 = GUICtrlCreateButton("3", 72, 40, 27, 25, 0)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
;GUICtrlSetColor(-1, 0x800000)
$4 = GUICtrlCreateButton("4", 8, 72, 27, 25, 0)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
;GUICtrlSetColor(-1, 0x800000)
$5 = GUICtrlCreateButton("5", 40, 72, 27, 25, 0)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
;GUICtrlSetColor(-1, 0x800000)
$6 = GUICtrlCreateButton("6", 72, 72, 27, 25, 0)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
;GUICtrlSetColor(-1, 0x800000)
$7 = GUICtrlCreateButton("7", 8, 104, 27, 25, 0)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
;GUICtrlSetColor(-1, 0x800000)
$8 = GUICtrlCreateButton("8", 40, 104, 27, 25, 0)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
;GUICtrlSetColor(-1, 0x800000)
$9 = GUICtrlCreateButton("9", 72, 104, 27, 25, 0)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
;GUICtrlSetColor(-1, 0x800000)
$o = GUICtrlCreateButton("0", 40, 136, 27, 25, 0)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
;GUICtrlSetColor(-1, 0x800000)
$min = GUICtrlCreateButton("-", 8, 136, 27, 25, 0)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
;GUICtrlSetColor(-1, 0x800000)
$plus = GUICtrlCreateButton("+", 72, 136, 27, 25, 0)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
;GUICtrlSetColor(-1, 0x800000)
$som = GUICtrlCreateInput("", 8, 8, 89, 21)
$clear = GUICtrlCreateButton('c', 97, 8, 20, 20 )
$deel = GUICtrlCreateButton("/", 8, 168, 27, 25, 0)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
;GUICtrlSetColor(-1, 0x800000)
$keer = GUICtrlCreateButton("x", 40, 168, 27, 25, 0)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
;GUICtrlSetColor(-1, 0x800000)
$c = GUICtrlCreateButton(",", 72, 168, 27, 25, 0)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
;GUICtrlSetColor(-1, 0x800000)
$is = GUICtrlCreateButton("=", 8, 200, 92, 25, 0)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
;GUICtrlSetColor(-1, 0x800000)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit

Case $1
calc(1)

Case $2
calc(2)

Case $3
calc(3)

Case $4
calc(4)

Case $5
calc(5)

Case $6
calc(6)

Case $7
calc(7)

Case $8
calc(8)

Case $9
calc(9)

Case $o
calc('nul')

Case $deel
calc('/')

Case $keer
calc('x')

Case $is
calc('get')

Case $plus
calc('+')

Case $min
calc('-')

Case $c
calc('.')

Case $clear
GUICtrlSetData($som, '' )

EndSwitch
WEnd

func calc($num)
if $num='get' then
get()
Else
if $num='nul' then
GUICtrlSetData($som, GUICtrlRead($som)&0)
Else
GUICtrlSetData($som, GUICtrlRead($som)&$num)
EndIf

EndIf
EndFunc

func get()
$p = Execute(StringReplace(GUICtrlRead($som), 'x', '*'))
if @error then
GUICtrlSetData($som, 'Error' )
Else
GUICtrlSetData($som, $p )
EndIf

EndFunc

Screenshot:

Posted Image

Edited by ludocus
Link to comment
Share on other sites

Thank You for sharing, keep up the good effort.

As ACS said (even though the way it was said sounded a bit harsh to me) you could make the code briefer by using loops or multiple Case values per case.

#include <GUIConstants.au3>

Opt("GUIOnEventMode", 1)

Global $SSB = StringSplit("1|2|3|4|5|6|7|8|9|-|0|+|/|*|.|C|%|=", "|"), $bX = 8, $bY = 40

$Form1 = GUICreate("Lc", 108, 233, 193, 125)
GUISetOnEvent($GUI_EVENT_CLOSE, "Close", $Form1)
GUISetBkColor(0xFFFF00)
$som = GUICtrlCreateInput("", 8, 8, 89, 21)
For $i = 1 To $SSB[0]
    GUICtrlCreateButton($SSB[$i], $bX, $bY, 27, 25, $BS_CENTER)
    GUICtrlSetOnEvent(-1, "Event")
    GUICtrlSetFont(-1, 10, 700, 0, "MS Sans Serif")
    If Not Mod($i, 3) Then
        $bX = 8
        $bY += 32
    Else
        $bX += 32
    EndIf
Next
$SSB = 0
GUISetState(@SW_SHOW)

While 1
    Sleep(100)
WEnd

Func Event()
    Switch GUICtrlRead(@GUI_CtrlId)
        Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-", "*", "/", "."
            GUICtrlSetData($som,  GUICtrlRead($som) & GUICtrlRead(@GUI_CtrlId)) 
        Case "="
            If Execute(GUICtrlRead($som)) Then GUICtrlSetData($som, Execute(GUICtrlRead($som)))
        Case "C"
            GUICtrlSetData($som, "")
        Case "%"
            ;??? Percent, leave this one for you :P
    EndSwitch
EndFunc

Func Close()
    Exit
EndFunc

Cheers

Edited by smashly
Link to comment
Share on other sites

  • 3 months later...

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