Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title></title>
  4. <style>
  5. #display {
  6. background-color: #c0ffc0;
  7. display: block;
  8. width: 10cm;
  9. text-align: right;
  10. font-size: 24px;
  11. }
  12. button {
  13. background-color: #c0c0c0;
  14. color: #404040
  15. border: 3px outset #c0c0c0;
  16. }
  17. button.selected {
  18. border: 2px inset #8080ff;
  19. background-color: #c0c0ff;
  20. color: black;
  21. font-weight: bold;
  22. }
  23. input[type=number] {
  24. text-align: right;
  25. }
  26. </style>
  27. <script>
  28. let precision = 10;
  29. let inputType = 'b9';
  30. let display = '';
  31. let displayEl;
  32. let stackTbl;
  33. let stack = [];
  34. window.addEventListener('load', (e) => {
  35. const inputTypeEl = document.querySelector('#inputType');
  36. const precisionEl = document.querySelector('#precision');
  37. stackTbl = document.querySelector('#stack-tbl>tbody');
  38. displayEl = document.querySelector('#display');
  39. inputTypeEl.addEventListener('input',
  40. () => {
  41. inputType = inputTypeEl.value
  42. });
  43. precisionEl.addEventListener('input',
  44. () => {
  45. precision = parseInt(precisionEl.value)
  46. refreshStack();
  47. });
  48. });
  49.  
  50. window.addEventListener('keypress', (e) => {
  51. console.log(e);
  52. console.log([display, precision, inputType]);
  53. if(inputType === 'b9'){
  54. if(/^[1-9]$/.test(e.key)){
  55. display += e.key;
  56. }
  57. }else if(inputType === 'dec'){
  58. if(/^[0-9]$/.test(e.key)){
  59. display += e.key;
  60. }
  61. }
  62. if(e.key === '.' && (display.indexOf('.') === -1)){
  63. display += '.';
  64. }
  65. if(e.key === '-' && display){
  66. if(display[0] === '-'){
  67. display = display.substr(1);
  68. }else{
  69. display = '-' + display;
  70. }
  71. }
  72. displayEl.innerText = display;
  73. if(e.key.toLowerCase() === 'enter'){
  74. stack.push(from_base(display, inputType === 'b9' ? 9 : 10));
  75. console.log(stack);
  76. refreshStack();
  77. }
  78. });
  79. function from_base(s, base){
  80. let i1 = 0;
  81. let r1 = 0;
  82. let r2 = 0;
  83. if(s[0] === '-'){
  84. return -from_base(s.substr(1), base);
  85. }
  86. // compute the integral part
  87. for(i1 = 0; i1 < s.length && s[i1] !== '.'; ++i1){
  88. r1 = base * r1 + Number(s[i1]);
  89. }
  90. // compute the fractionary part
  91. for(i2 = s.length-1; i2 > i1; --i1){
  92. r2 = (r2 + Number(s[i2])) / base;
  93. }
  94. return r1 + r2;
  95. }
  96.  
  97. function refreshStack(){
  98. while(stackTbl.rows.length){
  99. stackTbl.rows.deleteRow(0);
  100. }
  101. for(let i = 0; i < stack.length; ++i){
  102. let row = stackTbl.insertRow(i);
  103. row.insertCell().textContent = i;
  104. row.insertCell().textContent = to_decimal(stack[i], precision);
  105. row.insertCell().textContent = to_base9(stack[i], precision);
  106. }
  107. }
  108.  
  109. function to_base9(n, nplaces){
  110. let s = '';
  111. for(let i1 = 0; i1 < nplaces; ++i1){
  112. n = n * 9;
  113. }
  114. n = Math.round(n);
  115. while(n >= 1 || s.length < nplaces + 2){
  116. const t = Math.floor((n-1)/9);
  117. const r = n - 9*t;
  118. n = t;
  119. s = r.toString() + s;
  120. if(s.length == nplaces){
  121. s = '.' + s;
  122. }
  123. }
  124. return s;
  125. }
  126.  
  127.  
  128. function to_decimal(n, nplaces){
  129. let s = '';
  130. for(let i1 = 0; i1 < nplaces; ++i1){
  131. n = n * 10;
  132. }
  133. n = Math.round(n);
  134. while(n >= 1 || s.length < nplaces + 2){
  135. const t = Math.floor(n/10);
  136. const r = n - 10*t;
  137. n = t;
  138. s = r.toString() + s;
  139. if(s.length == nplaces){
  140. s = '.' + s;
  141. }
  142. }
  143. return s;
  144. }
  145. </script>
  146. </head>
  147. <body>
  148. <form>
  149. <label>Input format
  150. <select id="inputType">
  151. <option value="b9">Special Base 9 (1-9)</option>
  152. <option value="dec">Standard decimal</option>
  153. </select>
  154. </label>
  155. <br>
  156. <label>Output precision
  157. <input id="precision" type="number" min="0" max="15" step="1" value="10">
  158. </label>
  159. <hr>
  160. <div name="display" id="display">&nbsp;</div>
  161. <table id="stack-tbl" width="100%">
  162. <thead>
  163. <tr><td>position</td><td>Decimal</td><td>Special base 9</td></td>
  164. </thead>
  165. <tbody>
  166. </tbody>
  167. </table>
  168. </form>
  169. </body>
  170.  
  171. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement