-
Notifications
You must be signed in to change notification settings - Fork 0
/
iStackInfix.cpp
171 lines (166 loc) · 4.36 KB
/
iStackInfix.cpp
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <ctype.h>
#include "iStackInfix.h"
namespace Imaginer{
namespace Utils {
iStackInfix::iStackInfix(char* expname):_expname(expname)
{
Init(OPERATE, &_sign);
Init(OPERAND, &_number);
}
iStackInfix::~iStackInfix()
{
if(_sign)
{
free(_sign);
_sign = NULL;
}
if(_number)
{
free(_number);
_number = NULL;
}
}
char iStackInfix::PRICMP(char cur, char top)
{
switch (cur)
{
case '+':
case '-':
if (top == '+' || top == '-' || top == '*' || top == '/')
return '<'; // 扫描的小于栈顶
else
return '>'; // 扫描的大于栈顶
break;
case '*':
case '/':
if (top == '*' || top == '/')
return '<';
else
return '>';
break;
case '(':
if (top == ')')
{
throw FexpException("function expression invaild,'(' not match");
}
return '>';
break;
case ')':
if (top == '(')
return '=';
else if (top == '#')
{
throw FexpException("function expression invaild,')' not match");
}
else
{
return '<';
}
break;
case '#':
return '<';
break;
}
return '\0';
}
double iStackInfix::Parser(void)
{
double result, loperand, roperand;
double data = 0, expn;
char cur, topSign;
bool ispoint = false, isnum = false;
Push(_sign, '#');
cur = next();
Top(_sign, &topSign);
while (cur != '#' || topSign != '#')
{
if(isspace(cur))
{
cur = next();
continue;
}
switch (cur) {
case '.':
if(ispoint)
{
throw FexpException("function expression invaild, mutil '.'");
}else{
ispoint = true;
expn = 0.1;
}
cur = next();
break;
case '0' ... '9':
if (ispoint)
{
data = data + expn * (cur - '0');
expn *= 0.1;
}else{
data = data * 10 + (cur - '0');
}
isnum = true;
cur = next();
break;
case '+':case '-':
case '*':case '/':
case '(':case ')':
case '#':
if (isnum)
{
Push(_number, data);
isnum = false;
ispoint = false;
data = 0;
}
Top(_sign, &topSign);
switch (PRICMP(cur, topSign))
{
case '<': // 扫描运算符优先级小于栈顶元素
Pop(_sign, &topSign);
Pop(_number, &loperand);
Pop(_number, &roperand);
switch (topSign)
{
case '+':
result = roperand + loperand;
break;
case '-':
result = roperand - loperand;
break;
case '*':
result = roperand * loperand;
break;
case '/':
result = roperand / loperand;
break;
}
Push(_number, result);
break;
case '>': // 扫描运算符优先级大于栈顶元素
Push(_sign, cur);
cur = next();
break;
case '=': // 扫描运算符为右括号,匹配到了左括号
Pop(_sign, &topSign);
cur = next();
break;
}
break;
case '\0':case '\n':
cur = '#';
break;
default:
throw FexpException("function expression invaild, invaild letter");
break;
}
Top(_sign, &topSign);
}
Pop(_number, &result); // 将结果从栈中取出来
if (!Empty(_number))
{ // 如果取出后栈不为空则表示输入的表达式不正确
throw FexpException("function expression invaild, operand is not empty");
}
return result;
}
}//namespace Utils
}//namespace Imaginer