Skip to content

Commit e2a174f

Browse files
Merge pull request wangzheng0822#64 from SeasClouds/master
[php]四则运算bug修复
2 parents b9ed4f1 + 30afdf3 commit e2a174f

File tree

5 files changed

+97
-79
lines changed

5 files changed

+97
-79
lines changed

php/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.idea
22
vendor
3+
*my*
4+
Queue

php/08_stack/Compute.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ function expression($str)
2828
array_push($operStack, $arr[$i]);
2929
break;
3030
case '*':
31+
$arrLen = count($operStack);
32+
while ($operStack[$arrLen-1] === '/'){
33+
compute($numStack, $operStack);
34+
$arrLen--;
35+
}
36+
array_push($operStack, $arr[$i]);
37+
break;
38+
3139
case '/':
3240
case '(':
3341
array_push($operStack, $arr[$i]);
@@ -70,7 +78,9 @@ function compute(&$numStack, &$operStack){
7078
case '-':
7179
array_push($numStack, array_pop($numStack) - $num);
7280
break;
73-
81+
case '(':
82+
throw new \Exception("不匹配的(", 2);
83+
break;
7484
}
7585
}
7686
expression('-1+2-(1+2*3)');

php/09_queue/Sequential.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
class LoopQueue
4+
{
5+
private $MaxSzie;
6+
private $data = [];
7+
private $head = 0;
8+
private $tail = 0;
9+
10+
/**
11+
* 初始化队列大小 最后的位置不存放数据,实际大小 = size++
12+
*/
13+
public function __construct($size = 10)
14+
{
15+
$this->MaxSzie = ++$size;
16+
}
17+
18+
/**
19+
* 队列满条件 ($this->tail+1) % $this->MaxSzie == $this->head
20+
*/
21+
public function enQueue($data)
22+
{
23+
if (($this->tail+1) % $this->MaxSzie == $this->head)
24+
return -1;
25+
26+
$this->data[$this->tail] = $data;
27+
$this->tail = (++$this->tail) % $this->MaxSzie;
28+
}
29+
30+
public function deQueue()
31+
{
32+
if ($this->head == $this->tail)
33+
return NULL;
34+
35+
$data = $this->data[$this->head];
36+
unset($this->data[$this->head]);
37+
$this->head = (++$this->head) % $this->MaxSzie;
38+
return $data;
39+
}
40+
41+
public function getLength()
42+
{
43+
return ($this->tail - $this->head + $this->MaxSzie) % $this->MaxSzie;
44+
}
45+
}
46+
47+
$queue = new LoopQueue(4);
48+
// var_dump($queue);
49+
$queue->enQueue(1);
50+
$queue->enQueue(2);
51+
$queue->enQueue(3);
52+
$queue->enQueue(4);
53+
// $queue->enQueue(5);
54+
var_dump($queue->getLength());
55+
$queue->deQueue();
56+
$queue->deQueue();
57+
$queue->deQueue();
58+
$queue->deQueue();
59+
$queue->deQueue();
60+
var_dump($queue);

php/11_sort/Sort.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
function insertSort(&$arr)
4+
{
5+
$i = 0;
6+
$len = count($arr);
7+
8+
while($i < $len){
9+
$data = $arr[$i+1];
10+
for ($j = $i;$j >=0 ;$j-- ){
11+
if ($data >= $arr[$j]){
12+
array_splice($arr, $i+1, 1);
13+
array_splice($arr, ++$j, 0, $data);
14+
break;
15+
}
16+
}
17+
18+
$i++;
19+
}
20+
}
21+
22+
$arr = [1,4,6,2,3,5,4];
23+
insertSort($arr);
24+
var_dump($arr);

php/Stack/Compute.php

Lines changed: 0 additions & 78 deletions
This file was deleted.

0 commit comments

Comments
 (0)