Creating Advanced Calculator With 4 Lines of Code

Here I share with you how to create a full-blown advanced calculator with just 4 lines of code in simple editor like  notepad. Interesting han? Sure it is.

This is actually part of many scripts I had created way back in 2002/2003 using VBScript to automate various Windows tasks (I wasn’t in the web development field by the time ;- ) . Today, I came across this piece of script on my computer and though of sharing with you.

Well, there is nothing really fancy, I have put the MSScript.ocx active-x control to use. It’s eval function can evaluate most complex math expressions easily. So here is how to create the calculator:

1.  Open Notepad and jot down the following 4 lines of code in it:

Set SC = CreateObject("ScriptControl")
SC.Language = "VBScript"

Val = InputBox("Enter simple or advanced expression to evaluate." & vbCrLf & vbCrLf & "Example:" & vbCrLf & vbCrLf & "(5+5)/2" & vbCrLf & vbCrLf & "or" & vbCrLf & vbCrLf & "Log(99)/2*25+3-(10^2 Mod 5)-Sin(60)+Tan(90)+2")

If Trim (Val) <> "" Then MsgBox "Answer =  " & SC.Eval(Val),,"Result"

2. Now save the file as calculator.vbs by selecting All Files from Save as type combo box. Note that vbs extension in the file name to denote that this file is vbscript and is to be run by Windows Scripting Host (wscript.exe).

3. Double click the saved file and there you are.

Have fun 😉

One thought on “Creating Advanced Calculator With 4 Lines of Code

Leave a comment