一个功能强大的计算器,几乎可以进行各种运算,有了它不用烦了

- 中国WEB开发者网络 (http://www.webasp.net)
-- 网页特效 (http://www.webasp.net/javascript/)
--- 一个功能强大的计算器,几乎可以进行各种运算,有了它不用烦了 (http://www.webasp.net/javascript/1/709.htm)
-- 发布日期: 2005-06-30
<!-- 网页特效代码由[中国WEB开发者网络:http://www.ChinaWebDev.com]提供! -->
<!-- 要实现此效果需要 1 个步骤: -->

<!-- 第 1 步: -->
<!-- 把下面的代码加到<BODY></BODY>区域中: -->

<script language="JavaScript">
function getinput(func) {
var a = document.mainform.total;
var b = document.mainform.memory1;
var c = document.mainform.memory2;
if (document.mainform.mode[0].checked) {
var mode = 1 //help mode
}
else {
var mode = 0 //non-help mode
}
if (func==power) {
return power(mode, a)
}
if (func==equa) {
return equa(mode)
}
if (func==spec) {
return spec(mode, a)
}
if (func==cubed) {
return cubed(mode, a)
}
if (func==squared) {
return squared(mode, a)
}
if (func=="mem1") {
return mem(mode, a, b)
}
if (func=="mem2") {
return mem(mode, a, c)
}
if (func=="rcl1") {
return rcl(mode, b, a)
}
if (func=="rcl2") {
return rcl(mode, c, a)
}
if (func==logrythm) {
return logrythm(mode, a)
}
if (func==sine) {
return sine(mode, a)
}
if (func==cosine) {
return cosine(mode, a)
}
if (func==tangent) {
return tangent(mode, a)
}
if (func==squareroot) {
return squareroot(mode, a)
}
if (func==fractions) {
return fractions(mode, a)
}
if (func==negpos) {
return negpos(mode, a)
}
if (func==mc) {
return mc(mode, b, c)
}
if (func==scie) {
return scie(mode, a)
}
if (func==unscie) {
return unscie(mode, a)
}
}

//enough variables

//evaluate the value in the total textbox
function calc(obj, objw) {
if (objw.value=="") {
objw.value=''
}
objw.value = eval(obj.value)
}
//add more characters to the total textbox
function more(obj, where) {
if (where.value=="" || where.value=="0") {
where.value = obj
}
else {
where.value += obj
}
}
//clear the total textbox value and start with this new character
function doit(obj, where) {
where.value = obj
}
//clear the value
function cleart(obj) {
obj.value = '0'
}
//oops, backspace
function less(obj) {
obj.value = obj.value.substring(0, obj.value.length - 1)
}
//powers to any given number
function power(mode, obj) {
if (mode==1) {
alert("Power allows you to give powers to any given number.")
}
if (obj.value!="" && obj.value!="0") {
aa = prompt("Do you want to use the number in the total textbox as the base number?", "y or n");
if (aa=="y") {
a = obj.value
}
else {
a = prompt("Enter a base number:", "")
}
b = prompt("Enter an exponent:", "");
if (aa=="y") {
doit(Math.pow(a, b), obj)
}
else {
more('(' + Math.pow(a, b) + ')', obj)
}
}
else {
a = prompt("Enter a base:", "");
b = prompt("Enter an exponent:", "");
doit(Math.pow(a, b), obj)
}
}
//solve for unknown variable ("x") in any given expression
function equa(mode) {
if (mode==1) {
alert("Equasion\+ is a handy little tool that will solve for \"x\" in any expression. Ex: 5*x=10, it will alert you this: x=2. But, you have to tell it exactly what you want. Method is what property of math you will be using. Ex: addition, subtraction, division, or multiplication. Remember, *=multiplication, /=division, \+=addition, and -=subtraction.")
}
a = prompt("What method will you be using? Ex: \/, *, +, or -", "");
if (a=="/") {
f = prompt("Is \"x\" being divided?", "y or n")
if (f=="y") {
b = prompt("\"x\" is being divided by what?", "");
c = prompt("And what is the answer?", "");
d = c*b;
}
else {
b = prompt("What number is being divided by \"x\"?", "");
c = prompt("And what is the answer?", "");
d = b/c;
}
}
if (a=="*") {
b = prompt("What is being multiplied by \"x\"?", "");
c = prompt("And what is the answer?", "");
d = c/b;
}
if (a=="+") {
b = prompt("What is being added to \"x\"?", "");
c = prompt("And what is the answer?", "");
d = c-b
}
if (a=="-") {
f = prompt("Is \"x\" being subtracted?", "y or n")
if (f=="y") {
b = prompt("\"x\" is being subtracted from what?", "");
c = prompt("And what is the answer?", "");
d = b-c
}
else {
b = prompt("What number is being subtracted from \"x\"?", "");
c = prompt("And what is the answer?", "");
d = (b*1)+(c*1)
}
}
if (a=="") {
d = 0
}
alert("x = "+d+"")
}
//multiply any number by any number, any number of times
function spec(mode, obj) {
if (mode==1) {
alert("Spec is handy tool that allows you to multiply a number by another number any number of times. It works kind of like powers, except the only difference is that in powers, you have multiply a number by itself any number of times. This function allows you to multiply any number by any number, any number of times.")
}
if (obj.value!="" && obj.value!="0") {
aa = prompt("Do you want to use the number in the total textbox as the first number?", "y or n")
if (aa=="y") {
a = obj.value
}
else {
a = prompt("A number please as the first number:", "")
}
b = prompt("And you want to multiply the number by what number?", "");
c = prompt("And how many times will you being multiplying the first by the second?", "");
d = Math.pow(b,c);
if (aa=="y") {
doit(a*d, obj)
}
else {
more('(' + a*d + ')', obj)
}
}
else {
a = prompt("A number please as the first number:", "");
b = prompt("And you want to multiply the number by what number?", "");
c = prompt("And how many times will you being multiplying the first by the second?", "");
d = Math.pow(b,c);
doit(a*d, obj)
}
}
//cube any given number
function cubed(mode, obj) {
if (mode==1) {
alert("Cubed allows you to cube any given number.")
}
if (obj.value!="" && obj.value!="0") {
aa = prompt("Do you want to cube the current number in the total textbox?", "y or n");
if (aa=="y") {
doit(Math.pow(obj.value, 3), obj)
}
else {
a = prompt("Enter a number to cube:", "");
more('(' + Math.pow(a, 3) + ')', obj)
}
}
else {
a = prompt("Enter a number to cube:", "");
doit(Math.pow(a, 3), obj)
}
}
//square any given number
function squared(mode, obj) {
if (mode==1) {
alert("Squared allows you to square any given number")
}
if (obj.value!="" && obj.value!="0") {
aa = prompt("Do you want to square the current number in the total textbox?", "y or n");
if (aa=="y") {
doit(Math.pow(obj.value, 2), obj)
}
else {
a = prompt("Enter a number to be squared:", "");
more('(' + Math.pow(a, 2) + ')', obj)
}
}
else {
a = prompt("Enter a number to square:", "");
doit(Math.pow(a, 2), obj)
}
}
//store any number in memory
function mem(mode, obj, where) {
if (mode==1) {
alert("Pressing the memory button allows you to save the current number in the total textbox for later use. You can Recall the remembered number by pressing the Rcl button.")
}
where.value = obj.value
}
//recall any numbers in memory
function rcl(mode, obj, where) {
if (mode==1) {
alert("Rcl is a neat function that works with Memory. It recalls and gives you the number you saved in the coresponding memory.")
}
if (obj.value=="") {
more('', where)
}
else {
more('(' + obj.value + ')', where)
}
}
//find the logarythm of any given number
function logrythm(mode, obj) {
if (mode==1) {
alert("This function returns the natural logarythm of any given number")
}
if (obj.value!="" && obj.value!="0") {
aa = prompt("Do you want to find the logarythm of the number in the total textbox?", "y or n");
if (aa=="y") {
doit(Math.log(obj.value), obj)
}
else {
a = prompt("Enter a number to find the logarythm of:", "");
more('(' + Math.log(a) + ')', obj)
}
}
else {
a = prompt("Enter a number to find the logarythm of:", "");
doit(Math.log(a), obj)
}
}
//find the sine of any given number
function sine(mode, obj) {
if (mode==1) {
alert("This function returns the sine of any given number")
}
if (obj.value!="" && obj.value!="0") {
aa = prompt("Do you want to find the sine of the number in the total textbox?", "y or n");
if (aa=="y") {
doit(Math.sin(obj.value), obj)
}
else {
a = prompt("Enter a number to find the sine of:", "");
more('(' + Math.sin(a) + ')', obj)
}
}
else {
a = prompt("Enter a number to find the sine of:", "");
doit(Math.sin(a), obj)
}
}
//find the cosine of any given number
function cosine(mode, obj) {
if (mode==1) {
alert("This function returns the cosine of any given number")
}
if (obj.value!="" && obj.value!="0") {
aa = prompt("Do you want to find the cosine of the number in the total textbox?", "y or n");
if (aa=="y") {
doit(Math.cos(obj.value), obj)
}
else {
a = prompt("Enter a number to find the cosine of:", "");
more('(' + Math.cos(a) + ')', obj)
}
}
else {
a = prompt("Enter a number to find the cosine of:", "");
doit(Math.cos(a), obj)
}
}
//find the tangent of any given number
function tangent(mode, obj) {
if (mode==1) {
alert("This function returns the tangent of any given number")
}
if (obj.value!="" && obj.value!="0") {
aa = prompt("Do you want to find the tangent of the number in the total textbox?", "y or n");
if (aa=="y") {
doit(Math.tan(obj.value), obj)
}
else {
a = prompt("Enter a number to find the tangent of:", "");
more('(' + Math.tan(a) + ')', obj)
}
}
else {
a = prompt("Enter a number to find the tangent of:", "");
doit(Math.tan(a), obj)
}
}
//find the squareroot of any given number
function squareroot(mode, obj) {
if (mode==1) {
alert("This function gives you the square root of any given number")
}
if (obj.value!="" && obj.value!="0") {
aa = prompt("Do you want to find the square root of the current number in the total text box?", "y or n");
if (aa=="y") {
doit(Math.sqrt(obj.value), obj)
}
else {
a = prompt("Enter a number to find the square root of:", "");
more('(' + Math.sqrt(a) + ')', obj)
}
}
else {
a = prompt("Enter a number to find the square root of:", "");
doit(Math.sqrt(a), obj)
}
}
//fractions are cool............
function fractions(mode, obj) {
if (mode==1) {
alert("Frac is a neat function that allows you to input fractions. The script will automaticly convert the fraction you give into a decimal soit will understand it. Just make sure you know the math terms: in 2 and2 fifths, 2 is the base, the next 2 is the numerator, and 5 is the denominator. If there is no base number, leave the prompt box that asks for a base empty. Without a base you can just do simple fractions such as 2 fifths.")
}
if (obj.value!="" && obj.value!="0") {
aa = prompt("Do you want to use the number in the total textbox as the base?", "y or n");
if (aa=="y") {
a = obj.value
}
else {
a = prompt("Enter a base:", "");
}
b = prompt("Enter a numerator:", "");
c = prompt("Enter a denominator", "");
d = b/c;
e = (d*1)+(a*1);
if (aa=="y") {
doit(e, obj)
}
else {
more('(' + e + ')', obj)
}
}
else {
a = prompt("Enter a base:", "");
b = prompt("Enter a numerator:", "");
c = prompt("Enter a denominator:", "");
d = b/c;
e = (d*1)+(a*1);
doit(e, obj)
}
if (e=="nullNaN") {
more("", obj)
}
if (e=="NaN") {
more("", obj)
}
}
//change the sign of any given number
function negpos(mode, obj) {
if (mode==1) {
alert("+/- allows the you to convert a negative(-) number to a positive(+) and a positive number to a negative. And then it puts it in the total textbox with any other number that is already in there.")
}
obj.value *= (-1)
}
//clear any numbers in memory
function mc(mode, obj1, obj2) {
if (mode==1) {
alert("Memory Clear is an easy little function that erases the values of both memorys.")
}
obj1.value = '';
obj2.value = ''
}
//convert any number into scientific notation
function scie(mode, obj) {
if (mode==1) {
alert("Scie is cool function that allows you to convert any given number into scientific notation. The format of the answer is as follows: Ex: input:120, output:1.2*10^2, would be 1.2 multiplied by 10 to the second power.")
}
if (obj.value!="" && obj.value!="0") {
aa = prompt("Do you want to convert the current number in the total textbox?", "y or n");
if (aa=="y") {
a = obj.value
}
else {
a = prompt("Enter a number to convert:", "")
}
}
else {
a = prompt("Enter a number to convert:", "")
}
var b = 0;
while (a>10) { //divide by ten until variable is no longer greater han 10
a /= 10;
b += 1
}
doit(a + '*10^' + b, obj)
}
//convert any number out of scientific notation to standard form
function unscie(mode, obj) {
if (mode==1) { alert("UnScie does the exact opposite of Scie. It allows you to input number in scientific notation and it will output the number in standard notation. Ex: input:1.2*10^2, output:120")
}
a = prompt("Enter the number that is the decimal:", "");
b = prompt("Enter the power that is given to 10:", "");
c = Math.pow(10, b);
d = a*c;
if (obj.value!="" && obj.value!="0") {
e = prompt("Do you want to use this result as a part of another calculation in the total textbox?", "y or n");
if (e=="y") {
more('(' + d + ')', obj)
}
else {
doit(d, obj)
}
}
else {
doit(d, obj)
}
}
//HELP!!
function helpround() {
if (document.mainform.mode[0].checked) {
alert("Round is an easy function that simply rounds the number in the total textbox to the nearest whole number.")
}
}
function helppi () {
if (document.mainform.mode[0].checked) {
alert("PI is an easy function that just gives you PI. 3.14......")
}
}

</script>
<form name="mainform"><center>
<table border=1 cellspacing=20 cellpadding=1>
<td><table border=0 cellspacing=1 cellpadding=1>
<td><b>Scientific Notation:</b><br>
<input type="button" value="Scie" onClick="getinput(scie)">
<input type="button" value="UnScie" onClick="getinput(unscie)"></td>
<tr>
<td><b>Memory:</b><br>
<input type="button" value=" M1 " onClick="getinput('mem1')">
<input type="button" value=" M2 " onClick="getinput('mem2')">
<input type="button" value=" Rcl1 " onClick="getinput('rcl1')">
<br>
<center>
<input type="button" value=" Rcl2 " onClick="getinput('rcl2')">
<input type="button" value=" MC " onClick="getinput(mc)">
<tr>
<td><b>Special functions:</b><br>
<input type="button" value=" sin " onClick="getinput(sine)">
<input type="button" value=" cos " onClick="getinput(cosine)">
<input type="button" value=" tan " onClick="getinput(tangent)">
<br>
<input type="button" value=" log " onClick="getinput(logrythm)">
<input type="button" value=" Spec " onClick="getinput(spec)">
<input type="button" value=" PI " onClick="helppi(); more('(' + Math.PI + ')', document.mainform.total)">
</table><td>
<table border=1 cellspacing=1 cellpadding=1>
<td><b>模式:
<input name="mode" type="radio" checked>帮助
<input type="radio" name="mode">不要帮助<tr>
<td><input type="text" name="total" size=20 value="0">
<td><input type="button" value=" C " onClick="cleart(document.mainform.total)">
</table>
<table border=1 cellpadding=0 cellspacing=0>
<td><input type="button" value=" 1 " onClick="more(1, document.mainform.total)">
<td><input type="button" value=" 2 " onClick="more(2, document.mainform.total)">
<td><input type="button" value=" 3 " onClick="more(3, document.mainform.total)">
<td><input type="button" value=" ) " onClick="more(')', document.mainform.total)">
<td><input type="button" value=" + " onClick="more('+', document.mainform.total)">
<tr>
<td><input type="button" value=" 4 " onClick="more(4, document.mainform.total)">
<td><input type="button" value=" 5 " onClick="more(5, document.mainform.total)">
<td><input type="button" value=" 6 " onClick="more(6, document.mainform.total)">
<td><input type="button" value=" ( " onClick="more('(', document.mainform.total)">
<td><input type="button" value=" - " onClick="more('-', document.mainform.total)">
<tr>
<td><input type="button" value=" 7 " onClick="more(7, document.mainform.total)">
<td><input type="button" value=" 8 " onClick="more(8, document.mainform.total)">
<td><input type="button" value=" 9 " onClick="more(9, document.mainform.total)">
<td><input type="button" value=" . " onClick="more('.', document.mainform.total)">
<td><input type="button" value=" / " onClick="more('/', document.mainform.total)">
<tr>
<td><input type="button" value=" <-- " onClick="less(document.mainform.total)">
<td><input type="button" value=" 0 " onClick="more(0, document.mainform.total)">
<td><input type="button" value=" = " onClick="calc(document.mainform.total, document.mainform.total)">
<td><input type="button" value=" +/- " onClick="getinput(negpos)">
<td><input type="button" value=" * " onClick="more('*', document.mainform.total)">
</table>
<td>
<table border=1 cellspacing=1 cellpaddng=1>
<td><b>Powers:</b><br>
<input type="button" value=" ^2 " onClick="getinput(squared)">
<input type="button" value=" ^3 " onClick="getinput(cubed)">
<input type="button" value=" x^y " onClick="getinput(power)">
<tr>
<td><b>Finding "x":</b><br>
<input type="button" value="Equasion+" onClick="getinput(equa)">
<tr>
<td><b>Fractions:</b><br>
<input type="button" value="Frac" onClick="getinput(fractions)">
<tr>
<td><b>Misc:</b><br>
<input type="button" value="Sqrt" onClick="getinput(squareroot)">
<input type="button" value="Round" onClick="helpround();
doit(Math.round(document.mainform.total.value), document.mainform.total)">
</table>
</table>

<input type="button" value="About" onClick="alert('南京有线台陆晓庆制作!!')">
<input type="hidden" name="memory1">
<input type="hidden" name="memory2">



webasp.net