Advertisement
Sax

ffs

Sax
Dec 3rd, 2014
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 7.11 KB | None | 0 0
  1. <HEAD>
  2.  <style type='text/css'>
  3.             body {
  4.                 font-family: "Chalkduster";
  5.             }
  6.             h1, h2, h3, h4, h5, h6 {
  7.               display: inline;
  8.               margin: 0;
  9.               font-weight: normal;
  10.             }
  11.             input
  12. {
  13.   font-family: "Chalkduster";
  14.   font-size:0.95em;
  15. }
  16.  
  17.         </style>
  18.  
  19.  
  20. <script type="text/javascript">
  21. <!--
  22. /* This script and many more are available free online at
  23. The JavaScript Source!! http://www.javascriptsource.com
  24. Created by: Matthew Roy :: http://matthewroy.com */
  25.  
  26. /*****************************************
  27. **   Matthew C. Roy
  28. **   Big Business Websites
  29. **   For Small Business Prices!
  30. **   http://www.matthewroy.com
  31. **
  32. **   You may use this script freely for
  33. **   non-commercial use as long
  34. **   as this header is left intact.
  35. *****************************************/
  36.  
  37. var n1, n2, d1, d2, An, Ad, Op;
  38. var int
  39. var neg=1;
  40.  
  41. function solve(){
  42.  //If all fields are numbers
  43.  if(!isNaN(document.calc.n1.value)&&!isNaN(document.calc.d1.value)&&!isNaN(document.calc.n2.value)&&!isNaN(document.calc.d2.value)){
  44.  //If no fields are blank
  45.  if(document.calc.n1.value!=''&&document.calc.d1.value!=''&&document.calc.n2.value!=''&&document.calc.d2.value!=''){
  46.    //Set variables:
  47.    n1=document.calc.n1.value;// Numerator 1
  48.    d1=document.calc.d1.value;// Numerator 2
  49.    n2=document.calc.n2.value;// Denominator 1
  50.    d2=document.calc.d2.value;// Denominator 2
  51.    Op=document.calc.Op.value;// Operator
  52.    } else {
  53.    //If blank field
  54.    alert('Please fill-in all fields!');
  55.    }
  56.  } else {
  57.  //If field has non-number
  58.  alert('Please enter only Numbers into the fields!');
  59.  }
  60.  
  61.  //Which Operation
  62.  switch (Op){
  63.  case '+':
  64.    //add fractions using formula ((n1*d2)+(n2*d1)) over (d1*d2)
  65.    if (d1==d2){
  66.      Ad=d1; //Answer Denominator
  67.      An=parseInt(n1)+parseInt(n2); //Answer Numerator
  68.    }
  69.    else{
  70.      if (Math.max(d1,d2) % Math.min(d1,d2) == 0){
  71.        Ad=Math.max(d1,d2) //Answer Denominator
  72.        An=parseInt(Ad/d1*n1) + parseInt(Ad/d2*n2) //Answer Numerator
  73.      }
  74.      else{
  75.        Ad=(d1*d2) //Answer Denominator
  76.        An=parseInt(n1*d2)+parseInt(n2*d1) //Answer Numerator
  77.      }
  78.    }
  79.    if(document.calc.reduce.checked==1){
  80.      reduce();
  81.    } else {
  82.      display();
  83.      document.calc.Anr.value = An;
  84.      document.calc.Adr.value = Ad;
  85.    }
  86.   break
  87.  
  88.  case '-':
  89.    //subtract fractions using formula ((n1*d2)-(n2*d1)) over (d1*d2)
  90.    if (d1==d2){
  91.      Ad=d1; //Answer Denominator
  92.      An=(n1-n2); //Answer Numerator
  93.    }
  94.    else{
  95.      if (Math.max(d1,d2) % Math.min(d1,d2) == 0){
  96.        Ad=Math.max(d1,d2) //Answer Denominator
  97.        An=Ad/d1*n1 - Ad/d2*n2 //Answer Numerator
  98.      }
  99.      else{
  100.        Ad=(d1*d2) //Answer Denominator
  101.        An=(n1*d2)-(n2*d1) //Answer Numerator
  102.      }
  103.    }
  104.    if(document.calc.reduce.checked==1){
  105.      reduce();
  106.    } else {
  107.      display();
  108.      document.calc.Anr.value = An;
  109.      document.calc.Adr.value = Ad;
  110.    }
  111.   break
  112.  
  113.  case '*':
  114.    //multiply fractions using formula (n1*n2) over (d1*d2)
  115.    An=n1*n2;//Answer Numerator
  116.    Ad=d1*d2; //Answer Denominator
  117.    if(document.calc.reduce.checked==1){
  118.            reduce();
  119.    } else {
  120.      display();
  121.    }
  122.    break
  123.  
  124.  case '/':
  125.    //divide fractions using formula (n1*d2) over (d1*n2)
  126.    An=n1*d2;//Answer Numerator
  127.    Ad=d1*n2;//Answer Denominator
  128.    if(document.calc.reduce.checked==1){
  129.      reduce();
  130.    } else {
  131.      display();
  132.    }
  133.   break
  134.  }
  135. }
  136.  
  137. function reduce() {
  138.  neg=1; //1 if positive, -1 if negative
  139.  //convert to strings
  140.  ng=An+'';
  141.  dg=Ad+''
  142.  if(ng.indexOf('-')!=-1){  //check to see if answer is negative.
  143.    neg=-1
  144.  }
  145.  if(dg.indexOf('-')!=-1){
  146.    neg=-1
  147.  }
  148.  if(ng.indexOf('-')!=-1&&dg.indexOf('-')!=-1)  {//if both numerator and denominator are negative the answer is positive
  149.    neg=1
  150.  }
  151.  var factorX //highest common factor
  152.  
  153.  if ( An == 0 || Ad == 0 ) {
  154.    factorX=1;
  155.    return;
  156.  }
  157.  
  158.  An = Math.abs( An );
  159.  Ad = Math.abs( Ad );
  160.  
  161.  var factorX = 1;
  162.  
  163.  //Find common factors of Numerator and Denominator
  164.  for ( var x = 2; x <= Math.min( An, Ad ); x ++ ) {
  165.    var check1 = An / x;
  166.    if ( check1 == Math.round( check1 ) ) {
  167.      var check2 = Ad / x;
  168.      if ( check2 == Math.round( check2 ) ) {
  169.        factorX = x;
  170.      }
  171.    }
  172.  }
  173.  
  174.  Anr=(An/factorX)//*neg;  //divide by highest common factor to reduce fraction then multiply by neg to make positive or negative
  175.  Adr=Ad/factorX;  //divide by highest common factor to reduce fraction
  176.  
  177.  document.calc.An.value = An*neg;
  178.  document.calc.Ad.value = Ad;
  179.  document.calc.Anr.value = Anr*neg;
  180.  document.calc.Adr.value = Adr;
  181.  
  182. }
  183.  
  184. function display(){
  185.  //Display answer
  186.  document.calc.An.value = An;
  187.  document.calc.Ad.value = Ad;
  188.  document.calc.Anr.value = An;
  189.  document.calc.Adr.value = Ad;
  190. }
  191.  
  192. // -->
  193. </script>
  194. </HEAD>
  195.  
  196. <!-- STEP TWO: Copy this code into the BODY of your HTML document  -->
  197.  
  198. <BODY>
  199.  
  200. <table width="250" align="center" border="0" cellspacing="0" cellpadding="1" style="background-color:#ffffff;border:1px #000000 solid;">
  201.   <tr>
  202.     <td align="center" valign="middle">
  203.       <h3>Calculadora de Quebrados</h4>
  204.       <form name="calc">
  205.         <table width="150" border="0" cellspacing="0" cellpadding="5">
  206.           <tr>
  207.             <td style="border-bottom:4px #000000 solid;"><input type="text" onkeypress="return event.charCode >= 49 && event.charCode <= 57" size="1"  name="n1" id="n1" tabindex="1"></td>
  208.            <td rowspan="2" align="center" valign="middle">
  209.              <select name="Op" tabindex="3">
  210.              <option value="+">+</option>
  211.              <option value="-">-</option>
  212.              <option value="*">x</option>
  213.              <option value="/">÷</option>
  214.              </select>
  215.            </td>
  216.            <td style="border-bottom:4px #000000 solid;"><input type="text" onkeypress="return event.charCode >= 49 && event.charCode <= 57" size="1" name="n2" id="n2" tabindex="4"></td>
  217.            <td rowspan="2" align="center" valign="middle"><input type="button" value=" = "onClick="solve();" tabindex="6"></td>
  218.             <td style="border-bottom:4px #000000 solid;"><input type="text" size="1" name="An" id="An" readonly="1"></td>
  219.             <td rowspan="2" align="center" valign="middle">=</td>
  220.             <td style="border-bottom:4px #000000 solid;"><input type="text" size="1" name="Anr" id="Anr" readonly="1"></td>
  221.  
  222.           </tr>
  223.           <tr>
  224.             <td><input type="text" onkeypress="return event.charCode >= 49 && event.charCode <= 57" size="1" name="d1" id="d1" tabindex="2"></td>
  225.            <td><input type="text" onkeypress="return event.charCode >= 49 && event.charCode <= 57" size="1" name="d2" id="d2" tabindex="5"></td>
  226.            <td><input type="text" size="1" name="Ad" id="Ad" readonly="1"></td>
  227.            <td><input type="text" size="1" name="Adr" id="Adr" readonly="1"></td>
  228.        </tr>
  229.        </table>
  230.      <br><input type="checkbox" name="reduce" id="reduce" checked> Reducir
  231.    </form>
  232.    </td>
  233.  </tr>
  234. </table>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement