forked from m9rco/algorithm-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystemSwitch.php
More file actions
99 lines (92 loc) · 2.76 KB
/
SystemSwitch.php
File metadata and controls
99 lines (92 loc) · 2.76 KB
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
<?php
/**
* SystemSwitch
*
* @author Pu ShaoWei <[email protected]>
* @date 2017/10/16
* @license MIT
* -------------------------------------------------------------
* 十进制整数转换为二、八、十六进制整数 n = (n div d) * d + n mod d
* -------------------------------------------------------------
*/
class SystemSwitch
{
/**
* @var array
*/
protected $systemGather;
/**
* @var int
*/
protected $input;
/**
* @var mixed
*/
protected $output;
/**
* SystemSwitch constructor.
*
* @param $input
* @param $output
*/
public function __construct($input, $output)
{
$this->systemGather = array (2, 8, 16);
$this->input = $input;
$this->output = $output;
}
public function run()
{
$before = $this->input;
$stack = new StackExample();
while ($this->input != 0) {
$mod = $this->input % $this->output;
$stack->setPushStack($mod);
$this->input = (int)($this->input - $mod) / $this->output;
}
$output = '';
if ($this->output == 16) {
$output .= '0x';
} else if ($this->output == 8) {
$output .= '0';
}
foreach ($stack->getAllPopStack() as $value) {
if ($this->output == 16) {
switch ($value) {
case 10:
$value = 'A';
break;
case 11:
$value = 'B';
break;
case 12:
$value = 'C';
break;
case 13:
$value = 'D';
break;
case 14:
$value = 'E';
break;
case 15:
$value = 'F';
break;
}
}
$output .= $value;
}
// 因为输出语句会自动将整型的数转换为10进制输出
// 也即如果转换后的结果为0xff,直接将0xff输出会得到255,所以返回一数组
return array (
'before' => $before, // 转换之前
'after' => intval($output, $this->output), // 转换后的整型数(整型)
'string' => $output // 转换后的整型数的字符串表示(字符串型)
);
}
}
// load the stack
define("DS", DIRECTORY_SEPARATOR);
require_once dirname(__DIR__) . DS . 'Structure' . DS . 'StackExample.php';
$systemObj = new SystemSwitch(6, 16);
$result = $systemObj->run();
var_dump($result);