Commit cba12c78 authored by pogrema's avatar pogrema
Browse files

Commit

parent 1aba05a3
No related merge requests found
Showing with 112 additions and 0 deletions
+112 -0
<?php
function convertor($num, $set, $submit)
{
if(isset($submit))
{
if($set == 'decimalToBinary' || $set == 'binaryToDecimal')
{
if($set == 'decimalToBinary') {
$result = $num;
echo "<p>Decimal: $num </p>";
while ($num > 0) {
$residue = $num % 2;
$num = floor($num / 2);
echo $num . "-". $residue ."<br>";
}
echo "<p>Binary: " . decbin($result) . "</p>";
} else {
echo "<p>Binary: $num </p>";
$decimal = 0;
$num = str_split($num);
$num = array_reverse($num);
foreach ($num as $key=>$digit) {
if($digit ==1) {
echo $digit." 2^" . $key . " = ".pow(2, $key)."<br>";
} else {
echo $digit." 2^" . $key . " = ".pow(0, $key)."<br>";
}
}
for ($i = 0; $i < count($num); $i++) {
if ($num[$i] == 1) {
$decimal += pow(2, $i);
}
} echo "<p>Decimal: $decimal </p>";
}
}
}
}
?>
\ No newline at end of file
index.php 0 → 100644
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Convertor</title>
</head>
<body>
<form method="post" class="flex" action="">
<h2>Type a number</h2>
<input class="field" type="nuber" name="number" id="inDumber">
<select class="field" name="selection" id="idSelection">
<option value="" disabled selected>Choose an option</option>
<option value="decimalToBinary">Decimal To Binary</option>
<option value="binaryToDecimal">Binary To Decimal</option>
</select>
<input class="field pointer" type="submit" name="submit">
<div class="column field">
<?php
require_once 'convertor.php';
$num = $_POST['number'];
$set = $_POST['selection'];
$submit = $_POST['submit'];
convertor($num, $set, $submit);
?>
</div>
</form>
</body>
</html>
\ No newline at end of file
style.css 0 → 100644
body {
display: flex;
justify-content: center;
align-items: center;
background-color: rgb(228, 228, 228);
}
.flex {
display: flex;
justify-content: center;
align-items: center;
background-color: white;
padding: 2rem;
flex-direction: column;
border-radius: 1rem;
margin: 0 auto;
}
.field {
position: relative;
display: flex;
border: none;
outline: none;
justify-content: center;
align-items: center;
background-color: rgb(228, 228, 228);
border-radius: 1rem;
padding: 0.5rem;
min-width: 16rem;
margin: 0.5rem;
}
.column {
display: flex;
min-height: 1rem;
flex-direction: column;
}
.pointer {
cursor: pointer;
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment