fork download
  1. <html>
  2. <head></head>
  3. <body>
  4. <?php
  5. class Average{
  6. var $samples; //配列
  7.  
  8. function Average(){
  9. $this->samples = array();
  10. }
  11.  
  12. //追加
  13. function add($value){
  14. $this->samples[sizeof($this->samples)] = $value;
  15. }
  16.  
  17. //配列件数表示
  18. function countValues(){
  19. return sizeof($this->samples);
  20. }
  21.  
  22. //最大値
  23. function getMax(){
  24. $max = 0;
  25. foreach ($this->samples as $value) {
  26. if ($max < $value) {
  27. $max = $value;
  28. }
  29. }
  30. return $max;
  31. }
  32.  
  33. //平均値
  34. function getAverage(){
  35. if(0 == sizeof($this->samples)){
  36. return 0;
  37. }
  38. $sum = 0;
  39. foreach ($this->samples as $value) {
  40. $sum += $value;
  41. }
  42. return $sum / sizeof($this->samples);
  43. }
  44. }
  45. $avg = new Average;
  46. for ($i=1; $i <=10 ; $i++) {
  47. $avg->add($i);
  48. }
  49. echo $avg->getAverage();
  50. ?>
  51.  
  52. </body>
  53. </html>
Success #stdin #stdout 0.03s 52480KB
stdin
Standard input is empty
stdout
<html> 
<head></head> 
<body> 
5.5
</body> 
</html>