Skip to content

Commit 6dabcfa

Browse files
committed
PHP Calculator
PHP file for Calculator
1 parent 46db09f commit 6dabcfa

File tree

2 files changed

+214
-0
lines changed

2 files changed

+214
-0
lines changed

calc.php

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3+
<?php
4+
if (isset($_REQUEST['cal'])) {
5+
$a=$_REQUEST['cal'];
6+
//Replacing root symbol with ~ since it has 3 charectors
7+
$a=str_replace('','~',$a);
8+
$bb=preg_replace("/[0-9.]/",'',$a);
9+
$cc=preg_replace("/[^a-z0-9.]/i",'#',$a);
10+
$dd=explode("#",$cc);
11+
$b2=$a;
12+
//Doing all raising and root in serial order
13+
for ($i=0;$i<strlen($bb);$i++) {
14+
if (substr($bb,$i,1) == "^") {
15+
$b=pow($dd[$i],$dd[$i + 1]);
16+
$b2=str_replace($dd[$i]."^".$dd[$i + 1],$b,$b2);
17+
$dd[$i + 1]=$b;
18+
} elseif (substr($bb,$i,1) == "~") {
19+
$b=pow($dd[$i],1/$dd[$i + 1]);
20+
$b2=str_replace($dd[$i]."~".$dd[$i + 1],$b,$b2);
21+
$dd[$i + 1]=$b;
22+
}
23+
}
24+
25+
$b=$b2;
26+
27+
$bb=preg_replace("/[0-9.]/",'',$b2);
28+
$cc=preg_replace("/[^a-z0-9.]/i",'#',$b2);
29+
$dd=explode("#",$cc);
30+
//Doing all Multiplication and Division in serial order
31+
for ($i=0;$i<strlen($bb);$i++) {
32+
if (substr($bb,$i,1) == "*") {
33+
$b=$dd[$i] * $dd[$i + 1];
34+
$b2=str_replace($dd[$i]."*".$dd[$i + 1],$b,$b2);
35+
$dd[$i + 1]=$b;
36+
} elseif (substr($bb,$i,1) == "/") {
37+
$b=$dd[$i] / $dd[$i + 1];
38+
$b2=str_replace($dd[$i]."/".$dd[$i + 1],$b,$b2);
39+
$dd[$i + 1]=$b;
40+
}
41+
}
42+
$b=$b2;
43+
44+
$bb=preg_replace("/[0-9.]/",'',$b2);
45+
$cc=preg_replace("/[^a-z0-9.]/i",'#',$b2);
46+
$dd=explode("#",$cc);
47+
//Doing all Addition and Substraction in serial order
48+
for ($i=0;$i<strlen($bb);$i++) {
49+
if (substr($bb,$i,1) == " ") {
50+
$b=$dd[$i] + $dd[$i + 1];
51+
$dd[$i + 1]=$b;
52+
} elseif (substr($bb,$i,1) == "-") {
53+
$b=$dd[$i] - $dd[$i + 1];
54+
$dd[$i + 1]=$b;
55+
}
56+
}
57+
echo $b;exit;
58+
} else {
59+
?>
60+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
61+
<head>
62+
<title>KRISCALCULATOR</title>
63+
<link rel="stylesheet" type="text/css" href="kris.css">
64+
<style>
65+
input::-webkit-outer-spin-button,
66+
input::-webkit-inner-spin-button
67+
{
68+
-webkit-appearance: none;
69+
margin: 0;
70+
}
71+
input[type=number] {
72+
-moz-appearance: textfield;
73+
}
74+
</style>
75+
</head>
76+
<body>
77+
<form>
78+
<br><div style='float: center;width:700px;margin: auto;'>
79+
<script>
80+
function action1(a) {
81+
document.getElementById("inpt").focus();
82+
if (a=="D") {
83+
document.getElementById("display1").innerHTML="";
84+
document.getElementById("display2").innerHTML="";
85+
document.getElementById("inpt").value="";
86+
return;
87+
}
88+
var str1="0123456789(.)";
89+
var str2=document.getElementById("inpt").value;
90+
if (a=="C") {
91+
str2=str2.substr(0,str2.length-1);
92+
document.getElementById("inpt").value=str2;
93+
return;
94+
}
95+
var cstr=document.getElementById("display1").innerHTML;
96+
if (str1.includes(a)) {
97+
document.getElementById("inpt").value=str2 + a;
98+
return;} else {
99+
if (str2==""){
100+
if (cstr.substr(cstr.length-1,1)!="=") {
101+
document.getElementById("display1").innerHTML=cstr.substr(0,cstr.length-1)+a;
102+
return;
103+
} else {
104+
var str3=document.getElementById("display2").innerHTML;
105+
if (str3!="") {
106+
document.getElementById("display1").innerHTML=str3+a;
107+
}
108+
return;}
109+
} else {
110+
if (cstr.substr(cstr.length-1,1)=="=") {
111+
document.getElementById("display1").innerHTML=str2+a;
112+
document.getElementById("display2").innerHTML=str2
113+
document.getElementById("inpt").value="";
114+
return;
115+
}
116+
}
117+
cstr=cstr+str2;
118+
}
119+
120+
if (window.XMLHttpRequest) {
121+
// code for IE7+, Firefox, Chrome, Opera, Safari
122+
xmlhttp = new XMLHttpRequest();
123+
} else {
124+
// code for IE6, IE5
125+
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
126+
}
127+
xmlhttp.onreadystatechange = function() {
128+
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
129+
var r=xmlhttp.responseText;
130+
document.getElementById("display1").innerHTML=cstr + a;
131+
document.getElementById("display2").innerHTML=r;
132+
document.getElementById("inpt").value="";
133+
}
134+
}
135+
xmlhttp.open("POST","calc.php",true);
136+
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
137+
xmlhttp.send("cal="+cstr);
138+
}
139+
function myFunction (evt) {
140+
var kCode = (evt.which) ? evt.which : evt.keyCode
141+
if (kCode == 107) {action1("+");
142+
evt.preventDefault();
143+
return false;}
144+
else if (kCode == 109) {action1("-");
145+
evt.preventDefault();
146+
return false;}
147+
else if (kCode == 106) {action1("*");return false;}
148+
else if (kCode == 111) {action1("/");return false;}
149+
else if (kCode == 46) {action1("D");return false;}
150+
else if (kCode == 13) {action1("=");
151+
evt.preventDefault();
152+
return false;}
153+
else if (kCode == 61) {action1("=");return false;}
154+
}
155+
</script>
156+
<?php
157+
echo "<table class='tablem'><tr><td colspan=4 class='full'><input type='number' onkeydown='myFunction(event)' id='inpt' value='' class='numbfl' autocomplete='off' autofocus /></td>";
158+
echo "<tr><td colspan=4 class='full' id='display1'></td>";
159+
echo "<tr><td colspan=4 class='full' id='display2'></td>";
160+
echo "<tr><td class='onefr' onclick=action1('D');>DEL</td><td class='onefr' onclick=action1('√');>√</td><td class='onefr' onclick=action1('^');>^</td><td class='onefr' onclick=action1('=');>=</td>";
161+
echo "<tr><td class='onefr' onclick=action1('1');>1</td><td class='onefr' onclick=action1('2');>2</td><td class='onefr' onclick=action1('3');>3</td><td class='onefr' onclick=action1('+');>+</td>";
162+
echo "<tr><td class='onefr' onclick=action1('4');>4</td><td class='onefr' onclick=action1('5');>5</td><td class='onefr' onclick=action1('6');>6</td><td class='onefr' onclick=action1('-');>-</td>";
163+
echo "<tr><td class='onefr' onclick=action1('7');>7</td><td class='onefr' onclick=action1('8');>8</td><td class='onefr' onclick=action1('9');>9</td><td class='onefr' onclick=action1('*');>*</td>";
164+
echo "<tr><td class='onefr' onclick=action1('.');>.</td><td class='onefr' onclick=action1('0');>0</td><td class='onefr' onclick=action1('C');>C</td><td class='onefr' onclick=action1('/');>/</td>";
165+
}
166+
?>
167+
</form></body></html>

kris.css

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
body {text-align:center;
2+
margin: 0px;
3+
padding: 2px;
4+
font:100% Arial,Helvetica,sans-serif;
5+
overflow:auto;}
6+
.tablem {
7+
margin: 0 auto;
8+
border:1;
9+
text-align:center;
10+
font-family: tahoma, verdana, arial, helvetica, sans-serif;
11+
font-size: 14px;
12+
border-collapse: collapse;
13+
color: #C0C0C0;
14+
}
15+
.tablem, th, td
16+
{
17+
border: 1px solid #777777;
18+
border-collapse: collapse;
19+
}
20+
.full {
21+
text-align:right;
22+
vertical-align:middle;
23+
height:30px;
24+
width:250px;
25+
padding-right: 4px;
26+
background-color: #F0F0F0;
27+
color: #000000;
28+
}
29+
.onefr {
30+
vertical-align:middle;
31+
height:50px;
32+
width:60px;
33+
background-color: #66FFFF;
34+
color: #000000;
35+
}
36+
.numbfl {
37+
text-align:right;
38+
vertical-align:middle;
39+
height:100%;
40+
width:98%;
41+
border:none;
42+
background-color: #F0F0F0;
43+
padding-right: 2%;
44+
}
45+
.numbfl:focus {
46+
background-color: #FFA07A;
47+
}

0 commit comments

Comments
 (0)