Show sourcecode
The following files exists in this folder. Click to view.
webbserv1-dice/exercises/dice7/
CDice.php
CDiceImage.php
CHistogram.php
css.css
dice-7.php
dicefaces_32x32.jpg
CHistogram.php
57 lines ASCII Unix (LF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
<?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;
}
}
