Den fredag 27 mars 2015 kl. 17:42:28 UTC+1 skrev jonas.t...@gmail.com:
> I can say i had hope for less code, those checks of lenghts ain't pretty.
>
> <SCRIPT LANGUAGE="Javascript">
>
> /* MULTIPLY TWO VARIABLES */
>
> function naiveMult(base, multOne, multTwo)
> {
> document.calc.comment.value = "[Multiply] "+ multOne + sign + multTwo+" \n"
>
> total = [];
> result = [];
> total[0] = 0;
> for (var i = 0; i < lengthTwo; i ++ )
> {
> multresult = [];
> remainder = 0;
> tempVal = 0;
> tempDig = 0;
> j = 0;
>
> if(i > 0)
> {
> for(zero = 0; zero < i; zero ++ ) multresult[zero] = 0;
> }
>
> while(j < length)
> {
> tempVal = (multOne[i] * multTwo[j]) + remainder;
> remainder = 0;
>
> if (tempVal >= base)
> {
> tempDig = tempVal % base;
> remainder = (tempVal - tempDig) / base;
> multresult[j + i] = tempDig;
> }
> else
> {
> multresult[j + i] = tempVal;
> }
> j ++ ;
> }
>
> if(remainder != 0)
> {
> multresult[j + i] = remainder;
> }
> document.calc.comment.value += "\n SubSUM= " + total;
> document.calc.comment.value += "\n SubPROD= " + multresult;
> // KOLLA NOGA
> if (multresult.length >= total.length)
> {
> mlength = multresult.length;
> mlengthTwo = total.length;
> }
> else
> {
> mlength = total.length;
> mlengthTwo = multresult.length;
> }
>
> for (k = 0; k < mlength; k ++ )
> {
> if (isNaN(multresult[k])) multresult[k] = 0;
> if (isNaN(total[k])) total[k] = 0;
> }
> naiveAdd(base, mlength, total, multresult);
> total = result;
> }
> document.calc.comment.value += "\nTOTAL PRDDUKT= " + total;
> }
>
>
> /* ADD TWO VARIABLES */
>
> function naiveAdd(base, alength, addOne, addTwo)
> {
> document.calc.comment.value += "\nAdd termOne+termTwo =\n" + addOne+"\n" + addTwo + " + ";
> addResult = [];
> remainder = 0;
> for (i = 0; i < alength; i ++ )
> {
> addResult[i] = addOne[i] + addTwo[i] + remainder;
> document.calc.comment.value += "\n subAdd = " + addResult[i];
> if (addResult[i] >= base)
> {
> addResult[i] = addResult[i] - base;
> //document.calc.comment.value += "\n *IF* Addresult = " + addResult[i];
> remainder = 1;
> }
> else
> {
> remainder = 0;
> }
> }
>
> if (remainder == 1) addResult[i ++ ] = 1;
> else addResult.splice(i, 1);
> result = addResult;
> document.calc.comment.value += "\n SUBSUM= " + addResult;
> return result;
> }
>
>
>
>
> /* SUBTRACT TWO VARIABLES */
>
> function naiveSub(base, subOne, subTwo)
> {
> result = [];
> for (i = 0; i < length; i ++ )
> {
> j = 0;
>
> if (subOne[i] < subTwo[i])
> {
> subOne[i] += base;
> j = i;
> while (subOne[j + 1] == 0)
> {
> subOne[j + 1] += base - 1;
> j ++ ;
>
> }
> subOne[j + 1] += - 1;
> }
> j = 0;
> result[i] = subOne[i] - subTwo[i];
>
> }
>
> while (result[length - 1] == 0)
> {
> result.splice(length - 1, 1);
> length += - 1;
> }
> }
>
>
>
>
>
> /* PREPARE STRING PAIR */
>
> function strPrep(sign, strOne, strTwo)
> {
>
> arrOne = strOne.split("");
> arrTwo = strTwo.split("");
> arrOne.reverse();
> arrTwo.reverse();
> if (arrOne.length >= arrTwo.length)
> {
> length = arrOne.length;
> lengthTwo = arrTwo.length;
> }
> else
> {
> length = arrTwo.length;
> lengthTwo = arrOne.length;
> }
> for (i = 0; i < length; i ++ )
> {
> arrOne[i] = parseInt(arrOne[i]);
> arrTwo[i] = parseInt(arrTwo[i]);
> if (isNaN(arrOne[i])) arrOne[i] = 0;
> if (isNaN(arrTwo[i])) arrTwo[i] = 0;
> }
> }
>
> /* MULTIPLY Container */
>
> function multWrap(sign, multiply, strBase)
> {
> tONE = parseInt(multiply[0]);
> tTWO = parseInt(multiply[1]);
> if(tONE <= tTWO)
> {
> arrOne = multiply[0];
> arrTwo = multiply[1];
> }
> else
> {
> arrOne = multiply[1];
> arrTwo = multiply[0];
> }
> strPrep(sign, arrOne, arrTwo);
> naiveMult(base, arrOne, arrTwo);
> document.calc.msum.value = sum;
> out = total.reverse().toString();
> document.calc.result.value = out;
> }
>
> /* SUBTRACTION Container */
>
> function subWrap(sign, strEval, strBase)
> {
> tONE = parseInt(subtract[0]);
> tTWO = parseInt(subtract[1]);
> if(tONE < tTWO)
> {
> strOne = subtract[1];
> strTwo = subtract[0];
> }
> else
> {
> strOne = subtract[0];
> strTwo = subtract[1];
> }
>
>
> strPrep(sign, strOne, strTwo);
> naiveSub(base, arrOne, arrTwo);
> out = result.reverse().toString();
> document.calc.result.value = out;
> }
>
> /* ADDITION Container */
>
> function addWrap(sign, strEval, strBase)
> {
> strOne = adder[0];
> strTwo = adder[1];
> strPrep(sign, strOne, strTwo);
> naiveAdd(base, length, arrOne, arrTwo);
> out = result.reverse().toString();
> document.calc.result.value = out;
> }
>
> /* PARSE THE STRING */
>
> function parser(strEval, strBase)
> {
>
> sign = "*";
> multiply = strEval.split("*");
> // document.calc.firstval.value = multiply[0];
> // document.calc.secval.value = multiply[1];
> multWrap(sign, multiply, strBase)
> document.calc.tO.value = strOne;
> // document.calc.tT.value = strTwo;
> // sign = "-";
> // subtract = strEval.split("-");
> // subWrap(sign, subtract, strBase);
> //sign = "+";
> //adder = strEval.split("+");
> //addWrap(sign, adder, strBase);
> }
>
> /* [MAIN PROGRAM] */
> /* [TAKE INPUT FROM FORM] [PARSE THE STRING] [CALCULATE RESULT] */
> /* [GENERIC CALCULATIONS] [USING BIGNUMB OPERATORS] [IN ANYBASE] [UPON OPERANDS] */
>
> function fetchValues()
> {
>
> strEval = document.calc.eval.value;
> strBase = document.calc.formbase.value;
> document.calc.trash.value = "1";
> base = parseInt(strBase);
> parser(strEval, strBase);
>
>
> }
>
> /* GLOBAL */
> var multiply = [];
> var subtract = [];
> var adder = [];
> var arrOne = [];
> var arrTwo = [];
> // var addOne = [];
> // var addTwo = [];
> // var subOne = [];
> // var subTwo = [];
> var total = [];
> var result = [];
> var sum = [];
>
> length = 0;
> strOne = "";
> strTwo = "";
> base = 10;
> temp = "";
>
> </SCRIPT>
>
> <HTML>
> <body bgcolor="gold" onload="fetchValues()">
> <FORM name=calc onSubmit="fetchValues(); return false;">
> BASE <input type="text" name="formbase" value="10" size="4"><br>
> <input type=submit name="calc" value="Calc"><input type="text" name="eval" value="2213213219382198348348338*2382828248324834832" size="80"><br>
> RESULT<input type="text" name="result" value="" size="80"><br>
> Trash<input type="text" name="trash" value="" size="80"> <br>
> FIRSTVAL<input type="text" name="firstval" value="" size="80"><br>
> SECVAL<input type="text" name="secval" value="" size="80"><br>
> tONE Small<input type="text" name="tO" value="" size="80"><br>
> tTWO Big<input type="text" name="tT" value="" size="80"><br>
> Sum<input type="text" name="msum" value="" size="80"><br>
> <textarea name="comment" cols="80" rows="20" value=""></textarea>
> </FORM>
> </BODY>
> </HTML>
<SCRIPT LANGUAGE="Javascript">
/* BASE CHANGER */
function newBase(number, base)
{
number = parseInt(number);
document.calc.expand.value += "BASE = " + 10 + " NUMBER = " + number + "\n";
basestr = "";
basemultip = 1;
digit = 1;
while (basemultip < number)
{
basemultip = basemultip * base;
digit = digit + 1;
}
while (number > 0 && digit > 0)
{
i = 0;
while (i * basemultip <= number && i < base)
{
j = i;
i ++ ;
}
if (i > 0)
{
subtrahend = basemultip * j;
number = number - subtrahend;
basestr = basestr + j + ",";
}
else
{
basestr = basestr + 0 + ",";
}
basemultip = basemultip / base;
digit -- ;
}
while (digit > 0)
{
basestr = basestr + 0 + ",";
digit -- ;
}
basestr = basestr.substring(0, basestr.length - 1);
newarr = basestr.split(",");
i = 0;
while (newarr[i] == 0)
{
newarr.shift();
}
sanityCheck(base,newarr);
document.calc.expand.value += "BASE = " + base + " NUMBER = " + newarr + " <-Array length = " + newarr.length + "\n";
return newarr;
}
/* MULTIPLY TWO VARIABLES */
function naiveMult(base, multOne, multTwo)
{
document.calc.comment.value += "[Multiply][BASE] = " + base + " [TERMS] " + multOne + sign + multTwo + " \n"
document.calc.comment.value += "FACTOR ONE " + lengthTwo + " digits \n"
document.calc.comment.value += "FACTOR TWO " + lengthOne + " digits \n"
total = [];
result = [];
total[0] = 0;
for (var i = 0; i < lengthTwo; i ++ )
{
multresult = [];
remainder = 0;
tempVal = 0;
tempDig = 0;
j = 0;
if(i > 0)
{
for(zero = 0; zero < i; zero ++ ) multresult[zero] = 0;
}
while(j < lengthOne)
{
intmult ++ ;
tempVal = (multOne[i] * multTwo[j]) + remainder;
remainder = 0;
if (tempVal >= base)
{
tempDig = tempVal % base;
remainder = (tempVal - tempDig) / base;
multresult[j + i] = tempDig;
}
else
{
multresult[j + i] = tempVal;
}
j ++ ;
}
if(remainder != 0)
{
multresult[j + i] = remainder;
}
// document.calc.comment.value += "\n SubSUM= " + total;
// document.calc.comment.value += "\n SubPROD= " + multresult + "\n";
// KOLLA NOGA
if (multresult.length >= total.length)
{
mlength = multresult.length;
mlengthTwo = total.length;
}
else
{
mlength = total.length;
mlengthTwo = multresult.length;
}
for (k = 0; k < mlength; k ++ )
{
if (isNaN(multresult[k])) multresult[k] = 0;
if (isNaN(total[k])) total[k] = 0;
}
naiveAdd(base, mlength, total, multresult);
total = result;
}
}
/* ADD TWO VARIABLES */
function naiveAdd(base, alength, addOne, addTwo)
{
// document.calc.comment.value += "\nAdd termOne+termTwo =\n" + addOne + "\n" + addTwo + " + ";
addResult = [];
remainder = 0;
for (i = 0; i < alength; i ++ )
{
intadd ++ ;
addResult[i] = addOne[i] + addTwo[i] + remainder;
// document.calc.comment.value += "\n subAdd = " + addResult[i];
if (addResult[i] >= base)
{
addResult[i] = addResult[i] - base;
remainder = 1;
}
else
{
remainder = 0;
}
// document.calc.comment.value += " DigitValue = " + addResult[i];
// document.calc.comment.value += " carryDigit = " + remainder;
}
if (remainder == 1) addResult[i ++ ] = 1;
else addResult.splice(i, 1);
result = addResult;
return result;
}
/* SUBTRACT TWO VARIABLES */
function naiveSub(base, subOne, subTwo)
{
result = [];
for (i = 0; i < lengthOne; i ++ )
{
j = 0;
if (subOne[i] < subTwo[i])
{
subOne[i] += base;
j = i;
while (subOne[j + 1] == 0)
{
subOne[j + 1] += base - 1;
j ++ ;
}
subOne[j + 1] += - 1;
}
j = 0;
result[i] = subOne[i] - subTwo[i];
}
while (result[lengthOne - 1] == 0)
{
result.splice(lengthOne - 1, 1);
lengthOne += - 1;
}
}
/* PREPARE STRING PAIR */
function strPrep(sign, arrOne, arrTwo)
{
arrOne.reverse();
arrTwo.reverse();
// document.calc.newbase.value += "arrOne " + arrOne + " \n"
if (arrOne.length >= arrTwo.length)
{
lengthOne = arrOne.length;
lengthTwo = arrTwo.length;
}
else
{
lengthOne = arrTwo.length;
lengthTwo = arrOne.length;
}
for (i = 0; i < lengthOne; i ++ )
{
arrOne[i] = parseInt(arrOne[i]);
arrTwo[i] = parseInt(arrTwo[i]);
if (isNaN(arrOne[i])) arrOne[i] = 0;
if (isNaN(arrTwo[i])) arrTwo[i] = 0;
}
}
/* MULTIPLY Container */
function multWrap(sign, arrOne, arrTwo, base)
{
strPrep(sign, arrOne, arrTwo);
naiveMult(base, arrOne, arrTwo);
document.calc.comment.value += sum;
out = total.reverse();
sanityCheck(base,out);
document.calc.comment.value += "Integer ADD operations " + intadd + " \n"
document.calc.comment.value += "Integer MULT operations " + intmult + " \n"
document.calc.comment.value += "Product = " + out;
}
/* SUBTRACTION Container */
function subWrap(sign, strEval, strBase)
{
tONE = parseInt(subtract[0]);
tTWO = parseInt(subtract[1]);
if(tONE < tTWO)
{
strOne = subtract[1];
strTwo = subtract[0];
}
else
{
strOne = subtract[0];
strTwo = subtract[1];
}
strPrep(sign, strOne, strTwo);
naiveSub(base, arrOne, arrTwo);
out = result.reverse().toString();
// document.calc.result.value = out;
}
/* ADDITION Container */
function addWrap(sign, strEval, strBase)
{
strOne = adder[0];
strTwo = adder[1];
strPrep(sign, strOne, strTwo);
naiveAdd(base, lengthOne, arrOne, arrTwo);
out = result.reverse().toString();
// document.calc.result.value = out;
}
/* PARSE THE STRING */
function parser(strEval, strBase)
{
sign = "*";
document.calc.comment.value = "";
document.calc.expand.value = "";
genArr = strEval.split("*");
arrOne = newBase(genArr[0], base);
arrTwo = newBase(genArr[1], base);
document.calc.newbase.value = arrOne + sign + arrTwo;
temp = document.calc.newbase.value;
multWrap(sign, arrOne, arrTwo, base);
// document.calc.firstval.value = multiply[0];
// document.calc.secval.value = multiply[1];
// document.calc.tO.value = strOne;
// document.calc.tT.value = strTwo;
// sign = "-";
// subtract = strEval.split("-");
// subWrap(sign, subtract, strBase);
// sign = "+";
// adder = strEval.split("+");
// addWrap(sign, adder, strBase);
}
/* [MAIN PROGRAM] */
/* [TAKE INPUT FROM FORM] [PARSE THE STRING] [CALCULATE RESULT] */
/* [GENERIC CALCULATIONS] [USING BIGNUMB OPERATORS] [IN ANYBASE] [UPON OPERANDS] */
function sanityCheck(bmult, input)
{ totsane=0;
document.calc.expand.value += "[SANITY CHECK] BASE "+bmult+" \n";
btemp=1;
for(x = input.length-1;x >=0; x--)
{
sane = btemp * input[x];
document.calc.expand.value += "PartProd = "+btemp+"*"+input[x]+"="+ sane + " \n";
totsane+=sane
btemp=bmult*btemp;
}
document.calc.expand.value += "TotProd = "+totsane+" \n";
}
function fetchValues()
{
intmult = 0;
intadd = 0;
strEval = document.calc.eval.value;
strBase = document.calc.formbase.value;
base = parseInt(strBase);
parser(strEval, base);
}
/* GLOBAL */
var multiply = [];
var subtract = [];
var adder = [];
var arrOne = [];
var arrTwo = [];
var total = [];
var result = [];
var sum = [];
lengthOne = 0;
strOne = "";
strTwo = "";
base = 10;
temp = "";
sign = "";
</SCRIPT>
<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"/></head>
<body bgcolor="gold" onload="fetchValues()">
<H1>Big numbers</H1><P>
<FORM name=calc onSubmit="fetchValues(); return false;">
<B>
INPUT DEC-><input type=submit name="calc" value="Calc"> <input type="text" name="eval" value="12345*123456" size="60"><br>
BASE <input type="text" name="formbase" value="10" size="10"><br>
This calculation<input type="text" name="newbase" value="" size="60"><br>
<FONT COLOR="white">
EVAL STATS.<br>
<textarea name="expand" rows="20" cols="120"></textarea><br>
<!--
RESULT<input type="text" name="result" value="" size="80"><br>
FIRSTVAL<input type="text" name="firstval" value="" size="80"><br>
SECVAL<input type="text" name="secval" value="" size="80"><br>
tONE Small<input type="text" name="tO" value="" size="80"><br>
tTWO Big<input type="text" name="tT" value="" size="80"><br>
Sum<input type="text" name="msum" value="" size="80"><br>
-->
RESULT</B><br>
<textarea name="comment" cols="120" rows="40" value=""></textarea>
</FORM>
</body>
</html>