# Help making calculator

## Recommended Posts

I'd like to make a calculator for use in my business.

We are a copy service that charges rates per page.

I'd like to make a small little program where I can input the page count and it will give me the amount to charge my customers.

Our rates are as follows:

\$1 for every page up to 25 pages than \$0.50 for every page thereafter.

I know it shouldn't be much harder than making a simple algorithm but I'm not exactly the best at this kind of thing. Anyone out there that can help me?

##### Share on other sites

I should let you try by yourself, but this should do what you want:

```Global \$iCharge, \$iPages
\$iPages = InputBox("Amount of Pages?")
If \$iPages <=25 Then
\$iCharge = \$iPages
Else
\$iCharge = (\$iPages - 25) * 0.5 + 25
EndIf
MsgBox(0,"Charge:",\$iCharge & "\$")```
Edited by hannes08

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

##### Share on other sites

I suggest to use Koda for GUI design

Function for calculation is then very simple...

Edited by Zedna

##### Share on other sites

Quick and dirty example without polishing/error checking ...

```#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

\$Form1 = GUICreate("Calculator", 312, 108, 192, 114)
\$Label1 = GUICtrlCreateLabel("Pages:", 72, 24, 37, 17)
\$Input1 = GUICtrlCreateInput("", 112, 24, 121, 21)
\$Label2 = GUICtrlCreateLabel("Charge:", 72, 56, 38, 17)
\$Input2 = GUICtrlCreateInput("", 112, 56, 121, 21)
;GUICtrlSetState(-1, \$GUI_DISABLE)
GUICtrlSetBkColor(-1, 0x00ff00)
GUISetState(@SW_SHOW)

GUIRegisterMsg(\$WM_COMMAND, "WM_COMMAND")

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

Func GetCharge(\$iPages)
Local \$iCharge
If \$iPages <=25 Then
\$iCharge = \$iPages
Else
\$iCharge = (\$iPages - 25) * 0.5 + 25
EndIf
Return \$iCharge & " \$"
EndFunc

Func WM_COMMAND(\$hWnd, \$msg, \$wParam, \$lParam)
Local \$nNotifyCode = BitShift(\$wParam, 16)
Local \$nID = BitAND(\$wParam, 0xFFFF)

If \$nID = \$Input1 And \$nNotifyCode = \$EN_CHANGE Then
\$charge = GetCharge(\$pages)
GUICtrlSetData(\$Input2, \$charge)
EndIf

Return \$GUI_RUNDEFMSG
EndFunc```

EDIT: removed disable status and changed color of output

Edited by Zedna

##### Share on other sites

Wow, Koda almost made the GUI too easy. Now let me figure out how to get it to work.

##### Share on other sites

Wow, Koda almost made the GUI too easy. Now let me figure out how to get it to work.

and it will generate AU3 code for you.

Then add program logic into that "empty" GUI script ...

I created something for you to start in my previous post.

##### Share on other sites

Thank you very much.

##### Share on other sites

kazah!

```#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
;Opt("GUIOnEventMode", 1)
#region ### START Koda GUI section ### Form=
\$Form1 = GUICreate("Form1", 251, 161, 192, 124)
GUISetState(@SW_SHOW)
Local \$buttons[10][2] = [[GUICtrlCreateButton("1", 16, 32, 25, 25),1], _
[GUICtrlCreateButton("2", 49, 25, 25, 25),2], _
[GUICtrlCreateButton("3", 79, 42, 25, 25),3], _
[GUICtrlCreateButton("4", 17, 66, 25, 25),4], _
[GUICtrlCreateButton("5", 45, 62, 25, 25),5], _
[GUICtrlCreateButton("6", 95, 63, 25, 25),6], _
[GUICtrlCreateButton("7", 38, 98, 25, 25),7], _
[GUICtrlCreateButton("8", 76, 91, 25, 25),8], _
[GUICtrlCreateButton("9", 5, 95, 25, 25),9], _
[GUICtrlCreateButton("0", 69, 72, 25, 25),0]]
\$Exec = GUICtrlCreateButton("equails", 72, 128, 51, 25)
\$Cls = GUICtrlCreateButton("c", 184, 40, 49, 41)
Local \$operators[3][2] = [[GUICtrlCreateButton("x", 144, 48, 25, 25),'*'], _
[GUICtrlCreateButton("-", 147, 74, 25, 25),'-'], _
[GUICtrlCreateButton("+", 136, 94, 25, 25),'+']]
\$Input1 = GUICtrlCreateInput("", 96, 8, 137, 21)
#endregion ### END Koda GUI section ###
Local \$Temp, \$Ex
Local \$x, \$y, \$s
While 1
\$M = GUIGetMsg()
If -3 = \$M Then Exit
For \$I = 0 To UBound(\$buttons) - 1
If \$Buttons[\$I][0] = \$M Then
EndIf
Next
For \$I = 0 To UBound(\$operators) - 1
If \$operators[\$I][0] = \$M Then
\$Ex = \$operators[\$I][1]
GUICtrlSetData(\$Input1,'')
EndIf
Next
If \$Exec = \$M Then
GUICtrlSetData(\$Input1,Execute(GUICtrlRead(\$Input1) & " " & \$Ex & " " & \$Temp))
EndIf
If \$cls = \$M Then
\$Temp = ''
\$Ex = ''
GUICtrlSetData(\$Input1,'')
EndIf
\$x = Random(0, 251, 1)
\$Y = Random(0, 161, 1)
\$S = Random(10, 55, 1)
\$p = WinGetPos(\$Form1)
\$1 = DllCall("gdi32.dll", "long", "CreateRectRgn", "long", 0, "long", 0, "long", \$p[2], "long", \$p[3])
\$2 = DllCall("gdi32.dll", "long", "CreateEllipticRgn", "long", \$x, "long", \$y, "long", \$x + \$s, "long", \$y + \$s)
\$3 = DllCall("gdi32.dll", "long", "CreateRectRgn", "long", 0, "long", 0, "long", 0, "long", 0)
DllCall("gdi32.dll", "long", "CombineRgn", "long", \$3[0], "long", \$1[0], "long", \$2[0], "int", 4)
DllCall("user32.dll", "long", "SetWindowRgn", "hwnd", \$Form1, "long", \$3[0], "int", 1)
WEnd```
Edited by ApudAngelorum

