Click to See Complete Forum and Search --> : Basic Calculator
jafang
Mar 14th, 2001, 12:20 AM
Hi guys
I need a Calculator in VB script that has just basic
+ - * / =
can someone help me please...
Thanks in Advance
Active
Mar 14th, 2001, 12:33 AM
Use the Eval Function.
Eg:
Dim answer,strCalc
StrCalc = "2+(5*10)"
answer = Eval(strCalc)
answer will now have 52.
jafang
Mar 14th, 2001, 01:16 AM
is it possible that you give me the complete script.
I'm new to VB Script.
Dim answer,strCalc
StrCalc = "2+(5*10)"
answer = Eval(strCalc)
answer will now have 52.
thanks
Jerry Grant
Mar 14th, 2001, 06:51 AM
Try this:
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE>Simple calculator</TITLE>
<SCRIPT language="VBscript">
Function DoCalc()
Dim strData, num1, num2, op
num1 = oForm.oNumber1.Value
num2 = oForm.oNumber2.Value
op = oForm.oOperation.options(oForm.oOperation.selectedIndex).value
If isnumeric(num1) And isnumeric(num2) Then
If num2 = 0 And op = "/" Then
MsgBox "Cannot divide by zero!", ,"Simple calculator"
Else
strData = num1 & op & num2
oForm.oResult.value = Eval(strData)
End If
Else
MsgBox "Cannot calculate the inputs given!", ,"Simple calculator"
End If
End Function
</SCRIPT>
</HEAD>
<BODY>
<FORM name="oForm">
<TABLE cellSpacing=1 cellPadding=1 width="75%" border=1>
<TR>
<TD>First Number</TD>
<TD>Operation</TD>
<TD>Second Number</TD>
<TD>Result</TD></TR>
<TR>
<TD><INPUT name="oNumber1"></INPUT></TD>
<TD>
<SELECT name=oOperation>
<OPTION value="+" selected>Add</OPTION>
<OPTION value="-">Subtract</OPTION>
<OPTION value="*">Muliply</OPTION>
<OPTION value="/">Divide</OPTION>
</SELECT>
</TD>
<TD><INPUT name="oNumber2"></TD>
<TD><INPUT name="oResult"></TD>
</TR>
</TABLE>
<P><INPUT type="button" value="Calc" onClick="DoCalc"></P>
</FORM>
</BODY>
</HTML>
It will need a bit more work, but shows the basics. :cool:
Mark Sreeves
Mar 14th, 2001, 08:13 AM
http://msdn.microsoft.com/scripting/JScript/samples/calculator/jscalc.htm
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.