Den tisdag 17 mars 2015 kl. 11:05:56 UTC+1 skrev jonas.t...@gmail.com:
> Den tisdag 17 mars 2015 kl. 09:29:20 UTC+1 skrev jonas.t...@gmail.com:
> > Den söndag 15 mars 2015 kl. 15:05:43 UTC+1 skrev jonas.t...@gmail.com:
> > > I think it is easily understood code and not to convoluted.
> > > ***digit places must be coma separated for any base***
> > > ***use decimal notation not base 64***
> > > <SCRIPT LANGUAGE="Javascript">
> > >
> > > function naiveAdd(base,arrOne,arrTwo) {
> > > if (arrOne.length>=arrTwo.length) {length=arrOne.length;} else {length=arrTwo.length;}
> > > out="";
> > > remainder=0;
> > > for (i=0;i<length;i++){
> > > one=arrOne[i];
> > > two=arrTwo[i];
> > > one=parseInt(one);
> > > two=parseInt(two);
> > > if (isNaN(one)) one = 0;
> > > if (isNaN(two)) two = 0;
> > > sum=one+two+remainder;
> > >
> > > if (sum>=base) { sum=sum-base; remainder=1;} else {remainder=0;}
> > > out=","+sum+out;
> > > }
> > > if (remainder==1) out=remainder+out;
> > > return out;
> > > }
> > > base=16;
> > > strOne="2,2";
> > > strTwo="13,13";
> > > arrOne=strOne.split(",");
> > > arrTwo=strTwo.split(",");
> > > arrOne.reverse();
> > > arrTwo.reverse();
> > > naiveAdd(base,arrOne,arrTwo);
> > > document.write("Sum = ",out,"<BR>");
> > > </SCRIPT>
> >
> > Better or worse?
> > Is there a way to get rid of leading zeros in array, i am not even sure how they get there?
> >
> > <SCRIPT LANGUAGE="Javascript">
> >
> > function naiveAdd(base,arrOne,arrTwo) {
> > remainder=0;
> > for (i=0;i<length;i++){
> > sum=arrOne[i]+arrTwo[i]+remainder;
> > if (sum>=base) { sum=sum-base; remainder=1;} else {remainder=0;}
> > result[i]=sum;
> > }
> > if (remainder==1) result[i++]=1;
> > }
> >
> > function strPrep(strOne,strTwo) {
> > out="";
> > arrOne=strOne.split(",");
> > arrTwo=strTwo.split(",");
> > arrOne.reverse();
> > arrTwo.reverse();
> > if (strOne.length>=strTwo.length) {length=strOne.length;} else {length=strTwo.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;
> > }
> > }
> >
> > /*GLOBAL*/
> > var arrOne = [];
> > var arrTwo = [];
> > var result = [];
> > length;
> >
> > /*INPUT*/
> > base=10;
> > strOne="9,1";
> > strTwo="2,0";
> >
> > /*MAIN*/
> > j=0;
> > strPrep(strOne,strTwo);
> > naiveAdd(base,arrOne,arrTwo);
> > out=result.reverse().toString();
> > document.write("Sum = ",out,"<BR>");
> > </SCRIPT>
>
>
> Oops maybe length of array would been preferable.
<SCRIPT LANGUAGE="Javascript">
function naiveAdd(base,arrOne,arrTwo) {
remainder=0;
for (i=0;i<length;i++){
result[i]=arrOne[i]+arrTwo[i]+remainder;
if (result[i]>=base) { result[i]=result[i]-base; remainder=1;}
else {remainder=0;}
}
if (remainder==1) result[i++]=1; else result.splice(i, 1);;
}
function strPrep(strOne,strTwo) {
arrOne=strOne.split(",").reverse();
arrTwo=strTwo.split(",").reverse();
if (arrOne.length>=arrTwo.length) {length=arrOne.length;} else {length=arrTwo.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;
}
}
/*GLOBAL*/
var arrOne = [];
var arrTwo = [];
var result = [];
length;
/*INPUT*/
base=2;
strOne="1,1";
strTwo="1,0";
/*MAIN*/
j=0;
strPrep(strOne,strTwo);
naiveAdd(base,arrOne,arrTwo);
out=result.reverse().toString();
document.write("Sum = ",out,"<BR>");
</SCRIPT>