Things that I've done..

Icon Resource Editor: icon resource editor

AutoIt Piano: a piano

AutoIt Unlocker: unlocks files when you want to delete them

Colorful tooltips: a wrapper for the tool tips UDF

Rouge GoogleBot: a full screen animation

ASciTE text editor: a text editor written in autoit

Warning: Posts by this user are subject to change or may disappear without notice.

##### Share on other sites

Thank you very much.

I'm glad I could help on useful script.

When you finish it post here your final script so also others can see the result ...

##### Share on other sites

Just hint for you:

You can store some constants that can change in the future in INI file.

So in future when number of pages/charge per page changes then you will just edit your (text) INI file without need of recompiling of program.

In this case INI file could be something like this (calculator.ini):

```[Settings]
PagesCost1=25
Cost1=1
Cost2=0.5```

Look into helpfile at function IniRead how to use INI files.

EDIT: Here is solution using INI file without any hardcoded numbers in script

```#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Global \$ini_PagesCost1, \$ini_Cost1, \$ini_Cost2

\$ini = 'calculator.ini'
\$ini_PagesCost1 = IniRead(\$ini, 'Settings', 'PagesCost1', '25')
\$ini_Cost1 = IniRead(\$ini, 'Settings', 'Cost1', '1')
\$ini_Cost2 = IniRead(\$ini, 'Settings', 'Cost2', '0.5')

\$Form1 = GUICreate("Calculator", 312, 108, 192, 114)
\$Label1 = GUICtrlCreateLabel("Pages:", 72, 24, 37, 17)
\$Input1 = GUICtrlCreateInput("", 112, 24, 121, 21)
\$Label2 = GUICtrlCreateLabel("Charge:", 72, 56, 38, 17)
\$Input2 = GUICtrlCreateInput("", 112, 56, 121, 21)
;GUICtrlSetState(-1, \$GUI_DISABLE)
GUICtrlSetBkColor(-1, 0x00ff00)
GUISetState(@SW_SHOW)

GUIRegisterMsg(\$WM_COMMAND, "WM_COMMAND")

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

Func GetCharge(\$iPages)
Local \$iCharge
If \$iPages <= \$ini_PagesCost1 Then
\$iCharge = \$iPages * \$ini_Cost1
Else
\$iCharge = \$ini_PagesCost1 * \$ini_Cost1 + (\$iPages - \$ini_PagesCost1) * \$ini_Cost2
EndIf
Return \$iCharge & " \$"
EndFunc

Func WM_COMMAND(\$hWnd, \$msg, \$wParam, \$lParam)
Local \$nNotifyCode = BitShift(\$wParam, 16)
Local \$nID = BitAND(\$wParam, 0xFFFF)

If \$nID = \$Input1 And \$nNotifyCode = \$EN_CHANGE Then
\$charge = GetCharge(\$pages)
GUICtrlSetData(\$Input2, \$charge)
EndIf

Return \$GUI_RUNDEFMSG
EndFunc```
Edited by Zedna

##### Share on other sites

Another idea for improvement:

You can show in another edits also:

- values from INI file (for example in statusbar)

- number of pages for cost1 and summary cost of all pages for cost1

- number of pages for cost2 and summary cost of all pages for cost2

Edited by Zedna

## Create an account

Register a new account

×

• Wiki

• Back

• Git