fork download
  1. <?php
  2. class Employee
  3. {
  4. public $salary;
  5. public $cofee;
  6. public $pages;
  7.  
  8. public function __construct($salary, $cofee, $pages)
  9. {
  10. $this->salary = $salary;
  11. $this->cofee = $cofee;
  12. $this->pages = $pages;
  13. }
  14. }
  15.  
  16. $me = new Employee(500, 20, 200);
  17. $ma = new Employee(400, 15, 150);
  18. $in = new Employee(200, 5, 50);
  19. $an = new Employee(800, 50, 5);
  20.  
  21. class Department
  22. {
  23. public $name;
  24. public $meEmployees = array(0, 0, 0); //array(lvl1,lvl2,lvl3)
  25. public $maEmployees = array(0, 0, 0);
  26. public $inEmployees = array(0, 0, 0);
  27. public $anEmployees = array(0, 0, 0);
  28. public $head = array("" => 0); //array(type=>lvl)
  29. }
  30.  
  31. $purch = new Department;
  32. $purch->name = "Закупок";
  33. $purch->meEmployees[0] = 9;
  34. $purch->meEmployees[1] = 3;
  35. $purch->meEmployees[2] = 2;
  36. $purch->maEmployees[0] = 2;
  37. $purch->head = array(
  38. "me" => 2
  39. );
  40.  
  41. $sale = new Department;
  42. $sale->name = "Продаж";
  43. $sale->meEmployees[0] = 12;
  44. $sale->maEmployees[0] = 6;
  45. $sale->anEmployees[0] = 3;
  46. $sale->anEmployees[1] = 12;
  47. $sale->head = array(
  48. "ma" => 2
  49. );
  50.  
  51. $advert = new Department;
  52. $advert->name = "Рекламы";
  53. $advert->maEmployees[0] = 15;
  54. $advert->maEmployees[1] = 10;
  55. $advert->meEmployees[0] = 8;
  56. $advert->inEmployees[0] = 2;
  57. $advert->head = array(
  58. "ma" => 3
  59. );
  60.  
  61. $logist = new Department;
  62. $logist->name = "Логистики";
  63. $logist->meEmployees[0] = 13;
  64. $logist->meEmployees[1] = 5;
  65. $logist->meEmployees[0] = 5;
  66. $logist->head = array(
  67. "me" => 1
  68. );
  69.  
  70.  
  71. function drawTable($array)
  72. {
  73. //ПОИСК МАКСИМАЛЬНОЙ ШИРИНЫ ЯЧЕЙКИ
  74. $maxWidth = 0;
  75. $cells = 0;
  76. foreach ($array as $key => $value) {
  77. $cells = $cells < count($value) ? count($value) : $cells;
  78. foreach ($value as $word)
  79. $maxWidth = $maxWidth < (mb_strlen($word)) ? mb_strlen($word) : $maxWidth;
  80. }
  81.  
  82. //ОТРИСОВКА ТАБЛИЦЫ
  83. drawLine($maxWidth, $cells);
  84. foreach ($array as $key => $value) {
  85. echo "|";
  86.  
  87. foreach ($value as $word) {
  88. $word = mb_strlen($word) < $maxWidth ? $word . str_repeat(" ", $maxWidth - mb_strlen($word)) : $word;
  89. echo $word . "|";
  90. }
  91. echo "\n";
  92. drawLine($maxWidth, $cells);
  93. }
  94. }
  95.  
  96. function drawLine($maxWidth, $cells)
  97. {
  98. echo "+";
  99. for ($i = 1; $i <= $cells; $i++)
  100. echo str_repeat("-", $maxWidth) . "+";
  101. echo "\n";
  102. }
  103.  
  104. $table[0] = array(
  105. "Департамент",
  106. "сотр.",
  107. "з/п",
  108. "кофе",
  109. "стр.",
  110. "постр.з/п"
  111. );
  112. $table[1] = countDep($purch, $me, $ma, $in, $an);
  113. $table[2] = countDep($sale, $me, $ma, $in, $an);
  114. $table[3] = countDep($advert, $me, $ma, $in, $an);
  115. $table[4] = countDep($logist, $me, $ma, $in, $an);
  116.  
  117. function countDep($obj, $me, $ma, $in, $an)
  118. {
  119. $result[0] = $obj->name;
  120. $result[1] = array_sum($obj->meEmployees) + array_sum($obj->maEmployees) + array_sum($obj->inEmployees) + array_sum($obj->anEmployees);
  121.  
  122. $sum = 0;
  123. $sumCofee = 0;
  124. $sumPages = 0;
  125. for ($typeEmplCounter = 1; $typeEmplCounter <= 4; $typeEmplCounter++) {
  126. switch ($typeEmplCounter) {
  127. case "1":
  128. $typeEmpl = 'meEmployees';
  129. $emplCl = $me;
  130. break;
  131. case "2":
  132. $typeEmpl = 'maEmployees';
  133. $emplCl = $ma;
  134. break;
  135. case "3":
  136. $typeEmpl = 'inEmployees';
  137. $emplCl = $in;
  138. break;
  139. case "4":
  140. $typeEmpl = 'anEmployees';
  141. $emplCl = $an;
  142. break;
  143. }
  144. foreach ($obj->$typeEmpl as $key => $empl) {
  145. $k = $key > 0 ? ($key > 1 ? 1.5 : 1.25) : 1; // :3 УЧЁТ ЛЕВЕЛА :3
  146. $sum += $k * $empl * $emplCl->salary;
  147. $sumCofee += $empl * $emplCl->cofee;
  148. $sumPages += $empl * $emplCl->pages;
  149. }
  150.  
  151. }
  152.  
  153. $result[2] = $sum;
  154. $result[3] = $sumCofee;
  155. $result[4] = $sumPages;
  156. $result[5] = round($sum / $sumPages, 2);
  157. return $result;
  158. }
  159.  
  160. //ПОКА ЧТО БЕЗ УЧЕТА НАЧАЛЬНИКОВ ОТДЕЛА И ГРАФ "СРЕДНЕГО"-"ВСЕГО"
  161. drawTable($table);
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
+-----------+-----------+-----------+-----------+-----------+-----------+
|Департамент|сотр.      |з/п        |кофе       |стр.       |постр.з/п  |
+-----------+-----------+-----------+-----------+-----------+-----------+
|Закупок    |16         |8675       |310        |3100       |2.8        |
+-----------+-----------+-----------+-----------+-----------+-----------+
|Продаж     |33         |22800      |1080       |3375       |6.76       |
+-----------+-----------+-----------+-----------+-----------+-----------+
|Рекламы    |35         |15400      |545        |5450       |2.83       |
+-----------+-----------+-----------+-----------+-----------+-----------+
|Логистики  |10         |5625       |200        |2000       |2.81       |
+-----------+-----------+-----------+-----------+-----------+-----------+