Initial commit

I like to start new repos with this phrase:
Let's get it started!
This commit is contained in:
Masahiko AMANO 2020-09-26 14:37:23 +03:00
commit 0e9cf0e6de
5 changed files with 232 additions and 0 deletions

2
.gitattributes vendored Normal file
View File

@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# PersonalHomeCalc ![PHP | WEB](https://img.shields.io/badge/WEB-PHP-blue.svg)
A simple but cute calc on PHP :3

30
calc.php Normal file
View File

@ -0,0 +1,30 @@
<?php
function getData($pname) {
if (isset($_GET[$pname])) {
return $_GET[$pname];
} else {
return "";
}
}
$number1=getData('number1');
$oper=getData('oper');
$number2=getData('number2');
if ($oper=="+") {
$res=['rcode'=>'1','result'=>(int)$number1+(int)$number2];
} else if ($oper == "-") {
$res=['rcode'=>'1','result'=>(int)$number1-(int)$number2];
} else if ($oper=="*") {
$res=['rcode'=>'1','result'=>(int)$number1*(int)$number2];
} else if ($oper=="/") {
if ((int)$number2 == 0) {
$res=['rcode'=>'0','result'=>"division by zero"];
} else {
$res=['rcode'=>'1','result'=>(int)$number1/(int)$number2];
}
} else {
$res=['rcode'=>'0','result'=>"unknown operation"];
}
$data=$res;
header('Content-type: application/json');
echo json_encode($data);
?>

BIN
favicon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 83 KiB

197
index.php Normal file
View File

@ -0,0 +1,197 @@
<?php
function getData($pname) {
if (isset($_GET[$pname])) {
return $_GET[$pname];
} else {
return "";
}
}
function createButton($btname,$class,$func,$title) {
?>
<div class="btn <?=$class?>" onclick="<?=$func?>;" title="<?=$title?>"><?=$btname?></div>
<!-- <input name="stack" type="submit" value="<?=$btname;?>" class="btn <?=$class?>" onclick="<?=$func?>;"> -->
<?php
}
?>
<html>
<head>
<title>Personal Home Calc</title>
<link rel="shortcut icon" href="favicon.png">
<style>
html,body{
margin: 0;
padding: 0;
background: #111;
font-family: "Comic Sans MS";
overflow: hidden;
}
#prompt{
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 18%;
margin-bottom: 5vh;
background: #222;
border: 0;
box-shadow: 0 2vh 2vh #060606;
font-size: 50px;
color: #eee;
text-shadow: 0 0 5px white;
text-align: center;
}
.hidd{
display: none;
}
.row{
margin: 3% 0;
display: flex;
justify-content: space-around;
}
.btn{
display: flex;
justify-content: center;
align-items: center;
width: 20%;
height: 20%;
margin-bottom: 2px;
border: 0;
border-radius: 10px;
font-size: 60px;
color: #eee;
cursor: pointer;
}
.btn:hover{
box-shadow: 0 0 10px white;
}
.btn1{
background: #EF3038;
}
.btn2{
background: #0047AB;
}
.btn1:active{
background: #77181C;
color: #aaa;
}
.btn2:active{
background: #002456;
color: #aaa;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script>
var p1='0';
var tp='';
var p2='0';
var sol='0';
var pmt='0';
var allow=true;
function zap() {
pmt+='=';
document.getElementById('prompt').innerHTML=pmt;
var number1=p1;
var oper=tp;
var number2=p2;
$.ajax({
url: "calc.php",
type: "GET",
data: {'number1': number1, 'oper': oper, 'number2': number2},
dataType: "json",
success: function(response) {
if (response.rcode=="0") {
alert(response.result)
cle()
} else {
sol=response.result;
pmt+=sol;
document.getElementById('prompt').innerHTML=pmt;
allow=false;
p1='0';
tp='';
p2='0';
}
}
});
}
function numb(num) {
if (Number(p1)==0 && (tp=='')) {
p1=num;
pmt=String(p1);
document.getElementById('prompt').innerHTML=pmt;
} else if ((Number(p1)>0) && (tp=='')) {
p1+=num;
pmt=String(p1);
document.getElementById('prompt').innerHTML=pmt;
} else if (Number(p2)==0 && tp!='') {
p2=num;
pmt=String(p1)+tp+String(p2);
document.getElementById('prompt').innerHTML=pmt;
} else if (Number(p2)>0 && tp!='') {
p2+=num;
pmt=String(p1)+tp+String(p2);
document.getElementById('prompt').innerHTML=pmt;
}
}
function operation(op) {
if (tp!='') {alert('За раз можно использовать только одну операцию!');return;}
if (!allow) {
p1=Number(sol);
pmt=sol;
allow=true;
}
// if (p1=='0' && p2=='0' && tp=='' && Number(document.getElementById('prompt').value)>0) {
// p1=document.getElementById('prompt').innerHTML
// }
tp=op;
pmt+=tp;
document.getElementById('prompt').innerHTML=pmt;
}
function cle() {
p1='0';
tp='';
p2='0';
sol='0';
pmt='0';
allow=true;
numb(0);
}
</script>
</head>
<body>
<div id="prompt">0</div>
<div class="row">
<?php
for ($i=1;$i<=3;$i++) {
createButton($i,'btn1',"numb('".$i."')",$i);
}
createButton("+",'btn2',"operation('+')","Сложение");
?>
</div>
<div class="row">
<?php
for ($i=4;$i<=6;$i++) {
createButton($i,'btn1',"numb('".$i."')",$i);
}
createButton("-",'btn2',"operation('-')","Вычитание");
?>
</div>
<div class="row">
<?php
for ($i=7;$i<=9;$i++) {
createButton($i,'btn1',"numb('".$i."')",$i);
}
createButton("*",'btn2',"operation('*')","Умножение");
?>
</div>
<div class="row">
<?php
createButton("0",'btn1',"numb('0')",0);
createButton("AC",'btn2',"cle()","Очистить");
createButton("=",'btn2',"zap()","Считать");
createButton("/",'btn2',"operation('/')","Деление");
?>
</div>
</body>
</html>