Skip to content

Instantly share code, notes, and snippets.

@lucazanzini
Last active August 29, 2015 13:58
Show Gist options
  • Save lucazanzini/10146398 to your computer and use it in GitHub Desktop.
Save lucazanzini/10146398 to your computer and use it in GitHub Desktop.
Equations Solver
<!DOCTYPE html>
<html>
<head>
<title>Equations Solver</title>
<meta http-equiv="Content-Type" content="text/html" />
<meta charset="utf-8" />
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<form method="post" action="solve.php">
<table class="box">
<tr>
<td><input type="text" name="a" onfocus="this.value=''" onblur="if(this.value == '') this.value='Valore di A'" value="Valore di A" /> x<sup>2</sup></td>
<td>
</td>
<td><input type="text" name="b" onfocus="this.value=''" onblur="if(this.value == '') this.value='Valore di B'" value="Valore di B" /> x</td>
<td>
</td>
<td><input type="text" name="c" onfocus="this.value=''" onblur="if(this.value == '') this.value='Valore di C'" value="Valore di C" /></td>
<td>= 0</td>
</tr>
<tr>
<td colspan="6"><input type="submit" name="solve" value=" Risolvi" /></td>
</tr>
</table>
</form>
</body>
</html>
<?php
if (isset($_POST['solve'])) {
$a = $_POST['a'];
$b = $_POST['b'];
$c = $_POST['c'];
if (is_numeric($a)) {
if (!is_numeric($b) && is_numeric($c)) {
if (($c * $a) < 0) {
$alert = 'L\'equazione &egrave; pura.';
$x = array
(
1 => sqrt(-($c / $a)),
2 => sqrt(-($c / $a))
);
} else {
$alert = 'L\'equazione non ha soluzioni reali.';
}
} elseif (!is_numeric($c) && is_numeric($b)) {
$alert = 'L\'equazione &egrave; spuria.';
$x = array
(
1 => 0,
2 => -($b / $a)
);
} elseif (!is_numeric($b) && !is_numeric($c)) {
$alert = 'Devi inserire un valore per il coefficiente di primo grado e il termine noto.';
} else {
$delta = $b ^ 2 - 4 * $a * $c;
if ($delta > 0) {
$alert = 'L\'equazione ha due soluzioni reali distinte.';
$x = array
(
1 => ((- $b) - sqrt($delta)) / (2 * $a),
2 => ((- $b) + sqrt($delta)) / (2 * $a)
);
} elseif ($delta = 0) {
$alert = 'L\'equazione ha due soluzioni reali coincidenti.';
$x = array
(
1 => (- $b) / (2 * $a),
2 => (- $b) / (2 * $a)
);
} else {
$alert = 'L\'equazione non ha soluzioni reali.';
}
}
} else {
$alert = 'Devi inserire un valore per il coefficiente di secondo grado.';
}
} else {
die('Restricted access.');
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Equations Solver</title>
<meta http-equiv="Content-Type" content="text/html" />
<meta charset="utf-8" />
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div class="display">
<?php
echo '<em>' . $alert . '</em><br /><br />';
if (isset($x)) {
foreach ($x as $key => $value) {
echo '<strong>x<sub>' . $key . '</sub></strong> = <em>' . $value . '</em><br />';
}
}
?>
</div>
</body>
</html>
/*/ MAIN SETTINGS /*/
@import url(http://fonts.googleapis.com/css?family=Ubuntu);
* {
margin: 0;
padding: 0;
outline: none;
}
html, body {
font-size: 1em;
background: #eeeeee;
font-family: Ubuntu;
}
a {
color: #222222;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
input {
padding: 0.3em;
width: 7em;
}
/*/ TABLE & DISPLAY SETTINGS /*/
table.box {
margin: 10em auto;
padding: 2em;
background: rgba(255, 255, 255, 0.7);
box-shadow: 0 0 0.3em 0.1em #a5a5a5;
-moz-box-shadow: 0 0 0.3px 0.1em #a5a5a5;
-webkit-box-shadow: 0 0 0.3em 0.1em #a5a5a5;
-o-box-shadow: 0 0 0.3em 0.1em #a5a5a5;
}
td {
padding: 0.5em;
text-align: center;
}
div.display {
width: 20em;
margin: 10em auto;
padding: 2em;
background: rgba(255, 255, 255, 0.7);
box-shadow: 0 0 0.3em 0.1em #a5a5a5;
-moz-box-shadow: 0 0 0.3px 0.1em #a5a5a5;
-webkit-box-shadow: 0 0 0.3em 0.1em #a5a5a5;
-o-box-shadow: 0 0 0.3em 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment