Den lördag 21 mars 2015 kl. 10:40:44 UTC+1 skrev jonas.t...@gmail.com:
> Den lördag 21 mars 2015 kl. 10:23:35 UTC+1 skrev jonas.t...@gmail.com:
> > Den lördag 21 mars 2015 kl. 10:11:20 UTC+1 skrev jonas.t...@gmail.com:
> > > Have i missed anything?
> > >
> > > /* SUBTRACT TWO VARIABLE */
> > >
> > > function naiveSub(base,arrOne,arrTwo) {
> > > document.calc.trash.value="5";
> > > for (i=0;i<length;i++){
> > > if(arrOne[i]<arrTwo[i]){arrOne[i]+=base;
> > > j=i;
> > > while (arrOne[j+1]==0) {arrOne[j+1]+=base;
> > > j++;
> > > LoopedTrue=1;
> > > }
> > > if (LoopedTrue==1){arrOne[j+1]=arrOne[j+1]-1;}
> > > j=0;LoopedTrue=0;
> > > result[i]=arrOne[i]-arrTwo[i];
> > > }
> > > if (result[i]==0) result.splice(i++, 1);
> > > }
> > >
> > > ************************************************************************
> > > The full programs so far, the real expression parser will have to wait until the operands work.
> > >
> > > <SCRIPT LANGUAGE="Javascript">
> > >
> > >
> > > /*MULTIPLY TWO VARIABLES*/
> > >
> > > function naiveMult(base,arrOne,arrTwo) {
> > >
> > > document.calc.trash.value="hello";
> > > }
> > >
> > > /* SUBTRACT TWO VARIABLE */
> > >
> > > function naiveSub(base,arrOne,arrTwo) {
> > > document.calc.trash.value="5";
> > > for (i=0;i<length;i++){
> > > if(arrOne[i]<arrTwo[i]){arrOne[i]+=base;
> > > j=i;
> > > while (arrOne[j+1]==0) {arrOne[j+1]+=base;
> > > j++;
> > > LoopedTrue=1;
> > > }
> > > if (LoopedTrue==1){arrOne[j+1]=arrOne[j+1]-1;}
> > > j=0;LoopedTrue=0;
> > > result[i]=arrOne[i]-arrTwo[i];
> > > }
> > > if (result[i]==0) result.splice(i++, 1);
> > > }
> > >
> > > /*ADD TWO VARIABLES*/
> > >
> > > 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);;
> > > }
> > >
> > > /*PREPARE STRING PAIR*/
> > >
> > > function strPrep(sign,strOne,strTwo) {
> > > document.calc.trash.value="2";
> > > if (sign=="-" && strOne<strTwo){ temp=strOne; strOne=strTwo; strTwo=temp; }
> > > arrOne=strOne.split("");
> > > arrTwo=strTwo.split("");
> > > arrOne.reverse();
> > > arrTwo.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;
> > > }
> > > }
> > >
> > > /* MULTIPLY Container*/
> > >
> > > function multWrap(sign,strEval,strBase){
> > > strOne=adder[0];
> > > strTwo=adder[1];
> > > strPrep(strOne,strTwo);
> > > naiveMult(base,arrOne,arrTwo);
> > > out=result.reverse().toString();
> > > document.calc.result.value=out;
> > > }
> > >
> > > /* SUBTRACTION Container*/
> > >
> > > function subWrap(sign,strEval,strBase){
> > > document.calc.trash.value="4";
> > > 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(strOne,strTwo);
> > > naiveAdd(base,arrOne,arrTwo);
> > > out=result.reverse().toString();
> > > document.calc.result.value=out;
> > > }
> > >
> > > /*PARSE THE STRING*/
> > >
> > > function parser(strEval,strBase){
> > > document.calc.trash.value="2";
> > > //sign="*";
> > > //multiply=strEval.split("*");
> > > //multWrap(sign,multiply,strBase)
> > > sign="-";
> > > subtract=strEval.split("-");
> > > subWrap(sign,subtract,strBase);
> > > //sign="+";
> > > //adder=strEval.split("+");
> > > //addWrapper(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 subtract = [];
> > > var adder = [];
> > > var arrOne = [];
> > > var arrTwo = [];
> > > var result = [];
> > > length; base=10;temp="";
> > >
> > > </SCRIPT>
> > >
> > > <HTML>
> > > <BODY>
> > > <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="7000-6" size="80"><br>
> > > RESULT<input type="text" name="result" value="" size="80"><br>
> > > Trash<input type="text" name="trash" value="" size="80"><br>
> > > </FORM>
> > > </BODY>
> > > </HTML>
> >
> > Well a lack of paranthese, and i forgot that to carry the borrow down with one for consecutive zeros.
> >
> > /* SUBTRACT TWO VARIABLE */
> >
> > function naiveSub(base,arrOne,arrTwo) {
> > document.calc.trash.value="5";
> > for (i=0;i<length;i++){
> > if(arrOne[i]<arrTwo[i]){arrOne[i]+=base;
> > j=i;
> > while (arrOne[j+1]==0) {arrOne[j+1]+=base-1; j++; LoopedTrue=1;}
> > }
> > if (LoopedTrue==1){arrOne[j+1]=arrOne[j+1]-1;}
> > j=0;LoopedTrue=0;
> > result[i]=arrOne[i]-arrTwo[i];
> > }
> > if (result[i]==0) result.splice(i++, 1);
> > }
>
> It seem to work now.
>
> 300010001234561789-190010001234560788=2,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1
>
> Can anyone see for which it does fail if it does?
> The sign is stored and not printed out so don't worry about it, just worry about the digits.
>
> I suspect it will work for base 2.
Well i suspect i got lucky alot of bugs. Must reset j and loop to begin with.
/* SUBTRACT TWO VARIABLE */
function naiveSub(base,arrOne,arrTwo) {
LoopedTrue=0;j=0;
document.calc.trash.value="5";
for (i=0;i<length;i++){
if(arrOne[i]<arrTwo[i]){arrOne[i]+=base;
j=i;
while (arrOne[j+1]==0) {arrOne[j+1]+=base-1; j++; LoopedTrue=1;}
}
if (LoopedTrue==1){arrOne[j+1]=arrOne[j+1]-1;}
j=0;LoopedTrue=0;
result[i]=arrOne[i]-arrTwo[i];
}
if (result[i]==0) result.splice(i++, 1);
}