js实现分数计算器的代码

js实现分数计算器的方法,js实现分数计算器的代码教程如下:

实现结果:实现分数的加减乘除

js实现代码如下:

http://www.itmop.com///hksfgfdda///0....///
<!-- jumpstart calculator
function launch(file)
{
hwnd = window.open(
file,"calc","toolbar=no,directories=no,menubar=no, width=600,height=170");
hwnd = window.open(file,"calc");
}

function intDivide( n1, n2 ){
var n = 0;
while (n1 >= n2 ){
n++;
n1 -= n2;
}
return n;
}

//--- simplify fraction
//--- determine greatest common divisor
function simplify( n1, n2 )
{
var min = ( n1 > n2 ) ? n2 : n1;
var gcd =1;

for( var i=1;i<=min; i++) if ( ( n1 % i == 0 ) && ( n2 % i ) == 0 ) gcd =i;

f = ( n1 / gcd ) + "/" + ( n2 / gcd );
return f;

}

function Calculate(){

var n = 0;
var d = 0;

var n1 = 0;
var d1 = 0;
var x1 = 0;

var f1 = document.forms["fraction"].f1.value;
var p1 = f1.indexOf("/");
var s1 = f1.indexOf(" ");


window.status ="Working...";
if ( p1 > 0 ){


if (s1 > 0){
x1 = eval( f1.substring( 0, s1 ) ); // the whole number portion
n1 = eval(f1.substring(s1, p1) );
d1 = eval( f1.substring( p1 + 1 ) );
n1 = n1 + (d1 * x1);
}
else {
n1 = eval( f1.substring( 0, p1 ) );
}
d1 = eval( f1.substring( p1 + 1 ) );
if( d1==0){
alert("Denominator CAN NOT be '0' in the first fraction!");
return false
}
} else if ( ( f1 != null ) && ( eval( f1 ) != 0 ) ){
n1 = eval( f1 );
d1 = 1;

} else {
alert("First fraction IS NOT correct!");
return false;
}

var n2 = 0;
var d2 = 0;
var x2 = 0;

var f2 = document.forms["fraction"].f2.value;
var p2 = f2.indexOf("/");
var s2 = f2.indexOf(" ");

if ( p2 > 0 ){

if (s2 > 0){
x2 = eval( f2.substring( 0, s2 ) ); // Whole number part
n2 = eval(f2.substring(s2, p2) );
d2 = eval( f2.substring( p2 + 1 ) );
n2 = n2 + (d2 * x2);
}
else {
n2 = eval( f2.substring( 0, p2 ) );
}
d2 = eval( f2.substring( p2 + 1 ) );
// d2 = eval( f2.substring( p2 + 1 ) );
if( d2==0){
alert("Denominator CAN NOT be '0' in the second fraction!");
return false
}
} else if ( ( f2 != null ) && ( eval( f2 ) != 0 )){

n2 = eval( f2 );
d2 = 1;
} else {
alert("Invalid Second Fraction");
return false;
}
if (document.forms["fraction"].f[0].checked ) {
n = n1 * d2 + n2 * d1;
d = d1 * d2;
}
else if (document.forms["fraction"].f[1].checked ) {
n = n1 * d2 - n2 * d1;
d = d1 * d2;
} else if (document.forms["fraction"].f[2].checked ) {
n = n1 * n2;
d = d1 * d2;
} else if (document.forms["fraction"].f[3].checked ) {
n = n1 * d2;
d = d1 * n2;
}

//--- Show mixed number if possible
var n3 = Math.abs( n );
var d3 = Math.abs( d );
var out = "";
var wh = 0;
var exc = false;

if ( (( n < 0 ) || ( d < 0 )) && (( n > 0 ) || ( d > 0 )) ) exc = true;
if ( n3 >= d3 ){
//--- find whole number portion
wh = intDivide( n3, d3 );
if ( exc ) wh = -wh;
out = wh.toString();
}
n3 = n3 % d3;
if ( n3 > 0 ) {
var f4 = simplify( n3, d3 );

p = f4.indexOf("/");
n = eval( f4.substring( 0, p ) );
d = eval( f4.substring( p + 1 ) );
if ( wh != 0 ) out += " ";
if ( wh == 0 && exc ) n = -n;
out += n.toString() + "/" + d.toString();
}
document.forms["fraction"].f3.value = out;
window.status = "Complete!";
return true;
}

//-->

显示效果:分数计算器在线使用

参考文章:http://tishang.net/articles/1685779607.html

快乐下载

软件屋下载

中元手游网

铁盒下载

unix时间戳

分数计算器

平均数计算器

URL编码

Unicode编码转换

robots文件在线生成

posted @ 2023-06-03 16:09  kaiser099  阅读(53)  评论(0编辑  收藏  举报