parserNumbers.cpp
6.85 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* ReconhecedorSaldo.java
* Created on 11 de Junho de 2007, 14:21, by giuliano
*
* parseNumbers.cpp
* Convertido para C++ em:
* @date 21/10/2009
* @author Derzu Omaia
*
*/
#include "parserNumbers.h"
#include "dprintf.h"
#include "stringTokenizer.h"
#include <stdlib.h>
using namespace std;
// TODO fazer o parser de numbers romanos
namespace Util {
const string ParserNumbers::centenas [] = {"cento","duzentos","trezentos","quatrocentos","quinhentos",
"seiscentos","setecentos","oitocentos","novecentos"};
const string ParserNumbers::dezenas1 [] = {"dez","onze","doze","treze","catorze","quinze",
"dezesseis","dezessete","dezoito","dezenove"};
const string ParserNumbers::dezenas2 [] = {"vinte","trinta","quarenta","cinquenta",
"sessenta","setenta","oitenta","noventa"};
const string ParserNumbers::milhar [] = {"mil", "milhao", "bilhao"};
const string ParserNumbers::milhares [] = {"mil", "milhoes", "bilhoes"};
const string ParserNumbers::unidades [] = {"zero", "um", "dois", "tr�s", "quatro", "cinco", "seis", "sete", "oito", "nove"};
ParserNumbers::ParserNumbers()
{
}
string ParserNumbers::pontua(string num)
{
string aux = "";
int cont = 0;
// DDPRINTF("num.length() = %d\n", num.length());
for(int i = num.length()-1 ; i >= 0; i--)
{
cont++;
if( ( cont % 3 == 1) && (i < (int)num.length()-1) )
aux = aux+".";
// DDPRINTF("i = %d\n", i);
aux = aux + num.at(i);
}
num = aux;
aux = "";
for(int i = num.length()-1 ; i >= 0; i--)
aux = aux + num.at(i);
return aux;
}
string ParserNumbers::analisaTrinca(string num)
{
string aux = "";
bool centena = false;
bool dezena = false;
if(num.compare("100")==0)
{
return "cem";
}
else
{
if(num.length()==3)
{
int indice = (num.at(0)-'0')-1;
if(indice >= 0)
{
aux = centenas[indice];
centena = true;
}
}
if(num.length() >= 2)
{
int indice = (num.at(num.length()-2)-'0')-1;
if(indice == 0)
{
indice = (num.at(num.length()-1)-'0');
if(centena)
{
aux = aux + " e ";
}
aux = aux + dezenas1[indice];
dezena = true;
}
else
if(indice >= 1)
{
indice = (num.at(num.length()-2)-'0')-1;
if(centena)
{
aux = aux + " e ";
}
aux = aux + dezenas2[indice-1];
//Unidades
indice = (num.at(num.length()-1)-'0');
if(indice > 0)
{
aux = aux + " e ";
aux = aux + unidades[indice];
}
dezena = true;
}
else
{
indice = (num.at(num.length()-1)-'0');
if(indice>0)
{
if(centena)
{
aux = aux + " e ";
}
//DDPRINTF("Unidade: %d\n", indice);
aux = aux + unidades[indice];
}
}
}
else
{
int indice = (num.at(num.length()-1)-'0');
if(dezena)
{
aux = aux + " e ";
}
aux = aux + unidades[indice];
}
}
return aux;
}
// TODO Verificar se tem $R o $D ou euro
string ParserNumbers::parse(string num)
{
StringTokenizer st1 = StringTokenizer(num.c_str(),",");
string inteiro = st1.getNext();
StringTokenizer st = StringTokenizer(pontua(inteiro).c_str(), ".");
string texto = "";
int tokens = st.getLength();
//DPRINTF("num eh %s\n", num.c_str());
//DPRINTF("inteiro eh %s\n", inteiro.c_str());
//DPRINTF("tokens eh %d\n", tokens);
if(tokens <= 4)
{
string lastToken = "";
while(st.hasNext())
{
tokens--;
lastToken = st.getNext();
//DPRINTF("lastToken eh %s\n", lastToken.c_str());
string aux = analisaTrinca(lastToken);
//DPRINTF("aux eh %s\n", aux.c_str());
//DPRINTF("aux.length() %d\n", aux.length());
// TODO ta com o problema do 7 mill, ajeitar.
texto += aux;
if(tokens>0)
{
if (aux.length()>0)
{
if((lastToken.at(lastToken.length()-1)-'0') == 1)
{
texto += " "+milhar[tokens-1] +" e ";
}
else
{
texto += " "+milhares[tokens-1] +" e ";
}
}
}
}
// comentei essa parte
/* if(atoi(lastToken.c_str()) == 1)
texto += " real";
else
texto += " reais";*/
string aux2 = "";
if(st1.hasNext())
aux2 = st1.getNext();
if(atoi(aux2.c_str()) > 0)
{
//DPRINTF("entrei aqui");
texto += " e "+analisaTrinca("0"+aux2);
if(atoi(aux2.c_str()) == 1)
{
texto += " centavo";
}
else
{
texto += " centavos";
}
}
//DDPRINTF("Final: %s \n", texto.c_str());
}
else
DDDPRINTF("Numero invalido!\n");
// remove o "e" do final
//while (texto.at(texto.size()-1)==' ' || texto.at(texto.size()-1)=='e')
while (texto.at(texto.size()-2)==' ' && texto.at(texto.size()-1)=='e')
texto.erase(texto.begin()+texto.size()-1);
return texto;
}
}