legatmichau

kalkulatorek js

Oct 27th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 4.69 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="pl">
  3.     <head>
  4.         <meta charset="utf-8">
  5.         <title>Kalkulator</title>
  6.         <style>
  7.             body{  
  8.                 font-family: Comic Sans MS;
  9.             }
  10.             #kalkulator{
  11.                 height:460px;
  12.                 width:450px;
  13.                 background-color:LightGray;
  14.                 border:3px solid black;
  15.                
  16.             }
  17.             #lcd{
  18.                 margin:25px;;
  19.                 height:70px;
  20.                 width:400px;
  21.                 background-color:Gray;
  22.                 border: 3px solid black;
  23.                 text-align:right;
  24.                 font-size:50px;
  25.             }
  26.             .radius{
  27.                 border-top-right-radius: 10px;
  28.                 border-top-left-radius: 10px;
  29.                 border-bottom-right-radius: 10px;
  30.                 border-bottom-left-radius: 10px;
  31.             }
  32.             .przycisk{
  33.                 text-align:center;
  34.                 padding:10px;
  35.                 font-size:30px;
  36.                 background-color:gray;
  37.                 height:76px;
  38.                 width:76px;
  39.                 border:3px solid black;
  40.             }
  41.         </style>
  42.         <script type="text/javascript">
  43.            
  44.             //zmienne globalne--------------------------------------------------------------
  45.            
  46.             var x="";
  47.             var y="";
  48.             var z=0;
  49.             var op;
  50.        
  51.             //funkcje liczb i wyswietlania--------------------------------------------------
  52.            
  53.             function zmiana() {
  54.                 if(z==1){
  55.                     z=0;
  56.                 }   else if(z==0){
  57.                     z=1;
  58.                 }
  59.             }
  60.  
  61.             function cyfra(n)
  62.             {
  63.                 if (z==0){
  64.                    
  65.                     x+=n;
  66.                     document.getElementById("lcd").value=x;
  67.                 }
  68.                 else if (z==1)
  69.                 {
  70.                
  71.                     y+=n;
  72.                     document.getElementById("lcd").value=y;
  73.                 }
  74.             }
  75.            
  76.             function ostatnia()
  77.             {
  78.                 if (z==0){
  79.                     x=x.substring(0,x.length-1);
  80.                     document.getElementById("lcd").value=x;
  81.                 }
  82.                 else if (z==1)
  83.                 {
  84.                     y=y.substring(0,y.length-1);
  85.                     document.getElementById("lcd").value=y;
  86.                 }
  87.             }
  88.            
  89.             //operatory------------------------------------------------------------------------
  90.             function operator(o)
  91.             {
  92.                     op=o;
  93.                     if (op=='pierwiastek'){
  94.                     x = Math.sqrt(x);
  95.                     document.getElementById("lcd").value= x;
  96.                        
  97.                     }else  if (op=='potega'){
  98.                     x = x*x;
  99.                     document.getElementById("lcd").value=x;
  100.                     }else  if (op=='1/x'){
  101.                     x = 1/x;
  102.                     document.getElementById("lcd").value=x;
  103.                     }else{
  104.                     z++;
  105.                     document.getElementById("lcd").value=y;
  106.                     }
  107.             }
  108.            
  109.             //funkcja obliczająca--------------------------------------------------------------
  110.            
  111.             function rownasie()
  112.             {          
  113.                 if (op=='+'){
  114.                     x=x-(-y);
  115.                 }else if (op=='-'){
  116.                     x=x-y;
  117.                 }else if (op=='*'){
  118.                     x=x*y;
  119.                 }else if (op=='/'){
  120.                     if((!isNaN(x))&&(!isNaN(y))&&(y!=0)){
  121.                         x = x/y;
  122.                     }else{
  123.                     alert("nie dziel zjebie przez 0");
  124.                     }
  125.                 }
  126.                 document.getElementById("lcd").value= x;
  127.                 z=0;
  128.                 y=0;
  129.             }
  130.            
  131.         </script>
  132.     </head>
  133.     <body>
  134.         <center>
  135.             <div id="kalkulator" class="radius">
  136.                 <input  type="text" id="lcd">
  137.                 <table>
  138.                     <tr>
  139.                         <td>
  140.                             <input class="radius przycisk" type="button" value="+" onclick="operator('+')" />
  141.                             <input class="radius przycisk" type="button" value="-" onclick="operator('-')" />
  142.                             <input class="radius przycisk" type="button" value="*" onclick="operator('*')" />
  143.                             <input class="radius przycisk" type="button" value="/" onclick="operator('/')" />
  144.                             <input class="radius przycisk" type="button" value="C" onclick="location.reload()" />
  145.                         </td>
  146.                     </tr>
  147.                     <tr>
  148.                         <td>
  149.                             <input class="radius przycisk" type="button" value=7 onclick="cyfra(7)" />
  150.                             <input class="radius przycisk" type="button" value=8 onclick="cyfra(8)" />
  151.                             <input class="radius przycisk" type="button" value=9 onclick="cyfra(9)" />
  152.                             <input class="radius przycisk" type="button" value="√x" onclick="operator('pierwiastek')" />
  153.                             <input class="radius przycisk" type="button" value="CE" onclick="ostatnia()" />
  154.                         </td>
  155.                     </tr>
  156.                     <tr>
  157.                         <td>
  158.                             <input class="radius przycisk" type="button" value=4 onclick="cyfra(4)" />
  159.                             <input class="radius przycisk" type="button" value=5 onclick="cyfra(5)" />
  160.                             <input class="radius przycisk" type="button" value=6 onclick="cyfra(6)" />
  161.                             <input class="radius przycisk" type="button" value=0 onclick="cyfra(0)" />
  162.                             <input class="radius przycisk" type="button" value='x^2' onclick="operator('potega')" />
  163.                         </td>
  164.                     </tr>
  165.                     <tr>
  166.                         <td>
  167.                             <input class="radius przycisk" type="button" value=1 onclick="cyfra(1)" />
  168.                             <input class="radius przycisk" type="button" value=2 onclick="cyfra(2)" />
  169.                             <input class="radius przycisk" type="button" value=3 onclick="cyfra(3)" />
  170.                             <input class="radius przycisk" type="button" value="=" onclick="rownasie()" />
  171.                             <input class="radius przycisk" type="button" value="1/x" onclick="operator('1/x')" />
  172.                            
  173.                         </td>
  174.                     </tr>
  175.                 </table>
  176.             </div>
  177.         </center>
  178.     </body>
  179. </html>
Advertisement
Add Comment
Please, Sign In to add comment