Click here to Skip to main content
15,884,976 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
XML
<html>
<head>
<script language="vbscript">
dim op,p,mi,mu,di,md,equ
function num1()
f1.t1.value=eval(f1.t1.value+f1.b1.value)
end function
function num2()
f1.t1.value=eval(f1.t1.value+f1.b2.value)
end function
function num3()
f1.t1.value=eval(f1.t1.value+f1.b3.value)
end function
function num4()
f1.t1.value=eval(f1.t1.value+f1.b4.value)
end function
function num5()
f1.t1.value=eval(f1.t1.value+f1.b5.value)
end function
function num6()
f1.t1.value=eval(f1.t1.value+f1.b6.value)
end function
function num7()
f1.t1.value=eval(f1.t1.value+f1.b7.value)
end function
function num8()
f1.t1.value=eval(f1.t1.value+f1.b8.value)
end function
function num9()
f1.t1.value=eval(f1.t1.value+f1.b9.value)
end function
function num0()
f1.t1.value=eval(f1.t1.value+f1.b0.value)
end function
function dot1()
f1.t1.value=f1.t1.value+"."
end function

function plus()
p=eval(f1.t1.value)
op="+"
f1.t1.value=" "
end function

function minus()
p=eval(f1.t1.value)
op="-"
f1.t1.value=" "
end function

function mul()
p=eval(f1.t1.value)
op="*"
f1.t1.value=" "
end function

function div()
p=eval(f1.t1.value)
op="/"
f1.t1.value=" "
end function

fuction equal()
equ=eval(f1.t1.value)
if op="+" then
f1.t1.value=p+equ

elseif op="-" then
f1.t1.value=p-equal

elseif op="*" then
f1.t1.value=p*equal

elseif op="/" then
f1.t1.value=p/equal
end if
end function
</script>
</head>

<body bgcolor="pink">
<center>
<font color="green">
<h1>Calculator Program</h1>
</font>
<hr>
<form name=f1>
<br>
<table border="2" cellspacing="5" cellpadding="5">
<tr>
<td><input type="text" name="t1" value=" "></td>
<td><input type="reset" name="c" value="C"></td>
</tr>
</table>

<table border="2" bordercolor="blue" cellspacing="5" cellpadding="10">
<tr>
<td><input type="button" name="b1" value="1" onclick="num1()"></td>
<td><input type="button" name="b2" value="2" onclick="num2()"></td>
<td><input type="button" name="b3" value="3" onclick="num3()"></td>
<td><input type="button" name="p" value="+" onclick="plus()"></td>
</tr>
<tr>
<td><input type="button" name="b4" value="4" onclick="num4()"></td>
<td><input type="button" name="b5" value="5" onclick="num5()"></td>
<td><input type="button" name="b6" value="6" onclick="num6()"></td>
<td><input type="button" name="mi" value="-" onclick="minus()"></td>
</tr>
<tr>
<td><input type="button" name="b7" value="7" onclick="num7()"></td>
<td><input type="button" name="b8" value="8" onclick="num8()"></td>
<td><input type="button" name="b9" value="9" onclick="num9()"></td>
<td><input type="button" name="mu" value="*" onclick="mul()"></td>
</tr>
<tr>
<td><input type="button" name="b0" value="0" onclick="num0()"></td>
<td><input type="button" name="dot" value="." onclick="dot1()"></td>
<td><input type="button" name="equ" value="=" onclick="equal()"></td>
<td><input type="button" name="di" value="/" onclick="div()"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
Posted

1 solution

First of all, you cannot really use VBScript for programming of a "real" Web site. It is supported, basically, only by IE, so, who will need such page? To publish your work on a Web site, you really need to write in JavaScript.

You idea to use eval is great, but in your next steps you go nowhere. Things are much, much simpler than that: it's enough to use only one eval in whole calculator, with much, much better results.

Please see my calculator available in my CodeProject article, complete with detail explanations of all the ideas and techniques: JavaScript Calculator[^].

Good luck,
—SA
 
Share this answer
 
Comments
CPallini 15-Apr-15 12:50pm    
5.Didn't know about the article. Time to read. :-)
Sergey Alexandrovich Kryukov 15-Apr-15 13:13pm    
Thank you, Carlo.
It's a really simple thing, but there are few quite delicate moments on top of it...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900