-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
113 lines (102 loc) · 2.64 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
// 0. заполняем массив через функцию rand(1,100), размера который придет параметром в конструктор
// 1. максимум
// 2. минимум
// 3. парные
// 4. не парные
// 5. все цифры что делятся на 4
class Number
{
private $num;
private $arr = [];
public function __construct($num)
{
$this->num = $num;
for ($i = 0; $i < $this->num; $i++) {
$this->arr[] = rand(0,100);
}
}
public function getArray($desc = true)
{
for ($j = 0; $j < $this->num; $j++) {
for ($i = $j + 1; $i < $this->num; $i++) {
if ($desc) {
if ($this->arr[$i] > $this->arr[$j]) {
list($this->arr[$i], $this->arr[$j]) = [$this->arr[$j], $this->arr[$i]];
}
}
else {
if ($this->arr[$i] < $this->arr[$j]) {
list($this->arr[$i], $this->arr[$j]) = [$this->arr[$j], $this->arr[$i]];
}
}
}
}
return $this->arr;
}
public function maxNum($max = null)
{
foreach($this->arr as $data) {
if($max === null) $max = $data;
if($data > $max) $max = $data;
}
return $max;
}
public function minNum($min = null)
{
foreach($this->arr as $data) {
if($min === null) $min = $data;
if($data < $min) $min = $data;
}
return $min;
}
public function evenNum($arr = null)
{
foreach ($this->arr as $k => $v)
{
if ($v % 2 == 0) {
$arr[] = $v;
}
}
return $arr;
}
public function oddNum($arr = null)
{
foreach ($this->arr as $k => $v)
{
if ($v % 2 == 1) {
$arr[] = $v;
}
}
return $arr;
}
public function fourNum($arr = null)
{
foreach ($this->arr as $k => $v)
{
if ($v % 4 == 0) {
$arr[] = $v;
}
}
return $arr;
}
}
$number = rand(5,10);
echo 'number: ' . $number . "\n";
$arr = new Number($number);
print "\n";
print 'Max number: ';
print_r($arr->maxNum());
print "\n";
print 'Min number: ';
print_r($arr->minNum());
print "\n\n";
print 'Even number: ';
print_r($arr->evenNum());
print "\n";
print 'Odd number: ';
print_r($arr->oddNum());
print "\n";
print "все цифры что делятся на 4:\n";
print_r($arr->fourNum());
print "\n";