Esecizi in php su moduli

This commit is contained in:
2014-10-26 12:40:01 +01:00
parent c318b09580
commit 3924e6bf32
7 changed files with 410 additions and 2 deletions

46
functions.php Normal file
View File

@@ -0,0 +1,46 @@
<?php
function fattoriale ($input){
if ($input == 1){
return 1;
} else {
return (fattoriale($input -1 ) * $input);
}
}
function isPrime($num) {
//1 is not prime. See: http://en.wikipedia.org/wiki/Prime_number#Primality_of_one
if($num == 1)
return false;
//2 is prime (the only even number that is prime)
if($num == 2)
return true;
/**
* if the number is divisible by two, then it's not prime and it's no longer
* needed to check other even numbers
*/
if($num % 2 == 0) {
return false;
}
/**
* Checks the odd numbers. If any of them is a factor, then it returns false.
* The sqrt can be an aproximation, hence just for the sake of
* security, one rounds it to the next highest integer value.
*/
for($i = 3; $i <= ceil(sqrt($num)); $i = $i + 2) {
if($num % $i == 0)
return false;
}
return true;
}
?>

View File

@@ -1,7 +1,8 @@
<?php <?php
include ('header.html'); require_once ('functions.php');
include('header.html');
/////////////////////////// ///////////////////////////
echo<<<_css echo<<<_css
@@ -18,12 +19,37 @@ echo "\n";
if ($_GET == null){ if ($_GET == null){
print <<<_HTML
<form method="get" action="php_3.php">
First name: <input type="number" name="numero" autofocus required placeholder="Insert the number here" min="1" max="15" ><br>
<input type="submit" value="Send">
</form>
_HTML;
} else { } else {
echo "fattoriale " . fattoriale($_GET['numero']) . "\n";
echo "<br>";
echo "primi ";
for ($i = 1;$i < $_GET['numero'] ; $i++)
{
if (isPrime($i))
echo "$i ";
}
echo "<br>";
echo "tabellina ";
for ($i = 1;$i <= 10 ; $i++)
{
echo $i * $_GET['numero'] . " ";
}
echo "cioa";
} }

56
php_4.php Normal file
View File

@@ -0,0 +1,56 @@
<?php
require_once ('functions.php');
include('header.html');
///////////////////////////
echo<<<_css
<style>
</style>
_css;
echo "\n";
///////////////////////////
$interi = array (5,10,50,-20,54,100,200);
echo "min " . min($interi) . "\n";
echo "<br>";
echo "min " . max($interi) . "\n";
echo "<br>";
echo "somma " . array_sum($interi) . "\n";
echo "<br>";
echo "media " . array_sum($interi)/count($interi) . "\n";
echo "<br>";
echo "reverse ";
$rinteri=array_reverse($interi);
for ($i = 0;$i<count($rinteri);$i ++)
echo $rinteri[$i] . " ";
echo "\n<br>";
for ($i = 0;$i<count($interi);$i ++)
{
if ($interi[$i] < 100)
echo $interi[$i] . " piccolo; ";
else
echo $interi[$i] . " grande; ";
}
echo "\n<br>";
include ('footer.html');
?>

47
php_5.php Normal file
View File

@@ -0,0 +1,47 @@
<?php
require_once ('functions.php');
include('header.html');
///////////////////////////
echo<<<_css
<style>
</style>
_css;
echo "\n";
///////////////////////////
echo<<<_html
<form method="get" action="stampa.php">
Nome: <input type="text" required autofocus name="name" ><br>
Mail: <input type="email" required name ="mail" ><br>
Password: <input type="password" required name ="password_1" ><br>
Verifica Password: <input type="password" required name ="password_2" ><br>
<input type="submit" value="Invia">
</form>
_html;
include ('footer.html');
?>

82
php_6.php Normal file
View File

@@ -0,0 +1,82 @@
<?php
require_once ('functions.php');
include('header.html');
///////////////////////////
echo<<<_css
<style>
input.prezzo {
border: 0px solid;
text-align: center;
}
table, td {
border: 1px solid black;
}
</style>
_css;
echo "\n";
///////////////////////////
if ($_GET == null){
echo<<<_html
<form method="get" >
Primo<input type="checkbox" name="primo" value="Primo"/> Prezzo: <input class="prezzo" type="text" name="primo_prezzo" value="100" size ="1" readonly> euro <br>
Secondo<input type="checkbox" name="secondo" value="Secondo"/> Prezzo: <input class="prezzo" type="text" name="secondo_prezzo" value="45" size ="1" readonly> euro <br>
Frutta<input type="checkbox" name="frutta" value="Frutta"/> Prezzo: <input class="prezzo" type="text" name="frutta_prezzo" value="10" size ="1" readonly> euro <br>
Costo servizio: <input class="prezzo" type="text" name="servizio_prezzo" value="1" size ="1" readonly> euro <br>
<input type="submit" value="Invia">
</form>
_html;
}else {
$totale = 0;
echo "<table>";
echo "<tr><td colspan=\"2\"> Costo Servizio " . $_GET['servizio_prezzo'] . "</td></tr>\n"; $totale+=$_GET['servizio_prezzo'];
if (array_key_exists("primo", $_GET)) { echo "<tr><td> Primo </td><td>" . $_GET['primo_prezzo'] . "</td></tr>\n";$totale+=$_GET['primo_prezzo'];}
if (array_key_exists("secondo", $_GET)) { echo "<tr><td> Secondo </td><td>" . $_GET['secondo_prezzo'] . "</td></tr>\n";$totale+=$_GET['secondo_prezzo'];}
if (array_key_exists("frutta", $_GET)) { echo "<tr><td> Frutta </td><td>" . $_GET['frutta_prezzo'] . "</td></tr>\n";$totale+=$_GET['frutta_prezzo'];}
echo "<tr><td colspan=\"2\"> Totale " . $totale . "</td></tr>\n";
echo "</table>";
}
include ('footer.html');
?>

100
php_7.php Normal file
View File

@@ -0,0 +1,100 @@
<?php
require_once ('functions.php');
include('header.html');
///////////////////////////
echo<<<_css
<style>
h1 {
margin: 1.5em;
}
table {
border: 1px solid black;
}
td.upper {
border-bottom:1px solid black;
}
td.lower {
border-top:1px solid black;
}
</style>
_css;
echo "\n";
///////////////////////////
if ($_GET == null){
echo<<<_html
<table> <tr> <td class="upper"> <h1> Scelta Utente </h1></td></tr>
<tr> <td class="mid">
<form id="form" method="get" >
Nome:<input type="text" name="nome" required autofocus/> <br>
Password:<input type="password" name="password" required/> <br><br>
Quali argomenti vorresti approfondire?<br>
<input type="checkbox" name="op1" value="Informazioni su HTML"/> Informazioni su HTML<br>
<input type="checkbox" name="op2" value="Immagini"/> Immagini<br>
<input type="checkbox" name="op3" value="Collegamenti e url"/> Collegamenti e url<br>
<input type="checkbox" name="op4" value="Oggetti multimediali"/> Oggetti multimediali<br>
<input type="checkbox" name="op5" value="XHTML versione 1.0"/> XHTML versione 1.0<br>
</form>
</td></tr>
<tr> <td class="lower">
<input type="submit" value="Invia!" form="form"/> <input type="reset" value="Cancella tutto!" form="form"/>
</td></tr>
</table>
_html;
}else {
echo "<table>";
echo "<tr><td colspan=\"2\"> Nome " . $_GET['nome'] . " Password : " . $_GET['password'] . "</td></tr>\n";
if (array_key_exists("op1", $_GET)) { echo "<tr><td> Approfondimento </td><td>" . $_GET['op1'] . "</td></tr>\n";}
if (array_key_exists("op2", $_GET)) { echo "<tr><td> Approfondimento </td><td>" . $_GET['op2'] . "</td></tr>\n";}
if (array_key_exists("op3", $_GET)) { echo "<tr><td> Approfondimento </td><td>" . $_GET['op3'] . "</td></tr>\n";}
if (array_key_exists("op4", $_GET)) { echo "<tr><td> Approfondimento </td><td>" . $_GET['op4'] . "</td></tr>\n";}
if (array_key_exists("op5", $_GET)) { echo "<tr><td> Approfondimento </td><td>" . $_GET['op5'] . "</td></tr>\n";}
echo "</table>";
}
include ('footer.html');
?>

51
stampa.php Normal file
View File

@@ -0,0 +1,51 @@
<?php
require_once ('functions.php');
include('header.html');
///////////////////////////
echo<<<_css
<style>
</style>
_css;
echo "\n";
///////////////////////////
if ($_GET == null)
echo "No data to display\n";
else {
if ($_GET['password_1'] == $_GET['password_2'] ) {
echo "Nome " . $_GET['name'] . "\n<br>";
echo "Mail " . $_GET['mail'] . "\n<br>";
echo "Password " . $_GET['password_1'] . "\n<br>";
} else {
echo 'Dati Inseriti invalidi';
}
}
include ('footer.html');
?>