Advertisement
Sax

Untitled

Sax
Dec 3rd, 2014
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. <HEAD>
  3.  <style type='text/css'>
  4.             body {
  5.                 font-family: "Chalkduster";
  6.             }
  7.             h1, h2, h3, h4, h5, h6 {
  8.               display: inline;
  9.               margin: 0;
  10.               font-weight: normal;
  11.             }
  12.             input
  13. {
  14.     font-family: "Chalkduster";
  15.     font-size:0.95em;
  16. }
  17.  
  18.         </style>
  19.  
  20.  
  21. <script type="text/javascript">
  22. <!--
  23. /* This script and many more are available free online at
  24. The JavaScript Source!! http://www.javascriptsource.com
  25. Created by: Matthew Roy :: http://matthewroy.com */
  26.  
  27. /*****************************************
  28. **   Matthew C. Roy
  29. **   Big Business Websites
  30. **   For Small Business Prices!
  31. **   http://www.matthewroy.com
  32. **
  33. **   You may use this script freely for
  34. **   non-commercial use as long
  35. **   as this header is left intact.
  36. *****************************************/
  37.  
  38. var n1, n2, d1, d2, An, Ad, Op;
  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.     An=(n1*d2)+(n2*d1) //Answer Numerator
  66.     Ad=(d1*d2) //Answer Denominator
  67.     if(document.calc.reduce.checked==1){
  68.       reduce();
  69.     } else {
  70.       display();
  71.     }
  72.    break
  73.  
  74.   case '-':
  75.     //subtract fractions using formula ((n1*d2)-(n2*d1)) over (d1*d2)
  76.     An=(n1*d2)-(n2*d1)//Answer Numerator
  77.     Ad=(d1*d2)//Answer Denominator
  78.     if(document.calc.reduce.checked==1){
  79.       reduce();
  80.     } else {
  81.       display();
  82.     }
  83.    break
  84.  
  85.   case '*':
  86.     //multiply fractions using formula (n1*n2) over (d1*d2)
  87.     An=n1*n2;//Answer Numerator
  88.     Ad=d1*d2; //Answer Denominator
  89.     if(document.calc.reduce.checked==1){
  90.             reduce();
  91.     } else {
  92.       display();
  93.     }
  94.     break
  95.  
  96.   case '/':
  97.     //divide fractions using formula (n1*d2) over (d1*n2)
  98.     An=n1*d2;//Answer Numerator
  99.     Ad=d1*n2;//Answer Denominator
  100.     if(document.calc.reduce.checked==1){
  101.       reduce();
  102.     } else {
  103.       display();
  104.     }
  105.    break
  106.   }
  107. }
  108.  
  109. function reduce() {
  110.   neg=1; //1 if positive, -1 if negative
  111.   //convert to strings
  112.   ng=An+'';
  113.   dg=Ad+''
  114.   if(ng.indexOf('-')!=-1){  //check to see if answer is negative.
  115.     neg=-1
  116.   }
  117.   if(dg.indexOf('-')!=-1){
  118.     neg=-1
  119.   }
  120.   if(ng.indexOf('-')!=-1&&dg.indexOf('-')!=-1)  {//if both numerator and denominator are negative the answer is positive
  121.     neg=1
  122.   }
  123.   var factorX //highest common factor
  124.  
  125.   if ( An == 0 || Ad == 0 ) {
  126.     factorX=1;
  127.     return;
  128.   }
  129.  
  130.   An = Math.abs( An );
  131.   Ad = Math.abs( Ad );
  132.  
  133.   var factorX = 1;
  134.  
  135.   //Find common factors of Numerator and Denominator
  136.   for ( var x = 2; x <= Math.min( An, Ad ); x ++ ) {
  137.     var check1 = An / x;
  138.     if ( check1 == Math.round( check1 ) ) {
  139.       var check2 = Ad / x;
  140.       if ( check2 == Math.round( check2 ) ) {
  141.         factorX = x;
  142.       }
  143.     }
  144.   }
  145.  
  146.   An=(An/factorX)*neg;  //divide by highest common factor to reduce fraction then multiply by neg to make positive or negative
  147.   Ad=Ad/factorX;  //divide by highest common factor to reduce fraction
  148.   display();
  149. }
  150.  
  151. function display(){
  152.   //Display answer
  153.   document.calc.An.value = An;
  154.   document.calc.Ad.value = Ad;
  155. }
  156.  
  157. // -->
  158. </script>
  159. </HEAD>
  160.  
  161. <!-- STEP TWO: Copy this code into the BODY of your HTML document  -->
  162.  
  163. <BODY>
  164.  
  165. <table width="250" align="center" border="0" cellspacing="0" cellpadding="1" style="background-color:#ffffff;border:1px #000000 solid;">
  166.   <tr>
  167.     <td align="center" valign="middle">
  168.       <h3>Calculadora de Quebrados</h4>
  169.       <form name="calc">
  170.         <table width="150" border="0" cellspacing="0" cellpadding="5">
  171.           <tr>
  172.             <td style="border-bottom:4px #000000 solid;"><input type="text" size="1"  name="n1" id="n1" tabindex="1"></td>
  173.             <td rowspan="2" align="center" valign="middle">
  174.               <select name="Op" tabindex="3">
  175.               <option value="+">+</option>
  176.               <option value="-">-</option>
  177.               <option value="*">x</option>
  178.               <option value="/">÷</option>
  179.               </select>
  180.             </td>
  181.             <td style="border-bottom:4px #000000 solid;"><input type="text" size="1" name="n2" id="n2" tabindex="4"></td>
  182.             <td rowspan="2" align="center" valign="middle"><input type="button" value=" = "onClick="solve();" tabindex="6"></td>
  183.             <td style="border-bottom:4px #000000 solid;"><input type="text" size="1" name="An" id="An" readonly="1"></td>
  184.           </tr>
  185.           <tr>
  186.             <td><input type="text" size="1" name="d1" id="d1" tabindex="2"></td>
  187.             <td><input type="text" size="1" name="d2" id="d2" tabindex="5"></td>
  188.             <td><input type="text" size="1" name="Ad" id="Ad" readonly="1"></td>
  189.         </tr>
  190.         </table>
  191.       <br><input type="checkbox" name="reduce" id="reduce" checked> Reducir
  192.     </form>
  193.     </td>
  194.   </tr>
  195. </table>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement