实验二 php基本程序设计
实验目的:
- 掌握PHP语法基本元素,掌握数据类型、变量和常量、运算符、表达式的使用;
- 掌握PHP流程控制;
实验内容及要求:
求一个一维数组的最大值。
<?php /** * Created by PhpStorm. * User: 13313 * Date: 2019/4/19 * Time: 8:20 */ header('Content-type:text/html;charset=utf-8'); $arr1=[-1,5,9,8,2,5]; $max=$arr1[0]; for($i=0;$i<count($arr1);$i++) { if($max<$arr1[$i]) { $max=$arr1[$i]; } } echo $max.'是最大值。';
求一个一维数组的元素之和。
3. <?php /** * Created by PhpStorm. * User: 13313 * Date: 2019/4/19 * Time: 8:34 */ header('Content-type:text/html;charset=utf-8'); $arr1=[-1,5,9,8,2,5]; $sum=0; for($i=0;$i<count($arr1);$i++) { $sum=$sum+$arr1[$i]; } foreach ($arr1 as $v) { echo $v.' '; } echo '<br/>','数组和:'.$sum;
求一个数的阶乘。界面如下图:
1 <?php 2 /** 3 * Created by PhpStorm. 4 * User: 13313 5 * Date: 2019/4/19 6 * Time: 8:40 7 */ 8 $sum=0; 9 if(!empty($_POST)) 10 { 11 $sum=1; 12 $n=$_POST['num']; 13 if($n<0) 14 { 15 $sum=0; 16 } 17 if($n==0) 18 { 19 $sum=1; 20 } 21 for($i=1;$i<=$n;$i++) 22 { 23 $sum=$sum*$i; 24 } 25 } 26 27 ?> 28 <!doctype html> 29 <html lang="en"> 30 <head> 31 <meta charset="UTF-8"> 32 <meta name="viewport" 33 content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> 34 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 35 <title>阶乘</title> 36 </head> 37 <style type="text/css"> 38 td{ 39 text-align: center; 40 } 41 </style> 42 <body> 43 <form action="" method="post"> 44 <table border="1"> 45 <tr> 46 47 <td colspan="2"> 48 求阶乘 49 </td> 50 </tr> 51 <tr> 52 <th> 53 请输入一个数 54 </th> 55 <td> 56 <input type="text" id="num" name="num"> 57 </td> 58 </tr> 59 <tr> 60 <td colspan="2"> 61 <input type="submit" value="提交"> 62 </td> 63 </tr> 64 </table> 65 <p>结果:</p> 66 <p> 67 <?php echo $sum; ?> 68 </p> 69 </form> 70 </body> 71 </html>
打印水仙花数。打印水仙花数
水仙花数的特点:三位的数字,满足的条件是abc=a3+b3+c3
1 <?php 2 /** 3 * Created by PhpStorm. 4 * User: 13313 5 * Date: 2019/4/19 6 * Time: 9:05 7 */ 8 header('Content-type:text/html;charset=utf-8'); 9 $sum=0; 10 $a=0; 11 $b=0; 12 $c=0; 13 echo '水仙花数:'.'<br/>'; 14 for($i=1;$i<=9;$i++) 15 { 16 for($j=0;$j<9;$j++) 17 { 18 for($n=0;$n<9;$n++) 19 { 20 $m=$i*100+$j*10+$n; 21 $M=pow($i,3)+pow($j,3)+pow($n,3); 22 if($m==$M) 23 { 24 echo $i.' '.$j.' '.$n.'<br/>'; 25 } 26 } 27 } 28 }
设计一个计算器。如下图所示:
<?php /** * Created by PhpStorm. * User: 13313 * Date: 2019/4/19 * Time: 9:18 */ $Sum=0; if(!empty($_POST)){ $n1=$_POST['n1']; $n2=$_POST['n2']; $m=$_POST['C']; if($m=='+'){ $Sum= $n1+$n2; } if($m=='-'){ $Sum= $n1-$n2; } if($m=='*'){ $Sum= $n1*$n2; } if($m=='/'){ $Sum= $n1/$n2; } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>计算器</title> </head> <body> <form action="" method="post"> 第一个数:<input name="n1" type="text" > <br> 请操作:<br/> <input type="radio" name="C" value="+">+ <input type="radio" name="C" value="-">- <input type="radio" name="C" value="*">* <input type="radio" name="C" value="/">/ <br> 第一个数:<input name="n2" type="text" > <br> <input type="submit" value="提交"><br> 结果:<input type="text"value="<?php echo $Sum;?>"> </form> </body> </html>
作业验证
输出100 以内(不含100 )能被3 整除的所有整数
能被3整除的个位数为6的数
输出1 ~ 100(不包含100) 之间的偶数,5 个数字一行输出
操场上100多人排队,3人一组多1人,4人一组多2人,5人一组多3人,共多少人?
两个自然数相除,商3余10,被除数,除数,商,余数的和是163,求被除数,除数.
输出一个五行四列的表格
假设某人有100,000 现金。每经过一次路口需要进行一次交费。交费规则为当他现金大于50,000 时每次需要交5%如果现金小于等于50,000 时每次交5,000。请写一程序计算此人可以经过多少次这个路口。
<?php /** * Created by PhpStorm. * User: 13313 * Date: 2019/3/29 * Time: 10:46 */ for($i=0;$i<100;$i++) { if($i%3==0) { echo $i; echo ' '; } } 2: <?php /** * Created by PhpStorm. * User: 13313 * Date: 2019/3/29 * Time: 10:48 */ for($i=0;$i<100;$i++) { if($i%3==0) { if($i<10) { if($i==6) { echo $i; echo ' '; } } if($i>10) { $n=$i%10; if($n==6) { echo $i; echo ' '; } } } } 3: <?php /** * Created by PhpStorm. * User: 13313 * Date: 2019/3/29 * Time: 10:54 */ $n=0; for($i=1;$i<100;$i++) { if($i%2==0) { echo $i; echo " "; ++$n; } if($n%5==0) { echo'<br>'; } } 4: <?php /** * Created by PhpStorm. * User: 13313 * Date: 2019/3/29 * Time: 10:59 */ for($i=100;$i<200;$i++) { if($i%3==1&&$i%4==2&&$i%5==3) { echo $i.' '; } } 5: <?php /** * Created by PhpStorm. * User: 13313 * Date: 2019/3/29 * Time: 11:02 */ $n=0; $m=0; echo' <meta charset="UTF-8">'; for(;;) { ++$n; if($n>=150) { echo'NULL'; break; } for($m=1;$m<$n;$m++) { if($m+$n==150&&$n%($m*3)==10) { echo ' '.$n." :".$m." "; } } } 6: <?php /** * Created by PhpStorm. * User: 13313 * Date: 2019/3/29 * Time: 11:16 */?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>五行四列表格</title> </head> <body> <table border="1" width="50%" > <?php for($i=1;$i<=5;$i++):?> <tr> <?php for($j=1;$j<=4;$j++):?> <td></td> <?php endfor;?> </tr> <?php endfor;?> </table> </body> </html> 7: <?php /** * Created by PhpStorm. * User: 13313 * Date: 2019/3/29 * Time: 11:20 */ $n=100000; $m=0; echo' <meta charset="UTF-8">'; for(;;) { if($n<=0)break; ++$m; if($n>50000) { $n=$n*0.05; echo "目前现金: ".$n." 第".$m."次缴费"; echo'<br>'; } else { $n=$n-5000; echo "目前现金: ".$n." 第".$m."次缴费 "; echo'<br>'; } } echo "可以经过".$m."个路口"; ?>
1:
2:
3:
4:
5:
6:
7:
实验体会:
实践才能检验自己的弊端,这次实验虽然简单,但是仍有一些需要注意的地方,需要完善。