logoWebbserverprogrammering 1

Show sourcecode

The following files exists in this folder. Click to view.

webbserv1-dice/exercises/dice6/

CDice.php
CHistogram.php
dice-6.php

CHistogram.php

57 lines ASCII Unix (LF)
<?php
/**
 * A class for printing a histogram.
 *
 */
class CHistogram {

  
/**
   * Properties
   *
   */


 /**
  * Contructor
  */
 
public function __construct() {
   echo 
__METHOD__;
 }
 
 
/**
  * Destructor
  */
 
public function __destruct() {
   echo 
__METHOD__;
 }

  
/**
   * Print the histogram
   *
   * @param array $values the values to print out the histogram from.
   */
  
public function GetHistogram($values, $faces=6) {

    
// Calculate occurences for each key
    
$res = array();
 for (
$i=1; $i<$faces+1; $i++) $res[$i]=0;
 foreach(
$values as $value) {
  
$res[$value]++;
 }

    
// Prepare out a textual representation of the histogram
    
$html = "<ol>";
    foreach(
$res as $rvalue) {
  
$html .= "<li>";
  for (
$i=1;$i<=$rvalue;$i++) {
   
$html .= "*";
  }
       
$html .= " ($rvalue st)</li>";
    }
    
$html .= "</ol>";

    return 
$html;
  }


}