Advertisement
Sax

ffsv3

Sax
Dec 3rd, 2014
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.55 KB | None | 0 0
  1. <HEAD>
  2. <style type='text/css'>
  3.  
  4. /* http://meyerweb.com/eric/tools/css/reset/
  5. v2.0 | 20110126
  6. License: none (public domain)
  7. */
  8.  
  9. html, body, div, span, applet, object, iframe,
  10. h1, h2, h3, h4, h5, h6, p, blockquote, pre,
  11. a, abbr, acronym, address, big, cite, code,
  12. del, dfn, em, img, ins, kbd, q, s, samp,
  13. small, strike, strong, sub, sup, tt, var,
  14. b, u, i, center,
  15. dl, dt, dd, ol, ul, li,
  16. fieldset, form, label, legend,
  17. table, caption, tbody, tfoot, thead, tr, th, td,
  18. article, aside, canvas, details, embed,
  19. figure, figcaption, footer, header, hgroup,
  20. menu, nav, output, ruby, section, summary,
  21. time, mark, audio, video {
  22. margin: 0;
  23. padding: 0;
  24. border: 0;
  25. font-size: 100%;
  26. font: inherit;
  27. vertical-align: baseline;
  28. }
  29. /* HTML5 display-role reset for older browsers */
  30. article, aside, details, figcaption, figure,
  31. footer, header, hgroup, menu, nav, section {
  32. display: block;
  33. }
  34. body {
  35. line-height: 1;
  36. }
  37. ol, ul {
  38. list-style: none;
  39. }
  40. blockquote, q {
  41. quotes: none;
  42. }
  43. blockquote:before, blockquote:after,
  44. q:before, q:after {
  45. content: '';
  46. content: none;
  47. }
  48. table {
  49. border-collapse: collapse;
  50. border-spacing: 0;
  51. }
  52.  
  53. body {
  54. font-family: "Chalkduster";
  55. }
  56. /*h1, h2, h3, h4, h5, h6 {
  57. display: inline;
  58. margin: 0;
  59. font-weight: normal;
  60. } */
  61.  
  62. input{
  63. font-family: "Chalkduster";
  64. font-size:0.95em;
  65. }
  66.  
  67. h3{
  68. padding: 10 0 10 0;
  69. }
  70.  
  71. td{
  72. vertical-align: middle;
  73. padding: 5px;
  74. }
  75. </style>
  76.  
  77.  
  78. <script type="text/javascript">
  79. <!--
  80. /* This script and many more are available free online at
  81. The JavaScript Source!! http://www.javascriptsource.com
  82. Created by: Matthew Roy :: http://matthewroy.com */
  83.  
  84. /*****************************************
  85. ** Matthew C. Roy
  86. ** Big Business Websites
  87. ** For Small Business Prices!
  88. ** http://www.matthewroy.com
  89. **
  90. ** You may use this script freely for
  91. ** non-commercial use as long
  92. ** as this header is left intact.
  93. *****************************************/
  94.  
  95. var n1, n2, d1, d2, An, Ad, Op;
  96. var neg=1;
  97.  
  98. function solve(){
  99. zeroing()
  100. //If all fields are numbers
  101. if(!isNaN(document.calc.n1.value)&&!isNaN(document.calc.d1.value)&&!isNaN(document.calc.n2.value)&&!isNaN(document.calc.d2.value)){
  102. //If no fields are blank
  103. if(document.calc.n1.value!=''&&document.calc.d1.value!=''&&document.calc.n2.value!=''&&document.calc.d2.value!=''){
  104. //Set variables:
  105. n1=document.calc.n1.value;// Numerator 1
  106. d1=document.calc.d1.value;// Numerator 2
  107. n2=document.calc.n2.value;// Denominator 1
  108. d2=document.calc.d2.value;// Denominator 2
  109. Op=document.calc.Op.value;// Operator
  110. } else {
  111. //If blank field
  112. alert('Please fill-in all fields!');
  113. }
  114. } else {
  115. //If field has non-number
  116. alert('Please enter only Numbers into the fields!');
  117. }
  118.  
  119. //Which Operation
  120. switch (Op){
  121. case '+':
  122. //add fractions using formula ((n1*d2)+(n2*d1)) over (d1*d2)
  123. if (d1==d2){
  124. Ad=d1; //Answer Denominator
  125. An=parseInt(n1)+parseInt(n2); //Answer Numerator
  126. }
  127. else{
  128. if (Math.max(d1,d2) % Math.min(d1,d2) == 0){
  129. Ad=Math.max(d1,d2) //Answer Denominator
  130. An=parseInt(Ad/d1*n1) + parseInt(Ad/d2*n2) //Answer Numerator
  131. }
  132. else{
  133. Ad=(d1*d2) //Answer Denominator
  134. An=parseInt(n1*d2)+parseInt(n2*d1) //Answer Numerator
  135. }
  136. }
  137. if(document.calc.reduce.checked==1){
  138. reduce();
  139. } else {
  140. display();
  141. document.calc.Anr.value = An;
  142. document.calc.Adr.value = Ad;
  143. }
  144. break
  145.  
  146. case '-':
  147. //subtract fractions using formula ((n1*d2)-(n2*d1)) over (d1*d2)
  148. if (d1==d2){
  149. Ad=d1; //Answer Denominator
  150. An=(n1-n2); //Answer Numerator
  151. }
  152. else{
  153. if (Math.max(d1,d2) % Math.min(d1,d2) == 0){
  154. Ad=Math.max(d1,d2) //Answer Denominator
  155. An=Ad/d1*n1 - Ad/d2*n2 //Answer Numerator
  156. }
  157. else{
  158. Ad=(d1*d2) //Answer Denominator
  159. An=(n1*d2)-(n2*d1) //Answer Numerator
  160. }
  161. }
  162. if(document.calc.reduce.checked==1){
  163. reduce();
  164. } else {
  165. display();
  166. document.calc.Anr.value = An;
  167. document.calc.Adr.value = Ad;
  168. }
  169. break
  170.  
  171. case '*':
  172. //multiply fractions using formula (n1*n2) over (d1*d2)
  173. An=n1*n2;//Answer Numerator
  174. Ad=d1*d2; //Answer Denominator
  175. if(document.calc.reduce.checked==1){
  176. reduce();
  177. } else {
  178. display();
  179. }
  180. break
  181.  
  182. case '/':
  183. //divide fractions using formula (n1*d2) over (d1*n2)
  184. An=n1*d2;//Answer Numerator
  185. Ad=d1*n2;//Answer Denominator
  186. if(document.calc.reduce.checked==1){
  187. reduce();
  188. } else {
  189. display();
  190. }
  191. break
  192. }
  193. }
  194.  
  195. function reduce() {
  196. neg=1; //1 if positive, -1 if negative
  197. //convert to strings
  198. ng=An+'';
  199. dg=Ad+''
  200. if(ng.indexOf('-')!=-1){ //check to see if answer is negative.
  201. neg=-1
  202. }
  203. if(dg.indexOf('-')!=-1){
  204. neg=-1
  205. }
  206. if(ng.indexOf('-')!=-1&&dg.indexOf('-')!=-1) {//if both numerator and denominator are negative the answer is positive
  207. neg=1
  208. }
  209. var factorX //highest common factor
  210.  
  211. if ( An == 0 || Ad == 0 ) {
  212. factorX=1;
  213. return;
  214. }
  215.  
  216. An = Math.abs( An );
  217. Ad = Math.abs( Ad );
  218.  
  219. var factorX = 1;
  220.  
  221. //Find common factors of Numerator and Denominator
  222. for ( var x = 2; x <= Math.min( An, Ad ); x ++ ) {
  223. var check1 = An / x;
  224. if ( check1 == Math.round( check1 ) ) {
  225. var check2 = Ad / x;
  226. if ( check2 == Math.round( check2 ) ) {
  227. factorX = x;
  228. }
  229. }
  230. }
  231.  
  232. Anr=(An/factorX)//*neg; //divide by highest common factor to reduce fraction then multiply by neg to make positive or negative
  233. Adr=Ad/factorX; //divide by highest common factor to reduce fraction
  234.  
  235. document.calc.An.value = An*neg;
  236. document.calc.Ad.value = Ad;
  237. document.calc.Anr.value = Anr*neg;
  238. document.calc.Adr.value = Adr;
  239.  
  240. }
  241.  
  242. function display(){
  243. //Display answer
  244. document.calc.An.value = An;
  245. document.calc.Ad.value = Ad;
  246. document.calc.Anr.value = An;
  247. document.calc.Adr.value = Ad;
  248. }
  249.  
  250. function zeroing(){
  251. //if denominator is 0
  252. if (document.calc.d1.value=="0" || document.calc.d2.value=="0"){
  253. alert("QU\xC9");
  254. document.calc.An.value = "x";
  255. document.calc.Ad.value = "x";
  256. document.calc.Anr.value = "x";
  257. document.calc.Adr.value = "x";
  258. }
  259. }
  260.  
  261. // -->
  262. </script>
  263. </HEAD>
  264.  
  265. <!-- STEP TWO: Copy this code into the BODY of your HTML document -->
  266.  
  267. <BODY>
  268.  
  269. <table width="250" align="center" border="0" cellspacing="0" cellpadding="1" style="background-color:#ffffff;border:1px #000000 solid;">
  270. <tr>
  271. <td align="center" valign="middle">
  272. <h3>Calculadora de Quebrados</h3>
  273. <form name="calc">
  274. <table width="150" border="0" cellspacing="0" cellpadding="5">
  275. <tr>
  276. <td style="border-bottom:4px #000000 solid;"><input type="text" onkeypress="return event.charCode >= 48 && event.charCode <= 57" size="1" name="n1" id="n1" tabindex="1"></td>
  277. <td rowspan="2" align="center" valign="middle">
  278. <select id="Op" name="Op" tabindex="3">
  279. <option value="+">+</option>
  280. <option value="-">-</option>
  281. <option value="*">x</option>
  282. <option value="/">÷</option>
  283. </select>
  284. </td>
  285. <td style="border-bottom:4px #000000 solid;"><input type="text" onkeypress="return event.charCode >= 48 && event.charCode <= 57" size="1" name="n2" id="n2" tabindex="4"></td>
  286. <td rowspan="2" align="center" valign="middle"><input type="button" value=" = "onClick="solve();" tabindex="6"></td>
  287. <td style="border-bottom:4px #000000 solid;"><input type="text" size="1" name="An" id="An" readonly="1"></td>
  288. <td rowspan="2" align="center" valign="middle">=</td>
  289. <td style="border-bottom:4px #000000 solid;"><input type="text" size="1" name="Anr" id="Anr" readonly="1"></td>
  290.  
  291. </tr>
  292. <tr>
  293. <td><input type="text" onkeypress="return event.charCode >= 48 && event.charCode <= 57" size="1" name="d1" id="d1" tabindex="2"></td>
  294. <td><input type="text" onkeypress="return event.charCode >= 48 && event.charCode <= 57" size="1" name="d2" id="d2" tabindex="5"></td>
  295. <td><input type="text" size="1" name="Ad" id="Ad" readonly="1"></td>
  296. <td><input type="text" size="1" name="Adr" id="Adr" readonly="1"></td>
  297. </tr>
  298. </table>
  299. <br><input type="checkbox" name="reduce" id="reduce" checked> Reducir
  300. </form>
  301. </td>
  302. </tr>
  303. </table>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement