+
1
|
list
|
skin
|
login
|
editor
α-wwwiki
::
next_prev
user:none
(2917 bytes)
_h1 next & prev _p We define two primitive functions next() and prev() operating on any ordered set of chars, with which it will be possible to build arithmetical operators for any set of chars : _ul "0123456789" (decimal) _ul "0123456789ABCDEF" (hexadecimal) _ul "01" (binary) _ul "abcdefghijklmnopqrstuvwxyz" (alphabetical) _ul "''([])''" (why not ?) _ul "$€£¥" (maybe useful for banksters) _ul and so on ... _p Enter a set of chars in symbols field and any subset of these chars in A and B fields, to display A+B : {center {@ style="border:1px solid black; padding-top:5px;"} symbols : {input {@ id="symbols" type="text" value="0123456789"}} {br} A = {input {@ id="A" type="text" value="99" style="background:#0ff; width:100px;"}} B = {input {@ id="B" type="text" value="1" style="background:#f0f; width:100px;"}} A+B = {span {@ id="output"}???} } {input {@ type="submit" value="compute" onclick="°° var arithmetic = function () { // modulus arithmetic var symbols = ''; var getSymbols = function ( s ) { symbols = s; }; var next = function ( n ) { for (var i=n.length-1; i >= 0; i--) { var c = n.charAt(i); if (c == symbols.charAt(symbols.length-1)) n = insert( n, i, symbols.charAt( 0 ) ); else { n = insert( n, i, symbols.charAt( symbols.indexOf(c) + 1 ) ); break; } } if (n.charAt(0) == symbols.charAt(0)) n = symbols.charAt(1) + n; return n; }; var prev = function ( n ) { if (n == symbols.charAt(0)) return 0; for (var i=n.length-1; i >=0; i--) { var c = n.charAt(i); if (c == symbols.charAt(0)) n = insert( n, i, symbols.charAt( symbols.length-1 ) ); else { n = insert( n, i, symbols.charAt( symbols.indexOf(c) - 1 ) ); break; } } if (n.charAt(0) == symbols.charAt(0)) n = n.substring(1, n.length ); if (n == '') n = symbols.charAt(0); return n; }; var insert = function ( str, pos, car ) { var N = str.length; if (pos < 0) pos = 0; if (pos > N-1) pos = N-1; return str.substring( 0, pos ) + car + str.substring( pos+1, N) ; }; var add = function ( a, b ) { while ( b != symbols[0] ) { a = next( a ); b = prev( b ); } return a; }; return { getSymbols:getSymbols, prev:prev, next:next, add:add }; }(); // global object arithmetic is created getId('output').innerHTML = function () { arithmetic.getSymbols( getId('symbols').value ); var A = getId('A').value; var B = getId('B').value; return arithmetic.add( A, B ); }(); // executed when code is called by input buttons °°"}} _p Note : You are adding chars, not numbers. _h3 code _p Have a look in the editor frame. _p See also page [[jslambda]] with a clever construct of the "next/succ" operator.