Advertisement
Sax

Untitled

Sax
Dec 3rd, 2014
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     }
  84.    break
  85.  
  86.   case '-':
  87.     //subtract fractions using formula ((n1*d2)-(n2*d1)) over (d1*d2)
  88.     if (d1==d2){
  89.       Ad=d1; //Answer Denominator
  90.       An=(n1-n2); //Answer Numerator
  91.     }
  92.     else{
  93.       if (Math.max(d1,d2) % Math.min(d1,d2) == 0){
  94.         Ad=Math.max(d1,d2) //Answer Denominator
  95.         An=Ad/d1*n1 - Ad/d2*n2 //Answer Numerator
  96.       }
  97.       else{
  98.         Ad=(d1*d2) //Answer Denominator
  99.         An=(n1*d2)-(n2*d1) //Answer Numerator
  100.       }
  101.     }
  102.     if(document.calc.reduce.checked==1){
  103.       reduce();
  104.     } else {
  105.       display();
  106.     }
  107.    break
  108.  
  109.   case '*':
  110.     //multiply fractions using formula (n1*n2) over (d1*d2)
  111.     An=n1*n2;//Answer Numerator
  112.     Ad=d1*d2; //Answer Denominator
  113.     if(document.calc.reduce.checked==1){
  114.             reduce();
  115.     } else {
  116.       display();
  117.     }
  118.     break
  119.  
  120.   case '/':
  121.     //divide fractions using formula (n1*d2) over (d1*n2)
  122.     An=n1*d2;//Answer Numerator
  123.     Ad=d1*n2;//Answer Denominator
  124.     if(document.calc.reduce.checked==1){
  125.       reduce();
  126.     } else {
  127.       display();
  128.     }
  129.    break
  130.   }
  131. }
  132.  
  133. function reduce() {
  134.   neg=1; //1 if positive, -1 if negative
  135.   //convert to strings
  136.   ng=An+'';
  137.   dg=Ad+''
  138.   if(ng.indexOf('-')!=-1){  //check to see if answer is negative.
  139.     neg=-1
  140.   }
  141.   if(dg.indexOf('-')!=-1){
  142.     neg=-1
  143.   }
  144.   if(ng.indexOf('-')!=-1&&dg.indexOf('-')!=-1)  {//if both numerator and denominator are negative the answer is positive
  145.     neg=1
  146.   }
  147.   var factorX //highest common factor
  148.  
  149.   if ( An == 0 || Ad == 0 ) {
  150.     factorX=1;
  151.     return;
  152.   }
  153.  
  154.   An = Math.abs( An );
  155.   Ad = Math.abs( Ad );
  156.  
  157.   var factorX = 1;
  158.  
  159.   //Find common factors of Numerator and Denominator
  160.   for ( var x = 2; x <= Math.min( An, Ad ); x ++ ) {
  161.     var check1 = An / x;
  162.     if ( check1 == Math.round( check1 ) ) {
  163.       var check2 = Ad / x;
  164.       if ( check2 == Math.round( check2 ) ) {
  165.         factorX = x;
  166.       }
  167.     }
  168.   }
  169.  
  170.   An=(An/factorX)*neg;  //divide by highest common factor to reduce fraction then multiply by neg to make positive or negative
  171.   Ad=Ad/factorX;  //divide by highest common factor to reduce fraction
  172.   display();
  173. }
  174.  
  175. function display(){
  176.   //Display answer
  177.   document.calc.An.value = An;
  178.   document.calc.Ad.value = Ad;
  179. }
  180.  
  181. // -->
  182. </script>
  183. </HEAD>
  184.  
  185. <!-- STEP TWO: Copy this code into the BODY of your HTML document  -->
  186.  
  187. <BODY>
  188.  
  189. <table width="250" align="center" border="0" cellspacing="0" cellpadding="1" style="background-color:#ffffff;border:1px #000000 solid;">
  190.   <tr>
  191.     <td align="center" valign="middle">
  192.       <h3>Calculadora de Quebrados</h4>
  193.       <form name="calc">
  194.         <table width="150" border="0" cellspacing="0" cellpadding="5">
  195.           <tr>
  196.             <td style="border-bottom:4px #000000 solid;"><input type="text" size="1"  name="n1" id="n1" tabindex="1"></td>
  197.             <td rowspan="2" align="center" valign="middle">
  198.               <select name="Op" tabindex="3">
  199.               <option value="+">+</option>
  200.               <option value="-">-</option>
  201.               <option value="*">x</option>
  202.               <option value="/">÷</option>
  203.               </select>
  204.             </td>
  205.             <td style="border-bottom:4px #000000 solid;"><input type="text" size="1" name="n2" id="n2" tabindex="4"></td>
  206.             <td rowspan="2" align="center" valign="middle"><input type="button" value=" = "onClick="solve();" tabindex="6"></td>
  207.             <td style="border-bottom:4px #000000 solid;"><input type="text" size="1" name="An" id="An" readonly="1"></td>
  208.           </tr>
  209.           <tr>
  210.             <td><input type="text" size="1" name="d1" id="d1" tabindex="2"></td>
  211.             <td><input type="text" size="1" name="d2" id="d2" tabindex="5"></td>
  212.             <td><input type="text" size="1" name="Ad" id="Ad" readonly="1"></td>
  213.         </tr>
  214.         </table>
  215.       <br><input type="checkbox" name="reduce" id="reduce" checked> Reducir
  216.     </form>
  217.     </td>
  218.   </tr>
  219. </table>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement