Sign in to follow this  
Followers 0
Isildur

Calculator

8 posts in this topic

Hello, I'm new here and I need some help. I tried to make a calculator like the Windows calc. Here is the code.

CODE
#include <GUIConstants.au3>

#include <String.au3>

#include <array.au3>

#include <Date.au3>

#include-once

; Maske wird erstellt

GUICreate ( "Taschenrechner" , 254, 226)

GUISetState (@SW_SHOW)

$ID_Menu_Bearbeiten = GUICtrlCreateMenu ("Bearbeiten")

$ID_Menuitem_Kopieren = GUICtrlCreateMenuitem ( "Kopieren Strg+C", $ID_Menu_Bearbeiten)

$ID_Menuitem_Einfuegen = GUICtrlCreateMenuitem ( "Einfügen Strg+V", $ID_Menu_Bearbeiten)

$ID_Menu_Ansicht = GUICtrlCreateMenu ( "Ansicht")

$ID_Menuitem_Standart = GUICtrlCreateMenuitem ( "Standart", $ID_Menu_Ansicht)

$ID_Menuitem_Wissenschaftlich = GUICtrlCreateMenuitem ("Wissenschaftlich", $ID_Menu_Ansicht)

$ID_Menuitem_separator2 = GUICtrlCreateMenuitem ("",$ID_Menu_Ansicht,2)

$ID_Menuitem_Zifferngruppierung = GUICtrlCreateMenuitem ("Zifferngruppierung", $ID_Menu_Ansicht)

GUICtrlSetState ($ID_Menuitem_Zifferngruppierung, $GUI_UnCHECKED)

GUICtrlSetState ($ID_Menuitem_Standart,$GUI_CHECKED)

GUICtrlSetState ($ID_Menuitem_Wissenschaftlich, $GUI_DROPACCEPTED)

$ID_Menu_Hilfe = GUICtrlCreateMenu("?")

$ID_Menuitem_Hilfethemen = GUICtrlCreateMenuitem ( "Hilfethemen", $ID_Menu_Hilfe)

$ID_Menuitem_separator1 = GUICtrlCreateMenuitem ("",$ID_Menu_Hilfe,2)

$ID_Menuitem_Info = GUICtrlCreateMenuitem ( "Info", $ID_Menu_Hilfe)

$ID_Input = GUICtrlCreateInput ( "" , 12, 5, 224, 21,$ES_RIGHT) ; + $ES_NUMBER)

$ID_Button_Back = GUICtrlCreateButton ( "Back" ,50, 33, 57, 25)

$ID_Button_CE = GUICtrlCreateButton ( "CE" , 114, 33, 57, 25)

$ID_Button_C = GUICtrlCreateButton ( "C" , 178, 33, 57, 25)

$ID_Button_MC = GUICtrlCreateButton ( "MC" , 12, 64, 33, 25)

$ID_Button_7 = GUICtrlCreateButton ( "7", 50, 64, 33, 25)

$ID_Button_8 = GUICtrlCreateButton ( "8", 88, 64, 33, 25)

$ID_Button_9 = GUICtrlCreateButton ( "9", 126, 64, 33, 25 )

$ID_Button_Division = GUICtrlCreateButton ( "/", 164, 64, 33, 25)

$ID_Button_sqrt = GUICtrlCreateButton ( "sqrt", 202, 64, 33, 25)

$ID_Button_MR = GUICtrlCreateButton ( "MR" , 12, 95, 33, 25)

$ID_Button_4 = GUICtrlCreateButton ( "4", 50, 95, 33, 25)

$ID_Button_5 = GUICtrlCreateButton ("5", 88, 95, 33, 25)

$ID_Button_6 = GUICtrlCreateButton ( "6", 126, 95, 33, 25)

$ID_Button_Multiplikation = GUICtrlCreateButton ( "*", 164, 95, 33, 25)

$ID_Button_Prozent = GUICtrlCreateButton ( "%", 202, 95, 33, 25)

$ID_Button_MS = GUICtrlCreateButton ( "MS", 12, 126, 33, 25)

$ID_Button_1 = GUICtrlCreateButton ( "1", 50, 126, 33, 25)

$ID_Button_2 = GUICtrlCreateButton ( "2", 88, 126, 33, 25)

$ID_Button_3 = GUICtrlCreateButton ( "3", 126, 126, 33, 25)

$ID_Button_Subtraktion = GUICtrlCreateButton ("-", 164, 126, 33, 25)

$ID_Button_1geteiltdurchx = GUICtrlCreateButton ("1/x", 202, 126, 33, 25)

$ID_Button_Mplus = GUICtrlCreateButton ( "M+", 12, 157, 33, 25)

$ID_Button_0 = GUICtrlCreateButton ( "0", 50, 157, 33, 25)

$ID_Button_plus_minus = GUICtrlCreateButton ( "+/-", 88, 157, 33, 25)

$ID_Button_Komma = GUICtrlCreateButton ( ",", 126, 157, 33, 25)

$ID_Button_Addition = GUICtrlCreateButton ( "+" , 164, 157, 33, 25)

$ID_Button_Gleich = GUICtrlCreateButton ("=" , 202, 157, 33, 25)

$ID_Frame = GUICtrlCreateLabel ( "", 12, 33, 33, 25,$SS_SUNKEN )

$M_2 = GUICtrlCreateLabel( "", 24, 40, 18, 15)

; Folgenden Tasten werden Funktionen zugewiesen

HotKeySet ( "-", "minus")

HotKeySet ("{ENTER}", "Enter")

HotKeySet ("*", "mal")

HotKeySet ("/", "durch")

HotKeySet("{NUMPADADD}", "plus")

HotKeySet("{NUMPADSUB}", "minus")

HotKeySet("{NUMPADDIV}", "durch")

HotKeySet ("{NUMPADMULT}", "mal")

HotKeySet ("{NUMPADENTER}", "Enter")

GUISetState ()

; Variablen werden auf 0 gesetzt

$subtraktion = 0

$addition = 0

$multiplikation = 0

$division = 0

$Memory = 0

$Zahl1 = 0

$Zahl2 = 0

$Ergebnis = 0

$Zustand = 0

$Zahl3 = 0

; Focusierung des Inputs

ControlClick ( "Taschenrechner", "", $id_input, "left")

; Ablauf beim Drücken von Buttons

While 1

$msg = GUIGetMsg()

; msg ()

If $msg = $ID_Button_1 Then

numberPressed(1)

EndIf

If $msg = $ID_Button_2 Then

numberPressed(2)

EndIf

If $msg = $ID_Button_3 Then

numberPressed(3)

EndIf

If $msg = $ID_Button_4 Then

numberPressed(4)

EndIf

If $msg = $ID_Button_5 Then

numberPressed(5)

EndIf

If $msg = $ID_Button_6 Then

numberPressed(6)

EndIf

If $msg = $ID_Button_7 Then

numberPressed(7)

EndIf

If $msg = $ID_Button_8 Then

numberPressed(8)

EndIf

If $msg = $ID_Button_9 Then

numberPressed(9)

EndIf

If $msg = $ID_Button_0 Then

numberPressed(0)

EndIf

; , - Button

If $msg = $ID_Button_Komma Then

ControlSend ( "Taschenrechner", "", $ID_Input, ".")

EndIf

; +/- - Button

If $msg = $ID_Button_plus_minus Then

ControlSend ( "Taschenrechner", "", $ID_Input, "-")

EndIf

; sqrt - Button

If $msg = $ID_Button_sqrt Then

ControlClick ("Taschenrechner", "", $ID_Input, "left")

$Zahl3 = GUICtrlRead ( $ID_Input)

$Zahl2 = $Zahl3 ^ (1/2)

ControlSetText ( "Taschenrechner","", $ID_Input, $Zahl2)

ControlFocus ( "Taschenrechner" , "", $ID_Input)

EndIf

; MS - Button

If $msg = $ID_Button_MS Then

$Memory = GUICtrlRead ($ID_Input)

$M_2 = GUICtrlCreateLabel( "M", 24, 40, 18, 15)

GUICtrlSetColor($M_2,0xff0000)

ControlFocus ( "Taschenrechner", "", $ID_Input)

$Zustand = 0

EndIf

; MC - Button

If $msg = $ID_Button_MC Then

$Memory = 0

GUICtrlDelete ( $M_2)

ControlFocus ( "Taschenrechner", "", $ID_Input)

$Zustand = 0

EndIf

; 1/X - Button

If $msg = $ID_Button_1geteiltdurchx Then

$Zahl3 = GUICtrlRead ($ID_Input)

$Zahl2 = 1/$Zahl3

ControlSetText ( "Taschenrechner","", $ID_Input, $Zahl2)

ControlFocus ( "Taschenrechner", "", $ID_Input)

EndIf

; % - Button

If $msg = $ID_Button_Prozent Then

$Zahl3 = GUICtrlRead ($ID_Input)

$Zahl2 = $Zahl1 / 100 * $Zahl3

ControlSetText ( "Taschenrechner", "", $ID_Input, $Zahl2)

ControlFocus ("Taschenrecher", "", $ID_Input)

EndIf

; Back - Button

If $msg = $ID_Button_Back Then

ControlSend ( "Taschenrechner", "", $ID_Input, "{BACKSPACE}")

EndIf

; Menuitem Kopieren

If $msg = $ID_Menuitem_Kopieren Then

$Kopie = GUICtrlRead ( $ID_Input)

ClipPut ( $Kopie )

EndIf

; Menuitem Einfügen

If $msg = $ID_Menuitem_Einfuegen Then

ControlClick ("Taschenrechner", "", $ID_Input, "left")

Send ("^v")

EndIf

; MR- Button

If $msg = $ID_Button_MR Then

ControlSetText ("Taschenrechner", "", $ID_Input, $Memory)

$Zustand = 0

EndIf

; M - Plus Button

If $msg = $ID_Button_Mplus Then

$M_2 = GUICtrlCreateLabel( "M", 24, 40, 18, 15)

GUICtrlSetColor($M_2,0xff0000)

ControlFocus ( "Taschenrechner", "", $ID_Input)

$Zahl1 = GUICtrlRead ($ID_Input)

$Memory = $Memory + $Zahl1

EndIf

; C - Button

If $msg = $ID_Button_C Then

ControlSetText ("Taschenrechner", "", $ID_Input, "")

$Zahl1 = 0

$Zahl2 = 0

$Ergebnis = 0

$addition = 0

$subtraktion = 0

$division = 0

$mulitplikation = 0

ControlFocus ( "Taschenrechner", "", $ID_Input)

EndIf

; CE - Button

IF $msg = $ID_Button_CE Then

ControlSetText ( "Taschenrechner", "", $ID_Input, "")

ControlFocus ( "Taschenrechner", "", $ID_Input)

EndIf

; Beenden

If $msg = $GUI_EVENT_CLOSE Then

ExitLoop

EndIf

; Additionsbutton <--

If $msg = $ID_Button_Addition Then

plus()

EndIf

; Subtraktionsbutton <--

If $msg = $ID_Button_Subtraktion Then

minus ()

EndIf

; Multiplikationsbutton <--

If $msg = $ID_Button_Multiplikation Then

mal()

EndIf

; Divisionsbutton <--

If $msg = $ID_Button_Division Then

durch ()

EndIf

; Gleichbutton <--

If $msg = $ID_Button_Gleich Then

Enter ()

EndIf

WEnd

; Funktionen beim Drücken bestimmter Tasten <--

; Funktion beim + drücken

Func plus()

$Zustand = 0

subtraktion ()

multiplikation ()

division ()

If $addition = 1 Then

$Zahl2 = GUICtrlRead ($ID_Input)

$Ergebnis = $Zahl1 + $Zahl2

$Zahl1 = $Ergebnis

ControlSetText ( "Taschenrechner", "", $ID_Input, $Zahl1)

$zustand = 0

EndIf

If $addition = 0 Then

$Zahl1 = GUICtrlRead ($ID_Input)

ControlSetText ("Taschenrechner", "", $ID_Input, "")

ControlFocus ("Taschenrechner", "", $ID_Input)

$addition = 1

$subtraktion = 0

$multiplikation = 0

$division = 0

EndIf

EndFunc

; Funktion beim - drücken

Func minus()

$Zustand = 0

multiplikation ()

division ()

addition ()

If $subtraktion = 1 Then

$zahl2 = GUICtrlRead ($ID_Input)

$Ergebnis = $Zahl1 - $Zahl2

$Zahl1 = $Ergebnis

ControlSetText ( "Taschenrechner", "", $ID_Input, $Zahl1)

$Zustand = 0

EndIf

If $subtraktion = 0 Then

$Zahl1 = GUICtrlRead ($ID_Input)

ControlSetText ("Taschenrechner", "", $ID_Input, "")

ControlFocus ("Taschenrechner", "", $ID_Input)

$addition = 0

$subtraktion = 1

$multiplikation = 0

$division = 0

EndIf

EndFunc

; Funktion beim * drücken

Func mal()

$Zustand = 0

subtraktion ()

division ()

addition ()

If $multiplikation = 1 Then

$Zahl2 = GUICtrlRead ($ID_Input)

$Ergebnis = $Zahl1 * $Zahl2

$Zahl1 = $Ergebnis

ControlSetText ( "Taschenrechner", "", $ID_Input, $Zahl1)

$Zustand = 0

EndIf

If $multiplikation = 0 Then

$Zahl1 = GUICtrlRead ($ID_Input)

ControlSetText ("Taschenrechner", "", $ID_Input, "")

ControlFocus ("Taschenrechner", "", $ID_Input)

$addition = 0

$subtraktion = 0

$multiplikation = 1

$division = 0

EndIf

EndFunc

; Funktion beim / drücken

Func durch()

$Zustand = 0

subtraktion ()

multiplikation ()

addition ()

If $division = 1 Then

$Zahl2 = GUICtrlRead ($ID_Input)

$Ergebnis = $Zahl1 / $Zahl2

$Zahl1 = $Ergebnis

ControlSetText ("Taschenrechner", "", $ID_Input, $Zahl1)

$Zustand = 0

EndIf

If $division = 0 Then

$Zahl1 = GUICtrlRead ($ID_Input)

ControlSetText ("Taschenrechner", "", $ID_Input, "")

ControlFocus ("Taschenrechner", "", $ID_Input)

$addition = 0

$subtraktion = 0

$multiplikation = 0

$division = 1

EndIf

EndFunc

; Funktion beim Enter drücken

Func Enter ()

$Zahl2 = GUICtrlRead ($ID_Input)

If $addition = 1 Then

$Ergebnis = $Zahl1 + $Zahl2

ControlSetText ("Taschenrechner", "", $ID_Input, $Ergebnis)

$addition = 0

EndIf

If $subtraktion = 1 Then

$Ergebnis = $Zahl1 - $Zahl2

ControlSetText ("Taschenrechner", "", $ID_Input, $Ergebnis)

$subtraktion = 0

EndIf

If $multiplikation = 1 Then

$Ergebnis = $Zahl1 * $Zahl2

ControlSetText ("Taschenrechner", "", $ID_Input, $Ergebnis)

$multiplikation = 0

EndIf

If $division = 1 Then

$Ergebnis = $Zahl1 / $Zahl2

ControlSetText ("Taschenrechner", "", $ID_Input, $Ergebnis)

$division = 0

EndIf

$Zustand = 0

EndFunc

Func Zustand ()

If $Zustand = 0 Then

ControlSetText ( "Taschenrechner", "", $ID_Input, "")

EndIf

EndFunc

Func numberPressed ($number)

Zustand ()

ControlSend ( "Taschenrechner", "", $ID_Input, $number)

$Zustand = 1

EndFunc

Func addition ()

If $addition = 1 Then

$Zahl2 = GUICtrlRead ($ID_Input)

$Ergebnis = $Zahl1 + $Zahl2

ControlSetText ( "Taschenrechner", "", $ID_Input, $Ergebnis)

EndIf

EndFunc

Func multiplikation ()

If $multiplikation = 1 Then

$Zahl2 = GUICtrlRead ($ID_Input)

$Ergebnis = $Zahl1 * $Zahl2

ControlSetText ( "Taschenrechner", "", $ID_Input, $Ergebnis)

EndIf

EndFunc

Func subtraktion ()

If $subtraktion = 1 Then

$Zahl2 = GUICtrlRead ($ID_Input)

$Ergebnis = $Zahl1 - $Zahl2

ControlSetText ( "Taschenrechner", "", $ID_Input, $Ergebnis)

EndIf

EndFunc

Func division ()

If $division = 1 Then

$Zahl2 = GUICtrlRead ($ID_Input)

$Ergebnis = $Zahl1 / $Zahl2

ControlSetText ("Taschenrechner", "", $ID_Input, $Ergebnis)

EndIf

EndFunc

Don't wonder about the names of the variabels. I'm from Germany :) . Please can you help me to make it better (like the calculator from windows). Thank you in advance.

Share this post


Link to post
Share on other sites



Very nice job....

,,,, specially for a 1st post

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

I want to program a calculator.

Share this post


Link to post
Share on other sites

I don't know how it's called in English. In German it's called "Punkt- vor Strichrechnung". It means that when you have e.g. 3+3*6 you have to multiply 3*6 and after that you add 3.

Share this post


Link to post
Share on other sites

Heres something to figure out.

On windows calculator

1 + 1 = 2

+ 1 = 3

Press = over and over, and it will add.

Though, not on yours ;3


# MY LOVE FOR YOU... IS LIKE A TRUCK- #

Share this post


Link to post
Share on other sites

I don't know how it's called in English. In German it's called "Punkt- vor Strichrechnung". It means that when you have e.g. 3+3*6 you have to multiply 3*6 and after that you add 3.

you mean "order of operations"?

like instead of doing "3 + 3 and then 6 * 6" ??


What goes around comes around... Payback's a bitch.

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