-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolynomial.php
132 lines (118 loc) · 3.11 KB
/
polynomial.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/** 5x^2 + 4x + 1**/
function lex($polynomial)
{
//symbol set
//constant is float
//power must positive integer,
//single variable, no more than 1 variable
//^
// +, -
//start char must a contant or an variable
$index = 0;
/*$token = getMonomial($polynomial, $index);
$tokens[] = $token['token'];
$index = $token['index'];*/
$sign = 1;
while (isset($polynomial[$index])) {
if (isSign($polynomial[$index])) {
if ($polynomial[$index] === '-') {
$sign = -1;
} else {
$sign = 1;
}
++$index;
}
$token = getMonomial($polynomial, $index);
$token['token']['sign'] = $sign;
$tokens[] = $token['token'];
$index = $token['index'];
}
return $tokens;
}
function getMonomial($polynomial, $index)
{
$signFlag = false;
$token = [
'sign' => 1,
'variable' => '',
'contant' => 1.0,
'power' => 0
];
while (isset($polynomial[$index])) {
if (isSign($polynomial[$index])) {
break;
}
if ($polynomial[$index] === '.' || ctype_digit($polynomial[$index])) {
//get constant
$value = span('isFloatStr', $polynomial, $index);
$index = $value['index'];
if (is_numeric($value['value'])) {
$token['contant'] = (float)$value['value'];
}
} else if (isCtrl($polynomial[$index])) {
$value = span('isCtrl', $polynomial, $index);
$index = $value['index'];
} else if (ctype_alpha($polynomial[$index])) {
$value = span('ctype_alpha', $polynomial, $index);
$index = $value['index'];
$token['variable'] = $value['value'];
} else if ($polynomial[$index] === '^') {
++$index;
$value = span('isFloatStr', $polynomial, $index);
$index = $value['index'];
if (is_numeric($value['value'])) {
$token['power'] = (float)$value['value'];
}
}
}
return ['token' => $token, 'index' => $index];
}
function isSign($char)
{
return $char === '-' || $char === '+';
}
function isFloatStr($char)
{
return $char === '.' || ctype_digit($char);
}
function isCtrl($char)
{
return ctype_cntrl($char) || ctype_space($char);
}
function span(callable $predict, $polynomial, $index)
{
$str = '';
while (
isset($polynomial[$index]) &&
$predict($polynomial[$index])
) {
$str .= $polynomial[$index];
++$index;
}
return ['value' => $str, 'index' => $index];
}
function emptyTokens($tokens)
{
return count($tokens) === 0;
}
function validVariable($tokens)
{
$b = true;
$variable = '';
foreach ($tokens as $token) {
if ($variable === '') {
$variable = $token['variable'];
} else {
if ($token['variable'] !== $variable) {
$b = false;
break;
}
}
}
return $b;
}
/*
$t = lex("-3x^4+0.9x^2-1");
var_dump($t);
**/