← Tutoriais

[PHP] Calculadora

Lida 4222 vezes

Offline

kingless 
Membro
Mensagens 857 Gostos 0
Troféus totais: 27
Trófeus: (Ver todos)
Super Combination Combination Topic Starter Poll Voter Poll Starter Level 5 Level 4 Level 3 Level 2 Level 1

Acabei de criar uma simples calculadora em PHP, é muito simples porque fiz tudo a correr (1 hora) por enquanto só é possível calcular 2 números de cada vez e deve ter algumas bugs.  :)

Demo:
http://www.kingless.net/calc.php

Download:
http://www.kingless.net/download.php?filename=calc.php

Source Code:
http://www.kingless.net/download.php?filename=calc.php;source

Código: [Seleccione]

<?php
     
////////////////////////////
    //// Calculadora em PHP ////
   ///      by kingless     ///
  //  <blastelf@gmail.com>  //
 ////////////////////////////
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Calculadora criada por kingless - blastelf@gmail.com</title>
<style type="text/css">

td {

color: #FFFF00;
font-weight: bold;
text-align: center;
border: 1px solid #000099;
}

input#calc {

background-color: #759475;
text-align: right;
color: #222722;
border-color: #000099;

}

table#calculadora {
border: 1px solid  #000099;
background-color: #3300cc;
}

#titulo {

font-family: arial, verdana, sans serif;
font-size: 1em;
font-weight: bold;
text-align: center;
}

</style>
</head>
<body>

<?php

class calculadora &#123;

var $nums '';
var $calc '';
var $op '';

function mostrar &#40;&#41; &#123;

$num = @$_GET['num'&#93;[0&#93;;
$calc = @$_GET['calc'&#93;;

if&#40;empty&#40; $num &#41; AND empty&#40; $calc &#41;&#41; &#123;
return false;
&#125;

if&#40;!isset&#40; $display &#41;&#41; &#123;
$display $calc;
&#125;

$this->nums .= empty&#40; $num &#41; ? $display &#58; $display.$num;

$this->setOperacao&#40;&#41;;

if&#40;!empty&#40; $this->op &#41;&#41; &#123;
$this->calc $this->nums.rawurldecode&#40; $this->op &#41;.$num;
&#125;

return $this->calcular&#40;&#41;;
&#125;

function setOperacao &#40;&#41; &#123;
if&#40;!empty&#40; $_GET['op'&#93; &#41;&#41; &#123;
$this->op $_GET['op'&#93;;
&#125;
&#125;

function calcular&#40;&#41; &#123;

$calc = empty&#40; $this->calc &#41; ? $this->nums &#58; $this->calc;

if&#40;isset&#40; $_GET['resultado'&#93; &#41;&#41; &#123;
if&#40;preg_match&#40; '/^&#40;[0-9&#93;+&#41;&#40;\/|\*|\+|\-&#41;&#40;[0-9&#93;+&#41;+$/', $calc, $match &#41;&#41; &#123;
switch&#40; $match[2&#93; &#41; &#123;
case "+"&#58;
return $match[1&#93;+$match[3&#93;;
break;
case "-"&#58;
return $match[1&#93;-$match[3&#93;;
break;
case "/"&#58;
return $match[1&#93;/$match[3&#93;;
break;
case "*"&#58;
return $match[1&#93;*$match[3&#93;;
break;
&#125;
&#125; else &#123;
return "Ocorreu um erro";
&#125;
&#125;
return $calc;
&#125;
&#125;
$calc = new calculadora&#40;&#41;;
?>


<form action="" method="get">

<!-- Calculadora criada por kingless <blastelf@gmail.com> -->

<table id="calculadora" cellpadding="10" cellspacing="0" align="center">
<tr>
   <td align="center" colspan="4">
       <span id="titulo">Calculadora</span><br/>
        <input id="calc" type="text" name="calc" value="<?=$calc->mostrar();?>" />
    </td>
</tr>
<tr>
   <td>
      <input type="submit" name="num[]" value="7" />
   </td>
   <td>
      <input type="submit" name="num[]" value="8" />
   </td>
   <td>
      <input type="submit" name="num[]" value="9" />
   </td>
   <td>
      <input type="submit" name="op" value="/" />
   </td>
</tr>
<tr>
   <td>
      <input type="submit" name="num[]" value="4" />
   </td>
   <td>
       <input type="submit" name="num[]" value="5" />
   </td>
   <td>
       <input type="submit" name="num[]" value="6" />
   </td>
   <td>
       <input type="submit" name="op" value="*" />
   </td>
</tr>
<tr>
    <td>
       <input type="submit" name="num[]" value="1" />
    </td>
     <td>
<input type="submit" name="num[]" value="2" />
    </td>
    <td>
       <input type="submit" name="num[]" value="3" />
    </td>
    <td>
       <input type="submit" name="op" value="-" />
    </td>
</tr>
<tr>
   <td>
       <input type="submit" name="num[]" value="0" />
   </td>
   <td>
       <input type="submit" name="num[]" value="." />
   </td>
   <td>
       <input type="submit" name="resultado" value="=" />
   </td>
   <td>
       <input type="submit" name="op" value="+" />
   </td>
</tr>
</table>
</form>
</body>
</html>
Offline

bfms 
Equipa
Mensagens 1769 Gostos 40
Feedback +1

Troféus totais: 35
Trófeus: (Ver todos)
Search Level 6 Apple User Super Combination Combination Topic Starter 50 Poll Votes 10 Poll Votes Poll Voter Poll Starter

Gostei... falta ai um botão para limpar no caso da pessoa querer fazer nova conta  :wink:
Offline

Spread 
Membro
Mensagens 1433 Gostos 2
Troféus totais: 29
Trófeus: (Ver todos)
Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Poll Starter Level 5 Level 4 Level 3 Level 2

Era mais interessante se conseguisses guardar os dados dos digitos e a operação antes de fazer post. No entanto é uma optima iniciativa ;)

Cumps 8)
Offline

kingless 
Membro
Mensagens 857 Gostos 0
Troféus totais: 27
Trófeus: (Ver todos)
Super Combination Combination Topic Starter Poll Voter Poll Starter Level 5 Level 4 Level 3 Level 2 Level 1

Citação de: "bfms"
Gostei... falta ai um botão para limpar no caso da pessoa querer fazer nova conta  :wink:

Já me tinha lembrado disso ontem  :) e arranjei uma solução que foi apenas colocar um link onde diz "Calculadora" e quando se quiser limpar é só clicar em "Calculadora". Essa foi a solução mais rápida que consegui arranjar para não ter que alterar o design da calculadora.  :wink:

Já agora.. Acabei de adicionar mais uma pequena função ao meu script de downloads para mostrar o source code com sintaxe colorida.

http://www.kingless.net/download.php?filename=calc.php;source
Offline

jsilas 
Elite
Mensagens 86 Gostos 0
Feedback +4

Troféus totais: 26
Trófeus: (Ver todos)
Super Combination Combination Topic Starter Poll Voter Level 5 Level 4 Level 3 Level 2 Level 1 50 Posts

http://globpt.com/calc.php

Bom codigo, ja o tenho a funcionar isto vai dar-me jeito para fazer contas quando tiver a vender backups. LOL

Obrigado a todos.
Offline

RaPtOr 
Membro
Mensagens 1173 Gostos 2
Feedback +1

Troféus totais: 30
Trófeus: (Ver todos)
Apple User Windows User Super Combination Combination Topic Starter 50 Poll Votes 10 Poll Votes Poll Voter Level 5 Level 4

Vender backups ?...

 :shock: