arithmetic_operate.cpp
3.49 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
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
/*
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* CC/LICENSE
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "tools.h"
#include "arithmetic_operate.h"
#include "parser.c"
ArithmeticOperate::ArithmeticOperate() {
_operator_map["+"] = PLUS;
_operator_map["-"] = MINUS;
_operator_map["*"] = TIMES;
_operator_map["/"] = DIVIDE;
_operator_map["%"] = MOD;
_operator_map["^"] = POW;
_operator_map["("] = LP;
_operator_map[")"] = RP;
}
ArithmeticOperate::~ArithmeticOperate() {
}
bool ArithmeticOperate::calculate(const char* str, int32_t& num) {
std::string expr;
token_seq_t tokens;
double result = 0.0;
num = 0;
if (NULL == str) {
return false;
}
expr = str;
uint32_t i = 0;
for (i = 0; i < expr.size(); ++i) {
if (!isdigit(expr.at(i))) {
break;
}
}
if (expr.size() == i) { //È«²¿¶¼ÊÇÊý×Ö
return false;
}
if (tokenize(expr, tokens) && calculate_tokens(tokens, result)) {
num = (int32_t)result;
return true;
}
return false;
}
bool ArithmeticOperate::tokenize(const std::string& input, token_seq_t& tokens) {
std::string s = input;
ivr_tools_t::trim(s);
const char* current = s.data();
const char* end = s.data() + s.size();
int32_t num_val = 0;
tokens.clear();
while (current < end) {
while ((' ' == *current) || ('\t' == *current)) {
++current;
}
const char* token_beg = current;
while (((*current >= '0') && (*current <= '9')) || '.' == *current) {
++current;
}
if (token_beg < current) {
std::string num_tok(token_beg, current - token_beg);
double val = 0.0;
if (!ivr_tools_t::str2double(num_tok, val)) {
return false;
}
tokens.push_back(std::make_pair(INTEGER, val));
++num_val;
continue;
}
for (size_t len = 1; len <= 3; ++len) {
if (current + len > end) {
return false;
}
std::string window(current, len);
std::map<std::string, int>::const_iterator op_it = _operator_map.find(window);
if (op_it == _operator_map.end()) {
continue;
}
tokens.push_back(std::make_pair(op_it->second, 0.0));
current += len;
break;
}
if (token_beg < current) {
continue;
}
return false;
}
return (num_val);
}
bool ArithmeticOperate::calculate_tokens(token_seq_t& tokens, double& val) {
void* parser = ParseAlloc(malloc);
result_t result;
for (size_t i = 0; i < tokens.size(); ++i) {
Parse(parser, tokens[i].first, tokens[i].second, &result);
}
Parse(parser, 0, 0.0, &result);
ParseFree(parser, free);
if (result.error_type != result_t::ACCEPTED) {
return false;
}
val = result.value;
return true;
}